Search in sources :

Example 96 with XMLReader

use of org.xml.sax.XMLReader in project tomcat by apache.

the class JspDocumentParser method getSAXParser.

/*
     * Gets SAXParser.
     *
     * @param validating Indicates whether the requested SAXParser should
     * be validating
     * @param jspDocParser The JSP document parser
     *
     * @return The SAXParser
     */
private static SAXParser getSAXParser(boolean validating, JspDocumentParser jspDocParser) throws Exception {
    ClassLoader original;
    if (Constants.IS_SECURITY_ENABLED) {
        PrivilegedGetTccl pa = new PrivilegedGetTccl();
        original = AccessController.doPrivileged(pa);
    } else {
        original = Thread.currentThread().getContextClassLoader();
    }
    try {
        if (Constants.IS_SECURITY_ENABLED) {
            PrivilegedSetTccl pa = new PrivilegedSetTccl(JspDocumentParser.class.getClassLoader());
            AccessController.doPrivileged(pa);
        } else {
            Thread.currentThread().setContextClassLoader(JspDocumentParser.class.getClassLoader());
        }
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);
        // Preserve xmlns attributes
        factory.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
        factory.setValidating(validating);
        if (validating) {
            // Enable DTD validation
            factory.setFeature("http://xml.org/sax/features/validation", true);
            // Enable schema validation
            factory.setFeature("http://apache.org/xml/features/validation/schema", true);
        }
        // Configure the parser
        SAXParser saxParser = factory.newSAXParser();
        XMLReader xmlReader = saxParser.getXMLReader();
        xmlReader.setProperty(LEXICAL_HANDLER_PROPERTY, jspDocParser);
        xmlReader.setErrorHandler(jspDocParser);
        return saxParser;
    } finally {
        if (Constants.IS_SECURITY_ENABLED) {
            PrivilegedSetTccl pa = new PrivilegedSetTccl(original);
            AccessController.doPrivileged(pa);
        } else {
            Thread.currentThread().setContextClassLoader(original);
        }
    }
}
Also used : PrivilegedGetTccl(org.apache.tomcat.util.security.PrivilegedGetTccl) SAXParser(javax.xml.parsers.SAXParser) XMLReader(org.xml.sax.XMLReader) PrivilegedSetTccl(org.apache.tomcat.util.security.PrivilegedSetTccl) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 97 with XMLReader

use of org.xml.sax.XMLReader in project asciidoctor-fopub by asciidoctor.

the class InputHandler method createMainSource.

/**
     * Creates a Source for the main input file. Processes XInclude if
     * available in the XML parser.
     *
     * @return the Source for the main input file
     */
protected Source createMainSource() {
    Source source;
    InputStream in;
    String uri;
    if (this.sourcefile != null) {
        try {
            in = new java.io.FileInputStream(this.sourcefile);
            uri = this.sourcefile.toURI().toASCIIString();
        } catch (FileNotFoundException e) {
            //handled elsewhere
            return new StreamSource(this.sourcefile);
        }
    } else {
        in = System.in;
        uri = null;
    }
    try {
        InputSource is = new InputSource(in);
        is.setSystemId(uri);
        XMLReader xr = getXMLReader();
        if (entityResolver != null) {
            xr.setEntityResolver(entityResolver);
        }
        source = new SAXSource(xr, is);
    } catch (SAXException e) {
        if (this.sourcefile != null) {
            source = new StreamSource(this.sourcefile);
        } else {
            source = new StreamSource(in, uri);
        }
    } catch (ParserConfigurationException e) {
        if (this.sourcefile != null) {
            source = new StreamSource(this.sourcefile);
        } else {
            source = new StreamSource(in, uri);
        }
    }
    return source;
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) FileNotFoundException(java.io.FileNotFoundException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException)

Example 98 with XMLReader

use of org.xml.sax.XMLReader in project asciidoctor-fopub by asciidoctor.

the class InputHandler method createXSLTSource.

/**
     * Creates a Source for the selected stylesheet.
     *
     * @return the Source for the selected stylesheet or null if there's no stylesheet
     */
protected Source createXSLTSource() {
    Source xslt = null;
    if (this.stylesheet != null) {
        if (entityResolver != null) {
            try {
                InputSource is = new InputSource(this.stylesheet.getPath());
                XMLReader xr = getXMLReader();
                xr.setEntityResolver(entityResolver);
                xslt = new SAXSource(xr, is);
            } catch (SAXException e) {
            // return StreamSource
            } catch (ParserConfigurationException e) {
            // return StreamSource
            }
        }
        if (xslt == null) {
            xslt = new StreamSource(this.stylesheet);
        }
    }
    return xslt;
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) StreamSource(javax.xml.transform.stream.StreamSource) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException)

Example 99 with XMLReader

use of org.xml.sax.XMLReader in project blade by biezhi.

the class XmlParser method parse.

/* ------------------------------------------------------------ */
public synchronized Node parse(InputSource source) throws IOException, SAXException {
    _dtd = null;
    Handler handler = new Handler();
    XMLReader reader = _parser.getXMLReader();
    reader.setContentHandler(handler);
    reader.setErrorHandler(handler);
    reader.setEntityResolver(handler);
    if (LOG.isDebugEnabled())
        LOG.debug("parsing: sid=" + source.getSystemId() + ",pid=" + source.getPublicId());
    _parser.parse(source, handler);
    if (handler._error != null)
        throw handler._error;
    Node doc = (Node) handler._top.get(0);
    handler.clear();
    return doc;
}
Also used : ContentHandler(org.xml.sax.ContentHandler) DefaultHandler(org.xml.sax.helpers.DefaultHandler) XMLReader(org.xml.sax.XMLReader)

Example 100 with XMLReader

use of org.xml.sax.XMLReader in project blade by biezhi.

the class XmlParser method parse.

/* ------------------------------------------------------------ */
/**
     * Parse InputStream.
     * @param in the input stream of the xml to parse
     * @return the root node of the xml
     * @throws IOException if unable to load the xml
     * @throws SAXException if unable to parse the xml
     */
public synchronized Node parse(InputStream in) throws IOException, SAXException {
    _dtd = null;
    Handler handler = new Handler();
    XMLReader reader = _parser.getXMLReader();
    reader.setContentHandler(handler);
    reader.setErrorHandler(handler);
    reader.setEntityResolver(handler);
    _parser.parse(new InputSource(in), handler);
    if (handler._error != null)
        throw handler._error;
    Node doc = (Node) handler._top.get(0);
    handler.clear();
    return doc;
}
Also used : InputSource(org.xml.sax.InputSource) ContentHandler(org.xml.sax.ContentHandler) DefaultHandler(org.xml.sax.helpers.DefaultHandler) XMLReader(org.xml.sax.XMLReader)

Aggregations

XMLReader (org.xml.sax.XMLReader)196 InputSource (org.xml.sax.InputSource)160 SAXException (org.xml.sax.SAXException)66 IOException (java.io.IOException)65 SAXSource (javax.xml.transform.sax.SAXSource)38 SAXParserFactory (javax.xml.parsers.SAXParserFactory)37 SAXParser (javax.xml.parsers.SAXParser)36 StringReader (java.io.StringReader)35 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)28 ExpatReader (org.apache.harmony.xml.ExpatReader)24 InputStream (java.io.InputStream)20 ContentHandler (org.xml.sax.ContentHandler)20 TransformerException (javax.xml.transform.TransformerException)17 DOMSource (javax.xml.transform.dom.DOMSource)15 StreamSource (javax.xml.transform.stream.StreamSource)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 InputStreamReader (java.io.InputStreamReader)9 Source (javax.xml.transform.Source)9 Transformer (javax.xml.transform.Transformer)9 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)9