org.apache.jasper.xmlparser.ParserUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsp-2.1-glassfish Show documentation
Show all versions of jsp-2.1-glassfish Show documentation
JSP2.1 Jasper implementation from Glassfish
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development * and Distribution License("CDDL") (collectively, the "License"). You * may not use this file except in compliance with the License. You can obtain * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific * language governing permissions and limitations under the License. * * When distributing the software, include this License Header Notice in each * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt. * Sun designates this particular file as subject to the "Classpath" exception * as provided by Sun in the GPL Version 2 section of the License file that * accompanied this code. If applicable, add the following below the License * Header, with the fields enclosed by brackets [] replaced by your own * identifying information: "Portions Copyrighted [year] * [name of copyright owner]" * * Contributor(s): * * If you wish your version of this file to be governed by only the CDDL or * only the GPL Version 2, indicate your decision by adding "[Contributor] * elects to include this software in this distribution under the [CDDL or GPL * Version 2] license." If you don't indicate a single choice of license, a * recipient has the option to distribute your version of this file under * either the CDDL, the GPL Version 2 or to extend the choice of license to * its licensees as provided above. However, if you add GPL Version 2 code * and therefore, elected the GPL Version 2 license, then the option applies * only if the new code is made subject to such option by the copyright * holder. * * * This file incorporates work covered by the following copyright and * permission notice: * * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.jasper.xmlparser; import java.io.IOException; import java.io.InputStream; import java.io.Reader; import java.util.HashMap; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import javax.xml.XMLConstants; import com.sun.org.apache.commons.logging.Log; import com.sun.org.apache.commons.logging.LogFactory; import org.apache.jasper.Constants; import org.apache.jasper.JasperException; import org.apache.jasper.compiler.Localizer; import org.w3c.dom.Comment; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.Text; import org.w3c.dom.ls.LSResourceResolver; import org.w3c.dom.ls.LSInput; import org.xml.sax.EntityResolver; import org.xml.sax.ErrorHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; /** * XML parsing utilities for processing web application deployment * descriptor and tag library descriptor files. FIXME - make these * use a separate class loader for the parser to be used. * * @author Craig R. McClanahan * @version $Revision: 1.11.6.3 $ $Date: 2009/04/29 21:28:41 $ */ public class ParserUtils { // Logger static Log log = LogFactory.getLog(ParserUtils.class); /** * An error handler for use when parsing XML documents. */ static ErrorHandler errorHandler = new MyErrorHandler(); /** * An entity resolver for use when parsing XML documents. */ static EntityResolver entityResolver = new MyEntityResolver(); /* SJSAS 6384538 public static boolean validating = false; */ static String schemaResourcePrefix; private static final String SCHEMA_LOCATION_ATTR = "schemaLocation"; private static HashMap
* that corresponds to the root node of the document tree. * * @param uri URI of the XML document being parsed * @param is Input source containing the deployment descriptor * * @exception JasperException if an I/O or parsing error has occurred */ public TreeNode parseXMLDocument(String uri, InputSource is) throws JasperException { return parseXMLDocument(uri, is, false); } /** * Parse the specified XML document, and return aschemaCache = new HashMap (); /** * List of the Public IDs that we cache, and their * associated location. This is used by * an EntityResolver to return the location of the * cached copy of a DTD. */ static final String[] CACHED_DTD_PUBLIC_IDS = { Constants.TAGLIB_DTD_PUBLIC_ID_11, Constants.TAGLIB_DTD_PUBLIC_ID_12, Constants.WEBAPP_DTD_PUBLIC_ID_22, Constants.WEBAPP_DTD_PUBLIC_ID_23, }; // START PWC 6386258 static final String[] CACHED_DTD_RESOURCE_PATHS = { Constants.TAGLIB_DTD_RESOURCE_PATH_11, Constants.TAGLIB_DTD_RESOURCE_PATH_12, Constants.WEBAPP_DTD_RESOURCE_PATH_22, Constants.WEBAPP_DTD_RESOURCE_PATH_23, }; static final String[] CACHED_SCHEMA_RESOURCE_PATHS = { Constants.TAGLIB_SCHEMA_RESOURCE_PATH_20, Constants.TAGLIB_SCHEMA_RESOURCE_PATH_21, Constants.WEBAPP_SCHEMA_RESOURCE_PATH_24, Constants.WEBAPP_SCHEMA_RESOURCE_PATH_25, }; // END PWC 6386258 // --------------------------------------------------------- Static Methods public static void setEntityResolver(EntityResolver er) { entityResolver = er; } // START PWC 6386258 /** * Sets the path prefix for .xsd resources */ public static void setSchemaResourcePrefix(String prefix) { schemaResourcePrefix = prefix; for (int i=0; i TreeNode TreeNode
* that corresponds to the root node of the document tree. * * @param uri URI of the XML document being parsed * @param is Input source containing the deployment descriptor * @param validate true if the XML document needs to be validated against * its DTD or schema, false otherwise * * @exception JasperException if an I/O or parsing error has occurred */ public TreeNode parseXMLDocument(String uri, InputSource is, boolean validate) throws JasperException { Document document = null; // Perform an XML parse of this document, via JAXP // START 6412405 ClassLoader currentLoader = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader( getClass().getClassLoader()); // END 6412405 try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); /* See CR 6399139 factory.setFeature( "http://apache.org/xml/features/validation/dynamic", true); */ DocumentBuilder builder = factory.newDocumentBuilder(); builder.setEntityResolver(entityResolver); builder.setErrorHandler(errorHandler); document = builder.parse(is); document.setDocumentURI(uri); if (validate) { Schema schema = getSchema(document); if (schema != null) { // Validate TLD against specified schema schema.newValidator().validate(new DOMSource(document)); } /* See CR 6399139 else { log.warn(Localizer.getMessage( "jsp.warning.dtdValidationNotSupported")); } */ } } catch (ParserConfigurationException ex) { throw new JasperException (Localizer.getMessage("jsp.error.parse.xml", uri), ex); } catch (SAXParseException ex) { throw new JasperException (Localizer.getMessage("jsp.error.parse.xml.line", uri, Integer.toString(ex.getLineNumber()), Integer.toString(ex.getColumnNumber())), ex); } catch (SAXException sx) { throw new JasperException (Localizer.getMessage("jsp.error.parse.xml", uri), sx); } catch (IOException io) { throw new JasperException (Localizer.getMessage("jsp.error.parse.xml", uri), io); // START 6412405 } finally { Thread.currentThread().setContextClassLoader(currentLoader); // END 6412405 } // Convert the resulting document to a graph of TreeNodes return (convert(null, document.getDocumentElement())); } /** * Parse the specified XML document, and return aTreeNode
* that corresponds to the root node of the document tree. * * @param uri URI of the XML document being parsed * @param is Input stream containing the deployment descriptor * * @exception JasperException if an I/O or parsing error has occurred */ public TreeNode parseXMLDocument(String uri, InputStream is) throws JasperException { return parseXMLDocument(uri, new InputSource(is), false); } /** * Parse the specified XML document, and return aTreeNode
* that corresponds to the root node of the document tree. * * @param uri URI of the XML document being parsed * @param is Input stream containing the deployment descriptor * @param validate true if the XML document needs to be validated against * its DTD or schema, false otherwise * * @exception JasperException if an I/O or parsing error has occurred */ public TreeNode parseXMLDocument(String uri, InputStream is, boolean validate) throws JasperException { return parseXMLDocument(uri, new InputSource(is), validate); } // ------------------------------------------------------ Protected Methods /** * Create and return a TreeNode that corresponds to the specified Node, * including processing all of the attributes and children nodes. * * @param parent The parent TreeNode (if any) for the new TreeNode * @param node The XML document Node to be converted */ protected TreeNode convert(TreeNode parent, Node node) { // Construct a new TreeNode for this node TreeNode treeNode = new TreeNode(node.getNodeName(), parent); // Convert all attributes of this node NamedNodeMap attributes = node.getAttributes(); if (attributes != null) { int n = attributes.getLength(); for (int i = 0; i < n; i++) { Node attribute = attributes.item(i); treeNode.addAttribute(attribute.getNodeName(), attribute.getNodeValue()); } } // Create and attach all children of this node NodeList children = node.getChildNodes(); if (children != null) { int n = children.getLength(); for (int i = 0; i < n; i++) { Node child = children.item(i); if (child instanceof Comment) continue; if (child instanceof Text) { String body = ((Text) child).getData(); if (body != null) { body = body.trim(); if (body.length() > 0) treeNode.setBody(body); } } else { TreeNode treeChild = convert(treeNode, child); } } } // Return the completed TreeNode graph return (treeNode); } // -------------------------------------------------------- Private Methods /* * Gets the compiled schema referenced by the given XML document. * * @param document The XML document to validate * * @return The schema against which to validate */ private static Schema getSchema(Document document) throws SAXException, JasperException { Schema schema = null; Element root = document.getDocumentElement(); NamedNodeMap map = root.getAttributes(); for (int i=0; map!=null && i