Search in sources :

Example 56 with SAXParseException

use of org.xml.sax.SAXParseException in project jackrabbit by apache.

the class ConfigurationParser method parseXML.

/**
     * Parses the given XML document and returns the DOM root element.
     * A custom entity resolver is used to make the included configuration
     * file DTD available using the specified public identifiers.
     *
     * @see ConfigurationEntityResolver
     * @param xml xml document
     * @param validate whether the XML should be validated
     * @return root element
     * @throws ConfigurationException if the configuration document could
     *                                not be read or parsed
     */
protected Element parseXML(InputSource xml, boolean validate) throws ConfigurationException {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(validate);
        DocumentBuilder builder = factory.newDocumentBuilder();
        if (validate) {
            builder.setErrorHandler(getErrorHandler());
        }
        builder.setEntityResolver(getEntityResolver());
        Document document = builder.parse(xml);
        return postParseModificationHook(document).getDocumentElement();
    } catch (ParserConfigurationException e) {
        throw new ConfigurationException("Unable to create configuration XML parser", e);
    } catch (SAXParseException e) {
        throw new ConfigurationException("Configuration file syntax error. (Line: " + e.getLineNumber() + " Column: " + e.getColumnNumber() + ")", e);
    } catch (SAXException e) {
        throw new ConfigurationException("Configuration file syntax error. ", e);
    } catch (IOException e) {
        throw new ConfigurationException("Configuration file could not be read.", e);
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Example 57 with SAXParseException

use of org.xml.sax.SAXParseException in project jackrabbit by apache.

the class DefaultLoginModuleTest method parseXML.

private static Element parseXML(InputSource xml, boolean validate) throws ConfigurationException {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(validate);
        DocumentBuilder builder = factory.newDocumentBuilder();
        if (validate) {
            builder.setErrorHandler(new ConfigurationErrorHandler());
        }
        builder.setEntityResolver(ConfigurationEntityResolver.INSTANCE);
        Document document = builder.parse(xml);
        return document.getDocumentElement();
    } catch (ParserConfigurationException e) {
        throw new ConfigurationException("Unable to create configuration XML parser", e);
    } catch (SAXParseException e) {
        throw new ConfigurationException("Configuration file syntax error. (Line: " + e.getLineNumber() + " Column: " + e.getColumnNumber() + ")", e);
    } catch (SAXException e) {
        throw new ConfigurationException("Configuration file syntax error. ", e);
    } catch (IOException e) {
        throw new ConfigurationException("Configuration file could not be read.", e);
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ConfigurationException(org.apache.jackrabbit.core.config.ConfigurationException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) ConfigurationErrorHandler(org.apache.jackrabbit.core.config.ConfigurationErrorHandler) SAXException(org.xml.sax.SAXException)

Example 58 with SAXParseException

use of org.xml.sax.SAXParseException in project karaf by apache.

the class FeatureDeploymentListener method parse.

protected Document parse(File artifact) throws Exception {
    if (dbf == null) {
        dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
    }
    DocumentBuilder db = dbf.newDocumentBuilder();
    db.setErrorHandler(new ErrorHandler() {

        public void warning(SAXParseException exception) throws SAXException {
        }

        public void error(SAXParseException exception) throws SAXException {
        }

        public void fatalError(SAXParseException exception) throws SAXException {
            throw exception;
        }
    });
    return db.parse(artifact);
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException)

Example 59 with SAXParseException

use of org.xml.sax.SAXParseException in project karaf by apache.

the class FeatureDetector method parse.

/**
     * Parse a features XML.
     *
     * @param artifact the features XML to parse.
     * @return the parsed document.
     * @throws Exception in case of parsing failure.
     */
private Document parse(File artifact) throws Exception {
    DocumentBuilder db = dbf.newDocumentBuilder();
    db.setErrorHandler(new ErrorHandler() {

        public void warning(SAXParseException exception) throws SAXException {
        }

        public void error(SAXParseException exception) throws SAXException {
        }

        public void fatalError(SAXParseException exception) throws SAXException {
            throw exception;
        }
    });
    return db.parse(artifact);
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException)

Example 60 with SAXParseException

use of org.xml.sax.SAXParseException in project jena by apache.

the class XMLLiteralType method isValid.

/**
     * Test whether the given string is a legal lexical form
     * of this datatype.
     */
@Override
public boolean isValid(final String lexicalForm) {
    /*
         * To check the lexical form we construct
         * a dummy RDF/XML document and parse it with
         * ARP. ARP performs an exclusive canonicalization,
         * the dummy document has exactly one triple.
         * If the lexicalForm is valid then the resulting
         * literal found by ARP is unchanged.
         * All other scenarios are either impossible
         * or occur because the lexical form is invalid.
         */
    final boolean[] status = new boolean[] { false, false, false };
    // status[0] true on error or other reason to know that this is not well-formed
    // status[1] true once first triple found
    // status[2] the result (good if status[1] and not status[0]).
    ARP arp = new ARP();
    arp.getHandlers().setErrorHandler(new ErrorHandler() {

        @Override
        public void fatalError(SAXParseException e) {
            status[0] = true;
        }

        @Override
        public void error(SAXParseException e) {
            status[0] = true;
        }

        @Override
        public void warning(SAXParseException e) {
            status[0] = true;
        }
    });
    arp.getHandlers().setStatementHandler(new StatementHandler() {

        @Override
        public void statement(AResource a, AResource b, ALiteral l) {
            /* this method is invoked exactly once
        	 * while parsing the dummy document.
        	 * The l argument is in exclusive canonical XML and
        	 * corresponds to where the lexical form has been 
        	 * in the dummy document. The lexical form is valid
        	 * iff it is unchanged.
        	 */
            if (status[1] || !l.isWellFormedXML()) {
                status[0] = true;
            }
            //throw new BrokenException("plain literal in XMLLiteral code.");
            status[1] = true;
            status[2] = l.toString().equals(lexicalForm);
        }

        @Override
        public void statement(AResource a, AResource b, AResource l) {
            status[0] = true;
        //throw new BrokenException("resource valued RDF/XML in XMLLiteral code.");
        }
    });
    try {
        arp.load(new StringReader("<rdf:RDF  xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>\n" + "<rdf:Description><rdf:value rdf:parseType='Literal'>" + lexicalForm + "</rdf:value>\n" + "</rdf:Description></rdf:RDF>"));
    } catch (IOException ioe) {
        throw new BrokenException(ioe);
    } catch (SAXException s) {
        return false;
    }
    return (!status[0]) && status[1] && status[2];
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) SAXParseException(org.xml.sax.SAXParseException) StatementHandler(org.apache.jena.rdfxml.xmlinput.StatementHandler) StringReader(java.io.StringReader) AResource(org.apache.jena.rdfxml.xmlinput.AResource) BrokenException(org.apache.jena.shared.BrokenException) ALiteral(org.apache.jena.rdfxml.xmlinput.ALiteral) IOException(java.io.IOException) ARP(org.apache.jena.rdfxml.xmlinput.ARP) SAXException(org.xml.sax.SAXException)

Aggregations

SAXParseException (org.xml.sax.SAXParseException)206 SAXException (org.xml.sax.SAXException)109 IOException (java.io.IOException)75 DocumentBuilder (javax.xml.parsers.DocumentBuilder)53 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)47 Document (org.w3c.dom.Document)47 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)42 InputSource (org.xml.sax.InputSource)39 ErrorHandler (org.xml.sax.ErrorHandler)34 Test (org.junit.Test)28 Element (org.w3c.dom.Element)24 InputStream (java.io.InputStream)20 ArrayList (java.util.ArrayList)18 NodeList (org.w3c.dom.NodeList)18 FileInputStream (java.io.FileInputStream)17 FileNotFoundException (java.io.FileNotFoundException)17 File (java.io.File)16 Node (org.w3c.dom.Node)13 StringReader (java.io.StringReader)12 URL (java.net.URL)11