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);
}
}
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;
}
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;
}
Aggregations