Search in sources :

Example 41 with Validator

use of javax.xml.validation.Validator 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 42 with Validator

use of javax.xml.validation.Validator 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 43 with Validator

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

the class PersistenceXmlParser method validate.

private void validate(Document document) {
    // todo : add ability to disable validation...
    final Validator validator;
    final String version = document.getDocumentElement().getAttribute("version");
    if ("2.1".equals(version)) {
        validator = v21Schema().newValidator();
    } else if ("2.0".equals(version)) {
        validator = v2Schema().newValidator();
    } else if ("1.0".equals(version)) {
        validator = v1Schema().newValidator();
    } else {
        throw new PersistenceException("Unrecognized persistence.xml version [" + version + "]");
    }
    List<SAXException> errors = new ArrayList<SAXException>();
    validator.setErrorHandler(new ErrorHandlerImpl(errors));
    try {
        validator.validate(new DOMSource(document));
    } catch (SAXException e) {
        errors.add(e);
    } catch (IOException e) {
        throw new PersistenceException("Unable to validate persistence.xml", e);
    }
    if (errors.size() != 0) {
        //report all errors in the exception
        StringBuilder errorMessage = new StringBuilder();
        for (SAXException error : errors) {
            errorMessage.append(extractInfo(error)).append('\n');
        }
        throw new PersistenceException("Invalid persistence.xml.\n" + errorMessage.toString());
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) PersistenceException(javax.persistence.PersistenceException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Validator(javax.xml.validation.Validator) SAXException(org.xml.sax.SAXException)

Example 44 with Validator

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

the class XmlClientConfigBuilderTest method testXSDConfigXML.

private void testXSDConfigXML(String xmlFileName) throws SAXException, IOException {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    URL schemaResource = XMLConfigBuilderTest.class.getClassLoader().getResource("hazelcast-client-config-3.9.xsd");
    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) XMLConfigBuilderTest(com.hazelcast.config.XMLConfigBuilderTest) 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 45 with Validator

use of javax.xml.validation.Validator in project languagetool by languagetool-org.

the class XMLValidator method validateInternal.

private void validateInternal(Source xmlSrc, URL xmlSchema) throws SAXException, IOException {
    Validator validator = getValidator(xmlSchema);
    validator.validate(xmlSrc);
}
Also used : Validator(javax.xml.validation.Validator)

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