private static Schema createSchema(ServiceInfo serviceInfo) {
Schema schema = serviceInfo.getProperty(Schema.class.getName(), Schema.class);
if (schema == null) {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Map<String, byte[]> schemaSourcesMap = new LinkedHashMap<String, byte[]>();
Map<String, Source> schemaSourcesMap2 = new LinkedHashMap<String, Source>();
try {
for (SchemaInfo si : serviceInfo.getSchemas()) {
Element el = si.getElement();
String baseURI = null;
try {
baseURI = el.getBaseURI();
} catch (Exception ex) {
//ignore - not DOM level 3
}
if (baseURI == null) {
baseURI = si.getSystemId();
}
DOMSource ds = new DOMSource(el, baseURI);
schemaSourcesMap2.put(si.getSystemId() + ":" + si.getNamespaceURI(), ds);
LoadingByteArrayOutputStream out = new LoadingByteArrayOutputStream();
StaxUtils.copy(el, StaxUtils.createXMLStreamWriter(out));
schemaSourcesMap.put(si.getSystemId() + ":" + si.getNamespaceURI(), out.toByteArray());
}
for (XmlSchema sch : serviceInfo.getXmlSchemaCollection().getXmlSchemas()) {
if (sch.getSourceURI() != null
&& !schemaSourcesMap.containsKey(sch.getSourceURI() + ":"
+ sch.getTargetNamespace())) {
InputStream ins = null;
try {
URL url = new URL(sch.getSourceURI());
ins = url.openStream();
} catch (Exception e) {
//ignore, we'll just use what we have. (though
//bugs in XmlSchema could make this less useful)
}
LoadingByteArrayOutputStream out = new LoadingByteArrayOutputStream();
if (ins == null) {
sch.write(out);
} else {
IOUtils.copyAndCloseInput(ins, out);
}
schemaSourcesMap.put(sch.getSourceURI() + ":"
+ sch.getTargetNamespace(), out.toByteArray());
Source source = new StreamSource(out.createInputStream(), sch.getSourceURI());
schemaSourcesMap2.put(sch.getSourceURI() + ":"
+ sch.getTargetNamespace(), source);
}
}
factory.setResourceResolver(new SchemaLSResourceResolver(schemaSourcesMap));
schema = factory.newSchema(schemaSourcesMap2.values()
.toArray(new Source[schemaSourcesMap2.size()]));
} catch (Exception ex) {
// Something not right with the schema from the wsdl.