Search in sources :

Example 36 with SchemaFactory

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

the class BpmnXMLConverter method createSchema.

protected Schema createSchema() throws SAXException {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = null;
    if (classloader != null) {
        schema = factory.newSchema(classloader.getResource(BPMN_XSD));
    }
    if (schema == null) {
        schema = factory.newSchema(BpmnXMLConverter.class.getClassLoader().getResource(BPMN_XSD));
    }
    if (schema == null) {
        throw new XMLException("BPMN XSD could not be found");
    }
    return schema;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) XMLException(org.activiti.bpmn.exceptions.XMLException) Schema(javax.xml.validation.Schema)

Example 37 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project ats-framework by Axway.

the class ConfigurationParser method inititalizeParser.

/**
     * Initializer method of the parser. Initializes the document instance.
     * @param inputStream - configuration file input stream to be parsed
     * @param systemId Provide a base for resolving relative URIs.
     * @throws IOException - if the streamed object is not found
     */
private void inititalizeParser(InputStream inputStream, String systemId) throws IOException, SAXException, ParserConfigurationException {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setIgnoringElementContentWhitespace(true);
    documentBuilderFactory.setNamespaceAware(true);
    documentBuilderFactory.setValidating(false);
    documentBuilderFactory.setIgnoringComments(true);
    try {
        // skip DTD validation
        documentBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
        documentBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        mDocument = documentBuilder.parse(inputStream, systemId);
        /* NOTE:
             * XSD Validation process is performed after the XML parsing (not during), 
             * because when we do it during the parsing, the XML Document element adds attributes which has a default values in the XSD.
             * In our case for example, it adds lock="true" for all 'table' elements and when the database is oracle
             * we log WARN messages. It's wrong. That's why we do the validation after parsing the XML.
             */
        ConfigurationParser.class.getClassLoader().getResource("agent_descriptor.xsd");
        // XSD Validation
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = schemaFactory.newSchema(this.getClass().getClassLoader().getResource("agent_descriptor.xsd"));
        Validator validator = schema.newValidator();
        validator.validate(new DOMSource(mDocument));
    } catch (ParserConfigurationException pce) {
        log.error(pce.getMessage());
        throw pce;
    } catch (IOException ioe) {
        log.error(ioe.getMessage());
        throw ioe;
    } catch (SAXException saxe) {
        log.error(saxe.getMessage());
        throw saxe;
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Schema(javax.xml.validation.Schema) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Validator(javax.xml.validation.Validator) SAXException(org.xml.sax.SAXException)

Example 38 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project claw-compiler by C2SM-RCM.

the class Configuration method validate.

/**
   * Validate the configuration file with the XSD schema.
   *
   * @param xsdPath Path to the XSD schema.
   * @throws Exception If configuration file is not valid.
   */
private void validate(String xsdPath) throws Exception {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Source schemaFile = new StreamSource(new File(xsdPath));
    Schema schema = factory.newSchema(schemaFile);
    Validator validator = schema.newValidator();
    validator.validate(new DOMSource(_document));
}
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 39 with SchemaFactory

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

the class SimpleNamespaceHandlerSet method getSchema.

public Schema getSchema() throws SAXException, IOException {
    if (schema == null) {
        final List<StreamSource> schemaSources = new ArrayList<StreamSource>();
        final List<InputStream> streams = new ArrayList<InputStream>();
        try {
            InputStream is = getClass().getResourceAsStream("/org/apache/aries/blueprint/blueprint.xsd");
            streams.add(is);
            schemaSources.add(new StreamSource(is));
            for (URI uri : namespaces.keySet()) {
                is = namespaces.get(uri).openStream();
                streams.add(is);
                schemaSources.add(new StreamSource(is));
            }
            SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            schemaFactory.setResourceResolver(new LSResourceResolver() {

                public LSInput resolveResource(String type, String namespace, String publicId, String systemId, String baseURI) {
                    try {
                        URL namespaceURL = namespaces.get(URI.create(namespace));
                        if (systemId != null && namespaceURL != null) {
                            URI systemIdUri = namespaceURL.toURI();
                            if (!URI.create(systemId).isAbsolute()) {
                                systemIdUri = systemIdUri.resolve(systemId);
                            }
                            if (!systemIdUri.isAbsolute() && "jar".equals(namespaceURL.getProtocol())) {
                                String urlString = namespaceURL.toString();
                                int jarFragmentIndex = urlString.lastIndexOf('!');
                                if (jarFragmentIndex > 0 && jarFragmentIndex < urlString.length() - 1) {
                                    String jarUrlOnly = urlString.substring(0, jarFragmentIndex);
                                    String oldFragment = urlString.substring(jarFragmentIndex + 1);
                                    String newFragment = URI.create(oldFragment).resolve(systemId).toString();
                                    String newJarUri = jarUrlOnly + '!' + newFragment;
                                    systemIdUri = URI.create(newJarUri);
                                }
                            }
                            InputStream resourceStream = systemIdUri.toURL().openStream();
                            return new LSInputImpl(publicId, systemId, resourceStream);
                        }
                    } catch (Exception ex) {
                    // ignore
                    }
                    return null;
                }
            });
            schema = schemaFactory.newSchema(schemaSources.toArray(new Source[schemaSources.size()]));
        } finally {
            for (InputStream is : streams) {
                is.close();
            }
        }
    }
    return schema;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) ArrayList(java.util.ArrayList) URI(java.net.URI) URL(java.net.URL) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) LSResourceResolver(org.w3c.dom.ls.LSResourceResolver) LSInput(org.w3c.dom.ls.LSInput)

Example 40 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project logging-log4j2 by apache.

the class XmlCompactFileAsyncAppenderValidationTest method validateXmlSchema.

private void validateXmlSchema(final File file) throws SAXException, IOException {
    final URL schemaFile = this.getClass().getClassLoader().getResource("Log4j-events.xsd");
    final Source xmlFile = new StreamSource(file);
    final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    final Schema schema = schemaFactory.newSchema(schemaFile);
    final Validator validator = schema.newValidator();
    validator.validate(xmlFile);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) URL(java.net.URL) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Validator(javax.xml.validation.Validator)

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