Search in sources :

Example 51 with Schema

use of javax.xml.validation.Schema in project tomee by apache.

the class JaxbJavaee method validateJavaee.

/**
     * validate the inputStream, which should be a Java EE standard deployment descriptor against its schema type
     * Note, this method will use the new Java EE 6 schema to validate the old descriptors after changing their namespace and version.
     *
     * @param type
     * @param in
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     */
public static void validateJavaee(final JavaeeSchema type, final InputStream in) throws ParserConfigurationException, SAXException, IOException {
    final URL javaeeSchemaURL = resolveJavaeeSchemaURL(type);
    if (javaeeSchemaURL == null) {
        throw new IllegalArgumentException("Can not find the xsd file against type:" + type);
    }
    final URL xmlSchemaURL = JaxbJavaee.getSchemaURL("xml.xsd");
    if (xmlSchemaURL == null) {
        throw new IllegalArgumentException("Can not find the xml.xsd file");
    }
    // get the parser
    final SAXParserFactory parserfactory = SAXParserFactory.newInstance();
    parserfactory.setNamespaceAware(true);
    parserfactory.setValidating(false);
    final SAXParser parser = parserfactory.newSAXParser();
    // get the xml filter
    final Javaee6SchemaFilter xmlFilter = new Javaee6SchemaFilter(parser.getXMLReader());
    // get the source
    final SAXSource sourceForValidate = new SAXSource(xmlFilter, new InputSource(in));
    // get the schema
    final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    final JaxbJavaeeSchemaResourceResolver resourceResolver = new JaxbJavaeeSchemaResourceResolver();
    schemaFactory.setResourceResolver(resourceResolver);
    final Schema schema = schemaFactory.newSchema(new Source[] { new StreamSource(xmlSchemaURL.openStream()), new StreamSource(javaeeSchemaURL.openStream()) });
    // validate
    schema.newValidator().validate(sourceForValidate);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) SAXParser(javax.xml.parsers.SAXParser) URL(java.net.URL) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 52 with Schema

use of javax.xml.validation.Schema in project jqa-core-framework by buschmais.

the class XmlReportTest method readReport.

private JqassistantReport readReport(String xmlReport) throws SAXException, JAXBException {
    SchemaFactory xsdFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = xsdFactory.newSchema(new StreamSource(XmlReportTest.class.getResourceAsStream("/META-INF/xsd/jqassistant-report-1.3.xsd")));
    JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
    StreamSource streamSource = new StreamSource(new StringReader(xmlReport));
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    unmarshaller.setSchema(schema);
    return unmarshaller.unmarshal(streamSource, JqassistantReport.class).getValue();
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 53 with Schema

use of javax.xml.validation.Schema 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 54 with Schema

use of javax.xml.validation.Schema 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 55 with Schema

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

Aggregations

Schema (javax.xml.validation.Schema)102 SchemaFactory (javax.xml.validation.SchemaFactory)72 Validator (javax.xml.validation.Validator)51 StreamSource (javax.xml.transform.stream.StreamSource)45 SAXException (org.xml.sax.SAXException)35 Source (javax.xml.transform.Source)29 DOMSource (javax.xml.transform.dom.DOMSource)25 IOException (java.io.IOException)22 JAXBContext (javax.xml.bind.JAXBContext)18 Document (org.w3c.dom.Document)18 InputStream (java.io.InputStream)17 File (java.io.File)15 URL (java.net.URL)15 DocumentBuilder (javax.xml.parsers.DocumentBuilder)14 JAXBException (javax.xml.bind.JAXBException)13 Unmarshaller (javax.xml.bind.Unmarshaller)13 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)13 InputSource (org.xml.sax.InputSource)13 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)12 SAXParseException (org.xml.sax.SAXParseException)9