Search in sources :

Example 11 with SchemaFactory

use of javax.xml.validation.SchemaFactory 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 12 with SchemaFactory

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

the class XmlSignatureProcessor method getSchema.

protected Schema getSchema(Message message) throws SAXException, XmlSignatureException, IOException {
    String schemaResourceUri = getSchemaResourceUri(message);
    if (schemaResourceUri == null || schemaResourceUri.isEmpty()) {
        return null;
    }
    InputStream is = ResourceHelper.resolveResourceAsInputStream(getConfiguration().getCamelContext().getClassResolver(), schemaResourceUri);
    if (is == null) {
        throw new XmlSignatureException("XML Signature component is wrongly configured: No XML schema found for specified schema resource URI " + schemaResourceUri);
    }
    byte[] bytes = null;
    try {
        bytes = IOConverter.toBytes(is);
    } finally {
        // and make sure to close the input stream after the schema has been loaded
        IOHelper.close(is);
    }
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    schemaFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
    schemaFactory.setResourceResolver(new DefaultLSResourceResolver(getConfiguration().getCamelContext(), getConfiguration().getSchemaResourceUri()));
    LOG.debug("Instantiating schema for validation");
    return schemaFactory.newSchema(new BytesSource(bytes));
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) BytesSource(org.apache.camel.BytesSource) XmlSignatureException(org.apache.camel.component.xmlsecurity.api.XmlSignatureException) InputStream(java.io.InputStream) DefaultLSResourceResolver(org.apache.camel.component.validator.DefaultLSResourceResolver)

Example 13 with SchemaFactory

use of javax.xml.validation.SchemaFactory 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)

Example 14 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project gocd by gocd.

the class MagicalGoConfigXmlWriterTest method shouldBeAValidXSD.

@Test
public void shouldBeAValidXSD() throws Exception {
    SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    factory.newSchema(new StreamSource(getClass().getResourceAsStream("/cruise-config.xsd")));
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) StreamSource(javax.xml.transform.stream.StreamSource) Test(org.junit.Test)

Example 15 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project graphhopper by graphhopper.

the class InstructionListTest method verifyGPX.

public void verifyGPX(String gpx) {
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = null;
    try {
        Source schemaFile = new StreamSource(getClass().getResourceAsStream("gpx-schema.xsd"));
        schema = schemaFactory.newSchema(schemaFile);
    // using more schemas: http://stackoverflow.com/q/1094893/194609
    } catch (SAXException e1) {
        throw new IllegalStateException("There was a problem with the schema supplied for validation. Message:" + e1.getMessage());
    }
    Validator validator = schema.newValidator();
    try {
        validator.validate(new StreamSource(new StringReader(gpx)));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Validator(javax.xml.validation.Validator) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException)

Aggregations

SchemaFactory (javax.xml.validation.SchemaFactory)92 Schema (javax.xml.validation.Schema)72 StreamSource (javax.xml.transform.stream.StreamSource)47 Validator (javax.xml.validation.Validator)39 SAXException (org.xml.sax.SAXException)29 Source (javax.xml.transform.Source)28 IOException (java.io.IOException)20 URL (java.net.URL)18 DOMSource (javax.xml.transform.dom.DOMSource)18 JAXBContext (javax.xml.bind.JAXBContext)17 File (java.io.File)16 InputStream (java.io.InputStream)16 InputSource (org.xml.sax.InputSource)14 DocumentBuilder (javax.xml.parsers.DocumentBuilder)13 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)12 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)12 Unmarshaller (javax.xml.bind.Unmarshaller)11 Test (org.junit.Test)10 Document (org.w3c.dom.Document)10 ByteArrayInputStream (java.io.ByteArrayInputStream)8