KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > webEditor > core > configuration


1 /* $Id: configuration.java,v 1.14 2001/04/10 17:14:51 agarcia3 Exp $
2     webEditor. The new way in content management
3     Copyright (C) 2001 Alfredo Garcia
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation.
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 General Public License for more details.
13 */

14
15 package webEditor.core;
16
17 import java.io.*;
18 import java.util.*;
19
20 import org.w3c.dom.*;
21 import org.apache.xerces.dom.*;
22 import org.apache.xml.serialize.*;
23
24 import webEditor.util.xml_parser;
25
26
27 /**
28  * Encapsulates the access to the has table configuration structures.
29  *
30  * @author <a HREF="mailto:agarcia@mundofree.com">Alfredo Garcia</a>
31  */

32
33 public class configuration
34 {
35    /**
36     * Table with the Configuration parameters
37     */

38    private static Hashtable configTable;
39
40    /**
41     * Access class to the configuration files
42     */

43    private static xml_parser myParser;
44
45
46    public configuration(String JavaDoc confFile)
47    {
48     // The class constructor just needs to read the config values
49
myParser = new xml_parser ();
50     configTable = myParser.parseConfFile (confFile);
51    }
52
53    public configuration(Hashtable givenTable)
54    {
55     // The class constructor just needs to read the config values
56
configTable = givenTable;
57    }
58
59    public configuration()
60    {
61     // Empty configuration
62
configTable = new Hashtable();
63    }
64    
65    /**
66     * Returns the stored value for the given "category" and "key"
67     * @param category Config category to read
68     * @param key Config key to read
69     * @return String Associated value or null if the key doesn't exists.
70     */

71    public String JavaDoc readValue (String JavaDoc category, String JavaDoc key)
72    {
73     String JavaDoc value = null;
74     Hashtable myTable = null;
75
76     myTable = (Hashtable) this.configTable.get (category);
77     if ( myTable == null ) {
78         return (null);
79     }
80
81     value = (String JavaDoc) myTable.get (key);
82     if ( value == null ) {
83         return (null);
84     }
85
86     return (value);
87    }
88
89    /**
90     * Stores into the configuration structure the given value
91     * If the category doesn't exists, a new category with this name
92     * will be created
93     * Any other previous value with the same "category" and "key"
94     * will be overwriten.
95     * @param category category of the key
96     * @param key key of the value
97     * @param value value to write
98     * @return void
99     */

100    public void storeValue (String JavaDoc category, String JavaDoc key, String JavaDoc value)
101    {
102     Hashtable myTable = null;
103
104     myTable = (Hashtable) this.configTable.get (category);
105     if ( myTable == null ) {
106         // We don't have this category, so we create one
107
myTable = new Hashtable();
108     }
109     myTable.put (key, (String JavaDoc) value);
110     this.configTable.put (category, (Hashtable) myTable);
111     return;
112    }
113
114    /**
115     * Reads all the values for a given category
116     * @param category Name of the category
117     * @return Hashtable Table with the values of the category
118     */

119    public Hashtable readCategory (String JavaDoc category)
120    {
121     Hashtable myTable = new Hashtable();
122
123     if ( category == null ) {
124         return (myTable);
125     }
126     myTable = (Hashtable) this.configTable.get (category);
127     return (myTable);
128    }
129
130   /**
131     * Stores into the configuration structure the given category
132     * If the category doesn't exists, a new category with this name
133     * will be created
134     * Any other previous values with the same "category"
135     * will be overwriten.
136     * @param category Name of the category
137     * @param values Hashtable with the category values
138     * @return void
139     */

140    public void storeCategory (String JavaDoc category, Hashtable values)
141    {
142     this.configTable.put (category, (Hashtable) values);
143     return;
144    }
145
146    /**
147     * Modify the content of the configuration file.
148     * @param dataStream Hash with the new values to write
149     * @param configDoc DOM tree of the document
150     * @return Document New DOM tree
151     */

152    public Document writeDoc (Hashtable dataStream, Document configDoc)
153    {
154     String JavaDoc rootTag = null;
155     String JavaDoc tagName = null;
156     String JavaDoc tagValue = null;
157
158     Element root = configDoc.getDocumentElement();
159     //rootTag = root.getTagName();
160
rootTag = "category";
161     NodeList list = configDoc.getElementsByTagName(rootTag).item(0).getChildNodes();
162
163     // Now, we look for document tags that appears in the hash, and
164
// we try to change its content
165
for (Enumeration e = dataStream.keys() ; e.hasMoreElements() ;) {
166         tagName = (String JavaDoc) e.nextElement();
167         tagValue = (String JavaDoc) dataStream.get (tagName);
168         for (int i=0; i < list.getLength(); i++) {
169             if (list.item(i).getAttributes().getNamedItem("code").getNodeValue().equals(tagName)) {
170                 Node source = list.item(i);
171                 Node myDocNode = source.cloneNode (true);
172                 myDocNode.getLastChild().setNodeValue (tagValue);
173                 Node parent = source.getParentNode();
174                 Node temp = parent.replaceChild (myDocNode, source);
175             }
176         }
177     }
178
179     return (configDoc);
180    }
181
182   /**
183     * Saves the modifications of the configuration file
184     * @param filePath Complete path of the configuration file
185     * @param doc DOM tree of the document
186     * @return void
187     */

188    public void saveConfig (String JavaDoc filePath, Document doc)
189    {
190     if (filePath == null) {
191         return;
192     }
193 try {
194     OutputFormat format = new OutputFormat (doc, "ISO-8859-1" ,true);
195     format.setIndent (2);
196     format.setLineWidth (80);
197     format.setPreserveSpace (true);
198     StringWriter stringOut = new StringWriter ();
199     XMLSerializer serial = new XMLSerializer (stringOut, format);
200     serial.asDOMSerializer ();
201
202     serial.serialize (doc.getDocumentElement());
203
204     // We need to delete the file before the write operation
205
File fd = new File (filePath);
206     if ( fd.exists() ) {
207         fd.delete();
208     }
209     RandomAccessFile myFile = new RandomAccessFile (filePath, "rw");
210     myFile.write (stringOut.toString().getBytes());
211     myFile.close();
212 }
213 catch (Exception JavaDoc e) {
214     e.printStackTrace();
215 }
216    }
217
218
219    /**
220     * Returns the internal hashtable
221     * @return Hashtable
222     */

223    public Hashtable returnData ()
224    {
225     return (configTable);
226    }
227
228 }
229
Popular Tags