KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > igfay > jfig > XMLJFigParser


1 package org.igfay.jfig;
2
3 import java.io.IOException JavaDoc;
4
5 import javax.xml.parsers.DocumentBuilder JavaDoc;
6 import javax.xml.parsers.ParserConfigurationException JavaDoc;
7
8 import org.apache.log4j.Logger;
9 import org.igfay.util.PrettyPrinter;
10 import org.w3c.dom.Document JavaDoc;
11 import org.w3c.dom.Element JavaDoc;
12 import org.w3c.dom.NodeList JavaDoc;
13 import org.xml.sax.InputSource JavaDoc;
14 import org.xml.sax.SAXException JavaDoc;
15
16 /**
17  * @author conrad4
18  *
19  * Parse configuration files that are in XML format.
20  */

21 public class XMLJFigParser extends JFigParser {
22
23     private static Logger logger = Logger.getLogger(XMLJFigParser.class);
24
25     public XMLJFigParser(JFig config, JFigLocatorIF jfigLocator) {
26         super(config, jfigLocator);
27     }
28
29     /**
30      * Process XML Configuration file
31      */

32     protected boolean processConfig() throws JFigException {
33         logger.debug("Process file " + getConfigFileName());
34         Element JavaDoc element = null;
35         try {
36             element = parse();
37         } catch (XMLParseException e) {
38             throw new JFigException(e.getMessage() + " Requested File: " + getConfigFileName());
39         }
40         if (logger.isDebugEnabled()) {
41             logger.debug("element\n");
42             PrettyPrinter.printNode(element);
43             logger.debug("process includes");
44         }
45         processIncludes(element);
46
47         logger.debug("process sections");
48         processSections(element);
49         return true;
50     }
51
52     /**
53      * @author conrad4
54      *
55      * For each include element, process the included configuration file
56      */

57     protected void processIncludes(Element JavaDoc element) throws JFigException {
58         NodeList JavaDoc nodeList = element.getElementsByTagName(JFigConstants.INCLUDE);
59         for (int i = 0; i < nodeList.getLength(); i++) {
60             Element JavaDoc node = (Element JavaDoc) nodeList.item(i);
61             String JavaDoc fileName = node.getAttribute(JFigConstants.NAME);
62             logger.debug("fileName " + fileName);
63             if (isNewFile(fileName)) {
64                 getAllConfigFiles().put(fileName, fileName);
65                 setConfigLocation(node);
66                 getJFigLocator().setConfigFileName(fileName);
67                 JFigParser parser = new XMLJFigParser(getConfig(), getJFigLocator());
68                 logger.debug("call parser to process " + fileName);
69                 parser.processConfig();
70             }
71
72         }
73     }
74
75     /**
76      * Get the configLocation of the included config file.
77      * If something is specified, reset.
78      * If nothing is specified, leave it alone.
79      * Note: Subsequent includes will use the last value set.
80      * It might be better to have them use the original value.
81      * However, it could get very complicated if one is mixing locations.
82      *
83      *
84      * @param element
85      * @return
86      * @throws JFigException
87      */

88     private String JavaDoc setConfigLocation(Element JavaDoc element) throws JFigException {
89         String JavaDoc locationValue = element.getAttribute("location");
90         if (locationValue != null && locationValue.length() > 0) {
91             getJFigLocator().setConfigLocation(locationValue);
92         }
93         logger.debug("~ locationValue -" + locationValue + "-");
94         return locationValue;
95     }
96
97     /**
98      * For each section element, find the entry elements
99      */

100     protected void processSections(Element JavaDoc element) {
101         NodeList JavaDoc sectionNodeList = element.getElementsByTagName(JFigConstants.SECTION);
102         for (int i = 0; i < sectionNodeList.getLength(); i++) {
103             Element JavaDoc sectionNode = (Element JavaDoc) sectionNodeList.item(i);
104             String JavaDoc sectionString = sectionNode.getAttribute(JFigConstants.NAME);
105             getConfigDictionary().getSectionNamed(sectionString, true);
106
107             processEntries(sectionNode, sectionString);
108         }
109     }
110
111     /**
112      * For each entry element, process key value pair
113      */

114     protected void processEntries(Element JavaDoc sectionNode, String JavaDoc sectionString) {
115
116         NodeList JavaDoc entryNodeList = sectionNode.getElementsByTagName(JFigConstants.ENTRY);
117         for (int j = 0; j < entryNodeList.getLength(); j++) {
118             Element JavaDoc entryNode = (Element JavaDoc) entryNodeList.item(j);
119             String JavaDoc keyString = entryNode.getAttribute(JFigConstants.KEY);
120             // If not value attribute is supplied, make it null
121
String JavaDoc valueString = null;
122             if (entryNode.getAttributeNode(JFigConstants.VALUE) != null) {
123                 valueString = entryNode.getAttribute(JFigConstants.VALUE);
124             }
125             getConfigDictionary().addKeyValueToSection(sectionString, keyString, valueString);
126         }
127     }
128
129     /**
130      * Ensure same file is not processed multiple times
131      *
132      * @param fileName
133      * @return boolean
134      */

135     protected boolean isNewFile(String JavaDoc fileName) {
136         boolean isNewFile = getAllConfigFiles().get(fileName) == null;
137         logger.debug("" + isNewFile);
138         return isNewFile;
139     }
140
141     /**
142      * Parse the config file into an xml document
143      * @return
144      * @throws JFigException
145      * @throws XMLParseException
146      */

147     protected Element JavaDoc parse() throws JFigException, XMLParseException {
148         InputSource JavaDoc isrc = new InputSource JavaDoc(getJFigLocator().getInputStream());
149         logger.info("Process file " + getConfigFileName());
150
151         javax.xml.parsers.DocumentBuilderFactory JavaDoc dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();
152         Document JavaDoc document = null;
153         try {
154             DocumentBuilder JavaDoc db = dbf.newDocumentBuilder();
155             document = db.parse(isrc);
156         } catch (ParserConfigurationException JavaDoc e) {
157             throw new XMLParseException(e.getMessage());
158         } catch (SAXException JavaDoc e) {
159             throw new XMLParseException(e);
160         } catch (IOException JavaDoc e) {
161
162             throw new XMLParseException(e.getMessage());
163         }
164         return document.getDocumentElement();
165     }
166
167 }
Popular Tags