Search in sources :

Example 1 with TreeWalker

use of org.apache.xml.utils.TreeWalker in project nokogiri by sparklemotion.

the class DOM2DTMExt method dispatchToEvents.

/**
     * Directly create SAX parser events from a subtree.
     *
     * @param nodeHandle The node ID.
     * @param ch A non-null reference to a ContentHandler.
     *
     * @throws org.xml.sax.SAXException
     */
public void dispatchToEvents(int nodeHandle, org.xml.sax.ContentHandler ch) throws org.xml.sax.SAXException {
    TreeWalker treeWalker = m_walker;
    ContentHandler prevCH = treeWalker.getContentHandler();
    if (null != prevCH) {
        treeWalker = new TreeWalker(null);
    }
    treeWalker.setContentHandler(ch);
    try {
        Node node = getNode(nodeHandle);
        treeWalker.traverseFragment(node);
    } finally {
        treeWalker.setContentHandler(null);
    }
}
Also used : Node(org.w3c.dom.Node) TreeWalker(org.apache.xml.utils.TreeWalker) ContentHandler(org.xml.sax.ContentHandler)

Example 2 with TreeWalker

use of org.apache.xml.utils.TreeWalker in project robovm by robovm.

the class ProcessorInclude method parse.

/**
   * Set off a new parse for an included or imported stylesheet.  This will 
   * set the {@link StylesheetHandler} to a new state, and recurse in with 
   * a new set of parse events.  Once this function returns, the state of 
   * the StylesheetHandler should be restored.
   *
   * @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
   * @param uri The Namespace URI, which should be the XSLT namespace.
   * @param localName The local name (without prefix), which should be "include" or "import".
   * @param rawName The qualified name (with prefix).
   * @param attributes The list of attributes on the xsl:include or xsl:import element.
   *
   * @throws org.xml.sax.SAXException Any SAX exception, possibly
   *            wrapping another exception.
   */
protected void parse(StylesheetHandler handler, String uri, String localName, String rawName, Attributes attributes) throws org.xml.sax.SAXException {
    TransformerFactoryImpl processor = handler.getStylesheetProcessor();
    URIResolver uriresolver = processor.getURIResolver();
    try {
        Source source = null;
        if (null != uriresolver) {
            // There is a user provided URI resolver.
            // At the startElement() call we would
            // have tried to obtain a Source from it
            // which we now retrieve
            source = handler.peekSourceFromURIResolver();
            if (null != source && source instanceof DOMSource) {
                Node node = ((DOMSource) source).getNode();
                // There is a user provided URI resolver.
                // At the startElement() call we would
                // have already pushed the system ID, obtained
                // from either the source.getSystemId(), if non-null
                // or from SystemIDResolver.getAbsoluteURI() as a backup
                // which we now retrieve.
                String systemId = handler.peekImportURL();
                // stylesheet module onto the stack.
                if (systemId != null)
                    handler.pushBaseIndentifier(systemId);
                TreeWalker walker = new TreeWalker(handler, new org.apache.xml.utils.DOM2Helper(), systemId);
                try {
                    walker.traverse(node);
                } catch (org.xml.sax.SAXException se) {
                    throw new TransformerException(se);
                }
                if (systemId != null)
                    handler.popBaseIndentifier();
                return;
            }
        }
        if (null == source) {
            String absURL = SystemIDResolver.getAbsoluteURI(getHref(), handler.getBaseIdentifier());
            source = new StreamSource(absURL);
        }
        // possible callback to a class that over-rides this method.
        source = processSource(handler, source);
        XMLReader reader = null;
        if (source instanceof SAXSource) {
            SAXSource saxSource = (SAXSource) source;
            // may be null
            reader = saxSource.getXMLReader();
        }
        InputSource inputSource = SAXSource.sourceToInputSource(source);
        if (null == reader) {
            // Use JAXP1.1 ( if possible )
            try {
                javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance();
                factory.setNamespaceAware(true);
                if (handler.getStylesheetProcessor().isSecureProcessing()) {
                    try {
                        factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
                    } catch (org.xml.sax.SAXException se) {
                    }
                }
                javax.xml.parsers.SAXParser jaxpParser = factory.newSAXParser();
                reader = jaxpParser.getXMLReader();
            } catch (javax.xml.parsers.ParserConfigurationException ex) {
                throw new org.xml.sax.SAXException(ex);
            } catch (javax.xml.parsers.FactoryConfigurationError ex1) {
                throw new org.xml.sax.SAXException(ex1.toString());
            } catch (NoSuchMethodError ex2) {
            } catch (AbstractMethodError ame) {
            }
        }
        if (null == reader)
            reader = XMLReaderFactory.createXMLReader();
        if (null != reader) {
            reader.setContentHandler(handler);
            // Push the absolute URI of the included/imported
            // stylesheet module onto the stack.
            handler.pushBaseIndentifier(inputSource.getSystemId());
            try {
                reader.parse(inputSource);
            } finally {
                handler.popBaseIndentifier();
            }
        }
    } catch (IOException ioe) {
        handler.error(XSLTErrorResources.ER_IOEXCEPTION, new Object[] { getHref() }, ioe);
    } catch (TransformerException te) {
        handler.error(te.getMessage(), te);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) Node(org.w3c.dom.Node) URIResolver(javax.xml.transform.URIResolver) DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) TransformerException(javax.xml.transform.TransformerException) XMLReader(org.xml.sax.XMLReader) StreamSource(javax.xml.transform.stream.StreamSource) TreeWalker(org.apache.xml.utils.TreeWalker) IOException(java.io.IOException) SAXSource(javax.xml.transform.sax.SAXSource)

Example 3 with TreeWalker

use of org.apache.xml.utils.TreeWalker in project robovm by robovm.

the class DOM2DTM method dispatchToEvents.

/**
   * Directly create SAX parser events from a subtree.
   *
   * @param nodeHandle The node ID.
   * @param ch A non-null reference to a ContentHandler.
   *
   * @throws org.xml.sax.SAXException
   */
public void dispatchToEvents(int nodeHandle, org.xml.sax.ContentHandler ch) throws org.xml.sax.SAXException {
    TreeWalker treeWalker = m_walker;
    ContentHandler prevCH = treeWalker.getContentHandler();
    if (null != prevCH) {
        treeWalker = new TreeWalker(null);
    }
    treeWalker.setContentHandler(ch);
    try {
        Node node = getNode(nodeHandle);
        treeWalker.traverseFragment(node);
    } finally {
        treeWalker.setContentHandler(null);
    }
}
Also used : Node(org.w3c.dom.Node) TreeWalker(org.apache.xml.utils.TreeWalker) ContentHandler(org.xml.sax.ContentHandler)

Example 4 with TreeWalker

use of org.apache.xml.utils.TreeWalker in project nokogiri by sparklemotion.

the class DOM2DTM method dispatchToEvents.

/**
 * Directly create SAX parser events from a subtree.
 *
 * @param nodeHandle The node ID.
 * @param ch A non-null reference to a ContentHandler.
 *
 * @throws org.xml.sax.SAXException
 */
public void dispatchToEvents(int nodeHandle, org.xml.sax.ContentHandler ch) throws org.xml.sax.SAXException {
    TreeWalker treeWalker = m_walker;
    ContentHandler prevCH = treeWalker.getContentHandler();
    if (null != prevCH) {
        treeWalker = new TreeWalker(null);
    }
    treeWalker.setContentHandler(ch);
    try {
        Node node = getNode(nodeHandle);
        treeWalker.traverseFragment(node);
    } finally {
        treeWalker.setContentHandler(null);
    }
}
Also used : Node(org.w3c.dom.Node) TreeWalker(org.apache.xml.utils.TreeWalker) ContentHandler(org.xml.sax.ContentHandler)

Example 5 with TreeWalker

use of org.apache.xml.utils.TreeWalker in project j2objc by google.

the class TransformerFactoryImpl method processFromNode.

public javax.xml.transform.Templates processFromNode(Node node) throws TransformerConfigurationException {
    try {
        TemplatesHandler builder = newTemplatesHandler();
        TreeWalker walker = new TreeWalker(builder, new org.apache.xml.utils.DOM2Helper(), builder.getSystemId());
        walker.traverse(node);
        return builder.getTemplates();
    } catch (org.xml.sax.SAXException se) {
        if (m_errorListener != null) {
            try {
                m_errorListener.fatalError(new TransformerException(se));
            } catch (TransformerConfigurationException ex) {
                throw ex;
            } catch (TransformerException ex) {
                throw new TransformerConfigurationException(ex);
            }
            return null;
        } else {
            // se.printStackTrace();
            throw new TransformerConfigurationException(XSLMessages.createMessage(XSLTErrorResources.ER_PROCESSFROMNODE_FAILED, null), se);
        // "processFromNode failed", se);
        }
    } catch (TransformerConfigurationException tce) {
        // Assume it's already been reported to the error listener.
        throw tce;
    }/* catch (TransformerException tce)
    {
      // Assume it's already been reported to the error listener.
      throw new TransformerConfigurationException(tce.getMessage(), tce);
    }*/
     catch (Exception e) {
        if (m_errorListener != null) {
            try {
                m_errorListener.fatalError(new TransformerException(e));
            } catch (TransformerConfigurationException ex) {
                throw ex;
            } catch (TransformerException ex) {
                throw new TransformerConfigurationException(ex);
            }
            return null;
        } else {
            // "processFromNode failed",
            throw new TransformerConfigurationException(XSLMessages.createMessage(XSLTErrorResources.ER_PROCESSFROMNODE_FAILED, null), e);
        // e);
        }
    }
}
Also used : TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) TreeWalker(org.apache.xml.utils.TreeWalker) TemplatesHandler(javax.xml.transform.sax.TemplatesHandler) TransformerException(javax.xml.transform.TransformerException) TransformerException(javax.xml.transform.TransformerException) StopParseException(org.apache.xml.utils.StopParseException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException)

Aggregations

TreeWalker (org.apache.xml.utils.TreeWalker)10 Node (org.w3c.dom.Node)8 IOException (java.io.IOException)6 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)4 TransformerException (javax.xml.transform.TransformerException)4 DOMSource (javax.xml.transform.dom.DOMSource)4 StopParseException (org.apache.xml.utils.StopParseException)4 ContentHandler (org.xml.sax.ContentHandler)4 InputSource (org.xml.sax.InputSource)4 XMLReader (org.xml.sax.XMLReader)4 Source (javax.xml.transform.Source)2 URIResolver (javax.xml.transform.URIResolver)2 SAXSource (javax.xml.transform.sax.SAXSource)2 TemplatesHandler (javax.xml.transform.sax.TemplatesHandler)2 StreamSource (javax.xml.transform.stream.StreamSource)2 StylesheetPIHandler (org.apache.xml.utils.StylesheetPIHandler)2