KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cofax > XMLConfig


1 /*
2  * XMLConfig is part of the Cofax content management system library.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Please see http://www.cofax.org for contact information and other related informaion.
19  *
20  * $Header: /cvsroot/cofax/cofax/src/org/cofax/XMLConfig.java,v 1.5.2.1 2006/12/11 16:28:44 fxrobin Exp $
21  */

22
23 package org.cofax;
24
25 import java.util.*;
26
27 import org.xml.sax.InputSource JavaDoc;
28 import org.w3c.dom.*;
29 import javax.xml.parsers.*;
30
31 /**
32  *
33  * Settings for CofaxServlet are stored in an XML data store accessible via this
34  * module.
35  *
36  * @author Rajiv Pant
37  * @author Karl Martino
38  * @author Sam Cohen
39  * @author FX Robin
40  * @version 1.5.1
41  *
42  */

43
44 public final class XMLConfig {
45
46     HashMap config; // stores all the fields in config
47

48     String JavaDoc xmlFileName; // name of the config, can be filesystem or URL
49

50     String JavaDoc lastError; // the last error recorded by the program
51

52     private Document doc; // Holds the parsed document. Set by load()
53

54     /**
55      * Class Constructor, perform initializations
56      */

57     public XMLConfig() {
58
59         this.config = new HashMap();
60
61     } // public XMLConfig()
62

63     /**
64      * Returns the last error recorderd by XMLConfig
65      */

66     public String JavaDoc getLastError() {
67
68         return lastError;
69
70     }
71
72     /**
73      * Set's the XMLFileName field
74      */

75     public void setXMLFileName(String JavaDoc xmlFileName) {
76
77         this.xmlFileName = "file:///" + xmlFileName.replace('\\', '/');
78
79     } // public void setXMLFileName(String xmlFileName)
80

81     /**
82      * Clears out all of the hash table contents
83      */

84     public void empty() {
85
86         config.clear();
87
88     } // public void empty()
89

90     /**
91      * Clears out all of the hash table contents
92      */

93     public void refresh() {
94
95         this.empty();
96         this.load();
97
98     } // public void refresh()
99

100     /**
101      * Inserts a tag and value pair from XML into the config structure
102      */

103     public boolean set(String JavaDoc fieldName, Object JavaDoc fieldValue) {
104
105         if (!fieldName.equals("")) {
106             config.put(fieldName, fieldValue);
107             return true;
108         } else
109             return false;
110
111     } // boolean set(String fieldName, Object fieldValue)
112

113     /**
114      * Returns a member of the internal hash as an object of its own type
115      */

116     public Object JavaDoc get(String JavaDoc fieldName) {
117
118         if (config.containsKey(fieldName))
119             return config.get(fieldName);
120         else
121             return null;
122
123     } // Object get(String fieldName)
124

125     /**
126      * Returns a member of the internal hash as a string
127      */

128     public String JavaDoc getString(String JavaDoc fieldName) {
129
130         String JavaDoc value = null;
131
132         try {
133             value = config.get(fieldName).toString();
134         } catch (Exception JavaDoc e) {
135             value = (String JavaDoc) config.get(fieldName);
136         }
137
138         if (value == null)
139             return "";
140         else
141             return value;
142
143     } // public String getString(String fieldName)
144

145     /**
146      * To load data from an XML file and into the config structure
147      */

148     public boolean load() {
149
150         NodeList xmlConfigNodeTree; // list of members in a config XML file
151
Node docNode; // a node from the above list
152
NodeList docNodesChildren; // a list of child node to a parent node
153
Node docNodeChild; // a node from the child list
154
String JavaDoc fieldName = ""; // field name
155
String JavaDoc fieldValue = ""; // field value
156

157         /* FX ROBIN : modifying de DOM parsing with JAXP standard */
158         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
159         DocumentBuilder parser;
160         try {
161             parser = factory.newDocumentBuilder(); // an XML parser object
162
} catch (Exception JavaDoc e) {
163             this.lastError = "ERROR in XMLConfig: parse error on: " + this.xmlFileName + "\n" + "EXCEPTION: " + e.toString();
164             return false;
165         }
166
167         int i;
168         Document doc; // the document representation
169

170         try {
171             doc = parser.parse(new InputSource JavaDoc(this.xmlFileName));
172         } catch (Exception JavaDoc e) {
173             this.lastError = "ERROR in XMLConfig: parse error on: " + this.xmlFileName + "\n" + "EXCEPTION: " + e.toString();
174             return false;
175         }
176
177         try {
178             xmlConfigNodeTree = doc.getElementsByTagName("XMLConfig");
179             docNode = xmlConfigNodeTree.item(0);
180             docNodesChildren = docNode.getChildNodes();
181             for (i = 0; i < docNodesChildren.getLength(); i++) {
182                 fieldName = "";
183                 fieldValue = "";
184                 docNodeChild = docNodesChildren.item(i);
185                 if (docNodeChild.getNodeType() == Node.ELEMENT_NODE) {
186                     fieldName = docNodeChild.getNodeName();
187
188                     NodeList fieldChildren = docNodeChild.getChildNodes();
189
190                     // If there are children of the node, continue....
191
if (fieldChildren != null && fieldChildren.getLength() > 0) {
192
193                         fieldValue = docNodeChild.getFirstChild().getNodeValue();
194                         if (!fieldName.equals("")) {
195                             this.set(fieldName, fieldValue);
196                         }
197                     }
198
199                 }
200             }
201         } catch (Exception JavaDoc e) {
202             this.lastError = "ERROR in XMLConfig : failed on: " + this.xmlFileName + " XML TAG: " + fieldName + " VALUE: " + fieldValue + "\n" + "EXCEPTION: "
203                     + e.toString();
204             return false;
205         }
206         return true;
207
208     } // public boolean loadFromFile()
209

210     /**
211      * Returns a list of key-value pairs (HashMaps) For example: <map>
212      * <section>front_page</section> <rank>20</rank> </map> will produce a
213      * list containing: key value --- ----- map:section front_page map:rank 20
214      */

215     public ArrayList getList(String JavaDoc listName) {
216         ArrayList keyValues = new ArrayList();
217
218         NodeList keyValueList = doc.getElementsByTagName(listName);
219
220         for (int forOne = 0; forOne < keyValueList.getLength(); forOne++) {
221
222             Node keyNode = keyValueList.item(forOne);
223             NodeList keyRow = keyNode.getChildNodes();
224             HashMap keyValuePair = new HashMap();
225             keyValuePair.clear();
226
227             for (int forTwo = 0; forTwo < keyRow.getLength(); forTwo++) {
228
229                 Node fieldNode = keyRow.item(forTwo);
230                 int type = fieldNode.getNodeType();
231                 if (type == Node.ELEMENT_NODE) {
232
233                     String JavaDoc tagName = "";
234                     String JavaDoc tagValue = "";
235
236                     tagName = fieldNode.getParentNode().getNodeName() + ":" + fieldNode.getNodeName();
237
238                     // Get the element node's children
239
NodeList children = fieldNode.getChildNodes();
240
241                     // If there are children of the node, continue....
242
if (children != null && children.getLength() > 0) {
243
244                         // If the first child is a text node, then get the
245
// value.
246
// Put the element name and value in the hashtable
247
// for processing...
248
if (children.item(0).getNodeType() == Node.TEXT_NODE) {
249
250                             tagValue = children.item(0).getNodeValue() + "";
251
252                             if (tagValue != null && !tagValue.equals("") && !tagName.equals("")) {
253                                 keyValuePair.put(tagName, tagValue);
254                             } else {
255                                 keyValuePair.put(tagName, "");
256                             }
257
258                         } // if (children.item(0).getNodeType() ==
259
// Node.TEXT_NODE)
260

261                     } // if (children != null && children.getLength() > 0)
262

263                 } // if (type == Node.ELEMENT_NODE)
264

265             } // for (int forTwo = 0; forTwo < mapRowNodeCount; forTwo++)
266

267             if (!keyValuePair.isEmpty()) {
268                 keyValues.add(keyValuePair);
269             }
270
271         } // for (int forOne = 0; forOne < countOfMaps; forOne++)
272

273         return keyValues;
274     }
275
276 }
277
Popular Tags