Search in sources :

Example 21 with ErrorHandler

use of org.xml.sax.ErrorHandler 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 22 with ErrorHandler

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

the class TestJspDocumentParser method testSchemaValidation.

@Test
public void testSchemaValidation() throws Exception {
    getTomcatInstanceTestWebapp(false, true);
    String path = "http://localhost:" + getPort() + "/test/valid.jspx";
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    dbf.setValidating(true);
    dbf.setFeature("http://apache.org/xml/features/validation/schema", true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    db.setErrorHandler(new ErrorHandler() {

        @Override
        public void warning(SAXParseException exception) throws SAXException {
            throw exception;
        }

        @Override
        public void error(SAXParseException exception) throws SAXException {
            throw exception;
        }

        @Override
        public void fatalError(SAXParseException exception) throws SAXException {
            throw exception;
        }
    });
    Document document = db.parse(path);
    Assert.assertEquals("urn:valid", document.getDocumentElement().getNamespaceURI());
    Assert.assertEquals("root", document.getDocumentElement().getLocalName());
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 23 with ErrorHandler

use of org.xml.sax.ErrorHandler in project gocd by gocd.

the class XmlSchema method validate_document_or_file.

IRubyObject validate_document_or_file(ThreadContext context, XmlDocument xmlDocument) {
    RubyArray errors = (RubyArray) this.getInstanceVariable("@errors");
    ErrorHandler errorHandler = new SchemaErrorHandler(context.getRuntime(), errors);
    setErrorHandler(errorHandler);
    try {
        validate(xmlDocument.getDocument());
    } catch (SAXException ex) {
        XmlSyntaxError xmlSyntaxError = (XmlSyntaxError) NokogiriService.XML_SYNTAXERROR_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::XML::SyntaxError"));
        xmlSyntaxError.setException(ex);
        errors.append(xmlSyntaxError);
    } catch (IOException ex) {
        throw context.getRuntime().newIOError(ex.getMessage());
    }
    return errors;
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) SchemaErrorHandler(nokogiri.internals.SchemaErrorHandler) IgnoreSchemaErrorsErrorHandler(nokogiri.internals.IgnoreSchemaErrorsErrorHandler) RubyArray(org.jruby.RubyArray) SchemaErrorHandler(nokogiri.internals.SchemaErrorHandler) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 24 with ErrorHandler

use of org.xml.sax.ErrorHandler in project facebook-recommender-demo by ManuelB.

the class WebXmlTest method testIsWebXmlValid.

@Test
public void testIsWebXmlValid() throws Exception {
    // http://www.edankert.com/validate.html
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);
    factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
    factory.setNamespaceAware(true);
    DocumentBuilder parser = factory.newDocumentBuilder();
    // rethrow all exception so test fails if web.xml is invalid
    parser.setErrorHandler(new ErrorHandler() {

        @Override
        public void warning(SAXParseException exception) throws SAXException {
            throw exception;
        }

        @Override
        public void fatalError(SAXParseException exception) throws SAXException {
            throw exception;
        }

        @Override
        public void error(SAXParseException exception) throws SAXException {
            throw exception;
        }
    });
    Document doc = parser.parse("src/main/webapp/WEB-INF/web.xml");
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) Test(org.junit.Test)

Example 25 with ErrorHandler

use of org.xml.sax.ErrorHandler in project Mycat-Server by MyCATApache.

the class ConfigUtil method getDocument.

public static Document getDocument(final InputStream dtd, InputStream xml) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);
    factory.setNamespaceAware(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    builder.setEntityResolver(new EntityResolver() {

        @Override
        public InputSource resolveEntity(String publicId, String systemId) {
            return new InputSource(dtd);
        }
    });
    builder.setErrorHandler(new ErrorHandler() {

        @Override
        public void warning(SAXParseException e) {
        }

        @Override
        public void error(SAXParseException e) throws SAXException {
            throw e;
        }

        @Override
        public void fatalError(SAXParseException e) throws SAXException {
            throw e;
        }
    });
    return builder.parse(xml);
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) EntityResolver(org.xml.sax.EntityResolver) SAXException(org.xml.sax.SAXException)

Aggregations

ErrorHandler (org.xml.sax.ErrorHandler)38 SAXException (org.xml.sax.SAXException)36 SAXParseException (org.xml.sax.SAXParseException)33 IOException (java.io.IOException)16 DocumentBuilder (javax.xml.parsers.DocumentBuilder)14 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)14 Document (org.w3c.dom.Document)13 InputSource (org.xml.sax.InputSource)9 InputStream (java.io.InputStream)7 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)7 Element (org.w3c.dom.Element)7 FileInputStream (java.io.FileInputStream)6 EntityResolver (org.xml.sax.EntityResolver)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 File (java.io.File)4 StringReader (java.io.StringReader)4 OutputFormat (com.sun.org.apache.xml.internal.serialize.OutputFormat)3 XMLSerializer (com.sun.org.apache.xml.internal.serialize.XMLSerializer)3 Attr (org.w3c.dom.Attr)3 NodeList (org.w3c.dom.NodeList)3