%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.configuration.AbstractFileConfiguration |
|
|
1 | /* |
|
2 | * Copyright 2004 The Apache Software Foundation. |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
15 | */ |
|
16 | ||
17 | package org.apache.commons.configuration; |
|
18 | ||
19 | import java.io.File; |
|
20 | import java.io.FileNotFoundException; |
|
21 | import java.io.FileOutputStream; |
|
22 | import java.io.IOException; |
|
23 | import java.io.InputStream; |
|
24 | import java.io.InputStreamReader; |
|
25 | import java.io.OutputStream; |
|
26 | import java.io.OutputStreamWriter; |
|
27 | import java.io.Reader; |
|
28 | import java.io.UnsupportedEncodingException; |
|
29 | import java.io.Writer; |
|
30 | import java.net.MalformedURLException; |
|
31 | import java.net.URL; |
|
32 | ||
33 | /** |
|
34 | * Partial implementation of the <code>FileConfiguration</code> interface. |
|
35 | * Developpers of file based configuration may wan't to extend this class, |
|
36 | * the two methods left to implement are {@see AbstractFileConfiguration#load(Reader)} |
|
37 | * and {@see AbstractFileConfiguration#save(Reader)}. |
|
38 | * |
|
39 | * @author Emmanuel Bourg |
|
40 | * @version $Revision: 1.4 $, $Date: 2004/10/04 21:45:10 $ |
|
41 | * @since 1.0-rc2 |
|
42 | */ |
|
43 | 310 | public abstract class AbstractFileConfiguration extends BaseConfiguration implements FileConfiguration |
44 | { |
|
45 | protected String fileName; |
|
46 | protected String basePath; |
|
47 | protected URL url; |
|
48 | ||
49 | /** |
|
50 | * Load the configuration from the underlying URL. If the URL is not |
|
51 | * specified, it attempts to locate the specified file name. |
|
52 | * |
|
53 | * @throws ConfigurationException |
|
54 | */ |
|
55 | public void load() throws ConfigurationException |
|
56 | { |
|
57 | 309 | if (url == null) |
58 | { |
|
59 | 0 | load(fileName); |
60 | } |
|
61 | else |
|
62 | { |
|
63 | 309 | load(url); |
64 | } |
|
65 | 309 | } |
66 | ||
67 | /** |
|
68 | * Locate the specified file and load the configuration. |
|
69 | * |
|
70 | * @param fileName the name of the file loaded |
|
71 | * |
|
72 | * @throws ConfigurationException |
|
73 | */ |
|
74 | public void load(String fileName) throws ConfigurationException |
|
75 | { |
|
76 | try |
|
77 | { |
|
78 | 1 | URL url = ConfigurationUtils.locate(basePath, fileName); |
79 | 1 | load(url); |
80 | } |
|
81 | 0 | catch (ConfigurationException e) |
82 | { |
|
83 | 0 | throw e; |
84 | } |
|
85 | 0 | catch (Exception e) |
86 | { |
|
87 | 0 | throw new ConfigurationException(e.getMessage(), e); |
88 | 1 | } |
89 | 1 | } |
90 | ||
91 | /** |
|
92 | * Load the configuration from the specified file. |
|
93 | * |
|
94 | * @param file the loaded file |
|
95 | * |
|
96 | * @throws ConfigurationException |
|
97 | */ |
|
98 | public void load(File file) throws ConfigurationException |
|
99 | { |
|
100 | try |
|
101 | { |
|
102 | 0 | load(file.toURL()); |
103 | } |
|
104 | 0 | catch (MalformedURLException e) |
105 | { |
|
106 | 0 | throw new ConfigurationException(e.getMessage(), e); |
107 | 0 | } |
108 | 0 | } |
109 | ||
110 | /** |
|
111 | * Load the configuration from the specified URL. |
|
112 | * |
|
113 | * @param url the URL of the file loaded |
|
114 | * |
|
115 | * @throws ConfigurationException |
|
116 | */ |
|
117 | public void load(URL url) throws ConfigurationException |
|
118 | { |
|
119 | 440 | InputStream in = null; |
120 | ||
121 | try |
|
122 | { |
|
123 | 440 | in = url.openStream(); |
124 | 440 | load(in); |
125 | 440 | } |
126 | 0 | catch (Exception e) |
127 | { |
|
128 | 0 | throw new ConfigurationException(e.getMessage(), e); |
129 | } |
|
130 | finally |
|
131 | { |
|
132 | // close the input stream |
|
133 | 0 | try |
134 | { |
|
135 | 440 | if (in != null) |
136 | { |
|
137 | 440 | in.close(); |
138 | } |
|
139 | } |
|
140 | 0 | catch (IOException e) |
141 | { |
|
142 | 0 | e.printStackTrace(); |
143 | 880 | } |
144 | 440 | } |
145 | 440 | } |
146 | ||
147 | /** |
|
148 | * Load the configuration from the specified stream, using the default |
|
149 | * platform specific encoding. |
|
150 | * |
|
151 | * @param in the input stream |
|
152 | * |
|
153 | * @throws ConfigurationException |
|
154 | */ |
|
155 | public void load(InputStream in) throws ConfigurationException |
|
156 | { |
|
157 | 440 | load(in, null); |
158 | 440 | } |
159 | ||
160 | /** |
|
161 | * Load the configuration from the specified stream, using the specified |
|
162 | * encoding. If the encoding is null the default encoding is used. |
|
163 | * |
|
164 | * @param in the input stream |
|
165 | * @param encoding the encoding used. <code>null</code> to use the default encoding |
|
166 | * |
|
167 | * @throws ConfigurationException |
|
168 | */ |
|
169 | public void load(InputStream in, String encoding) throws ConfigurationException |
|
170 | { |
|
171 | 440 | Reader reader = null; |
172 | ||
173 | 440 | if (encoding != null) |
174 | { |
|
175 | try |
|
176 | { |
|
177 | 0 | reader = new InputStreamReader(in, encoding); |
178 | } |
|
179 | 0 | catch (UnsupportedEncodingException e) |
180 | { |
|
181 | 0 | throw new ConfigurationException("The requested encoding is not supported, try the default encoding.", e); |
182 | 0 | } |
183 | } |
|
184 | ||
185 | 440 | if (reader == null) |
186 | { |
|
187 | 440 | reader = new InputStreamReader(in); |
188 | } |
|
189 | ||
190 | 440 | load(reader); |
191 | 440 | } |
192 | ||
193 | /** |
|
194 | * Save the configuration. |
|
195 | * |
|
196 | * @throws ConfigurationException |
|
197 | */ |
|
198 | public void save() throws ConfigurationException |
|
199 | { |
|
200 | 4 | save(fileName); |
201 | 3 | } |
202 | ||
203 | /** |
|
204 | * Save the configuration to the specified file. This doesn't change the |
|
205 | * source of the configuration, use setFileName() if you need it. |
|
206 | * |
|
207 | * @param fileName |
|
208 | * |
|
209 | * @throws ConfigurationException |
|
210 | */ |
|
211 | public void save(String fileName) throws ConfigurationException |
|
212 | { |
|
213 | try |
|
214 | { |
|
215 | // create a new file |
|
216 | 6 | save(ConfigurationUtils.constructFile(basePath, fileName)); |
217 | } |
|
218 | 0 | catch (ConfigurationException e) |
219 | { |
|
220 | 0 | throw e; |
221 | } |
|
222 | 1 | catch (Exception e) |
223 | { |
|
224 | 1 | throw new ConfigurationException(e.getMessage(), e); |
225 | 5 | } |
226 | 5 | } |
227 | ||
228 | /** |
|
229 | * Save the configuration to the specified URL if it's a file URL. |
|
230 | * This doesn't change the source of the configuration, use setURL() |
|
231 | * if you need it. |
|
232 | * |
|
233 | * @param url |
|
234 | * |
|
235 | * @throws ConfigurationException |
|
236 | */ |
|
237 | public void save(URL url) throws ConfigurationException |
|
238 | { |
|
239 | 0 | if ("file".equals(url.getProtocol())) |
240 | { |
|
241 | 0 | save(new File(url.getFile())); |
242 | } |
|
243 | 0 | } |
244 | ||
245 | /** |
|
246 | * Save the configuration to the specified file. This doesn't change the |
|
247 | * source of the configuration, use setFile() if you need it. |
|
248 | * |
|
249 | * @param file |
|
250 | * |
|
251 | * @throws ConfigurationException |
|
252 | */ |
|
253 | public void save(File file) throws ConfigurationException |
|
254 | { |
|
255 | 5 | OutputStream out = null; |
256 | ||
257 | try |
|
258 | { |
|
259 | 5 | out = new FileOutputStream(file); |
260 | 5 | save(out); |
261 | 5 | } |
262 | 0 | catch (FileNotFoundException e) |
263 | { |
|
264 | 0 | e.printStackTrace(); |
265 | 0 | } |
266 | finally |
|
267 | { |
|
268 | // close the output stream |
|
269 | 0 | try |
270 | { |
|
271 | 5 | if (out != null) |
272 | { |
|
273 | 5 | out.close(); |
274 | } |
|
275 | } |
|
276 | 0 | catch (IOException e) |
277 | { |
|
278 | 0 | e.printStackTrace(); |
279 | 10 | } |
280 | 5 | } |
281 | 5 | } |
282 | ||
283 | /** |
|
284 | * Save the configuration to the specified stream. |
|
285 | * |
|
286 | * @param out |
|
287 | * |
|
288 | * @throws ConfigurationException |
|
289 | */ |
|
290 | public void save(OutputStream out) throws ConfigurationException |
|
291 | { |
|
292 | 5 | save(out, null); |
293 | 5 | } |
294 | ||
295 | /** |
|
296 | * Save the configuration to the specified stream, using the specified |
|
297 | * encoding. If the encoding is null the default encoding is used. |
|
298 | * |
|
299 | * @param out |
|
300 | * @param encoding |
|
301 | * @throws ConfigurationException |
|
302 | */ |
|
303 | public void save(OutputStream out, String encoding) throws ConfigurationException |
|
304 | { |
|
305 | 5 | Writer writer = null; |
306 | ||
307 | 5 | if (encoding != null) |
308 | { |
|
309 | try |
|
310 | { |
|
311 | 0 | writer = new OutputStreamWriter(out, encoding); |
312 | } |
|
313 | 0 | catch (UnsupportedEncodingException e) |
314 | { |
|
315 | 0 | throw new ConfigurationException("The requested encoding is not supported, try the default encoding.", e); |
316 | 0 | } |
317 | } |
|
318 | ||
319 | 5 | if (writer == null) |
320 | { |
|
321 | 5 | writer = new OutputStreamWriter(out); |
322 | } |
|
323 | ||
324 | 5 | save(writer); |
325 | 5 | } |
326 | ||
327 | /** |
|
328 | * Return the name of the file. |
|
329 | */ |
|
330 | public String getFileName() |
|
331 | { |
|
332 | 15 | return fileName; |
333 | } |
|
334 | ||
335 | /** |
|
336 | * Set the name of the file. |
|
337 | * |
|
338 | * @param fileName the name of the file |
|
339 | */ |
|
340 | public void setFileName(String fileName) |
|
341 | { |
|
342 | 48 | this.fileName = fileName; |
343 | ||
344 | // update the URL |
|
345 | 48 | url = ConfigurationUtils.locate(basePath, fileName); |
346 | 48 | } |
347 | ||
348 | /** |
|
349 | * Return the base path. |
|
350 | */ |
|
351 | public String getBasePath() |
|
352 | { |
|
353 | 134 | return basePath; |
354 | } |
|
355 | ||
356 | /** |
|
357 | * Set the base path. Relative configurations are loaded from this path. |
|
358 | * |
|
359 | * @param basePath the base path. |
|
360 | */ |
|
361 | public void setBasePath(String basePath) |
|
362 | { |
|
363 | 220 | this.basePath = basePath; |
364 | ||
365 | // todo: update the url |
|
366 | 220 | } |
367 | ||
368 | /** |
|
369 | * Return the file where the configuration is stored. |
|
370 | */ |
|
371 | public File getFile() |
|
372 | { |
|
373 | 3 | if (url != null && "file".equals(url.getProtocol())) |
374 | { |
|
375 | 1 | return new File(url.getFile()); |
376 | } |
|
377 | else |
|
378 | { |
|
379 | 2 | return ConfigurationUtils.constructFile(getBasePath(), getFileName()); |
380 | } |
|
381 | } |
|
382 | ||
383 | /** |
|
384 | * Set the file where the configuration is stored. |
|
385 | * |
|
386 | * @param file |
|
387 | */ |
|
388 | public void setFile(File file) |
|
389 | { |
|
390 | 75 | if (file != null) |
391 | { |
|
392 | try |
|
393 | { |
|
394 | 75 | setURL(file.toURL()); |
395 | } |
|
396 | 0 | catch (MalformedURLException e) |
397 | { |
|
398 | 0 | e.printStackTrace(); |
399 | 75 | } |
400 | } |
|
401 | else |
|
402 | { |
|
403 | 0 | url = null; |
404 | } |
|
405 | 75 | } |
406 | ||
407 | /** |
|
408 | * Return the URL where the configuration is stored. |
|
409 | */ |
|
410 | public URL getURL() |
|
411 | { |
|
412 | 0 | return url; |
413 | } |
|
414 | ||
415 | /** |
|
416 | * The URL where the configuration is stored. |
|
417 | * |
|
418 | * @param url |
|
419 | */ |
|
420 | public void setURL(URL url) |
|
421 | { |
|
422 | 77 | this.url = url; |
423 | ||
424 | // update the base path |
|
425 | 77 | basePath = ConfigurationUtils.getBasePath(url); |
426 | 77 | if (basePath != null && basePath.startsWith("file:")) |
427 | { |
|
428 | 76 | basePath = basePath.substring(5); |
429 | } |
|
430 | ||
431 | // update the file name |
|
432 | 77 | fileName = ConfigurationUtils.getFileName(url); |
433 | 77 | } |
434 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |