Example usage for javax.xml.validation Validator setErrorHandler

List of usage examples for javax.xml.validation Validator setErrorHandler

Introduction

In this page you can find the example usage for javax.xml.validation Validator setErrorHandler.

Prototype

public abstract void setErrorHandler(ErrorHandler errorHandler);

Source Link

Document

Sets the ErrorHandler to receive errors encountered during the validate method invocation.

Usage

From source file:at.tfr.securefs.module.validation.SchemaValidationModule.java

@Override
public ModuleResult apply(InputStream input, ModuleConfiguration moduleConfiguration)
        throws ModuleException, IOException {
    SchemaResolver errorHandler = null;//from w  ww  .  j av  a  2s.c  o  m
    moduleStatistics.getCalls().incrementAndGet();

    try {
        // parse an XML document into a DOM tree
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        dbf.setValidating(true);
        dbf.setCoalescing(true);
        dbf.setCoalescing(true);

        if (schema == null) {
            loadSchema(moduleConfiguration);
        }

        DOMImplementationLS domImpl = (DOMImplementationLS) dbf.newDocumentBuilder().getDOMImplementation();
        errorHandler = new SchemaResolver(configuration.getSchemaPath(), domImpl);

        Validator validator = schema.newValidator();
        validator.setErrorHandler(errorHandler);
        validator.setResourceResolver(errorHandler);

        validator.validate(new StreamSource(input));

    } catch (SAXParseException e) {
        moduleStatistics.getFailures().incrementAndGet();
        String message = "error validating: " + getCurrent() + " err: " + e;
        log.debug(message, e);
        log.info(message);
        if (moduleConfiguration.isMandatory()) {
            throw new ModuleException(message, e);
        } else {
            return new ModuleResult(false, e);
        }
    } catch (Exception e) {
        moduleStatistics.getErrors().incrementAndGet();
        String message = "error validating: " + getCurrent() + " err: " + e;
        log.info(message, e);
        log.warn(message);
        if (moduleConfiguration.isMandatory()) {
            throw new IOException(message, e);
        } else {
            return new ModuleResult(false, e);
        }
    }

    if (errorHandler != null && errorHandler.getErrors().size() > 0) {

        moduleStatistics.getFailures().incrementAndGet();

        String message = "Validation errors for File: " + getCurrent() + " errors: " + errorHandler.getErrors();
        if (log.isDebugEnabled()) {
            log.debug(message);
            if (log.isTraceEnabled()) {
                log.trace("Validation warnings for File: " + getCurrent() + " errors: "
                        + errorHandler.getWarnings());
            }
        }
        if (moduleConfiguration.isMandatory()) {
            throw new ModuleException(message);
        } else {
            log.info("accepted errors - " + message);
        }
        return new ModuleResult(false, errorHandler.getErrors().get(0));
    }

    moduleStatistics.getSuccesses().incrementAndGet();
    return new ModuleResult(true);
}

From source file:mx.bigdata.cfdi.CFDv3.java

public void validate(ErrorHandler handler) throws Exception {
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(getClass().getResource(XSD));
    Validator validator = schema.newValidator();
    if (handler != null) {
        validator.setErrorHandler(handler);
    }// ww  w  .java2s.co m
    validator.validate(new JAXBSource(CONTEXT, document));
}

From source file:mx.bigdata.sat.cfdi.TFDv1.java

public void validar(ErrorHandler handler) throws Exception {
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(getClass().getResource(XSD));
    Validator validator = schema.newValidator();
    if (handler != null) {
        validator.setErrorHandler(handler);
    }//from   www.j a v a2 s  . c o  m
    validator.validate(new JAXBSource(CONTEXT, tfd));
}

From source file:com.predic8.membrane.core.interceptor.schemavalidation.SOAPMessageValidatorInterceptor.java

private List<Validator> getValidators() throws Exception {
    log.info("Get validators for WSDL: " + wsdl);
    WSDLParserContext ctx = new WSDLParserContext();
    ctx.setInput(wsdl);// w  w  w  .  ja v a 2 s.  c  o m
    SchemaFactory sf = SchemaFactory.newInstance(Constants.XSD_NS);
    sf.setResourceResolver(new SOAModelResourceResolver(wsdl));
    List<Validator> validators = new ArrayList<Validator>();
    for (Schema schema : getEmbeddedSchemas(ctx)) {
        log.info("Adding embedded schema: " + schema);
        Validator validator = sf
                .newSchema(new StreamSource(new ByteArrayInputStream(schema.getAsString().getBytes())))
                .newValidator();
        validator.setErrorHandler(new SchemaValidatorErrorHandler());
        validators.add(validator);
    }
    return validators;
}

From source file:mx.bigdata.cfdi.TFDv1.java

public void validate(ErrorHandler handler) throws Exception {
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(getClass().getResource(XSD));
    Validator validator = schema.newValidator();
    if (handler != null) {
        validator.setErrorHandler(handler);
    }//from   ww  w .java  2 s  . co  m
    validator.validate(new JAXBSource(CONTEXT, tfd));
}

From source file:com.predic8.membrane.core.interceptor.schemavalidation.AbstractXMLSchemaValidator.java

protected List<Validator> createValidators() throws Exception {
    SchemaFactory sf = SchemaFactory.newInstance(Constants.XSD_NS);
    List<Validator> validators = new ArrayList<Validator>();
    for (Schema schema : getSchemas()) {
        log.debug("Creating validator for schema: " + schema);
        StreamSource ss = new StreamSource(new StringReader(schema.getAsString()));
        ss.setSystemId(location);//from w  ww . ja v a 2s  . c o  m
        sf.setResourceResolver(resourceResolver.toLSResourceResolver());
        Validator validator = sf.newSchema(ss).newValidator();
        validator.setResourceResolver(resourceResolver.toLSResourceResolver());
        validator.setErrorHandler(new SchemaValidatorErrorHandler());
        validators.add(validator);
    }
    return validators;
}

From source file:mx.bigdata.sat.cfdi.TFDv11c33.java

public void validar(ErrorHandler handler) throws Exception {
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Source[] schemas = new Source[XSD.length];
    for (int i = 0; i < XSD.length; i++) {
        schemas[i] = new StreamSource(getClass().getResourceAsStream(XSD[i]));
    }//from  w w w .j a va2 s .c  o  m
    Schema schema = sf.newSchema(schemas);
    Validator validator = schema.newValidator();
    if (handler != null) {
        validator.setErrorHandler(handler);
    }
    validator.validate(new JAXBSource(CONTEXT, tfd));
}

From source file:mx.bigdata.sat.cfd.CFDv2.java

public void validar(ErrorHandler handler) throws Exception {
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(getClass().getResource(XSD));
    Validator validator = schema.newValidator();
    if (handler != null) {
        validator.setErrorHandler(handler);
    }/*ww  w .  j  a  va  2s.c  o m*/
    validator.validate(new JAXBSource(context, document));
}

From source file:com.adaptris.core.transform.XmlSchemaValidator.java

@Override
public void validate(AdaptrisMessage msg) throws CoreException {
    try (InputStream in = msg.getInputStream()) {
        Validator validator = this.obtainSchemaToUse(msg).newValidator();
        validator.setErrorHandler(new ErrorHandlerImp());
        validator.validate(new SAXSource(new InputSource(in)));
    } catch (SAXParseException e) {
        throw new ServiceException(String.format("Error validating message[%s] line [%s] column[%s]",
                e.getMessage(), e.getLineNumber(), e.getColumnNumber()), e);
    } catch (Exception e) {
        throw ExceptionHelper.wrapServiceException("Failed to validate message", e);
    }//from  w  ww . ja  v a2 s  .  c  o m
}

From source file:mx.bigdata.sat.cfd.CFDv22.java

public void validar(ErrorHandler handler) throws Exception {
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Source[] schemas = new Source[XSD.length];
    for (int i = 0; i < XSD.length; i++) {
        schemas[i] = new StreamSource(getClass().getResourceAsStream(XSD[i]));
    }/*from   w ww.  j  a v  a 2 s  . c o  m*/
    Schema schema = sf.newSchema(schemas);
    Validator validator = schema.newValidator();
    if (handler != null) {
        validator.setErrorHandler(handler);
    }
    validator.validate(new JAXBSource(context, document));
}