Search in sources :

Example 6 with TreeWalker

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

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 7 with TreeWalker

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

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 8 with TreeWalker

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

the class TransformerFactoryImpl method getAssociatedStylesheet.

/**
   * Get InputSource specification(s) that are associated with the
   * given document specified in the source param,
   * via the xml-stylesheet processing instruction
   * (see http://www.w3.org/TR/xml-stylesheet/), and that matches
   * the given criteria.  Note that it is possible to return several stylesheets
   * that match the criteria, in which case they are applied as if they were
   * a list of imports or cascades.
   *
   * <p>Note that DOM2 has it's own mechanism for discovering stylesheets.
   * Therefore, there isn't a DOM version of this method.</p>
   *
   *
   * @param source The XML source that is to be searched.
   * @param media The media attribute to be matched.  May be null, in which
   *              case the prefered templates will be used (i.e. alternate = no).
   * @param title The value of the title attribute to match.  May be null.
   * @param charset The value of the charset attribute to match.  May be null.
   *
   * @return A Source object capable of being used to create a Templates object.
   *
   * @throws TransformerConfigurationException
   */
public Source getAssociatedStylesheet(Source source, String media, String title, String charset) throws TransformerConfigurationException {
    String baseID;
    InputSource isource = null;
    Node node = null;
    XMLReader reader = null;
    if (source instanceof DOMSource) {
        DOMSource dsource = (DOMSource) source;
        node = dsource.getNode();
        baseID = dsource.getSystemId();
    } else {
        isource = SAXSource.sourceToInputSource(source);
        baseID = isource.getSystemId();
    }
    // What I try to do here is parse until the first startElement
    // is found, then throw a special exception in order to terminate 
    // the parse.
    StylesheetPIHandler handler = new StylesheetPIHandler(baseID, media, title, charset);
    // Use URIResolver. Patch from Dmitri Ilyin 
    if (m_uriResolver != null) {
        handler.setURIResolver(m_uriResolver);
    }
    try {
        if (null != node) {
            TreeWalker walker = new TreeWalker(handler, new org.apache.xml.utils.DOM2Helper(), baseID);
            walker.traverse(node);
        } else {
            // Use JAXP1.1 ( if possible )
            try {
                javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance();
                factory.setNamespaceAware(true);
                if (m_isSecureProcessing) {
                    try {
                        factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
                    } catch (org.xml.sax.SAXException e) {
                    }
                }
                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();
            }
            // Need to set options!
            reader.setContentHandler(handler);
            reader.parse(isource);
        }
    } catch (StopParseException spe) {
    // OK, good.
    } catch (org.xml.sax.SAXException se) {
        throw new TransformerConfigurationException("getAssociatedStylesheets failed", se);
    } catch (IOException ioe) {
        throw new TransformerConfigurationException("getAssociatedStylesheets failed", ioe);
    }
    return handler.getAssociatedStylesheet();
}
Also used : InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Node(org.w3c.dom.Node) TreeWalker(org.apache.xml.utils.TreeWalker) IOException(java.io.IOException) StylesheetPIHandler(org.apache.xml.utils.StylesheetPIHandler) StopParseException(org.apache.xml.utils.StopParseException) XMLReader(org.xml.sax.XMLReader)

Example 9 with TreeWalker

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

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)9 Node (org.w3c.dom.Node)7 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 InputSource (org.xml.sax.InputSource)4 XMLReader (org.xml.sax.XMLReader)4 ContentHandler (org.xml.sax.ContentHandler)3 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