Example usage for javax.xml.validation Validator validate

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

Introduction

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

Prototype

public void validate(Source source) throws SAXException, IOException 

Source Link

Document

Validates the specified input.

Usage

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);
    }// w w  w .  ja va 2 s .  c  om
}

From source file:cz.muni.fi.mir.mathmlcanonicalization.MathMLCanonicalizer.java

/**
 * Validate the configuration against XML Schema.
 *
 * @throws ConfigException if not valid/*from w  w  w  .j ava 2s  .  c o m*/
 */
private void validateXMLConfiguration(InputStream xmlConfigurationStream) throws IOException, ConfigException {
    assert xmlConfigurationStream != null;
    final SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    try {
        final Schema schema = sf
                .newSchema(MathMLCanonicalizer.class.getResource(Settings.getProperty("configSchema")));

        final Validator validator = schema.newValidator();
        validator.validate(new StreamSource(xmlConfigurationStream));
    } catch (SAXException ex) {
        throw new ConfigException("configuration not valid\n" + ex.getMessage(), ex);
    }
}

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  w  w. j a v  a 2s.c om*/
    Schema schema = sf.newSchema(schemas);
    Validator validator = schema.newValidator();
    if (handler != null) {
        validator.setErrorHandler(handler);
    }
    validator.validate(new JAXBSource(context, document));
}

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

public void validar(ErrorHandler handler) throws Exception {
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Source[] schemas = new Source[] { new StreamSource(getClass().getResourceAsStream(XSD)),
            new StreamSource(getClass().getResourceAsStream(XSD_TFD)) };
    Schema schema = sf.newSchema(schemas);
    Validator validator = schema.newValidator();
    if (handler != null) {
        validator.setErrorHandler(handler);
    }/*from   w  w w . j av  a2s .  c o m*/
    validator.validate(new JAXBSource(context, document));
}

From source file:com.denimgroup.threadfix.importer.impl.upload.SSVLChannelImporter.java

@Nonnull
@Override/*from w  ww. j av  a  2s .c om*/
public ScanCheckResultBean checkFile() {

    boolean valid = false;
    String[] schemaList = new String[] { "ssvl.xsd", "ssvl_v0.3.xsd" };

    for (String schemaFilePath : schemaList) {

        try {
            URL schemaFile = ResourceUtils.getResourceAsUrl(schemaFilePath);

            if (schemaFile == null) {
                throw new IllegalStateException("ssvl.xsd file not available from ClassLoader. Fix that.");
            }

            if (inputFileName == null) {
                throw new IllegalStateException("inputFileName was null, unable to load scan file.");
            }

            Source xmlFile = new StreamSource(new File(inputFileName));
            SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = schemaFactory.newSchema(schemaFile);
            Validator validator = schema.newValidator();
            validator.validate(xmlFile);

            valid = true;
            log.info(xmlFile.getSystemId() + " is valid");
            break;

        } catch (MalformedURLException e) {
            log.error("Code contained an incorrect path to the XSD file.", e);
        } catch (SAXException e) {
            log.warn("SAX Exception encountered, ", e);
        } catch (IOException e) {
            log.warn("IOException encountered, ", e);
        }
    }

    if (valid) {
        return testSAXInput(new SSVLChannelSAXValidator());
    } else {
        return new ScanCheckResultBean(ScanImportStatus.FAILED_XSD);
    }
}

From source file:io.aino.agents.wso2.mediator.factory.AinoMediatorFactory.java

private void validateXml(OMElement element, String schemaPath) throws SAXException, IOException {
    OMFactory doomFactory = DOOMAbstractFactory.getOMFactory();

    StAXOMBuilder doomBuilder = new StAXOMBuilder(doomFactory, element.getXMLStreamReader());

    Element domElement = (Element) doomBuilder.getDocumentElement();

    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Source source = new StreamSource(this.getClass().getResourceAsStream(schemaPath));

    Schema schema = factory.newSchema(source);

    Validator validator = schema.newValidator();
    validator.validate(new DOMSource(domElement));
}

From source file:edu.wustl.bulkoperator.templateImport.AbstractImportBulkOperation.java

/**
 *
 * @param operationName//  www. j av a  2 s. c o  m
 * @param dropdownName
 * @param csvFile
 * @param xmlFile
 * @param mappingXml
 * @return
 * @throws BulkOperationException
 * @throws SQLException
 * @throws IOException
 * @throws DAOException
 */
protected Set<String> validate(String operationName, String dropdownName, String csvFile, String xmlFile,
        String mappingXml, String xsdLocation)
        throws BulkOperationException, SQLException, IOException, DAOException {
    Set<String> errorList = null;
    CsvReader csvReader = null;
    try {
        csvReader = CsvFileReader.createCsvFileReader(csvFile, true);

    } catch (Exception exp) {
        ErrorKey errorkey = ErrorKey.getErrorKey("bulk.error.incorrect.csv.file");
        throw new BulkOperationException(errorkey, exp, "");
    }
    BulkOperationMetaData bulkOperationMetaData = null;
    try {
        SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
        File schemaLocation = new File(xsdLocation);
        Schema schema = factory.newSchema(schemaLocation);
        DigesterLoader digesterLoader = DigesterLoader.newLoader(new XmlRulesModule(mappingXml));
        Digester digester = digesterLoader.newDigester();
        digester.setValidating(true);
        digester.setXMLSchema(schema);
        Validator validator = schema.newValidator();
        Source xmlFileForValidation = new StreamSource(new File(xmlFile));
        validator.validate(xmlFileForValidation);
        InputStream inputStream = new FileInputStream(xmlFile);
        bulkOperationMetaData = digester.parse(inputStream);
    } catch (SAXException e) {
        logger.debug(e.getMessage());
        ErrorKey errorkey = ErrorKey.getErrorKey("bulk.error.xml.template");
        throw new BulkOperationException(errorkey, e, e.getMessage());
    }
    Collection<BulkOperationClass> classList = bulkOperationMetaData.getBulkOperationClass();
    if (classList == null) {
        ErrorKey errorkey = ErrorKey.getErrorKey("bulk.no.templates.loaded.message");
        throw new BulkOperationException(errorkey, null, "");
    } else {
        Iterator<BulkOperationClass> iterator = classList.iterator();
        if (iterator.hasNext()) {
            BulkOperationClass bulkOperationClass = iterator.next();
            TemplateValidator templateValidator = new TemplateValidator();
            errorList = templateValidator.validateXmlAndCsv(bulkOperationClass, operationName, csvReader);
        }
    }
    return errorList;
}

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

@Override
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  av  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));
}

From source file:eu.planets_project.tb.gui.backing.exp.utils.ExpTypeWeeUtils.java

private void checkValidXMLConfig(InputStream xmlWFConfig) throws Exception {
    InputStream bis = getClass().getClassLoader().getResourceAsStream("planets_wdt.xsd");
    try {//w  ww . jav a2 s.  c o m
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = factory.newSchema(new StreamSource(bis));
        Validator validator = schema.newValidator();
        // Validate file against schema
        XMLOutputter outputter = new XMLOutputter();
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(xmlWFConfig);
        validator.validate(new StreamSource(new StringReader(outputter.outputString(doc.getRootElement()))));
    } catch (Exception e) {
        String err = "The provided xmlWFConfig is not valid against the currently used planets_wdt_xsd schema";
        log.debug(err, e);
        throw new Exception(err, e);
    } finally {
        bis.close();
    }
}

From source file:hydrograph.ui.propertywindow.widgets.customwidgets.schema.GridRowLoader.java

private boolean validateXML(InputStream xml, InputStream xsd) {
    try {//  w  w w .java 2  s .c om
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        javax.xml.validation.Schema schema = factory.newSchema(new StreamSource(xsd));
        Validator validator = schema.newValidator();

        validator.validate(new StreamSource(xml));
        return true;
    } catch (SAXException | IOException ex) {
        MessageDialog.openError(Display.getCurrent().getActiveShell(), "Error",
                Messages.IMPORT_XML_FORMAT_ERROR + "-\n" + ex.getMessage());
        logger.error(Messages.IMPORT_XML_FORMAT_ERROR);
        return false;
    }
}