Search in sources :

Example 6 with LSResourceResolver

use of org.w3c.dom.ls.LSResourceResolver in project camel by apache.

the class ValidatorWithResourceResolverRouteTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    // we have to do it here, because we need the context created first
    CatalogManager.getStaticManager().setIgnoreMissingProperties(true);
    CatalogResolver catalogResolver = new CatalogResolver(true);
    URL catalogUrl = ResourceHelper.resolveMandatoryResourceAsUrl(context.getClassResolver(), "org/apache/camel/component/validator/catalog.cat");
    catalogResolver.getCatalog().parseCatalog(catalogUrl);
    LSResourceResolver resourceResolver = new CatalogLSResourceResolver(catalogResolver);
    JndiRegistry registry = (JndiRegistry) ((PropertyPlaceholderDelegateRegistry) context.getRegistry()).getRegistry();
    registry.bind("resourceResolver", resourceResolver);
    return new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").doTry().to("validator:org/apache/camel/component/validator/report.xsd?resourceResolver=#resourceResolver").to("mock:valid").doCatch(ValidationException.class).to("mock:invalid").doFinally().to("mock:finally").end();
        }
    };
}
Also used : JndiRegistry(org.apache.camel.impl.JndiRegistry) RouteBuilder(org.apache.camel.builder.RouteBuilder) LSResourceResolver(org.w3c.dom.ls.LSResourceResolver) CatalogLSResourceResolver(org.apache.camel.processor.validation.CatalogLSResourceResolver) CatalogLSResourceResolver(org.apache.camel.processor.validation.CatalogLSResourceResolver) CatalogResolver(org.apache.xml.resolver.tools.CatalogResolver) URL(java.net.URL)

Example 7 with LSResourceResolver

use of org.w3c.dom.ls.LSResourceResolver in project santuario-java by apache.

the class XMLSecurityUtils method loadXMLSecuritySchemas.

public static Schema loadXMLSecuritySchemas() throws SAXException {
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    schemaFactory.setResourceResolver(new LSResourceResolver() {

        @Override
        public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
            if ("http://www.w3.org/2001/XMLSchema.dtd".equals(systemId)) {
                ConcreteLSInput concreteLSInput = new ConcreteLSInput();
                concreteLSInput.setByteStream(ClassLoaderUtils.getResourceAsStream("bindings/schemas/XMLSchema.dtd", XMLSecurityConstants.class));
                return concreteLSInput;
            } else if ("XMLSchema.dtd".equals(systemId)) {
                ConcreteLSInput concreteLSInput = new ConcreteLSInput();
                concreteLSInput.setByteStream(ClassLoaderUtils.getResourceAsStream("bindings/schemas/XMLSchema.dtd", XMLSecurityConstants.class));
                return concreteLSInput;
            } else if ("datatypes.dtd".equals(systemId)) {
                ConcreteLSInput concreteLSInput = new ConcreteLSInput();
                concreteLSInput.setByteStream(ClassLoaderUtils.getResourceAsStream("bindings/schemas/datatypes.dtd", XMLSecurityConstants.class));
                return concreteLSInput;
            } else if ("http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-schema.xsd".equals(systemId)) {
                ConcreteLSInput concreteLSInput = new ConcreteLSInput();
                concreteLSInput.setByteStream(ClassLoaderUtils.getResourceAsStream("bindings/schemas/xmldsig-core-schema.xsd", XMLSecurityConstants.class));
                return concreteLSInput;
            } else if ("http://www.w3.org/2001/xml.xsd".equals(systemId)) {
                ConcreteLSInput concreteLSInput = new ConcreteLSInput();
                concreteLSInput.setByteStream(ClassLoaderUtils.getResourceAsStream("bindings/schemas/xml.xsd", XMLSecurityConstants.class));
                return concreteLSInput;
            }
            return null;
        }
    });
    Schema schema = schemaFactory.newSchema(new Source[] { new StreamSource(ClassLoaderUtils.getResourceAsStream("bindings/schemas/exc-c14n.xsd", XMLSecurityConstants.class)), new StreamSource(ClassLoaderUtils.getResourceAsStream("bindings/schemas/xmldsig-core-schema.xsd", XMLSecurityConstants.class)), new StreamSource(ClassLoaderUtils.getResourceAsStream("bindings/schemas/xenc-schema.xsd", XMLSecurityConstants.class)), new StreamSource(ClassLoaderUtils.getResourceAsStream("bindings/schemas/xenc-schema-11.xsd", XMLSecurityConstants.class)), new StreamSource(ClassLoaderUtils.getResourceAsStream("bindings/schemas/xmldsig11-schema.xsd", XMLSecurityConstants.class)) });
    return schema;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) LSResourceResolver(org.w3c.dom.ls.LSResourceResolver) ConcreteLSInput(org.apache.xml.security.stax.impl.util.ConcreteLSInput) LSInput(org.w3c.dom.ls.LSInput) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) ConcreteLSInput(org.apache.xml.security.stax.impl.util.ConcreteLSInput)

Example 8 with LSResourceResolver

use of org.w3c.dom.ls.LSResourceResolver in project cxf by apache.

the class XercesSchemaValidationUtils method tryToParseSchemas.

void tryToParseSchemas(XmlSchemaCollection collection, DOMErrorHandler handler) throws Exception {
    final List<DOMLSInput> inputs = new ArrayList<>();
    final Map<String, LSInput> resolverMap = new HashMap<>();
    for (XmlSchema schema : collection.getXmlSchemas()) {
        if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(schema.getTargetNamespace())) {
            continue;
        }
        Document document = new XmlSchemaSerializer().serializeSchema(schema, false)[0];
        DOMLSInput input = new DOMLSInput(document, schema.getTargetNamespace());
        resolverMap.put(schema.getTargetNamespace(), input);
        inputs.add(input);
    }
    try {
        Object schemaLoader = findMethod(impl, "createXSLoader").invoke(impl, new Object[1]);
        DOMConfiguration config = (DOMConfiguration) findMethod(schemaLoader, "getConfig").invoke(schemaLoader);
        config.setParameter("validate", Boolean.TRUE);
        config.setParameter("error-handler", handler);
        config.setParameter("resource-resolver", new LSResourceResolver() {

            public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
                return resolverMap.get(namespaceURI);
            }
        });
        Method m = findMethod(schemaLoader, "loadInputList");
        String name = m.getParameterTypes()[0].getName() + "Impl";
        name = name.replace("xs.LS", "impl.xs.util.LS");
        Class<?> c = Class.forName(name);
        Object inputList = c.getConstructor(LSInput[].class, Integer.TYPE).newInstance(inputs.toArray(new LSInput[0]), inputs.size());
        m.invoke(schemaLoader, inputList);
    } catch (InvocationTargetException e) {
        throw (Exception) e.getTargetException();
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DOMConfiguration(org.w3c.dom.DOMConfiguration) XmlSchemaSerializer(org.apache.ws.commons.schema.XmlSchemaSerializer) Method(java.lang.reflect.Method) Document(org.w3c.dom.Document) InvocationTargetException(java.lang.reflect.InvocationTargetException) XmlSchema(org.apache.ws.commons.schema.XmlSchema) LSResourceResolver(org.w3c.dom.ls.LSResourceResolver) LSInput(org.w3c.dom.ls.LSInput)

Aggregations

LSResourceResolver (org.w3c.dom.ls.LSResourceResolver)8 LSInput (org.w3c.dom.ls.LSInput)7 SchemaFactory (javax.xml.validation.SchemaFactory)5 ArrayList (java.util.ArrayList)4 StreamSource (javax.xml.transform.stream.StreamSource)4 IOException (java.io.IOException)3 URL (java.net.URL)3 XmlSchema (org.apache.ws.commons.schema.XmlSchema)3 SAXException (org.xml.sax.SAXException)3 InputStream (java.io.InputStream)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 Source (javax.xml.transform.Source)2 Schema (javax.xml.validation.Schema)2 XmlSchemaSerializer (org.apache.ws.commons.schema.XmlSchemaSerializer)2 DOMConfiguration (org.w3c.dom.DOMConfiguration)2 Document (org.w3c.dom.Document)2 LSInputSAXWrapper (com.sun.tools.xjc.reader.xmlschema.parser.LSInputSAXWrapper)1 BufferedReader (java.io.BufferedReader)1