Search in sources :

Example 71 with Validator

use of javax.xml.validation.Validator in project JMRI by JMRI.

the class XMLUtil method validate.

/**
     * Check whether a DOM tree is valid according to a schema. Example of
     * usage:
     * <pre>
     * Element fragment = ...;
     * SchemaFactory f = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
     * Schema s = f.newSchema(This.class.getResource("something.xsd"));
     * try {
     *     XMLUtil.validate(fragment, s);
     *     // valid
     * } catch (SAXException x) {
     *     // invalid
     * }
     * </pre>
     *
     * @param data   a DOM tree
     * @param schema a parsed schema
     * @throws SAXException if validation failed
     * @since org.openide.util 7.17
     */
public static void validate(Element data, Schema schema) throws SAXException {
    Validator v = schema.newValidator();
    final SAXException[] error = { null };
    v.setErrorHandler(new ErrorHandler() {

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

        @Override
        public void error(SAXParseException x) throws SAXException {
            // Just rethrowing it is bad because it will also print it to stderr.
            error[0] = x;
        }

        @Override
        public void fatalError(SAXParseException x) throws SAXException {
            error[0] = x;
        }
    });
    try {
        v.validate(new DOMSource(fixupAttrs(data)));
    } catch (IOException x) {
        assert false : x;
    }
    if (error[0] != null) {
        throw error[0];
    }
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) DOMSource(javax.xml.transform.dom.DOMSource) SAXParseException(org.xml.sax.SAXParseException) IOException(java.io.IOException) Validator(javax.xml.validation.Validator) SAXException(org.xml.sax.SAXException)

Example 72 with Validator

use of javax.xml.validation.Validator in project fc-java-sdk by aliyun.

the class XmlUtils method validateXml.

public static void validateXml(Node root, InputStream xsd) throws SAXException, IOException {
    try {
        Source source = new StreamSource(xsd);
        Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(source);
        Validator validator = schema.newValidator();
        validator.validate(new DOMSource(root));
    } finally {
        closeStream(xsd);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Validator(javax.xml.validation.Validator)

Example 73 with Validator

use of javax.xml.validation.Validator in project aries by apache.

the class BlueprintFileWriterTest method generatedXmlIsValid.

@Test
public void generatedXmlIsValid() throws Exception {
    Document document = readToDocument(xmlAsBytes, true);
    Source[] schemas = new StreamSource[] { new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/example.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/blueprint.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/ext/impl/blueprint-ext.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/ext/impl/blueprint-ext-1.1.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/ext/impl/blueprint-ext-1.2.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/ext/impl/blueprint-ext-1.3.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/ext/impl/blueprint-ext-1.4.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/ext/impl/blueprint-ext-1.5.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/transaction/parsing/transactionv12.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/jpa/blueprint/namespace/jpa_110.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/compendium/cm/blueprint-cm-1.0.0.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/compendium/cm/blueprint-cm-1.1.0.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/compendium/cm/blueprint-cm-1.3.0.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/compendium/cm/blueprint-cm-1.2.0.xsd")) };
    Source xmlFile = new DOMSource(document);
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = schemaFactory.newSchema(schemas);
    Validator validator = schema.newValidator();
    validator.validate(xmlFile);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Validator(javax.xml.validation.Validator) Test(org.junit.Test)

Example 74 with Validator

use of javax.xml.validation.Validator in project logging-log4j2 by apache.

the class XmlCompactFileAppenderValidationTest method validateXmlSchema.

private void validateXmlSchema(final File file) throws SAXException, IOException {
    final URL schemaFile = this.getClass().getClassLoader().getResource("Log4j-events.xsd");
    final Source xmlFile = new StreamSource(file);
    final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    final Schema schema = schemaFactory.newSchema(schemaFile);
    final Validator validator = schema.newValidator();
    validator.validate(xmlFile);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) URL(java.net.URL) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Validator(javax.xml.validation.Validator)

Example 75 with Validator

use of javax.xml.validation.Validator in project maven-plugins by apache.

the class DefaultChangesSchemaValidator method validateXmlWithSchema.

public XmlValidationHandler validateXmlWithSchema(File file, String schemaVersion, boolean failOnValidationError) throws SchemaValidatorException {
    Reader reader = null;
    try {
        String schemaPath = CHANGES_SCHEMA_PATH + "changes-" + schemaVersion + ".xsd";
        Schema schema = getSchema(schemaPath);
        Validator validator = schema.newValidator();
        XmlValidationHandler baseHandler = new XmlValidationHandler(failOnValidationError);
        validator.setErrorHandler(baseHandler);
        reader = new XmlStreamReader(file);
        validator.validate(new StreamSource(reader));
        reader.close();
        reader = null;
        return baseHandler;
    } catch (IOException e) {
        throw new SchemaValidatorException("IOException : " + e.getMessage(), e);
    } catch (SAXException e) {
        throw new SchemaValidatorException("SAXException : " + e.getMessage(), e);
    } catch (Exception e) {
        throw new SchemaValidatorException("Exception : " + e.getMessage(), e);
    } finally {
        IOUtil.close(reader);
    }
}
Also used : Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) Reader(java.io.Reader) XmlStreamReader(org.apache.commons.io.input.XmlStreamReader) XmlStreamReader(org.apache.commons.io.input.XmlStreamReader) IOException(java.io.IOException) Validator(javax.xml.validation.Validator) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException)

Aggregations

Validator (javax.xml.validation.Validator)80 Schema (javax.xml.validation.Schema)51 SchemaFactory (javax.xml.validation.SchemaFactory)39 StreamSource (javax.xml.transform.stream.StreamSource)38 DOMSource (javax.xml.transform.dom.DOMSource)30 Source (javax.xml.transform.Source)29 Test (org.junit.Test)21 SAXException (org.xml.sax.SAXException)21 IOException (java.io.IOException)17 MarshallingTest (org.orcid.jaxb.model.notification.custom.MarshallingTest)17 Document (org.w3c.dom.Document)13 InputStream (java.io.InputStream)9 URL (java.net.URL)9 DocumentBuilder (javax.xml.parsers.DocumentBuilder)9 Url (org.orcid.jaxb.model.common_v2.Url)8 InputSource (org.xml.sax.InputSource)8 StringReader (java.io.StringReader)7 JAXBSource (javax.xml.bind.util.JAXBSource)7 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)7 SAXParseException (org.xml.sax.SAXParseException)7