public static final QName XSD = new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "schema");
private Collection<? extends Source> aggregate(URL... urls) throws IOException, XMLStreamException {
if (urls.length == 1) {
return Collections.singletonList(new SAXSource(XMLDocumentHelper.getInputSource(urls[0])));
}
Map<String, Collection<URL>> map = new HashMap<String, Collection<URL>>();
for (URL url : urls) {
String tns = helper.readAttribute(url, XSD, "targetNamespace");
Collection<URL> collection = map.get(tns);
if (collection == null) {
collection = new HashSet<URL>();
map.put(tns, collection);
}
collection.add(url);
}
List<Source> sources = new ArrayList<Source>();
for (Map.Entry<String, Collection<URL>> e : map.entrySet()) {
if (e.getValue().size() == 1) {
sources.add(new SAXSource(XMLDocumentHelper.getInputSource(e.getValue().iterator().next())));
} else {
StringBuffer xsd = new StringBuffer("<schema xmlns=\"http://www.w3.org/2001/XMLSchema\"");
if (e.getKey() != null) {
xsd.append(" targetNamespace=\"").append(e.getKey()).append("\"");
}
xsd.append(">");
for (URL url : e.getValue()) {
xsd.append("<include schemaLocation=\"").append(url).append("\"/>");
}
xsd.append("</schema>");
SAXSource source = new SAXSource(new InputSource(new StringReader(xsd.toString())));
sources.add(source);
}
}
return sources;
}