KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcckit > util > PropertiesBasedConfigData


1 /*
2  * Copyright 2003-2004, Franz-Josef Elmer, All rights reserved
3  *
4  * This library is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details
13  * (http://www.gnu.org/copyleft/lesser.html).
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package jcckit.util;
20
21 import java.io.*;
22 import java.util.Properties JavaDoc;
23
24 /**
25  * Implementation of {@link FlatConfigData} based on
26  * <tt>java.util.Properties</tt>.
27  *
28  * @author Franz-Josef Elmer
29  */

30 public class PropertiesBasedConfigData extends FlatConfigData {
31   private final Properties JavaDoc _properties;
32
33   /**
34    * Creates an instance from the specified <tt>.properties</tt> file.
35    * @param fileName File name of the <tt>.properties</tt> file relative
36    * to the working directory or absolute.
37    * @throws IOException if the <tt>.properties</tt> does not exist or could
38    * not be read.
39    */

40   public PropertiesBasedConfigData(String JavaDoc fileName) throws IOException {
41     super(null);
42     _properties = new Properties JavaDoc();
43     _properties.load(new FileInputStream(fileName));
44   }
45
46   /**
47    * Creates an instance based on the specified properties.
48    * The path is undefined.
49    */

50   public PropertiesBasedConfigData(Properties JavaDoc properties) {
51     this(properties, null);
52   }
53
54   /** Creates an instance based on the specified properties and path. */
55   private PropertiesBasedConfigData(Properties JavaDoc properties, String JavaDoc path) {
56     super(path);
57     _properties = properties;
58   }
59
60   /**
61    * Returns the value for the specified full key. The call will be delegated
62    * to the wrapped <tt>java.util.properties</tt> object.
63    * @param fullKey The full key including path. <tt>null</tt> is not allowed.
64    * @return the value or <tt>null</tt> if not found.
65    */

66   protected String JavaDoc getValue(String JavaDoc fullKey) {
67     return _properties.getProperty(fullKey);
68   }
69
70   /**
71    * Returns a new instance of <tt>PropertiesBasedConfigData</tt>
72    * for the specified full path. The wrapped <tt>java.util.Properties</tt>
73    * will be the same as of this instance.
74    * @param path The full path.
75    * @return a new instance.
76    */

77   protected ConfigData createConfigData(String JavaDoc path) {
78     return new PropertiesBasedConfigData(_properties, path);
79   }
80 }
81
Popular Tags