1 17 18 package org.apache.jasper.xmlparser; 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 23 import javax.xml.parsers.DocumentBuilder ; 24 import javax.xml.parsers.DocumentBuilderFactory ; 25 import javax.xml.parsers.ParserConfigurationException ; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.apache.jasper.Constants; 30 import org.apache.jasper.JasperException; 31 import org.apache.jasper.compiler.Localizer; 32 import org.w3c.dom.Comment ; 33 import org.w3c.dom.Document ; 34 import org.w3c.dom.NamedNodeMap ; 35 import org.w3c.dom.Node ; 36 import org.w3c.dom.NodeList ; 37 import org.w3c.dom.Text ; 38 import org.xml.sax.EntityResolver ; 39 import org.xml.sax.ErrorHandler ; 40 import org.xml.sax.InputSource ; 41 import org.xml.sax.SAXException ; 42 import org.xml.sax.SAXParseException ; 43 44 45 53 54 public class ParserUtils { 55 56 59 static ErrorHandler errorHandler = new MyErrorHandler(); 60 61 64 static EntityResolver entityResolver = new MyEntityResolver(); 65 66 public static boolean validating = false; 68 69 70 72 82 public TreeNode parseXMLDocument(String uri, InputSource is) 83 throws JasperException { 84 85 Document document = null; 86 87 try { 89 DocumentBuilderFactory factory = 90 DocumentBuilderFactory.newInstance(); 91 factory.setNamespaceAware(true); 92 factory.setValidating(validating); 93 DocumentBuilder builder = factory.newDocumentBuilder(); 94 builder.setEntityResolver(entityResolver); 95 builder.setErrorHandler(errorHandler); 96 document = builder.parse(is); 97 } catch (ParserConfigurationException ex) { 98 throw new JasperException 99 (Localizer.getMessage("jsp.error.parse.xml", uri), ex); 100 } catch (SAXParseException ex) { 101 throw new JasperException 102 (Localizer.getMessage("jsp.error.parse.xml.line", 103 uri, 104 Integer.toString(ex.getLineNumber()), 105 Integer.toString(ex.getColumnNumber())), 106 ex); 107 } catch (SAXException sx) { 108 throw new JasperException 109 (Localizer.getMessage("jsp.error.parse.xml", uri), sx); 110 } catch (IOException io) { 111 throw new JasperException 112 (Localizer.getMessage("jsp.error.parse.xml", uri), io); 113 } 114 115 return (convert(null, document.getDocumentElement())); 117 } 118 119 120 130 public TreeNode parseXMLDocument(String uri, InputStream is) 131 throws JasperException { 132 133 return (parseXMLDocument(uri, new InputSource (is))); 134 } 135 136 137 139 140 147 protected TreeNode convert(TreeNode parent, Node node) { 148 149 TreeNode treeNode = new TreeNode(node.getNodeName(), parent); 151 152 NamedNodeMap attributes = node.getAttributes(); 154 if (attributes != null) { 155 int n = attributes.getLength(); 156 for (int i = 0; i < n; i++) { 157 Node attribute = attributes.item(i); 158 treeNode.addAttribute(attribute.getNodeName(), 159 attribute.getNodeValue()); 160 } 161 } 162 163 NodeList children = node.getChildNodes(); 165 if (children != null) { 166 int n = children.getLength(); 167 for (int i = 0; i < n; i++) { 168 Node child = children.item(i); 169 if (child instanceof Comment ) 170 continue; 171 if (child instanceof Text ) { 172 String body = ((Text ) child).getData(); 173 if (body != null) { 174 body = body.trim(); 175 if (body.length() > 0) 176 treeNode.setBody(body); 177 } 178 } else { 179 TreeNode treeChild = convert(treeNode, child); 180 } 181 } 182 } 183 184 return (treeNode); 186 } 187 } 188 189 190 192 class MyEntityResolver implements EntityResolver { 193 194 private Log log = LogFactory.getLog(MyEntityResolver.class); 196 197 public InputSource resolveEntity(String publicId, String systemId) 198 throws SAXException { 199 for (int i = 0; i < Constants.CACHED_DTD_PUBLIC_IDS.length; i++) { 200 String cachedDtdPublicId = Constants.CACHED_DTD_PUBLIC_IDS[i]; 201 if (cachedDtdPublicId.equals(publicId)) { 202 String resourcePath = Constants.CACHED_DTD_RESOURCE_PATHS[i]; 203 InputStream input = this.getClass().getResourceAsStream( 204 resourcePath); 205 if (input == null) { 206 throw new SAXException (Localizer.getMessage( 207 "jsp.error.internal.filenotfound", resourcePath)); 208 } 209 InputSource isrc = new InputSource (input); 210 return isrc; 211 } 212 } 213 if (log.isDebugEnabled()) 214 log.debug("Resolve entity failed" + publicId + " " + systemId); 215 log.error(Localizer.getMessage("jsp.error.parse.xml.invalidPublicId", 216 publicId)); 217 return null; 218 } 219 } 220 221 class MyErrorHandler implements ErrorHandler { 222 223 private Log log = LogFactory.getLog(MyErrorHandler.class); 225 226 public void warning(SAXParseException ex) throws SAXException { 227 if (log.isDebugEnabled()) 228 log.debug("ParserUtils: warning ", ex); 229 } 231 232 public void error(SAXParseException ex) throws SAXException { 233 throw ex; 234 } 235 236 public void fatalError(SAXParseException ex) throws SAXException { 237 throw ex; 238 } 239 } | Popular Tags |