Search in sources :

Example 56 with SchemaFactory

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

the class XmlUtil method newSAXParser.

/**
     * Factory method to create a SAXParser configured to validate according to a particular schema language and
     * optionally providing the schema sources to validate with.
     *
     * @param schemaLanguage the schema language used, e.g. XML Schema or RelaxNG (as per the String representation in javax.xml.XMLConstants)
     * @param namespaceAware will the parser be namespace aware
     * @param validating     will the parser also validate against DTDs
     * @param schemas        the schemas to validate against
     * @return the created SAXParser
     * @throws SAXException
     * @throws ParserConfigurationException
     * @since 1.8.7
     */
public static SAXParser newSAXParser(String schemaLanguage, boolean namespaceAware, boolean validating, Source... schemas) throws SAXException, ParserConfigurationException {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(validating);
    factory.setNamespaceAware(namespaceAware);
    if (schemas.length != 0) {
        SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage);
        factory.setSchema(schemaFactory.newSchema(schemas));
    }
    SAXParser saxParser = factory.newSAXParser();
    if (schemas.length == 0) {
        saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", schemaLanguage);
    }
    return saxParser;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) SAXParser(javax.xml.parsers.SAXParser) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 57 with SchemaFactory

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

the class SchemaReader method createSchema.

protected Schema createSchema() throws SAXException, IOException {
    SchemaFactory factory = getSchemaFactory();
    URL url = getSchemaUrl();
    if (url != null) {
        synchronized (this) {
            return factory.newSchema(url);
        }
    }
    File file = getSchemaFile();
    if (file != null) {
        synchronized (this) {
            return factory.newSchema(file);
        }
    }
    byte[] bytes = getSchemaAsByteArray();
    if (bytes != null) {
        synchronized (this) {
            return factory.newSchema(new StreamSource(new ByteArrayInputStream(schemaAsByteArray)));
        }
    }
    if (schemaResourceUri != null) {
        synchronized (this) {
            bytes = readSchemaResource();
            return factory.newSchema(new StreamSource(new ByteArrayInputStream(bytes)));
        }
    }
    Source source = getSchemaSource();
    synchronized (this) {
        return factory.newSchema(source);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamSource(javax.xml.transform.stream.StreamSource) File(java.io.File) URL(java.net.URL) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source)

Example 58 with SchemaFactory

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

the class CustomSchemaFactoryFeatureTest method createRegistry.

// Need to bind the CustomerSchemaFactory
protected JndiRegistry createRegistry() throws Exception {
    JndiRegistry registry = super.createRegistry();
    SchemaFactory mySchemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    mySchemaFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, false);
    registry.bind("MySchemaFactory", mySchemaFactory);
    return registry;
}
Also used : JndiRegistry(org.apache.camel.impl.JndiRegistry) SchemaFactory(javax.xml.validation.SchemaFactory)

Example 59 with SchemaFactory

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

the class XML method parseDocumentElement.

/**
     * Example schema locations:
     * http://docs.oasis-open.org/security/saml/v2.0/saml-schema-protocol-2.0.xsd
     * http://docs.oasis-open.org/security/saml/v2.0/saml-schema-assertion-2.0.xsd
     * http://www.w3.org/TR/2002/REC-xmlenc-core-20021210/xenc-schema.xsd
     * http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-schema.xsd
     *
     * @param xml
     * @return
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     */
public Element parseDocumentElement(String xml) throws ParserConfigurationException, SAXException, IOException {
    ClasspathResourceResolver resolver = new ClasspathResourceResolver();
    resolver.setResourcePackage(schemaPackageName);
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    schemaFactory.setResourceResolver(resolver);
    Source[] schemaSources = new Source[schemaLocations.size()];
    int i = 0;
    InputStream inStream = null;
    Schema schema;
    try {
        for (String schemaLocation : schemaLocations) {
            inStream = resolver.findResource(schemaLocation);
            //            if(in == null) log.debug("parseDocumentElement - InputStream is NULL");
            //            else           log.debug("parseDocumentElement - InputStream OK");
            schemaSources[i] = new StreamSource(inStream);
            log.debug("parseDocumentElement - schemaSources[" + i + "] :" + schemaSources[i].getSystemId());
            i++;
        }
        //        if(schemaSources!=null) log.debug("parseDocumentElement - schemaSources.length: " + schemaSources.length); 
        //        else                    log.debug("parseDocumentElement - schemaSources is null");
        schema = schemaFactory.newSchema(schemaSources);
    } catch (Exception e) {
        schema = null;
        log.error(e.getMessage());
    } finally {
        if (inStream != null)
            inStream.close();
    }
    //        Validator validator = schema.newValidator();
    //        validator.validate(new StreamSource(new ByteArrayInputStream(xml.getBytes())));
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    // bug #1038 prevent XXE
    factory.setExpandEntityReferences(false);
    // bug #1038 prevent XXE
    factory.setXIncludeAware(false);
    // fix for javax.xml.crypto.dsig.XMLSignatureException: javax.xml.crypto.URIReferenceException: com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException: Cannot resolve element with ID HostTrustAssertion
    factory.setSchema(schema);
    // ParserConfigurationException
    DocumentBuilder builder = factory.newDocumentBuilder();
    try (ByteArrayInputStream in = new ByteArrayInputStream(xml.getBytes())) {
        // SAXException, IOException
        Element document = builder.parse(in).getDocumentElement();
        return document;
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) Element(org.w3c.dom.Element) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 60 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project honeycomb by altamiracorp.

the class ConfigurationParser method checkValidConfig.

/**
     * Performs validation on the configuration content supplied by the
     * configuration supplier against the schema document provided by the
     * validation supplier.  Throws Runtime exception if validation fails.
     *
     * @param configSupplier The supplier that provides the configuration to inspect, not null
     * @param schemaSupplier The supplier that provides the schema used to inspect the configuration, not null
     */
private static void checkValidConfig(final InputSupplier<? extends InputStream> configSupplier, final InputSupplier<? extends InputStream> schemaSupplier) {
    try {
        final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        final Schema schema = schemaFactory.newSchema(new StreamSource(schemaSupplier.getInput()));
        final Validator validator = schema.newValidator();
        validator.validate(new StreamSource(configSupplier.getInput()));
    } catch (Exception e) {
        logger.error("Unable to validate honeycomb configuration.", e);
        throw new RuntimeException("Exception while validating honeycomb configuration.", e);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) Validator(javax.xml.validation.Validator) XPathExpressionException(javax.xml.xpath.XPathExpressionException)

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