Search in sources :

Example 86 with SchemaFactory

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

the class XmlCompactFileAppenderValidationTest 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)

Example 87 with SchemaFactory

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

the class JaxbUtil method getSchema.

private static Schema getSchema(String namespace) throws SAXException {
    Schema schema = SCHEMAS.get(namespace);
    if (schema == null) {
        String schemaLocation;
        switch(namespace) {
            case FeaturesNamespaces.URI_1_0_0:
                schemaLocation = "/org/apache/karaf/features/karaf-features-1.0.0.xsd";
                break;
            case FeaturesNamespaces.URI_1_1_0:
                schemaLocation = "/org/apache/karaf/features/karaf-features-1.1.0.xsd";
                break;
            case FeaturesNamespaces.URI_1_2_0:
                schemaLocation = "/org/apache/karaf/features/karaf-features-1.2.0.xsd";
                break;
            case FeaturesNamespaces.URI_1_2_1:
                schemaLocation = "/org/apache/karaf/features/karaf-features-1.2.1.xsd";
                break;
            case FeaturesNamespaces.URI_1_3_0:
                schemaLocation = "/org/apache/karaf/features/karaf-features-1.3.0.xsd";
                break;
            case FeaturesNamespaces.URI_1_4_0:
                schemaLocation = "/org/apache/karaf/features/karaf-features-1.4.0.xsd";
                break;
            case FeaturesNamespaces.URI_1_5_0:
                schemaLocation = "/org/apache/karaf/features/karaf-features-1.5.0.xsd";
                break;
            default:
                throw new IllegalArgumentException("Unsupported namespace: " + namespace);
        }
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        // root element has namespace - we can use schema validation
        URL url = JaxbUtil.class.getResource(schemaLocation);
        if (url == null) {
            throw new IllegalStateException("Could not find resource: " + schemaLocation);
        }
        schema = factory.newSchema(new StreamSource(url.toExternalForm()));
        SCHEMAS.put(namespace, schema);
    }
    return schema;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) URL(java.net.URL)

Example 88 with SchemaFactory

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

the class XSSFExportToXml method isValid.

/**
     * Validate the generated XML against the XML Schema associated with the XSSFMap
     *
     * @param xml the XML to validate
     * @return true, if document is valid
     * @throws SAXException If validating the document fails
     */
private boolean isValid(Document xml) throws SAXException {
    try {
        String language = "http://www.w3.org/2001/XMLSchema";
        SchemaFactory factory = SchemaFactory.newInstance(language);
        Source source = new DOMSource(map.getSchema());
        Schema schema = factory.newSchema(source);
        Validator validator = schema.newValidator();
        validator.validate(new DOMSource(xml));
        //if no exceptions where raised, the document is valid
        return true;
    } catch (IOException e) {
        LOG.log(POILogger.ERROR, "document is not valid", e);
    }
    return false;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) Schema(javax.xml.validation.Schema) IOException(java.io.IOException) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) Validator(javax.xml.validation.Validator)

Example 89 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project webservices-axiom by apache.

the class TestValidator method runTest.

protected void runTest() throws Throwable {
    SchemaFactory factory = new XMLSchemaFactory();
    DocumentBuilder builder = dbf.newDocumentBuilder();
    Schema schema = factory.newSchema(new DOMSource(builder.parse(TestValidator.class.getResourceAsStream("ipo.xsd"))));
    Validator validator = schema.newValidator();
    validator.validate(new DOMSource(builder.parse(TestValidator.class.getResourceAsStream("ipo_1.xml"))));
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) XMLSchemaFactory(org.apache.xerces.jaxp.validation.XMLSchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) XMLSchemaFactory(org.apache.xerces.jaxp.validation.XMLSchemaFactory) Schema(javax.xml.validation.Schema) Validator(javax.xml.validation.Validator)

Example 90 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project webservices-axiom by apache.

the class ValidateSample method validateUsingDOM.

// START SNIPPET: dom
public void validateUsingDOM(InputStream in, URL schemaUrl) throws Exception {
    OMMetaFactory mf = OMAbstractFactory.getMetaFactory(OMAbstractFactory.FEATURE_DOM);
    SOAPModelBuilder builder = OMXMLBuilderFactory.createSOAPModelBuilder(mf, in, "UTF-8");
    SOAPEnvelope envelope = builder.getSOAPEnvelope();
    OMElement bodyContent = envelope.getBody().getFirstElement();
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = schemaFactory.newSchema(schemaUrl);
    Validator validator = schema.newValidator();
    validator.validate(new DOMSource((Element) bodyContent));
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) Schema(javax.xml.validation.Schema) OMElement(org.apache.axiom.om.OMElement) Element(org.w3c.dom.Element) OMElement(org.apache.axiom.om.OMElement) SOAPModelBuilder(org.apache.axiom.soap.SOAPModelBuilder) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) OMMetaFactory(org.apache.axiom.om.OMMetaFactory) 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