| 1 package org.igfay.jfig; 2 3 import java.io.IOException ; 4 5 import javax.xml.parsers.DocumentBuilder ; 6 import javax.xml.parsers.ParserConfigurationException ; 7 8 import org.apache.log4j.Logger; 9 import org.igfay.util.PrettyPrinter; 10 import org.w3c.dom.Document ; 11 import org.w3c.dom.Element ; 12 import org.w3c.dom.NodeList ; 13 import org.xml.sax.InputSource ; 14 import org.xml.sax.SAXException ; 15 16 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 32 protected boolean processConfig() throws JFigException { 33 logger.debug("Process file " + getConfigFileName()); 34 Element 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 57 protected void processIncludes(Element element) throws JFigException { 58 NodeList nodeList = element.getElementsByTagName(JFigConstants.INCLUDE); 59 for (int i = 0; i < nodeList.getLength(); i++) { 60 Element node = (Element ) nodeList.item(i); 61 String 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 88 private String setConfigLocation(Element element) throws JFigException { 89 String 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 100 protected void processSections(Element element) { 101 NodeList sectionNodeList = element.getElementsByTagName(JFigConstants.SECTION); 102 for (int i = 0; i < sectionNodeList.getLength(); i++) { 103 Element sectionNode = (Element ) sectionNodeList.item(i); 104 String sectionString = sectionNode.getAttribute(JFigConstants.NAME); 105 getConfigDictionary().getSectionNamed(sectionString, true); 106 107 processEntries(sectionNode, sectionString); 108 } 109 } 110 111 114 protected void processEntries(Element sectionNode, String sectionString) { 115 116 NodeList entryNodeList = sectionNode.getElementsByTagName(JFigConstants.ENTRY); 117 for (int j = 0; j < entryNodeList.getLength(); j++) { 118 Element entryNode = (Element ) entryNodeList.item(j); 119 String keyString = entryNode.getAttribute(JFigConstants.KEY); 120 String 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 135 protected boolean isNewFile(String fileName) { 136 boolean isNewFile = getAllConfigFiles().get(fileName) == null; 137 logger.debug("" + isNewFile); 138 return isNewFile; 139 } 140 141 147 protected Element parse() throws JFigException, XMLParseException { 148 InputSource isrc = new InputSource (getJFigLocator().getInputStream()); 149 logger.info("Process file " + getConfigFileName()); 150 151 javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance(); 152 Document document = null; 153 try { 154 DocumentBuilder db = dbf.newDocumentBuilder(); 155 document = db.parse(isrc); 156 } catch (ParserConfigurationException e) { 157 throw new XMLParseException(e.getMessage()); 158 } catch (SAXException e) { 159 throw new XMLParseException(e); 160 } catch (IOException e) { 161 162 throw new XMLParseException(e.getMessage()); 163 } 164 return document.getDocumentElement(); 165 } 166 167 } | Popular Tags |