From 078a1c5db84467a52e9de4078d139ee6fded08c7 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 24 Jan 2013 14:56:45 +0100 Subject: [PATCH] Made EncodedResource based variant public; consistently detect XML properties across all variants Issue: SPR-9078 --- .../io/support/PropertiesLoaderUtils.java | 150 ++++++++++-------- 1 file changed, 86 insertions(+), 64 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/io/support/PropertiesLoaderUtils.java b/spring-core/src/main/java/org/springframework/core/io/support/PropertiesLoaderUtils.java index 6aaa486fd3..503f2bb1bb 100644 --- a/spring-core/src/main/java/org/springframework/core/io/support/PropertiesLoaderUtils.java +++ b/spring-core/src/main/java/org/springframework/core/io/support/PropertiesLoaderUtils.java @@ -49,10 +49,72 @@ public abstract class PropertiesLoaderUtils { /** - * Load properties from the given resource. + * Load properties from the given EncodedResource, + * potentially defining a specific encoding for the properties file. + * @see #fillProperties(java.util.Properties, EncodedResource) + */ + public static Properties loadProperties(EncodedResource resource) throws IOException { + Properties props = new Properties(); + fillProperties(props, resource); + return props; + } + + /** + * Fill the given properties from the given EncodedResource, + * potentially defining a specific encoding for the properties file. + * @param props the Properties instance to load into + * @param resource the resource to load from + * @throws IOException in case of I/O errors + */ + public static void fillProperties(Properties props, EncodedResource resource) + throws IOException { + + fillProperties(props, resource, new DefaultPropertiesPersister()); + } + + /** + * Actually load properties from the given EncodedResource into the given Properties instance. + * @param props the Properties instance to load into + * @param resource the resource to load from + * @param persister the PropertiesPersister to use + * @throws IOException in case of I/O errors + */ + static void fillProperties(Properties props, EncodedResource resource, PropertiesPersister persister) + throws IOException { + + InputStream stream = null; + Reader reader = null; + try { + String filename = resource.getResource().getFilename(); + if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) { + stream = resource.getInputStream(); + persister.loadFromXml(props, stream); + } + else if (resource.requiresReader()) { + reader = resource.getReader(); + persister.load(props, reader); + } + else { + stream = resource.getInputStream(); + persister.load(props, stream); + } + } + finally { + if (stream != null) { + stream.close(); + } + if (reader != null) { + reader.close(); + } + } + } + + /** + * Load properties from the given resource (in ISO-8859-1 encoding). * @param resource the resource to load from * @return the populated Properties instance * @throws IOException if loading failed + * @see #fillProperties(java.util.Properties, Resource) */ public static Properties loadProperties(Resource resource) throws IOException { Properties props = new Properties(); @@ -61,7 +123,7 @@ public abstract class PropertiesLoaderUtils { } /** - * Fill the given properties from the given resource. + * Fill the given properties from the given resource (in ISO-8859-1 encoding). * @param props the Properties instance to fill * @param resource the resource to load from * @throws IOException if loading failed @@ -69,7 +131,13 @@ public abstract class PropertiesLoaderUtils { public static void fillProperties(Properties props, Resource resource) throws IOException { InputStream is = resource.getInputStream(); try { - props.load(is); + String filename = resource.getFilename(); + if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) { + props.loadFromXML(is); + } + else { + props.load(is); + } } finally { is.close(); @@ -77,8 +145,8 @@ public abstract class PropertiesLoaderUtils { } /** - * Load all properties from the given class path resource, - * using the default class loader. + * Load all properties from the specified class path resource + * (in ISO-8859-1 encoding), using the default class loader. *

Merges properties if more than one resource of the same name * found in the class path. * @param resourceName the name of the class path resource @@ -90,8 +158,8 @@ public abstract class PropertiesLoaderUtils { } /** - * Load all properties from the given class path resource, - * using the given class loader. + * Load all properties from the specified class path resource + * (in ISO-8859-1 encoding), using the given class loader. *

Merges properties if more than one resource of the same name * found in the class path. * @param resourceName the name of the class path resource @@ -106,72 +174,26 @@ public abstract class PropertiesLoaderUtils { if (clToUse == null) { clToUse = ClassUtils.getDefaultClassLoader(); } - Properties properties = new Properties(); + Properties props = new Properties(); Enumeration urls = clToUse.getResources(resourceName); while (urls.hasMoreElements()) { URL url = (URL) urls.nextElement(); - InputStream is = null; + URLConnection con = url.openConnection(); + ResourceUtils.useCachesIfNecessary(con); + InputStream is = con.getInputStream(); try { - URLConnection con = url.openConnection(); - ResourceUtils.useCachesIfNecessary(con); - is = con.getInputStream(); - properties.load(is); + if (resourceName != null && resourceName.endsWith(XML_FILE_EXTENSION)) { + props.loadFromXML(is); + } + else { + props.load(is); + } } finally { - if (is != null) { - is.close(); - } + is.close(); } } - return properties; - } - - - /** - * Load the properties from the given encoded resource. - * @see #fillProperties - */ - static Properties loadProperties(EncodedResource resource) throws IOException { - Properties props = new Properties(); - fillProperties(props, resource, new DefaultPropertiesPersister()); return props; } - /** - * Actually load properties from the given EncodedResource into the given Properties instance. - * @param props the Properties instance to load into - * @param resource the resource to load from - * @param persister the PropertiesPersister to use - * @throws IOException in case of I/O errors - */ - static void fillProperties(Properties props, EncodedResource resource, PropertiesPersister persister) - throws IOException { - - InputStream stream = null; - Reader reader = null; - try { - String filename = resource.getResource().getFilename(); - if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) { - stream = resource.getInputStream(); - persister.loadFromXml(props, stream); - } - else if (resource.requiresReader()) { - reader = resource.getReader(); - persister.load(props, reader); - } - else { - stream = resource.getInputStream(); - persister.load(props, stream); - } - } - finally { - if (stream != null) { - stream.close(); - } - if (reader != null) { - reader.close(); - } - } - } - }