Search in sources :

Example 1 with StylesheetPIHandler

use of org.apache.xml.utils.StylesheetPIHandler 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 2 with StylesheetPIHandler

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

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();
            }
            if (m_isSecureProcessing) {
                reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
            }
            // 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)

Aggregations

IOException (java.io.IOException)2 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)2 DOMSource (javax.xml.transform.dom.DOMSource)2 StopParseException (org.apache.xml.utils.StopParseException)2 StylesheetPIHandler (org.apache.xml.utils.StylesheetPIHandler)2 TreeWalker (org.apache.xml.utils.TreeWalker)2 Node (org.w3c.dom.Node)2 InputSource (org.xml.sax.InputSource)2 XMLReader (org.xml.sax.XMLReader)2