Search in sources :

Example 1 with Validator

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

the class ValidatingProcessor method doProcess.

protected void doProcess(Exchange exchange) throws Exception {
    Schema schema;
    if (isUseSharedSchema()) {
        schema = getSchema();
    } else {
        schema = createSchema();
    }
    Validator validator = schema.newValidator();
    // the underlying input stream, which we need to close to avoid locking files or other resources
    Source source = null;
    InputStream is = null;
    try {
        Result result = null;
        // only convert to input stream if really needed
        if (isInputStreamNeeded(exchange)) {
            is = getContentToValidate(exchange, InputStream.class);
            if (is != null) {
                source = getSource(exchange, is);
            }
        } else {
            Object content = getContentToValidate(exchange);
            if (content != null) {
                source = getSource(exchange, content);
            }
        }
        if (shouldUseHeader()) {
            if (source == null && isFailOnNullHeader()) {
                throw new NoXmlHeaderValidationException(exchange, headerName);
            }
        } else {
            if (source == null && isFailOnNullBody()) {
                throw new NoXmlBodyValidationException(exchange);
            }
        }
        //CAMEL-7036 We don't need to set the result if the source is an instance of StreamSource
        if (source instanceof DOMSource) {
            result = new DOMResult();
        } else if (source instanceof SAXSource) {
            result = new SAXResult();
        } else if (source instanceof StAXSource || source instanceof StreamSource) {
            result = null;
        }
        if (source != null) {
            // create a new errorHandler and set it on the validator
            // must be a local instance to avoid problems with concurrency (to be
            // thread safe)
            ValidatorErrorHandler handler = errorHandler.getClass().newInstance();
            validator.setErrorHandler(handler);
            try {
                LOG.trace("Validating {}", source);
                validator.validate(source, result);
                handler.handleErrors(exchange, schema, result);
            } catch (SAXParseException e) {
                // can be thrown for non well formed XML
                throw new SchemaValidationException(exchange, schema, Collections.singletonList(e), Collections.<SAXParseException>emptyList(), Collections.<SAXParseException>emptyList());
            }
        }
    } finally {
        IOHelper.close(is);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DOMResult(javax.xml.transform.dom.DOMResult) InputStream(java.io.InputStream) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) StAXSource(javax.xml.transform.stax.StAXSource) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) StAXSource(javax.xml.transform.stax.StAXSource) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult) DOMResult(javax.xml.transform.dom.DOMResult) SAXSource(javax.xml.transform.sax.SAXSource) SAXResult(javax.xml.transform.sax.SAXResult) SAXParseException(org.xml.sax.SAXParseException) Validator(javax.xml.validation.Validator)

Example 2 with Validator

use of javax.xml.validation.Validator in project head by mifos.

the class ChartOfAccountsConfig method load.

/**
     * Factory method which loads the Chart of Accounts configuration from the
     * given filename. Given XML filename will be validated against
     * {@link FilePaths#CHART_OF_ACCOUNTS_SCHEMA}.
     *
     * @param chartOfAccountsXml
     *            a relative path to the Chart of Accounts configuration file.
     */
public static ChartOfAccountsConfig load(String chartOfAccountsXml) throws ConfigurationException {
    ChartOfAccountsConfig instance = null;
    Document document = null;
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder parser = dbf.newDocumentBuilder();
        if (FilePaths.CHART_OF_ACCOUNTS_DEFAULT.equals(chartOfAccountsXml)) {
            // default chart of accounts
            document = parser.parse(MifosResourceUtil.getClassPathResourceAsStream(chartOfAccountsXml));
        } else {
            // custom chart of accounts
            document = parser.parse(MifosResourceUtil.getFile(chartOfAccountsXml));
        }
        // create a SchemaFactory capable of understanding XML schemas
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        // load an XML schema
        ClassPathResource schemaFileResource = new ClassPathResource(FilePaths.CHART_OF_ACCOUNTS_SCHEMA);
        Source schemaFile = null;
        if (schemaFileResource.exists()) {
            InputStream in = ChartOfAccountsConfig.class.getClassLoader().getResourceAsStream(FilePaths.CHART_OF_ACCOUNTS_SCHEMA);
            schemaFile = new StreamSource(in);
        } else {
            schemaFile = new StreamSource(MifosResourceUtil.getFile(FilePaths.CHART_OF_ACCOUNTS_SCHEMA));
        }
        Schema schema = factory.newSchema(schemaFile);
        // create a Validator instance and validate document
        Validator validator = schema.newValidator();
        validator.validate(new DOMSource(document));
    } catch (IOException e) {
        throw new ConfigurationException(e);
    } catch (SAXException e) {
        throw new ConfigurationException(e);
    } catch (ParserConfigurationException e) {
        throw new ConfigurationException(e);
    }
    instance = new ChartOfAccountsConfig();
    instance.coaDocument = document;
    return instance;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) IOException(java.io.IOException) Document(org.w3c.dom.Document) ClassPathResource(org.springframework.core.io.ClassPathResource) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ConfigurationException(org.mifos.config.exceptions.ConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Validator(javax.xml.validation.Validator)

Example 3 with Validator

use of javax.xml.validation.Validator in project openblocks by mikaelhg.

the class WorkspaceController method validate.

/**
     * Validates the code blocks document against the schema
     * @param document The document to check
     * @throws RuntimeException If the validation failed
     */
private void validate(Document document) {
    try {
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        URL schemaUrl = ClassLoader.getSystemResource("edu/mit/blocks/codeblocks/codeblocks.xsd");
        Schema schema = schemaFactory.newSchema(schemaUrl);
        Validator validator = schema.newValidator();
        validator.validate(new DOMSource(document));
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    } catch (SAXException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) MalformedURLException(java.net.MalformedURLException) Schema(javax.xml.validation.Schema) IOException(java.io.IOException) URL(java.net.URL) Validator(javax.xml.validation.Validator) SAXException(org.xml.sax.SAXException)

Example 4 with Validator

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

the class XAdESSignaturePropertiesTest method validateAgainstSchema.

private void validateAgainstSchema(Document doc) throws Exception {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Source schema1 = new StreamSource(new File("target/test-classes/org/apache/camel/component/xmlsecurity/xades/XAdES.xsd"));
    Source schema2 = new StreamSource(new File("target/test-classes/org/apache/camel/component/xmlsecurity/xades/xmldsig-core-schema.xsd"));
    Schema schema = factory.newSchema(new Source[] { schema2, schema1 });
    Validator validator = schema.newValidator();
    validator.validate(new DOMSource(doc));
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) File(java.io.File) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Validator(javax.xml.validation.Validator)

Example 5 with Validator

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

the class ClientDiscoverySpiTest method testSchema.

@Test
public void testSchema() throws Exception {
    String xmlFileName = "hazelcast-client-discovery-spi-test.xml";
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    URL schemaResource = ClientDiscoverySpiTest.class.getClassLoader().getResource("hazelcast-client-config-3.9.xsd");
    Schema schema = factory.newSchema(schemaResource);
    InputStream xmlResource = ClientDiscoverySpiTest.class.getClassLoader().getResourceAsStream(xmlFileName);
    Source source = new StreamSource(xmlResource);
    Validator validator = schema.newValidator();
    validator.validate(source);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputStream(java.io.InputStream) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) URL(java.net.URL) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Validator(javax.xml.validation.Validator) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

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