View Javadoc

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  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          if (url == null)
58          {
59              load(fileName);
60          }
61          else
62          {
63              load(url);
64          }
65      }
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              URL url = ConfigurationUtils.locate(basePath, fileName);
79              load(url);
80          }
81          catch (ConfigurationException e)
82          {
83              throw e;
84          }
85          catch (Exception e)
86          {
87              throw new ConfigurationException(e.getMessage(), e);
88          }
89      }
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             load(file.toURL());
103         }
104         catch (MalformedURLException e)
105         {
106             throw new ConfigurationException(e.getMessage(), e);
107         }
108     }
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         InputStream in = null;
120 
121         try
122         {
123             in = url.openStream();
124             load(in);
125         }
126         catch (Exception e)
127         {
128             throw new ConfigurationException(e.getMessage(), e);
129         }
130         finally
131         {
132             // close the input stream
133             try
134             {
135                 if (in != null)
136                 {
137                     in.close();
138                 }
139             }
140             catch (IOException e)
141             {
142                 e.printStackTrace();
143             }
144         }
145     }
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         load(in, null);
158     }
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         Reader reader = null;
172 
173         if (encoding != null)
174         {
175             try
176             {
177                 reader = new InputStreamReader(in, encoding);
178             }
179             catch (UnsupportedEncodingException e)
180             {
181                 throw new ConfigurationException("The requested encoding is not supported, try the default encoding.", e);
182             }
183         }
184 
185         if (reader == null)
186         {
187             reader = new InputStreamReader(in);
188         }
189 
190         load(reader);
191     }
192 
193     /***
194      * Save the configuration.
195      *
196      * @throws ConfigurationException
197      */
198     public void save() throws ConfigurationException
199     {
200         save(fileName);
201     }
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             save(ConfigurationUtils.constructFile(basePath, fileName));
217         }
218         catch (ConfigurationException e)
219         {
220             throw e;
221         }
222         catch (Exception e)
223         {
224             throw new ConfigurationException(e.getMessage(), e);
225         }
226     }
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         if ("file".equals(url.getProtocol()))
240         {
241             save(new File(url.getFile()));
242         }
243     }
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         OutputStream out = null;
256 
257         try
258         {
259             out = new FileOutputStream(file);
260             save(out);
261         }
262         catch (FileNotFoundException e)
263         {
264             e.printStackTrace();
265         }
266         finally
267         {
268             // close the output stream
269             try
270             {
271                 if (out != null)
272                 {
273                     out.close();
274                 }
275             }
276             catch (IOException e)
277             {
278                 e.printStackTrace();
279             }
280         }
281     }
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         save(out, null);
293     }
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         Writer writer = null;
306 
307         if (encoding != null)
308         {
309             try
310             {
311                 writer = new OutputStreamWriter(out, encoding);
312             }
313             catch (UnsupportedEncodingException e)
314             {
315                 throw new ConfigurationException("The requested encoding is not supported, try the default encoding.", e);
316             }
317         }
318 
319         if (writer == null)
320         {
321             writer = new OutputStreamWriter(out);
322         }
323 
324         save(writer);
325     }
326 
327     /***
328      * Return the name of the file.
329      */
330     public String getFileName()
331     {
332         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         this.fileName = fileName;
343 
344         // update the URL
345         url = ConfigurationUtils.locate(basePath, fileName);
346     }
347 
348     /***
349      * Return the base path.
350      */
351     public String getBasePath()
352     {
353         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         this.basePath = basePath;
364 
365         // todo: update the url
366     }
367 
368     /***
369      * Return the file where the configuration is stored.
370      */
371     public File getFile()
372     {
373         if (url != null && "file".equals(url.getProtocol()))
374         {
375             return new File(url.getFile());
376         }
377         else
378         {
379             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         if (file != null)
391         {
392             try
393             {
394                 setURL(file.toURL());
395             }
396             catch (MalformedURLException e)
397             {
398                 e.printStackTrace();
399             }
400         }
401         else
402         {
403             url = null;
404         }
405     }
406 
407     /***
408      * Return the URL where the configuration is stored.
409      */
410     public URL getURL()
411     {
412         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         this.url = url;
423 
424         // update the base path
425         basePath = ConfigurationUtils.getBasePath(url);
426         if (basePath != null && basePath.startsWith("file:"))
427         {
428             basePath = basePath.substring(5);
429         }
430 
431         // update the file name
432         fileName = ConfigurationUtils.getFileName(url);
433     }
434 }