Skip to content

Commit 078a1c5

Browse files
committed
Made EncodedResource based variant public; consistently detect XML properties across all variants
Issue: SPR-9078
1 parent d46a82b commit 078a1c5

File tree

1 file changed

+86
-64
lines changed

1 file changed

+86
-64
lines changed

spring-core/src/main/java/org/springframework/core/io/support/PropertiesLoaderUtils.java

Lines changed: 86 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,72 @@ public abstract class PropertiesLoaderUtils {
4949

5050

5151
/**
52-
* Load properties from the given resource.
52+
* Load properties from the given EncodedResource,
53+
* potentially defining a specific encoding for the properties file.
54+
* @see #fillProperties(java.util.Properties, EncodedResource)
55+
*/
56+
public static Properties loadProperties(EncodedResource resource) throws IOException {
57+
Properties props = new Properties();
58+
fillProperties(props, resource);
59+
return props;
60+
}
61+
62+
/**
63+
* Fill the given properties from the given EncodedResource,
64+
* potentially defining a specific encoding for the properties file.
65+
* @param props the Properties instance to load into
66+
* @param resource the resource to load from
67+
* @throws IOException in case of I/O errors
68+
*/
69+
public static void fillProperties(Properties props, EncodedResource resource)
70+
throws IOException {
71+
72+
fillProperties(props, resource, new DefaultPropertiesPersister());
73+
}
74+
75+
/**
76+
* Actually load properties from the given EncodedResource into the given Properties instance.
77+
* @param props the Properties instance to load into
78+
* @param resource the resource to load from
79+
* @param persister the PropertiesPersister to use
80+
* @throws IOException in case of I/O errors
81+
*/
82+
static void fillProperties(Properties props, EncodedResource resource, PropertiesPersister persister)
83+
throws IOException {
84+
85+
InputStream stream = null;
86+
Reader reader = null;
87+
try {
88+
String filename = resource.getResource().getFilename();
89+
if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) {
90+
stream = resource.getInputStream();
91+
persister.loadFromXml(props, stream);
92+
}
93+
else if (resource.requiresReader()) {
94+
reader = resource.getReader();
95+
persister.load(props, reader);
96+
}
97+
else {
98+
stream = resource.getInputStream();
99+
persister.load(props, stream);
100+
}
101+
}
102+
finally {
103+
if (stream != null) {
104+
stream.close();
105+
}
106+
if (reader != null) {
107+
reader.close();
108+
}
109+
}
110+
}
111+
112+
/**
113+
* Load properties from the given resource (in ISO-8859-1 encoding).
53114
* @param resource the resource to load from
54115
* @return the populated Properties instance
55116
* @throws IOException if loading failed
117+
* @see #fillProperties(java.util.Properties, Resource)
56118
*/
57119
public static Properties loadProperties(Resource resource) throws IOException {
58120
Properties props = new Properties();
@@ -61,24 +123,30 @@ public static Properties loadProperties(Resource resource) throws IOException {
61123
}
62124

63125
/**
64-
* Fill the given properties from the given resource.
126+
* Fill the given properties from the given resource (in ISO-8859-1 encoding).
65127
* @param props the Properties instance to fill
66128
* @param resource the resource to load from
67129
* @throws IOException if loading failed
68130
*/
69131
public static void fillProperties(Properties props, Resource resource) throws IOException {
70132
InputStream is = resource.getInputStream();
71133
try {
72-
props.load(is);
134+
String filename = resource.getFilename();
135+
if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) {
136+
props.loadFromXML(is);
137+
}
138+
else {
139+
props.load(is);
140+
}
73141
}
74142
finally {
75143
is.close();
76144
}
77145
}
78146

79147
/**
80-
* Load all properties from the given class path resource,
81-
* using the default class loader.
148+
* Load all properties from the specified class path resource
149+
* (in ISO-8859-1 encoding), using the default class loader.
82150
* <p>Merges properties if more than one resource of the same name
83151
* found in the class path.
84152
* @param resourceName the name of the class path resource
@@ -90,8 +158,8 @@ public static Properties loadAllProperties(String resourceName) throws IOExcepti
90158
}
91159

92160
/**
93-
* Load all properties from the given class path resource,
94-
* using the given class loader.
161+
* Load all properties from the specified class path resource
162+
* (in ISO-8859-1 encoding), using the given class loader.
95163
* <p>Merges properties if more than one resource of the same name
96164
* found in the class path.
97165
* @param resourceName the name of the class path resource
@@ -106,72 +174,26 @@ public static Properties loadAllProperties(String resourceName, ClassLoader clas
106174
if (clToUse == null) {
107175
clToUse = ClassUtils.getDefaultClassLoader();
108176
}
109-
Properties properties = new Properties();
177+
Properties props = new Properties();
110178
Enumeration urls = clToUse.getResources(resourceName);
111179
while (urls.hasMoreElements()) {
112180
URL url = (URL) urls.nextElement();
113-
InputStream is = null;
181+
URLConnection con = url.openConnection();
182+
ResourceUtils.useCachesIfNecessary(con);
183+
InputStream is = con.getInputStream();
114184
try {
115-
URLConnection con = url.openConnection();
116-
ResourceUtils.useCachesIfNecessary(con);
117-
is = con.getInputStream();
118-
properties.load(is);
185+
if (resourceName != null && resourceName.endsWith(XML_FILE_EXTENSION)) {
186+
props.loadFromXML(is);
187+
}
188+
else {
189+
props.load(is);
190+
}
119191
}
120192
finally {
121-
if (is != null) {
122-
is.close();
123-
}
193+
is.close();
124194
}
125195
}
126-
return properties;
127-
}
128-
129-
130-
/**
131-
* Load the properties from the given encoded resource.
132-
* @see #fillProperties
133-
*/
134-
static Properties loadProperties(EncodedResource resource) throws IOException {
135-
Properties props = new Properties();
136-
fillProperties(props, resource, new DefaultPropertiesPersister());
137196
return props;
138197
}
139198

140-
/**
141-
* Actually load properties from the given EncodedResource into the given Properties instance.
142-
* @param props the Properties instance to load into
143-
* @param resource the resource to load from
144-
* @param persister the PropertiesPersister to use
145-
* @throws IOException in case of I/O errors
146-
*/
147-
static void fillProperties(Properties props, EncodedResource resource, PropertiesPersister persister)
148-
throws IOException {
149-
150-
InputStream stream = null;
151-
Reader reader = null;
152-
try {
153-
String filename = resource.getResource().getFilename();
154-
if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) {
155-
stream = resource.getInputStream();
156-
persister.loadFromXml(props, stream);
157-
}
158-
else if (resource.requiresReader()) {
159-
reader = resource.getReader();
160-
persister.load(props, reader);
161-
}
162-
else {
163-
stream = resource.getInputStream();
164-
persister.load(props, stream);
165-
}
166-
}
167-
finally {
168-
if (stream != null) {
169-
stream.close();
170-
}
171-
if (reader != null) {
172-
reader.close();
173-
}
174-
}
175-
}
176-
177199
}

0 commit comments

Comments
 (0)