Search in sources :

Example 36 with ErrorHandler

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

the class ARPHandlers method setErrorHandler.

/**
     * Sets the error handler, for both XML and RDF parse errors. XML errors are
     * reported by Xerces, as instances of SAXParseException; the RDF errors are
     * reported from ARP as instances of ParseException. Code that needs to
     * distingusih between them may look like:
     * 
     * <pre>
     *    void error( SAXParseException e ) throws SAXException {
     *      if ( e instanceof com.hp.hpl.jena.rdf.arp.ParseException ) {
     *           ...
     *      } else {
     *           ...
     *      }
     *    }
     * </pre>
     * 
     * <p>
     * See the ARP documentation for ErrorHandler for details of the
     * ErrorHandler semantics (in particular how to upgrade a warning to an
     * error, and an error to a.errorError).
     * </p>
     * <p>
     * The Xerces/SAX documentation for ErrorHandler is available on the web.
     * </p>
     * 
     * @param eh
     *            The error handler to use.
     * @return The previous error handler.
     */
public ErrorHandler setErrorHandler(ErrorHandler eh) {
    ErrorHandler old = errorHandler;
    errorHandler = eh;
    return old;
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) DefaultErrorHandler(org.apache.jena.rdfxml.xmlinput.impl.DefaultErrorHandler)

Example 37 with ErrorHandler

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

the class TestPropEltErrorMsg method runTest.

@Override
protected void runTest() {
    Attributes noAtts = new Atts();
    final StringBuffer buf = new StringBuffer();
    XMLHandler arp = new XMLHandler();
    arp.getHandlers().setErrorHandler(new ErrorHandler() {

        @Override
        public void warning(SAXParseException exception) {
            buf.append(exception.getMessage());
            buf.append("\n");
        }

        @Override
        public void error(SAXParseException e) {
            warning(e);
        }

        @Override
        public void fatalError(SAXParseException e) {
            warning(e);
        }
    });
    try {
        arp.initParse("http://example.org/", "");
        arp.startElement(RDF.getURI(), "RDF", "rdf:RDF", noAtts);
        arp.startElement(RDF.getURI(), "Description", "rdf:Description", noAtts);
        arp.startElement(RDF.getURI(), "value", "rdf:value", testAtts);
    } catch (SAXException e) {
    }
    //        System.err.println("===");
    //        System.err.println("\""+getName()+"\",");
    //        System.err.println("---");
    String contents = buf.toString();
    //        System.err.println("\""+(contents.length()>7?contents.substring(7).replace("\n","\\n"):"")+"\",");
    assertEquals("test data muddled", rslts[n * 2], getName());
    assertTrue("error message has changed.", contents.endsWith(rslts[n * 2 + 1]));
    contents = null;
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) XMLHandler(org.apache.jena.rdfxml.xmlinput.impl.XMLHandler) SAXParseException(org.xml.sax.SAXParseException) Attributes(org.xml.sax.Attributes) SAXException(org.xml.sax.SAXException)

Example 38 with ErrorHandler

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

the class SuperProperties method getDocumentBuilder.

private DocumentBuilder getDocumentBuilder() {
    if (builder == null) {
        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(true);
        try {
            builder = factory.newDocumentBuilder();
        } catch (final ParserConfigurationException e) {
            throw new Error(e);
        }
        builder.setErrorHandler(new ErrorHandler() {

            public void warning(final SAXParseException e) throws SAXException {
                throw e;
            }

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

            public void fatalError(final SAXParseException e) throws SAXException {
                throw e;
            }
        });
        builder.setEntityResolver(new EntityResolver() {

            public InputSource resolveEntity(final String publicId, final String systemId) throws SAXException, IOException {
                if (systemId.equals(PROP_DTD_NAME)) {
                    final InputSource result = new InputSource(new StringReader(PROP_DTD));
                    result.setSystemId(PROP_DTD_NAME);
                    return result;
                }
                throw new SAXException("Invalid DOCTYPE declaration: " + systemId);
            }
        });
    }
    return builder;
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) SAXParseException(org.xml.sax.SAXParseException) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) 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