1 16 17 package com.icesoft.jasper.xmlparser; 18 19 import com.icesoft.jasper.JasperException; 20 import com.icesoft.jasper.compiler.Localizer; 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 import org.w3c.dom.Comment ; 24 import org.w3c.dom.Document ; 25 import org.w3c.dom.NamedNodeMap ; 26 import org.w3c.dom.Node ; 27 import org.w3c.dom.NodeList ; 28 import org.w3c.dom.Text ; 29 import org.xml.sax.EntityResolver ; 30 import org.xml.sax.ErrorHandler ; 31 import org.xml.sax.SAXException ; 32 import org.xml.sax.SAXParseException ; 33 34 import javax.xml.parsers.DocumentBuilder ; 35 import javax.xml.parsers.DocumentBuilderFactory ; 36 import javax.xml.parsers.ParserConfigurationException ; 37 import java.io.IOException ; 38 import java.io.InputStream ; 39 40 41 49 50 public class ParserUtils { 51 52 static Log log = LogFactory.getLog(ParserUtils.class); 54 55 58 static ErrorHandler errorHandler = new MyErrorHandler(); 59 60 63 public static EntityResolver entityResolver = new CachedEntityResolver(); 64 65 public static boolean validating = false; 67 68 70 79 public TreeNode parseXMLDocument(String uri, InputStream is) 80 throws JasperException { 81 82 Document document = null; 83 84 try { 86 DocumentBuilderFactory factory = 87 DocumentBuilderFactory.newInstance(); 88 factory.setNamespaceAware(true); 89 factory.setValidating(validating); 90 DocumentBuilder builder = factory.newDocumentBuilder(); 91 builder.setEntityResolver(entityResolver); 92 builder.setErrorHandler(errorHandler); 93 document = builder.parse(is); 94 } catch (ParserConfigurationException ex) { 95 throw new JasperException 96 (Localizer.getMessage("jsp.error.parse.xml", uri), ex); 97 } catch (SAXParseException ex) { 98 throw new JasperException 99 (Localizer.getMessage("jsp.error.parse.xml.line", 100 uri, 101 Integer.toString(ex.getLineNumber()), 102 Integer.toString( 103 ex.getColumnNumber())), 104 ex); 105 } catch (SAXException sx) { 106 throw new JasperException 107 (Localizer.getMessage("jsp.error.parse.xml", uri), sx); 108 } catch (IOException io) { 109 throw new JasperException 110 (Localizer.getMessage("jsp.error.parse.xml", uri), io); 111 } 112 113 return (convert(null, document.getDocumentElement())); 115 } 116 117 119 120 127 protected TreeNode convert(TreeNode parent, Node node) { 128 129 TreeNode treeNode = new TreeNode(node.getNodeName(), parent); 131 132 NamedNodeMap attributes = node.getAttributes(); 134 if (attributes != null) { 135 int n = attributes.getLength(); 136 for (int i = 0; i < n; i++) { 137 Node attribute = attributes.item(i); 138 treeNode.addAttribute(attribute.getNodeName(), 139 attribute.getNodeValue()); 140 } 141 } 142 143 NodeList children = node.getChildNodes(); 145 if (children != null) { 146 int n = children.getLength(); 147 for (int i = 0; i < n; i++) { 148 Node child = children.item(i); 149 if (child instanceof Comment ) 150 continue; 151 if (child instanceof Text ) { 152 String body = ((Text ) child).getData(); 153 if (body != null) { 154 body = body.trim(); 155 if (body.length() > 0) 156 treeNode.setBody(body); 157 } 158 } else { 159 TreeNode treeChild = convert(treeNode, child); 160 } 161 } 162 } 163 164 return (treeNode); 166 } 167 } 168 169 171 199 200 class MyErrorHandler implements ErrorHandler { 201 public void warning(SAXParseException ex) 202 throws SAXException { 203 System.out.println("ParserUtils: warning " + ex); 204 } 206 207 public void error(SAXParseException ex) 208 throws SAXException { 209 throw ex; 210 } 211 212 public void fatalError(SAXParseException ex) 213 throws SAXException { 214 throw ex; 215 } 216 } 217 | Popular Tags |