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:gov.nih.nci.ncicb.cadsr.bulkloader.schema.validator.SchemaValidatorImpl.java

public SchemaValidationResult validate(File _xmlFile) {
    SchemaValidationResult result = new SchemaValidationResult();
    result.setInputFile(_xmlFile);//from w w w  .  jav a2s  . co m
    try {
        Validator validator = getValidator();
        Source source = getSource(_xmlFile);
        validator.validate(source);
    } catch (FileNotFoundException e) {
        result.setException(e);
        result.setStatus(SchemaValidationStatus.FILE_READ_FAILURE);
        result.setMessage(e.getMessage());
    } catch (SAXException e) {
        result.setException(e);
        result.setStatus(SchemaValidationStatus.FAILURE);
    } catch (IOException e) {
        result.setException(e);
        result.setStatus(SchemaValidationStatus.FAILURE);
    }

    return result;
}

From source file:com.smartitengineering.cms.spi.impl.type.validator.XMLSchemaBasedTypeValidator.java

protected boolean isValid(Document document) throws Exception {
    // create a SchemaFactory capable of understanding WXS schemas
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    final InputStream xsdStream = getClass().getClassLoader().getResourceAsStream(XSD_LOCATION);
    // load a WXS schema, represented by a Schema instance
    Source schemaFile = new StreamSource(xsdStream);
    schemaFile.setSystemId(CONTENT_TYPE_SCHEMA_URI);
    Schema schema = factory.newSchema(schemaFile);
    // create a Validator instance, which can be used to validate an instance document
    Validator validator = schema.newValidator();
    // validate the DOM tree
    try {//from   ww  w.j  a  v a  2  s.  c  o  m
        validator.validate(new DOMSource(document));
        return true;
    } catch (SAXException e) {
        e.printStackTrace();
        return false;
    }
}

From source file:cz.strmik.cmmitool.cmmi.DefaultRatingScalesProvider.java

private void validateScalesDocument(Document document) throws SAXException, IOException {
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Source schemaFile = new StreamSource(getClass().getClassLoader().getResourceAsStream(SCALES_XSD));
    Schema schema = schemaFactory.newSchema(schemaFile);
    Validator validator = schema.newValidator();
    validator.validate(new DOMSource(document));
}

From source file:net.geoprism.gis.sld.SLDValidator.java

public void validate(String sld) {
    try {//from w  w w  . j a v a2s  . c o  m
        SchemaFactory f = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Source xsdS = new StreamSource(xsd);
        Schema schema = f.newSchema(xsdS);
        Validator v = schema.newValidator();

        InputStream stream;
        stream = new ByteArrayInputStream(sld.getBytes("UTF-8"));
        Source s = new StreamSource(stream);
        v.validate(s);
    } catch (Throwable e) {
        log.error(sld, e);
        throw new ProgrammingErrorException("Invalid SLD", e);
    }
}

From source file:io.aino.agents.core.config.InputStreamConfigBuilder.java

/**
 * Validates the config file against XSD schema.
 *
 * @param stream InputStream to config file.
 *//*from   w ww  .  java2s  .c  om*/
private void validateSchema(InputStream stream) {
    try {
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        InputStream xsdStream = this.getClass().getClassLoader().getResourceAsStream(LOGGER_SCHEMA);
        Schema schema = factory.newSchema(new StreamSource(xsdStream));
        Validator validator = schema.newValidator();
        validator.validate(new StreamSource(stream));
    } catch (SAXException e) {
        throw new InvalidAgentConfigException("Failed to validate logger config.", e);
    } catch (IOException e) {
        throw new InvalidAgentConfigException("Failed to validate logger config.", e);
    }
}

From source file:eu.planets_project.pp.plato.evaluation.evaluators.ExperimentEvaluator.java

private String evaluateLogging(String logOutput) {
    if ((logOutput == null) || "".equals(logOutput)) {
        return "none";
    } else {/* w w  w.jav  a  2  s. c o  m*/
        String result = "text";

        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        try {
            Schema schema = factory.newSchema();
            Validator validator = schema.newValidator();

            validator.validate(new StreamSource(new StringReader(logOutput)));

            // ok, the log is well-formed XML
            result = "XML";
        } catch (SAXException e) {
            // no xml - this is ok
        } catch (IOException e) {
            log.error("logoutput-evaluator is not properly configured: ", e);
        }

        return result;
    }
}

From source file:gov.nih.nci.ncicb.tcga.dcc.common.util.BCRXMLFileReader.java

private void processXML(final InputStream in) throws SAXException, ParserConfigurationException, IOException {
    // Some of the following code is nabbed from http://www.ibm.com/developerworks/xml/library/x-javaxmlvalidapi.html
    final SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    final File schemaLocation = bcrXSD;
    final Schema schema;
    schema = factory.newSchema(schemaLocation);
    final DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true); // never forget this
    final DocumentBuilder builder = domFactory.newDocumentBuilder();
    document = builder.parse(in);/*from w ww.j  a v a 2 s.  co  m*/
    final DOMSource source = new DOMSource(document);
    final Validator validator = schema.newValidator();
    //Check to see if we actually want to validate docs or just skip.
    if (isValidate()) {
        validator.validate(source);
    }
}

From source file:eu.delving.test.TestMappingEngine.java

@Test
public void validateABMNode()
        throws IOException, SAXException, MappingException, XMLStreamException, MetadataException {
    Map<String, String> namespaces = createNamespaces("dc", "http://purl.org/dc/elements/1.1/", "dcterms",
            "http://purl.org/dc/terms/", "europeana", "http://www.europeana.eu/schemas/ese/", "delving",
            "http://schemas.delving.eu/", "abm", "http://abmu.org/abm");
    MappingEngine mappingEngine = MappingEngineFactory.newInstance(classLoader(), namespaces,
            new MockRecDefModel(), mapping("abm"));
    MappingResult result = mappingEngine.execute("validateABMNode", input("abm"));
    //        System.out.println(result.toXml());
    Source source = new DOMSource(result.root());
    Validator validator = validator(new SchemaVersion("abm", "1.0.7"));
    validator.validate(source);
    //        System.out.println("Fields:");
    //        for (Map.Entry<String, List<String>> entry : result.fields().entrySet()) {
    //            System.out.println(entry.getKey()+":"+entry.getValue());
    //        }//from   w  w w. j  a v a 2s.c om
    Assert.assertEquals("Number of fields unexpected", 24, result.fields().size());
    //        System.out.println("SystemFields:");
    //        System.out.println(result.copyFields());
}

From source file:com.amalto.core.schema.validation.XmlSchemaValidator.java

public void validate(Element element) throws ValidateException {
    javax.xml.validation.Validator validator = getValidator();
    SAXErrorHandler errorHandler = new SAXErrorHandler();
    try {/*from  w  w w .ja v a2  s  . c  o m*/
        validator.setErrorHandler(errorHandler);
        validator.validate(new DOMSource(element));
    } catch (Exception e) {
        // All errors are expected to be handled by the error handler.
        throw new RuntimeException("Unexpected validation issue.", e);
    }
    String errors = errorHandler.getErrors();
    if (!errors.isEmpty()) {
        throw new ValidateException(errors);
    }
    next.validate(element);
}

From source file:com.azaptree.services.spring.application.config.SpringApplicationServiceConfig.java

private SpringApplicationService parse(final InputStream xml) throws JAXBException {
    Assert.notNull(xml);/*from   ww  w  .j  a  v a  2 s .  co  m*/
    final JAXBContext jc = JAXBContext.newInstance(SpringApplicationService.class);
    final Unmarshaller u = jc.createUnmarshaller();
    final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    final ByteArrayOutputStream bos = new ByteArrayOutputStream(512);
    generateSchema(bos);
    try {
        final SpringApplicationService springApplicationService = (SpringApplicationService) u.unmarshal(xml);
        final Schema schema = schemaFactory
                .newSchema(new StreamSource(new ByteArrayInputStream(bos.toByteArray())));
        final Validator validator = schema.newValidator();
        validator.validate(new JAXBSource(jc, springApplicationService));
        return springApplicationService;
    } catch (SAXException | IOException e) {
        throw new IllegalArgumentException(
                "Failed to parse XML. The XML must conform to the following schema:\n" + bos, e);
    }
}