Search in sources :

Example 46 with SchemaFactory

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

the class XmlSchema method getSchema.

private Schema getSchema(Source source, String currentDir, String scriptFileName) throws SAXException {
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    SchemaResourceResolver resourceResolver = new SchemaResourceResolver(currentDir, scriptFileName, null);
    schemaFactory.setResourceResolver(resourceResolver);
    schemaFactory.setErrorHandler(new IgnoreSchemaErrorsErrorHandler());
    return schemaFactory.newSchema(source);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) IgnoreSchemaErrorsErrorHandler(nokogiri.internals.IgnoreSchemaErrorsErrorHandler)

Example 47 with SchemaFactory

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

the class XMLConfigBuilderTest method testXSDConfigXML.

private void testXSDConfigXML(String xmlFileName) throws Exception {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    URL schemaResource = XMLConfigBuilderTest.class.getClassLoader().getResource("hazelcast-config-3.9.xsd");
    assertNotNull(schemaResource);
    InputStream xmlResource = XMLConfigBuilderTest.class.getClassLoader().getResourceAsStream(xmlFileName);
    Schema schema = factory.newSchema(schemaResource);
    Source source = new StreamSource(xmlResource);
    Validator validator = schema.newValidator();
    try {
        validator.validate(source);
    } catch (SAXException ex) {
        fail(xmlFileName + " is not valid because: " + ex.toString());
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) 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) SAXException(org.xml.sax.SAXException)

Example 48 with SchemaFactory

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

the class AbstractXmlConfigHelper method schemaValidation.

protected void schemaValidation(Document doc) throws Exception {
    ArrayList<StreamSource> schemas = new ArrayList<StreamSource>();
    InputStream inputStream = null;
    String schemaLocation = doc.getDocumentElement().getAttribute("xsi:schemaLocation");
    schemaLocation = schemaLocation.replaceAll("^ +| +$| (?= )", "");
    // get every two pair. every pair includes namespace and uri
    String[] xsdLocations = schemaLocation.split("(?<!\\G\\S+)\\s");
    for (String xsdLocation : xsdLocations) {
        if (xsdLocation.isEmpty()) {
            continue;
        }
        String namespace = xsdLocation.split('[' + LINE_SEPARATOR + " ]+")[0];
        String uri = xsdLocation.split('[' + LINE_SEPARATOR + " ]+")[1];
        // if this is hazelcast namespace but location is different log only warning
        if (namespace.equals(xmlns) && !uri.endsWith(hazelcastSchemaLocation)) {
            LOGGER.warning("Name of the hazelcast schema location incorrect using default");
        }
        // if this is not hazelcast namespace then try to load from uri
        if (!namespace.equals(xmlns)) {
            inputStream = loadSchemaFile(uri);
            schemas.add(new StreamSource(inputStream));
        }
    }
    // include hazelcast schema
    schemas.add(new StreamSource(getClass().getClassLoader().getResourceAsStream(hazelcastSchemaLocation)));
    // document to InputStream conversion
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Source xmlSource = new DOMSource(doc);
    Result outputTarget = new StreamResult(outputStream);
    TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
    InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
    // schema validation
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = schemaFactory.newSchema(schemas.toArray(new Source[schemas.size()]));
    Validator validator = schema.newValidator();
    try {
        SAXSource source = new SAXSource(new InputSource(is));
        validator.validate(source);
    } catch (Exception e) {
        throw new InvalidConfigurationException(e.getMessage());
    } finally {
        for (StreamSource source : schemas) {
            closeResource(source.getInputStream());
        }
        closeResource(inputStream);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) ParseException(java.text.ParseException) NoSuchElementException(java.util.NoSuchElementException) HazelcastException(com.hazelcast.core.HazelcastException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) SAXSource(javax.xml.transform.sax.SAXSource) ByteArrayInputStream(java.io.ByteArrayInputStream) Validator(javax.xml.validation.Validator)

Example 49 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project hibernate-orm by hibernate.

the class LocalSchemaLocator method resolveLocalSchema.

public static Schema resolveLocalSchema(URL schemaUrl) {
    try {
        InputStream schemaStream = schemaUrl.openStream();
        try {
            StreamSource source = new StreamSource(schemaUrl.openStream());
            SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            return schemaFactory.newSchema(source);
        } catch (Exception e) {
            throw new XmlInfrastructureException("Unable to load schema [" + schemaUrl.toExternalForm() + "]", e);
        } finally {
            try {
                schemaStream.close();
            } catch (IOException e) {
                log.debugf("Problem closing schema stream - %s", e.toString());
            }
        }
    } catch (IOException e) {
        throw new XmlInfrastructureException("Stream error handling schema url [" + schemaUrl.toExternalForm() + "]");
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) IOException(java.io.IOException) IOException(java.io.IOException)

Example 50 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project hibernate-orm by hibernate.

the class PersistenceXmlParser method resolveLocalSchema.

private Schema resolveLocalSchema(String schemaName) {
    // These XSD resources should be available on the Hibernate ClassLoader
    final URL url = classLoaderService.locateResource(schemaName);
    if (url == null) {
        throw new XsdException("Unable to locate schema [" + schemaName + "] via classpath", schemaName);
    }
    try {
        InputStream schemaStream = url.openStream();
        try {
            StreamSource source = new StreamSource(url.openStream());
            SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            return schemaFactory.newSchema(source);
        } catch (SAXException e) {
            throw new XsdException("Unable to load schema [" + schemaName + "]", e, schemaName);
        } catch (IOException e) {
            throw new XsdException("Unable to load schema [" + schemaName + "]", e, schemaName);
        } finally {
            try {
                schemaStream.close();
            } catch (IOException e) {
                LOG.debugf("Problem closing schema stream [%s]", e.toString());
            }
        }
    } catch (IOException e) {
        throw new XsdException("Stream error handling schema url [" + url.toExternalForm() + "]", schemaName);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) XsdException(org.hibernate.internal.util.xml.XsdException) IOException(java.io.IOException) URL(java.net.URL) 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