KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > raptus > owxv3 > PropertiesFile


1 /*
2  * eAdmin/OWX
3  * Copyright (C) 1996-2003 OWX-Project Team <owx-team@gmx.net>
4  */

5
6 /******************************** Package */
7 package com.raptus.owxv3;
8
9
10 /******************************** Imports */
11 import java.io.*;
12 import java.util.*;
13
14
15 /******************************** Raptus-Header */
16 /**
17  * This class implements only the part which accesses the file as in Guy's old
18  * PropertiesFile-object. The part for accessing the loaded config is now in
19  * Configuration. I also added two method to set or get all Properties through a Hashtable.
20  *
21  * Original comment from Guy:
22  *
23  * Configuration file loader/writer for use with .properties files.
24  *
25  * NOTE: Due to an unknown implementation of the Properties class of Sun
26  * we cannot store comments in the configuration files.
27  *
28  *
29  * <hr>
30  * <table width="100%" border="0">
31  * <tr>
32  * <td width="24%"><b>Filename</b></td><td width="76%">ConfigManagerIFace.java</td>
33  * </tr>
34  * <tr>
35  * <td width="24%"><b>Author</b></td><td width="76%">Guy Zuercher (gzuercher@raptus.com),
36  * Pascal Mainini (pmainini@raptus.com)</td>
37  * </tr>
38  * <tr>
39  * <td width="24%"><b>Date</b></td><td width="76%">6th of August 2000</td>
40  * </tr>
41  * </table>
42  * <hr>
43  * <table width="100%" border="0">
44  * <tr>
45  * <td width="24%"><b>Date / Author</b></td><td width="76%"><b>Changes</b></td>
46  * </tr>
47  * <tr>
48  * <td width="24%">2001-03-30pm</td><td width="76%">Changed as described above.</td>
49  * </tr>
50  * <tr>
51  * <td width="24%">2001-03-27pm</td><td width="76%">included in TNG/2.0-branch</td>
52  * </tr>
53  * </table>
54  * <hr>
55  */

56 public class PropertiesFile extends Properties
57 {
58
59 /******************************** Members */
60   /**
61    * Configuration file header-string.
62    */

63   protected String JavaDoc cfgFileHeader = null;
64
65   /**
66    * Configuration file name
67    */

68   protected String JavaDoc fileName = null;
69
70
71 /******************************** Constructor */
72   /**
73    * Constructor is only setting the fileName and the Header
74    * an excplicit call to load() must be done to actually
75    * load any data.
76    *
77    * @param fileName the name of the configuration file as as string
78    */

79   public PropertiesFile(String JavaDoc fileName)
80   {
81       this.fileName = fileName;
82       cfgFileHeader = "\n# FILE: " + fileName + "\n#";
83   }
84
85
86 /******************************** Methods */
87   /**
88    * @return the name of the configuration file
89    */

90   public String JavaDoc getPropertiesFilename()
91   {
92       return fileName;
93   }
94
95   /**
96    * This method fills all properties in a hashtable using their key as the hash-key
97    * and storing their objects as Strings.
98    *
99    * @return The filled hashtable or null if something went wrong.
100    */

101   public Hashtable getPropertiesHash()
102   {
103       Hashtable ret = new Hashtable();
104
105       Enumeration e = propertyNames();
106       while(e.hasMoreElements())
107       {
108           String JavaDoc key = (String JavaDoc) e.nextElement();
109           String JavaDoc val = getProperty(key);
110
111           ret.put(key, val);
112       }
113
114       return ret;
115   }
116
117   /**
118    * This method retrieves all properties in a hashtable and fills them back to the
119    * parents' data
120    *
121    * @param hash The Hashtable to store back.
122    */

123   public void setPropertiesHash(Hashtable hash)
124   {
125       Enumeration e = hash.keys();
126       while(e.hasMoreElements())
127       {
128           String JavaDoc key = (String JavaDoc) e.nextElement();
129           String JavaDoc val = (String JavaDoc) hash.get(key);
130
131           setProperty(key,val);
132       }
133   }
134
135   /**
136    * This method actually tries to load the configuration file and
137    * read its contents into the parent's classes data.
138    *
139    * @return true if the file could have been opend and read,
140    * false if not
141    */

142   public boolean load()
143   {
144       boolean retval = false;
145
146       try
147       {
148           FileInputStream fis = new FileInputStream(fileName);
149           load(fis);
150           fis.close();
151
152           retval = true;
153       }
154       catch(FileNotFoundException e) {
155       }
156       catch(IOException e) {
157       }
158
159       return retval;
160   }
161
162   /**
163    * Saves the parents classes data.
164    *
165    * NOTE: Since the previous call to this.load() has probably thrown
166    * away an comments in the configuration file, (the're still
167    * in the file, but no more in the memory), saving these will
168    * just not bring them back ¦-(
169    *
170    * @return true if the file could have been opend and written,
171    * false if not
172    */

173   public boolean store()
174   {
175       boolean retval = false;
176
177       try
178       {
179           FileOutputStream fos = new FileOutputStream(fileName);
180           store(fos, cfgFileHeader);
181           fos.close();
182
183           retval = true;
184       }
185       catch(FileNotFoundException e)
186       {
187       }
188       catch(IOException e)
189       {
190       }
191
192       return retval;
193   }
194 }
195
196
Popular Tags