Search in sources :

Example 16 with SAXNotRecognizedException

use of org.xml.sax.SAXNotRecognizedException in project drools by kiegroup.

the class ExtensibleXmlParser method read.

/**
 * Read a <code>RuleSet</code> from an <code>InputSource</code>.
 *
 * @param in
 *            The rule-set input-source.
 *
 * @return The rule-set.
 * @throws ParserConfigurationException
 */
public Object read(final InputSource in) throws SAXException, IOException {
    if (this.docFragment == null) {
        DocumentBuilderFactory f;
        try {
            f = DocumentBuilderFactory.newInstance();
        } catch (FactoryConfigurationError e) {
            // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4633368
            try {
                f = (DocumentBuilderFactory) Class.forName("org.apache.xerces.jaxp.DocumentBuilderFactoryImpl").newInstance();
            } catch (Exception e1) {
                throw new RuntimeException("Unable to create new DOM Document", e1);
            }
        } catch (Exception e) {
            throw new RuntimeException("Unable to create new DOM Document", e);
        }
        // XXE protection start
        try {
            f.setFeature("http://xml.org/sax/features/external-general-entities", false);
            f.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        } catch (ParserConfigurationException e) {
            logger.warn("Unable to set parser features due to {}", e.getMessage());
        }
        // XXE protection end
        try {
            this.document = f.newDocumentBuilder().newDocument();
        } catch (Exception e) {
            throw new RuntimeException("Unable to create new DOM Document", e);
        }
        this.docFragment = this.document.createDocumentFragment();
    }
    SAXParser localParser = null;
    if (this.parser == null) {
        SAXParserFactory factory = null;
        try {
            factory = SAXParserFactory.newInstance();
        } catch (FactoryConfigurationError e) {
            // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4633368
            try {
                factory = (SAXParserFactory) Class.forName("org.apache.xerces.jaxp.SAXParserFactoryImpl").newInstance();
            } catch (Exception e1) {
                throw new RuntimeException("Unable to create new DOM Document", e1);
            }
        } catch (Exception e) {
            throw new RuntimeException("Unable to create new DOM Document", e);
        }
        factory.setNamespaceAware(true);
        // XXE protection start
        try {
            factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
            factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        } catch (ParserConfigurationException e) {
            logger.warn("Unable to set parser features due to {}", e.getMessage());
        }
        if (System.getProperty("drools.schema.validating") != null) {
            this.isValidating = Boolean.getBoolean("drools.schema.validating");
        }
        if (this.isValidating == true) {
            factory.setValidating(true);
            try {
                localParser = factory.newSAXParser();
            } catch (final ParserConfigurationException e) {
                throw new RuntimeException(e.getMessage());
            }
            try {
                localParser.setProperty(ExtensibleXmlParser.JAXP_SCHEMA_LANGUAGE, ExtensibleXmlParser.W3C_XML_SCHEMA);
            } catch (final SAXNotRecognizedException e) {
                boolean hideWarnings = Boolean.getBoolean("drools.schema.hidewarnings");
                if (!hideWarnings) {
                    logger.warn("Your SAX parser is not JAXP 1.2 compliant - turning off validation.");
                }
                localParser = null;
            }
        }
        if (localParser == null) {
            // not jaxp1.2 compliant so turn off validation
            try {
                this.isValidating = false;
                factory.setValidating(this.isValidating);
                localParser = factory.newSAXParser();
            } catch (final ParserConfigurationException e) {
                throw new RuntimeException(e.getMessage());
            }
        }
    } else {
        localParser = this.parser;
    }
    if (!localParser.isNamespaceAware()) {
        throw new RuntimeException("parser must be namespace-aware");
    }
    localParser.parse(in, this);
    return this.data;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) SAXParser(javax.xml.parsers.SAXParser) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) FactoryConfigurationError(javax.xml.parsers.FactoryConfigurationError) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Aggregations

SAXNotRecognizedException (org.xml.sax.SAXNotRecognizedException)16 SAXException (org.xml.sax.SAXException)10 SAXParser (javax.xml.parsers.SAXParser)9 SAXParserFactory (javax.xml.parsers.SAXParserFactory)9 SAXNotSupportedException (org.xml.sax.SAXNotSupportedException)8 IOException (java.io.IOException)5 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)5 XMLReader (org.xml.sax.XMLReader)5 InputSource (org.xml.sax.InputSource)4 SAXParseException (org.xml.sax.SAXParseException)3 InputStream (java.io.InputStream)2 DOMSource (javax.xml.transform.dom.DOMSource)2 SAXSource (javax.xml.transform.sax.SAXSource)2 StreamSource (javax.xml.transform.stream.StreamSource)2 DTMException (org.apache.xml.dtm.DTMException)2 DOM2DTM (org.apache.xml.dtm.ref.dom2dtm.DOM2DTM)2 SAX2DTM (org.apache.xml.dtm.ref.sax2dtm.SAX2DTM)2 SAX2RTFDTM (org.apache.xml.dtm.ref.sax2dtm.SAX2RTFDTM)2 XMLStringFactory (org.apache.xml.utils.XMLStringFactory)2 Picture (android.graphics.Picture)1