KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > configuration > xml > XMLConfigurationParser


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * XMLConfigurationException.java
20  *
21  * XMLConfigurationParser.java
22  *
23  * The parser for the XML configuration file.
24  */

25
26 // the package definition
27
package com.rift.coad.lib.configuration.xml;
28
29 // java imports
30
import java.util.Date JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.io.BufferedReader JavaDoc;
35 import java.io.File JavaDoc;
36 import java.io.FileReader JavaDoc;
37 import javax.xml.parsers.SAXParserFactory JavaDoc;
38 import javax.xml.parsers.SAXParser JavaDoc;
39 import org.xml.sax.InputSource JavaDoc;
40 import org.xml.sax.helpers.DefaultHandler JavaDoc;
41 import org.xml.sax.SAXException JavaDoc;
42 import org.xml.sax.Attributes JavaDoc;
43
44
45 /**
46  * This class is responsible for reading in the configuration information.
47  *
48  * @author Brett Chaldecott
49  */

50 public class XMLConfigurationParser {
51     
52     // the classes static member variables
53
private static final String JavaDoc GENERAL_SECTION = "GENERAL";
54     private static final String JavaDoc XML_CONFIG_PATH = "xml.config.path";
55     
56     /**
57      * This class stores the configuration information for a
58      */

59     public class XMLConfigInfo {
60         
61         // the classes private member variables
62
private String JavaDoc className = null;
63         private Map JavaDoc configInfo = null;
64         
65         
66         /**
67          * The constructor of the xml configuration information
68          */

69         public XMLConfigInfo(String JavaDoc className) {
70             this.className = className;
71             configInfo = new HashMap JavaDoc();
72         }
73         
74         
75         /**
76          * The method that returns the name of the class.
77          *
78          * @return The name of the class for which the configuration is stored.
79          */

80         public String JavaDoc getClassName() {
81             return className;
82         }
83         
84         
85         /**
86          * This method addes an entry to the config information map.
87          *
88          * @param entry The entry to add to the map.
89          */

90         public void addEntry(XMLConfigurationEntry entry) {
91             configInfo.put(entry.getKey(),entry);
92         }
93         
94         
95         /**
96          * This method returns the configuration information map.
97          *
98          * @return The map containing the configuration information.
99          */

100         public Map JavaDoc getConfigInfo() {
101             return configInfo;
102         }
103     }
104     
105     /**
106      * The inner class responsible for handling the contents of the Coadunation
107      * xml source document.
108      */

109     public class XMLConfigurationHandler extends DefaultHandler JavaDoc {
110         
111         // the classes static constant
112
private static final String JavaDoc CONFIGURATION = "configuration";
113         private static final String JavaDoc OBJECT_SECTION = "object";
114         private static final String JavaDoc OBJECT_SECTION_NAME = "name";
115         private static final String JavaDoc OBJECT_ENTRY = "entry";
116         private static final String JavaDoc OBJECT_ENTRY_KEY = "key";
117         private static final String JavaDoc OBJECT_ENTRY_TYPE = "type";
118         
119         // in section section variables
120
private boolean inConfiguration = false;
121         private boolean inObjectSection = false;
122         private boolean inObjectEntry = false ;
123         
124         // data variables
125
private String JavaDoc inData = null;
126         private XMLConfigInfo sectionConfig = null;
127         private XMLConfigurationEntry entry = null;
128         
129         // the classes private member variables
130
private Map JavaDoc configSections = null;
131         
132         /**
133          * The constructor of the xml configuration handler.
134          *
135          * @param configSections The configurtion sections
136          */

137         public XMLConfigurationHandler(Map JavaDoc configSections) {
138             this.configSections = configSections;
139         }
140         
141         
142         /**
143          * Parse the start of an element
144          */

145         public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName,
146                          Attributes JavaDoc attributes) throws SAXException JavaDoc {
147             try {
148                 // handle a package and retrieve the value information
149
if (qName.compareToIgnoreCase(CONFIGURATION) == 0) {
150                     inConfiguration = true;
151                 } else if (inConfiguration &&
152                         qName.compareToIgnoreCase(OBJECT_SECTION) == 0) {
153                     String JavaDoc name = (String JavaDoc)attributes.getValue(OBJECT_SECTION_NAME);
154                     if (name == null) {
155                         name = XMLConfigurationParser.GENERAL_SECTION;
156                     }
157                     sectionConfig = new XMLConfigInfo(name);
158                     inObjectSection = true;
159                 } else if (inConfiguration && inObjectSection &&
160                         qName.compareToIgnoreCase(OBJECT_ENTRY) == 0) {
161                     String JavaDoc name = (String JavaDoc)attributes.getValue(OBJECT_ENTRY_KEY);
162                     String JavaDoc type = (String JavaDoc)attributes.getValue(OBJECT_ENTRY_TYPE);
163                     entry = new XMLConfigurationEntry();
164                     entry.setKey(name);
165                     entry.setType(new XMLConfigurationType(type));
166                     inData = "";
167                     inObjectEntry = true;
168                 }
169             } catch (Exception JavaDoc ex) {
170                 throw new SAXException JavaDoc("Failed to handle the start element : " +
171                         ex.getMessage());
172             }
173         }
174         
175         /**
176          * Read in the characters
177          */

178         public void characters(char[] ch, int start, int length) {
179             if (inObjectEntry) {
180                 inData += new String JavaDoc(ch,start,length);
181             }
182         }
183         
184         /**
185          * Handle the end of an element
186          */

187         public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName)
188                 throws SAXException JavaDoc {
189             try {
190                 // handle a package and retrieve the value information
191
if (qName.compareToIgnoreCase(CONFIGURATION) == 0) {
192                     inConfiguration = false;
193                 } else if (inConfiguration &&
194                         qName.compareToIgnoreCase(OBJECT_SECTION) == 0) {
195                     configSections.put(sectionConfig.getClassName(),sectionConfig);
196                     inObjectSection = false;
197                 } else if (inConfiguration && inObjectSection &&
198                         qName.compareToIgnoreCase(OBJECT_ENTRY) == 0) {
199                     entry.setValueFromString(inData.trim());
200                     if (entry.isIntiailized() == false) {
201                         throw new SAXException JavaDoc(
202                                 "An entry has not been initialized");
203                     }
204                     sectionConfig.addEntry(entry);
205                     entry = null;
206                     inData = "";
207                     inObjectEntry = false;
208                 }
209             } catch (Exception JavaDoc ex) {
210                 throw new SAXException JavaDoc("Failed to handle the end element : " +
211                         ex.getMessage(),ex);
212             }
213         }
214         
215     }
216     
217     // classes private member variables
218
private Map JavaDoc configSections = null;
219     private XMLConfigurationHandler handler = null;
220     private long lastModifiedAtRead = 0;
221     private File JavaDoc configFile = null;
222     
223     /**
224      * Creates a new instance of XMLConfigurationParser
225      */

226     public XMLConfigurationParser() throws XMLConfigurationException {
227         try {
228             configSections = new HashMap JavaDoc();
229             handler = new XMLConfigurationHandler(configSections);
230             SAXParser JavaDoc parser = SAXParserFactory.newInstance().newSAXParser();
231             configFile = new File JavaDoc(
232                     System.getProperty(XML_CONFIG_PATH));
233             InputSource JavaDoc source = new InputSource JavaDoc(
234                     new FileReader JavaDoc(configFile));
235             parser.parse(source,handler);
236             lastModifiedAtRead = configFile.lastModified();
237         } catch (Exception JavaDoc ex) {
238             throw new XMLConfigurationException(
239                     "Failed load the configuration : " + ex.getMessage(),
240                     ex);
241         }
242     }
243     
244     
245     /**
246      * This method checks to see if the configuration file has been modified
247      * since it was last read and returns true if this is the case.
248      *
249      * @return TRUE if modified, FALSE if not.
250      */

251     public boolean modified() {
252         if (lastModifiedAtRead != configFile.lastModified()) {
253             return true;
254         }
255         return false;
256     }
257     
258     
259     /**
260      * This method returns a reference to the configuration class object.
261      *
262      * @return The reference to the xml configuration object
263      * @param classRef The reference to the class.
264      */

265     public XMLConfiguration getConfig(Class JavaDoc classRef) {
266         XMLConfiguration configuration = new XMLConfiguration(
267                 classRef.getName());
268         
269         copyConfig((XMLConfigInfo)configSections.get(GENERAL_SECTION),
270                 configuration);
271         copyConfig((XMLConfigInfo)configSections.get(
272                 classRef.getName()),configuration);
273         
274         return configuration;
275     }
276     
277     
278     /**
279      * This method copies the xml configuration.
280      *
281      * @param info The xml configuration information to copy.
282      * @param config The configuration class to copy the information into.
283      */

284     private void copyConfig(XMLConfigInfo info,XMLConfiguration config) {
285         if (info == null) {
286             return;
287         }
288         Map JavaDoc entries = info.getConfigInfo();
289         for (Iterator JavaDoc iter = entries.keySet().iterator(); iter.hasNext();) {
290             String JavaDoc key = (String JavaDoc)iter.next();
291             config.addConfigurationEntry(
292                     (XMLConfigurationEntry)entries.get(key));
293         }
294     }
295 }
296
Popular Tags