FileDocCategorySizeDatePackage
ParserUtils.javaAPI DocGlassfish v2 API20088Fri May 04 22:33:00 BST 2007org.apache.jasper.xmlparser

ParserUtils

public class ParserUtils extends Object
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 $ $Date: 2007/05/05 05:32:59 $

Fields Summary
static com.sun.org.apache.commons.logging.Log
log
static ErrorHandler
errorHandler
An error handler for use when parsing XML documents.
static EntityResolver
entityResolver
An entity resolver for use when parsing XML documents.
static String
schemaResourcePrefix
private static final String
SCHEMA_LOCATION_ATTR
private static HashMap
schemaCache
static final String[]
CACHED_DTD_PUBLIC_IDS
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_RESOURCE_PATHS
static final String[]
CACHED_SCHEMA_RESOURCE_PATHS
Constructors Summary
Methods Summary
protected TreeNodeconvert(TreeNode parent, org.w3c.dom.Node node)
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


        // 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 static javax.xml.validation.SchemagetSchema(org.w3c.dom.Document document)


        Schema schema = null;
        Element root = document.getDocumentElement();
        NamedNodeMap map = root.getAttributes();
        for (int i=0; map!=null && i<map.getLength(); i++) {
            if (SCHEMA_LOCATION_ATTR.equals(map.item(i).getLocalName())) {
                String schemaLocation = map.item(i).getNodeValue();
                if (Constants.SCHEMA_LOCATION_JSP_20.equals(schemaLocation)) {
                    schema = getSchema(
                            Constants.TAGLIB_SCHEMA_PUBLIC_ID_20);
                    break;
                } else if (Constants.SCHEMA_LOCATION_JSP_21.equals(
                                                schemaLocation)) {
                    schema = getSchema(
                            Constants.TAGLIB_SCHEMA_PUBLIC_ID_21);
                    break;
                } else if (Constants.SCHEMA_LOCATION_WEBAPP_24.equals(
                                                schemaLocation)) {
                    schema = getSchema(
                            Constants.WEBAPP_SCHEMA_PUBLIC_ID_24);
                    break;
                } else if (Constants.SCHEMA_LOCATION_WEBAPP_25.equals(
                                                schemaLocation)) {
                    schema = getSchema(
                            Constants.WEBAPP_SCHEMA_PUBLIC_ID_25);
                    break;
                } else {
                    throw new JasperException(Localizer.getMessage(
                        "jsp.error.parse.unknownTldSchemaLocation",
                        document.getDocumentURI(),
                        map.item(i).getNodeValue()));
                }
            }
        }

        return schema;
    
private static javax.xml.validation.SchemagetSchema(java.lang.String schemaPublicId)


        Schema schema = schemaCache.get(schemaPublicId);
        if (schema == null) {
            synchronized (schemaCache) {
                schema = schemaCache.get(schemaPublicId);
                if (schema == null) {
                    SchemaFactory schemaFactory = SchemaFactory.newInstance(
                        XMLConstants.W3C_XML_SCHEMA_NS_URI);
                    schemaFactory.setResourceResolver(
                        new MyLSResourceResolver());
                    schemaFactory.setErrorHandler(new MyErrorHandler());
                    schema = schemaFactory.newSchema(new StreamSource(
                    ParserUtils.class.getResourceAsStream(
                        schemaResourcePrefix + schemaPublicId)));
                    schemaCache.put(schemaPublicId, schema);
                }
            }
        }

        return schema;
    
public TreeNodeparseXMLDocument(java.lang.String uri, org.xml.sax.InputSource is)
Parse the specified XML document, and return a 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
exception
JasperException if an I/O or parsing error has occurred

        return parseXMLDocument(uri, is, false);
    
public TreeNodeparseXMLDocument(java.lang.String uri, org.xml.sax.InputSource is, boolean validate)
Parse the specified XML document, and return a 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


        Document document = null;

        // Perform an XML parse of this document, via JAXP
        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);
	}

        // Convert the resulting document to a graph of TreeNodes
        return (convert(null, document.getDocumentElement()));
    
public TreeNodeparseXMLDocument(java.lang.String uri, java.io.InputStream is)
Parse the specified XML document, and return a TreeNode 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

        return parseXMLDocument(uri, new InputSource(is), false);
    
public TreeNodeparseXMLDocument(java.lang.String uri, java.io.InputStream is, boolean validate)
Parse the specified XML document, and return a TreeNode 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

        return  parseXMLDocument(uri, new InputSource(is), validate);
    
public static voidsetDtdResourcePrefix(java.lang.String prefix)
Sets the path prefix for .dtd resources

        for (int i=0; i<CACHED_DTD_RESOURCE_PATHS.length; i++) {
            String path = CACHED_DTD_RESOURCE_PATHS[i];
            int index = path.lastIndexOf('/");
            if (index != -1) {
                CACHED_DTD_RESOURCE_PATHS[i] =
                    prefix + path.substring(index+1);
            }
        }
    
public static voidsetSchemaResourcePrefix(java.lang.String prefix)
Sets the path prefix for .xsd resources

    // END PWC 6386258


    // --------------------------------------------------------- Static Methods


    // START PWC 6386258
                
         

        schemaResourcePrefix = prefix;

        for (int i=0; i<CACHED_SCHEMA_RESOURCE_PATHS.length; i++) {
            String path = CACHED_SCHEMA_RESOURCE_PATHS[i];
            int index = path.lastIndexOf('/");
            if (index != -1) {
                CACHED_SCHEMA_RESOURCE_PATHS[i] =
                    prefix + path.substring(index+1);
            }
        }