@@ -49,10 +49,72 @@ public abstract class PropertiesLoaderUtils {
49
49
50
50
51
51
/**
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).
53
114
* @param resource the resource to load from
54
115
* @return the populated Properties instance
55
116
* @throws IOException if loading failed
117
+ * @see #fillProperties(java.util.Properties, Resource)
56
118
*/
57
119
public static Properties loadProperties (Resource resource ) throws IOException {
58
120
Properties props = new Properties ();
@@ -61,24 +123,30 @@ public static Properties loadProperties(Resource resource) throws IOException {
61
123
}
62
124
63
125
/**
64
- * Fill the given properties from the given resource.
126
+ * Fill the given properties from the given resource (in ISO-8859-1 encoding) .
65
127
* @param props the Properties instance to fill
66
128
* @param resource the resource to load from
67
129
* @throws IOException if loading failed
68
130
*/
69
131
public static void fillProperties (Properties props , Resource resource ) throws IOException {
70
132
InputStream is = resource .getInputStream ();
71
133
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
+ }
73
141
}
74
142
finally {
75
143
is .close ();
76
144
}
77
145
}
78
146
79
147
/**
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.
82
150
* <p>Merges properties if more than one resource of the same name
83
151
* found in the class path.
84
152
* @param resourceName the name of the class path resource
@@ -90,8 +158,8 @@ public static Properties loadAllProperties(String resourceName) throws IOExcepti
90
158
}
91
159
92
160
/**
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.
95
163
* <p>Merges properties if more than one resource of the same name
96
164
* found in the class path.
97
165
* @param resourceName the name of the class path resource
@@ -106,72 +174,26 @@ public static Properties loadAllProperties(String resourceName, ClassLoader clas
106
174
if (clToUse == null ) {
107
175
clToUse = ClassUtils .getDefaultClassLoader ();
108
176
}
109
- Properties properties = new Properties ();
177
+ Properties props = new Properties ();
110
178
Enumeration urls = clToUse .getResources (resourceName );
111
179
while (urls .hasMoreElements ()) {
112
180
URL url = (URL ) urls .nextElement ();
113
- InputStream is = null ;
181
+ URLConnection con = url .openConnection ();
182
+ ResourceUtils .useCachesIfNecessary (con );
183
+ InputStream is = con .getInputStream ();
114
184
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
+ }
119
191
}
120
192
finally {
121
- if (is != null ) {
122
- is .close ();
123
- }
193
+ is .close ();
124
194
}
125
195
}
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 ());
137
196
return props ;
138
197
}
139
198
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
-
177
199
}
0 commit comments