Search in sources :

Example 51 with ErrorHandler

use of org.xml.sax.ErrorHandler in project kie-wb-common by kiegroup.

the class PersistenceDescriptorXMLMarshaller method fromXML.

public static PersistenceDescriptorModel fromXML(InputStream xmlStream, boolean validate) throws Exception {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);
    DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(xmlStream);
    if (validate) {
        Validator validator = getPersistenceSchema().newValidator();
        validator.setErrorHandler(new ErrorHandler() {

            @Override
            public void warning(SAXParseException e) throws SAXException {
                // TODO add fine grained error processing if needed
                logger.warn("PersistenceDescriptorModel parsing error: ", e);
            }

            @Override
            public void error(SAXParseException e) throws SAXException {
                // TODO add fine grained error processing if needed
                logger.error("PersistenceDescriptorModel parsing error: ", e);
                throw e;
            }

            @Override
            public void fatalError(SAXParseException e) throws SAXException {
                // TODO add fine grained error processing if needed
                logger.error("PersistenceDescriptorModel parsing error: ", e);
                throw e;
            }
        });
        validator.validate(new DOMSource(document));
    }
    DOM2PersistenceDescriptorVisitor visitor = new DOM2PersistenceDescriptorVisitor(document);
    return visitor.visit();
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) Document(org.w3c.dom.Document) Validator(javax.xml.validation.Validator) SAXException(org.xml.sax.SAXException)

Example 52 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)

Example 53 with ErrorHandler

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

the class NTriple method processOpts.

private static int processOpts(String opts, String nextArg) {
    boolean usedNext = false;
    ARPOptions options = arp.getOptions();
    for (int i = 0; i < opts.length(); i++) {
        char opt = opts.charAt(i);
        if ("beiwD".indexOf(opt) != -1) {
            if (usedNext)
                usage();
            usedNext = true;
        }
        switch(opt) {
            case 'D':
                final int nStatements = Integer.parseInt(nextArg);
                rt.gc();
                rt.gc();
                startMem = (int) (rt.totalMemory() - rt.freeMemory());
                arp.getHandlers().setStatementHandler(new StatementHandler() {

                    int debugC = 0;

                    @Override
                    public void statement(AResource subj, AResource pred, AResource obj) {
                        statement(null, null, (ALiteral) null);
                    }

                    @Override
                    public void statement(AResource subj, AResource pred, ALiteral lit) {
                        if (++debugC % 100 == 0) {
                            System.out.println("T: " + debugC);
                            rt.gc();
                            System.out.println("M1: " + (rt.totalMemory() - rt.freeMemory() - startMem));
                            rt.gc();
                            System.out.println("M2: " + (rt.totalMemory() - rt.freeMemory() - startMem));
                        }
                        if (debugC == 1) {
                            rt.gc();
                            rt.gc();
                            startMem = (int) (rt.totalMemory() - rt.freeMemory());
                        }
                        if (debugC == nStatements) {
                            rt.gc();
                            System.err.println("Kill me now.");
                            try {
                                Thread.sleep(200000);
                            } catch (Exception e) {
                            // ignore
                            }
                        }
                    }
                });
                break;
            case 'x':
                options.setLaxErrorMode();
                break;
            case 's':
                options.setStrictErrorMode();
                break;
            case 't':
                arp.getHandlers().setStatementHandler(getSH(false));
                break;
            case 'r':
                options.setEmbedding(false);
                break;
            case 'R':
                options.setEmbedding(true);
                break;
            case 'n':
                numbers = true;
                break;
            case 'E':
                arp.getHandlers().setErrorHandler(new ErrorHandler() {

                    @Override
                    public void warning(SAXParseException exception) {
                    /* ignore */
                    }

                    @Override
                    public void error(SAXParseException exception) {
                    /* ignore */
                    }

                    @Override
                    public void fatalError(SAXParseException exception) {
                    /* ignore */
                    }
                });
                arp.setBadStatementHandler(new SH(System.err));
                break;
            case 'b':
                xmlBase = nextArg;
                break;
            case 'e':
                setErrorMode(nextArg, EM_ERROR);
                break;
            case 'i':
                setErrorMode(nextArg, EM_IGNORE);
                break;
            case 'w':
                setErrorMode(nextArg, EM_WARNING);
                break;
            case 'f':
                for (int j = 0; j < 400; j++) {
                    if (options.setErrorMode(j, -1) == EM_ERROR)
                        options.setErrorMode(j, EM_FATAL);
                }
                break;
            case 'u':
                options.setErrorMode(WARN_UNQUALIFIED_ATTRIBUTE, EM_IGNORE);
                options.setErrorMode(WARN_UNQUALIFIED_RDF_ATTRIBUTE, EM_IGNORE);
                break;
            default:
                usage();
        }
    }
    return usedNext ? 1 : 0;
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) SAXParseException(org.xml.sax.SAXParseException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 54 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 55 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)

Aggregations

ErrorHandler (org.xml.sax.ErrorHandler)69 SAXException (org.xml.sax.SAXException)59 SAXParseException (org.xml.sax.SAXParseException)55 DocumentBuilder (javax.xml.parsers.DocumentBuilder)26 IOException (java.io.IOException)25 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)24 Document (org.w3c.dom.Document)20 InputSource (org.xml.sax.InputSource)17 FileInputStream (java.io.FileInputStream)11 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)11 Element (org.w3c.dom.Element)10 File (java.io.File)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 InputStream (java.io.InputStream)8 Validator (javax.xml.validation.Validator)8 EntityResolver (org.xml.sax.EntityResolver)8 StreamSource (javax.xml.transform.stream.StreamSource)6 Schema (javax.xml.validation.Schema)6 SchemaFactory (javax.xml.validation.SchemaFactory)6 Node (org.w3c.dom.Node)5