Search in sources :

Example 1 with LSInputImpl

use of org.apache.cxf.common.xmlschema.LSInputImpl in project cxf by apache.

the class SchemaHandler method createSchema.

public static Schema createSchema(List<String> locations, String catalogLocation, final Bus bus) {
    SchemaFactory factory = SchemaFactory.newInstance(Constants.URI_2001_SCHEMA_XSD);
    try {
        List<Source> sources = new ArrayList<>();
        for (String loc : locations) {
            final List<URL> schemaURLs;
            if (loc.lastIndexOf('.') == -1 || loc.lastIndexOf('*') != -1) {
                schemaURLs = ClasspathScanner.findResources(loc, "xsd");
            } else {
                URL url = ResourceUtils.getResourceURL(loc, bus);
                schemaURLs = url != null ? Collections.singletonList(url) : Collections.emptyList();
            }
            if (schemaURLs.isEmpty()) {
                throw new IllegalArgumentException("Cannot find XML schema location: " + loc);
            }
            for (URL schemaURL : schemaURLs) {
                Reader r = new BufferedReader(new InputStreamReader(schemaURL.openStream(), StandardCharsets.UTF_8));
                StreamSource source = new StreamSource(r);
                source.setSystemId(schemaURL.toString());
                sources.add(source);
            }
        }
        if (sources.isEmpty()) {
            return null;
        }
        final OASISCatalogManager catalogResolver = OASISCatalogManager.getCatalogManager(bus);
        if (catalogResolver != null) {
            catalogLocation = catalogLocation == null ? SchemaHandler.DEFAULT_CATALOG_LOCATION : catalogLocation;
            URL catalogURL = ResourceUtils.getResourceURL(catalogLocation, bus);
            if (catalogURL != null) {
                try {
                    catalogResolver.loadCatalog(catalogURL);
                    factory.setResourceResolver(new LSResourceResolver() {

                        public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
                            try {
                                String resolvedLocation = catalogResolver.resolveSystem(systemId);
                                if (resolvedLocation == null) {
                                    resolvedLocation = catalogResolver.resolveURI(namespaceURI);
                                }
                                if (resolvedLocation == null) {
                                    resolvedLocation = catalogResolver.resolvePublic(publicId != null ? publicId : namespaceURI, systemId);
                                }
                                if (resolvedLocation != null) {
                                    InputStream resourceStream = ResourceUtils.getResourceStream(resolvedLocation, bus);
                                    if (resourceStream != null) {
                                        return new LSInputImpl(publicId, systemId, resourceStream);
                                    }
                                }
                            } catch (Exception ex) {
                            // ignore
                            }
                            return null;
                        }
                    });
                } catch (IOException ex) {
                    throw new IllegalArgumentException("Catalog " + catalogLocation + " can not be loaded", ex);
                }
            }
        }
        return factory.newSchema(sources.toArray(new Source[0]));
    } catch (Exception ex) {
        throw new IllegalArgumentException("Failed to load XML schema : " + ex.getMessage(), ex);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) LSInputImpl(org.apache.cxf.common.xmlschema.LSInputImpl) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) ArrayList(java.util.ArrayList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) URL(java.net.URL) IOException(java.io.IOException) LSResourceResolver(org.w3c.dom.ls.LSResourceResolver) LSInput(org.w3c.dom.ls.LSInput) BufferedReader(java.io.BufferedReader) OASISCatalogManager(org.apache.cxf.catalog.OASISCatalogManager)

Example 2 with LSInputImpl

use of org.apache.cxf.common.xmlschema.LSInputImpl in project cxf by apache.

the class SchemaResourceResolver method resolveResource.

public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
    Message msg = new Message("RESOLVE_SCHEMA", LOG, namespaceURI, systemId, baseURI);
    LOG.log(Level.FINE, msg.toString());
    if (NSFILEMAP.containsKey(namespaceURI)) {
        return loadLSInput(namespaceURI);
    }
    LSInput lsin = null;
    String resURL = null;
    if (systemId != null) {
        String schemaLocation = "";
        if (baseURI != null) {
            schemaLocation = baseURI.substring(0, baseURI.lastIndexOf('/') + 1);
        }
        if (systemId.indexOf("http://") < 0) {
            resURL = schemaLocation + systemId;
        } else {
            resURL = systemId;
        }
    } else if (namespaceURI != null) {
        resURL = namespaceURI;
    }
    if (resURL == null) {
        return null;
    }
    String localFile = resURL;
    if (resURL.startsWith("http://")) {
        String filename = NSFILEMAP.get(resURL);
        if (filename != null) {
            localFile = ToolConstants.CXF_SCHEMAS_DIR_INJAR + filename;
        }
    }
    try {
        msg = new Message("RESOLVE_FROM_LOCAL", LOG, localFile);
        LOG.log(Level.FINE, msg.toString());
        @SuppressWarnings("resource") final URIResolver resolver = new URIResolver(localFile);
        if (resolver.isResolved()) {
            lsin = new LSInputImpl();
            lsin.setSystemId(localFile);
            lsin.setByteStream(resolver.getInputStream());
        }
    } catch (IOException e) {
        return null;
    }
    return lsin;
}
Also used : LSInputImpl(org.apache.cxf.common.xmlschema.LSInputImpl) Message(org.apache.cxf.common.i18n.Message) LSInput(org.w3c.dom.ls.LSInput) URIResolver(org.apache.cxf.resource.URIResolver) IOException(java.io.IOException)

Example 3 with LSInputImpl

use of org.apache.cxf.common.xmlschema.LSInputImpl in project cxf by apache.

the class SchemaResourceResolver method loadLSInput.

private LSInput loadLSInput(String ns) {
    String path = ToolConstants.CXF_SCHEMAS_DIR_INJAR + NSFILEMAP.get(ns);
    URL url = getClass().getClassLoader().getResource(path);
    LSInput lsin = new LSInputImpl();
    lsin.setSystemId(url.toString());
    try {
        lsin.setByteStream(url.openStream());
    } catch (IOException e) {
        return null;
    }
    return lsin;
}
Also used : LSInputImpl(org.apache.cxf.common.xmlschema.LSInputImpl) LSInput(org.w3c.dom.ls.LSInput) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

IOException (java.io.IOException)3 LSInputImpl (org.apache.cxf.common.xmlschema.LSInputImpl)3 LSInput (org.w3c.dom.ls.LSInput)3 URL (java.net.URL)2 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 ArrayList (java.util.ArrayList)1 Source (javax.xml.transform.Source)1 StreamSource (javax.xml.transform.stream.StreamSource)1 SchemaFactory (javax.xml.validation.SchemaFactory)1 OASISCatalogManager (org.apache.cxf.catalog.OASISCatalogManager)1 Message (org.apache.cxf.common.i18n.Message)1 URIResolver (org.apache.cxf.resource.URIResolver)1 LSResourceResolver (org.w3c.dom.ls.LSResourceResolver)1