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:org.wikipedia.vlsergey.secretary.utils.AbstractDocumentBuilderPool.java

public DocumentBuilder borrow(ErrorHandler errorHandler) throws ParserConfigurationException {
    DocumentBuilder builder = borrow();
    builder.setErrorHandler(errorHandler);
    return builder;
}

From source file:org.wikipedia.vlsergey.secretary.utils.AbstractDocumentBuilderPool.java

public Document parse(File file, ErrorHandler errorHandler)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilder builder = borrow();
    builder.setErrorHandler(errorHandler);
    try {//  ww w  . j a  v  a 2 s.  c  om
        return builder.parse(file);
    } finally {
        returnObject(builder);
    }
}

From source file:ca.uhn.hunit.event.expect.AbstractXmlExpectMessage.java

@Override
public void receiveMessage(IExecutionContext theCtx, TestMessage<?> theMessage) throws TestFailureException {
    Document parsedMessage = (Document) theMessage.getParsedMessage();

    if (parsedMessage == null) {
        final String rawMessage = theMessage.getRawMessage();

        try {//from w  ww  .  j a v  a 2  s. co  m
            DocumentBuilder parser = myParserFactory.newDocumentBuilder();
            parser.setErrorHandler(new MyErrorHandler(LogFactory.INSTANCE.get(getTest())));
            parsedMessage = parser.parse(new InputSource(new StringReader(rawMessage)));
        } catch (ParserConfigurationException ex) {
            throw new UnexpectedTestFailureException("Unable to set up XML parser", ex);
        } catch (SAXException ex) {
            throw new IncorrectMessageReceivedException(getTest(), theMessage,
                    "Unable to parse incoming message: " + ex.getMessage());
        } catch (IOException ex) {
            throw new UnexpectedTestFailureException(ex);
        }

        TestMessage<Document> testMessage = new TestMessage<Document>(rawMessage, parsedMessage);
        validateMessage(testMessage);
    }
}

From source file:ar.com.tadp.xml.rinzo.core.resources.validation.XMLStringValidator.java

private void plainTextValidate(String fileName, String fileContent) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);/*from  www.  jav a 2s  .c o m*/
    factory.setValidating(true);
    try {
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setErrorHandler(this.errorHandler);
        builder.parse(new InputSource(new StringReader(fileContent)));
    } catch (Exception e) {
        //Do nothing because the errorHandler informs the error
    }
}

From source file:org.wikipedia.vlsergey.secretary.utils.AbstractDocumentBuilderPool.java

@Override
public DocumentBuilder makeObject() throws ParserConfigurationException {
    final DocumentBuilder newDocumentBuilder = getDocumentBuilderFactory().newDocumentBuilder();
    newDocumentBuilder.setErrorHandler(defaultErrorHandler);
    return newDocumentBuilder;
}

From source file:cz.cas.lib.proarc.common.export.mets.MetsUtils.java

/**
 *
 * Validates given XML file against an XSD schema
 *
 * @param file/*from ww  w. jav  a  2 s .  c  om*/
 * @param xsd
 * @return
 */
public static List<String> validateAgainstXSD(File file, InputStream xsd) throws Exception {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    factory.setResourceResolver(MetsLSResolver.getInstance());
    Schema schema = factory.newSchema(new StreamSource(xsd));
    DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
    dbfactory.setValidating(false);
    dbfactory.setNamespaceAware(true);
    dbfactory.setSchema(schema);
    DocumentBuilder documentBuilder = dbfactory.newDocumentBuilder();
    ValidationErrorHandler errorHandler = new ValidationErrorHandler();
    documentBuilder.setErrorHandler(errorHandler);
    documentBuilder.parse(file);
    return errorHandler.getValidationErrors();
}

From source file:cz.cas.lib.proarc.common.export.mets.MetsUtils.java

/**
 *
 * Validates given document agains an XSD schema
 *
 * @param document/*  w w w.  j a  v  a2 s. c  om*/
 * @param xsd
 * @return
 */
public static List<String> validateAgainstXSD(Document document, InputStream xsd) throws Exception {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    factory.setResourceResolver(MetsLSResolver.getInstance());
    Schema schema = factory.newSchema(new StreamSource(xsd));
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    DOMSource domSource = new DOMSource(document);
    StreamResult sResult = new StreamResult();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    sResult.setOutputStream(bos);
    transformer.transform(domSource, sResult);
    InputStream is = new ByteArrayInputStream(bos.toByteArray());
    DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
    dbfactory.setValidating(false);
    dbfactory.setNamespaceAware(true);
    dbfactory.setSchema(schema);
    DocumentBuilder documentBuilder = dbfactory.newDocumentBuilder();
    ValidationErrorHandler errorHandler = new ValidationErrorHandler();
    documentBuilder.setErrorHandler(errorHandler);
    documentBuilder.parse(is);
    return errorHandler.getValidationErrors();
}

From source file:esg.security.yadis.XrdsDoc.java

protected Document parseXmlInput(String input) throws XrdsParseException {
    if (input == null)
        throw new XrdsParseException("No XML message set");

    try {//w ww .  j ava 2  s. c  o  m
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        dbf.setValidating(true);
        dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
        dbf.setAttribute(JAXP_SCHEMA_SOURCE, new Object[] { this.getClass().getResourceAsStream(XRD_SCHEMA),
                this.getClass().getResourceAsStream(XRDS_SCHEMA), });
        DocumentBuilder builder = dbf.newDocumentBuilder();
        builder.setErrorHandler(new ErrorHandler() {
            public void error(SAXParseException exception) throws SAXException {
                throw exception;
            }

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

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

        return builder.parse(new ByteArrayInputStream(input.getBytes()));
    } catch (ParserConfigurationException e) {
        throw new XrdsParseException("Parser configuration error", e);
    } catch (SAXException e) {
        throw new XrdsParseException("Error parsing XML document", e);
    } catch (IOException e) {
        throw new XrdsParseException("Error reading XRDS document", e);
    }
}

From source file:com.mingo.parser.xml.dom.ContextConfigurationParser.java

/**
 * {@inheritDoc}// w  w w.j  a  v a 2 s . com
 */
@Override
public ContextConfiguration parse(InputStream xml) throws ParserException {
    LOGGER.debug("ContextConfiguration:: START PARSING");
    ContextConfiguration contextConfiguration;
    try {
        DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
        builder.setErrorHandler(parseErrorHandler);
        Document document = builder.parse(new InputSource(xml));
        contextConfiguration = new ContextConfiguration();
        Element element = document.getDocumentElement();
        contextConfiguration.setQuerySetConfiguration(parseQuerySetConfigTag(element));
        contextConfiguration.setQueryExecutorType(parseQueryExecutorTag(element));
        contextConfiguration.setQueryAnalyzerType(parseQueryAnalyzerTag(element));
        parseMongoTag(contextConfiguration, element);
        parseConvertersTag(contextConfiguration, element);
        contextConfiguration.setDefaultConverter(parseDefaultConverterTag(element));
    } catch (Exception e) {
        throw new ParserException(e);
    }
    return contextConfiguration;
}

From source file:WebAppConfig.java

/**
 * This constructor method is passed an XML file. It uses the JAXP API to
 * obtain a DOM parser, and to parse the file into a DOM Document object,
 * which is used by the remaining methods of the class.
 *///from www .  ja  v  a2s .  c  om
public WebAppConfig(File configfile) throws IOException, SAXException, ParserConfigurationException {
    // Get a JAXP parser factory object
    javax.xml.parsers.DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    // Tell the factory what kind of parser we want
    dbf.setValidating(false);
    // Use the factory to get a JAXP parser object
    javax.xml.parsers.DocumentBuilder parser = dbf.newDocumentBuilder();

    // Tell the parser how to handle errors. Note that in the JAXP API,
    // DOM parsers rely on the SAX API for error handling
    parser.setErrorHandler(new org.xml.sax.ErrorHandler() {
        public void warning(SAXParseException e) {
            System.err.println("WARNING: " + e.getMessage());
        }

        public void error(SAXParseException e) {
            System.err.println("ERROR: " + e.getMessage());
        }

        public void fatalError(SAXParseException e) throws SAXException {
            System.err.println("FATAL: " + e.getMessage());
            throw e; // re-throw the error
        }
    });

    // Finally, use the JAXP parser to parse the file. This call returns
    // A Document object. Now that we have this object, the rest of this
    // class uses the DOM API to work with it; JAXP is no longer required.
    document = parser.parse(configfile);
}