Example usage for javax.xml.parsers DocumentBuilder setErrorHandler

List of usage examples for javax.xml.parsers DocumentBuilder setErrorHandler

Introduction

In this page you can find the example usage for javax.xml.parsers DocumentBuilder setErrorHandler.

Prototype


public abstract void setErrorHandler(ErrorHandler eh);

Source Link

Document

Specify the ErrorHandler to be used by the parser.

Usage

From source file:Main.java

public static boolean validateWithDTDUsingDOM(String xml) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*ww w  .  j  av  a2 s.co m*/
    factory.setNamespaceAware(true);

    DocumentBuilder builder = factory.newDocumentBuilder();

    builder.setErrorHandler(new ErrorHandler() {
        public void warning(SAXParseException e) throws SAXException {
            System.out.println("WARNING : " + e.getMessage()); // do nothing
        }

        public void error(SAXParseException e) throws SAXException {
            System.out.println("ERROR : " + e.getMessage());
            throw e;
        }

        public void fatalError(SAXParseException e) throws SAXException {
            System.out.println("FATAL : " + e.getMessage());
            throw e;
        }
    });
    builder.parse(new InputSource(xml));
    return true;
}

From source file:Main.java

public static Document validate(File xml, File xmlSchema)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);/* w w w  . ja  va  2 s  .  co  m*/
    factory.setValidating(true);
    factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
            "http://www.w3.org/2001/XMLSchema");
    factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", xmlSchema);
    DocumentBuilder documentBuilder = factory.newDocumentBuilder();
    documentBuilder.setErrorHandler(new ErrorHandler() {
        public void warning(SAXParseException ex) throws SAXException {
            throw ex;
        }

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

        public void fatalError(SAXParseException ex) throws SAXException {
            throw ex;
        }
    });
    return documentBuilder.parse(xml);
}

From source file:Main.java

public static Document parse(File f, ErrorHandler errorHandler)
        throws TransformerException, IOException, SAXException, ParserConfigurationException {
    DocumentBuilder db = documentBuilder();
    db.setErrorHandler(errorHandler);
    try {//from  ww  w  . j  av a  2  s .  co  m
        return db.parse(f);
    } finally {
        db.reset();
    }
}

From source file:Main.java

public static Document toDocument(String xml, boolean namespaceAware, boolean ignoreDtd) throws Exception {
    Reader input = new StringReader(xml);
    InputSource is = new InputSource(input);
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(namespaceAware);

    // ignore dtd files
    if (ignoreDtd) {
        dbf.setValidating(false);/*from  ww  w  . j a  v  a 2s  .co m*/
        dbf.setFeature("http://xml.org/sax/features/namespaces", false);
        dbf.setFeature("http://xml.org/sax/features/validation", false);
        dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
        dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    }

    DocumentBuilder db = dbf.newDocumentBuilder();
    db.setErrorHandler(new ErrorHandler() {
        public void warning(SAXParseException exception) throws SAXException {
            throw exception;

        }

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

        public void error(SAXParseException exception) throws SAXException {
            throw exception;
        }
    });
    return db.parse(is);
}

From source file:Main.java

/**
 * Valida un documento XML con un esquema XML (XSD).
 * @param xml File to validate/* ww w  .  j  a  v  a  2s . c o m*/
 * @param xmlSchema File with the schema
 * @return Dom document
 * @throws ParserConfigurationException 
 * @throws SAXException 
 * @throws IOException  
 */
public static Document validate(File xml, File xmlSchema)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    factory.setNamespaceAware(true);
    factory.setValidating(true);
    factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
            "http://www.w3.org/2001/XMLSchema");
    factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", xmlSchema);

    // Parsing 
    DocumentBuilder documentBuilder = factory.newDocumentBuilder();
    documentBuilder.setErrorHandler(new ErrorHandler() {
        public void warning(SAXParseException ex) throws SAXException {
            throw ex;
        }

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

        public void fatalError(SAXParseException ex) throws SAXException {
            throw ex;
        }
    });

    return documentBuilder.parse(xml);
}

From source file:hadoopInstaller.io.XMLDocumentReader.java

public static Document parse(FileObject xmlDocument, FileObject xsdDocument)
        throws InstallerConfigurationParseError {

    try {/*from   w  w w .  ja  va  2s  .  co m*/
        // Validate against XML Schema
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
                .newSchema(new StreamSource(xsdDocument.getContent().getInputStream()));
        dbf.setValidating(false);
        dbf.setSchema(schema);
        DocumentBuilder db = dbf.newDocumentBuilder();
        db.setErrorHandler(new ParseErrorHandler());
        return db.parse(xmlDocument.getContent().getInputStream());
    } catch (ParserConfigurationException | SAXException | IOException e) {
        throw new InstallerConfigurationParseError(e, "XMLDocumentReader.ParseError", xmlDocument.getName()); //$NON-NLS-1$
    }
}

From source file:Main.java

public static String tryFormattingString(String s) {
    try {/*from  www .j  a v a 2  s. c  o m*/
        ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes());
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setErrorHandler(new ErrorHandler() {

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

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

            @Override
            public void error(SAXParseException exception) throws SAXException {
            }
        });
        Document doc = builder.parse(in);
        TransformerFactory tf = TransformerFactory.newInstance();
        // tf.setAttribute("indent-number", new Integer(2));
        Transformer trans = tf.newTransformer();
        DOMSource source = new DOMSource(doc);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        StreamResult result = new StreamResult(out);
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty(OutputKeys.METHOD, "xml");
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

        trans.transform(source, result);
        return out.toString();

    } catch (Exception e) {
        // Console.println("Could not validate the soapmessage, although I'll show it anyway..");
    }
    return s;
}

From source file:com.mycila.xmltool.XMLDocumentBuilderFactory.java

public static DocumentBuilder newDocumentBuilder(boolean ignoreNamespaces) {
    try {/*www .  ja v  a 2 s  .  c  o  m*/
        javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory
                .newInstance();
        factory.setNamespaceAware(!ignoreNamespaces);
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setErrorHandler(new XMLErrorHandler(true));
        builder.setEntityResolver(CachedEntityResolver.instance);
        return builder;
    } catch (ParserConfigurationException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}

From source file:Main.java

public static Document openXmlStream(InputStream stream, Schema schema, boolean isNamespaceAware,
        boolean isXIncludeAware) throws IOException, SAXParseException, SAXException {
    DocumentBuilder builder = newDocumentBuilder(schema, isNamespaceAware, isXIncludeAware);
    Document docu;/*from   w  w  w  .  j  ava  2  s.co  m*/

    builder.setErrorHandler(new org.xml.sax.ErrorHandler() {
        @Override
        public void error(SAXParseException exception) throws SAXParseException {
            throw new SAXParseException("Parse Error in instream, line " + exception.getLineNumber() + ": "
                    + exception.getMessage(), "", "instream", exception.getLineNumber(), 0);
        }

        @Override
        public void fatalError(SAXParseException exception) throws SAXParseException {
            throw new SAXParseException("Parse Error in instream, line " + exception.getLineNumber() + ": "
                    + exception.getMessage(), "", "instream", exception.getLineNumber(), 0);
        }

        @Override
        public void warning(SAXParseException exception) {
            System.err.println("Parse Warning: " + exception.getMessage() + exception.getLineNumber());
        }
    });
    docu = builder.parse(stream);

    return docu;
}

From source file:Main.java

public static Document openXmlFile(File file, Schema schema, boolean isNamespaceAware, boolean isXIncludeAware)
        throws IOException, SAXParseException, SAXException {
    final String fname = file.getCanonicalPath();
    DocumentBuilder builder = newDocumentBuilder(schema, isNamespaceAware, isXIncludeAware);
    Document docu;/*  w  w  w. ja  v  a 2  s . co  m*/

    builder.setErrorHandler(new org.xml.sax.ErrorHandler() {
        @Override
        public void error(SAXParseException exception) throws SAXParseException {
            //System.err.println("Parse Error in file " + fname + ", line " + exception.getLineNumber() + ": " + exception.getMessage());
            throw new SAXParseException("Parse Error in file " + fname + ", line " + exception.getLineNumber()
                    + ": " + exception.getMessage(), "", fname, exception.getLineNumber(), 0);
        }

        @Override
        public void fatalError(SAXParseException exception) throws SAXParseException {
            //System.err.println("Parse Fatal Error: " + exception.getMessage() + exception.getLineNumber());
            throw new SAXParseException("Parse Error in file " + fname + ", line " + exception.getLineNumber()
                    + ": " + exception.getMessage(), "", fname, exception.getLineNumber(), 0);
        }

        @Override
        public void warning(SAXParseException exception) {
            System.err.println("Parse Warning: " + exception.getMessage() + exception.getLineNumber());
        }
    });

    docu = builder.parse(file);
    return docu;
}