|
10 | 10 | import org.apache.tika.Tika; |
11 | 11 | import org.apache.tika.metadata.Metadata; |
12 | 12 | import org.apache.xerces.impl.xs.XMLSchemaLoader; |
| 13 | +import org.apache.xerces.util.XMLCatalogResolver; |
| 14 | +import org.apache.xerces.xni.XMLResourceIdentifier; |
| 15 | +import org.apache.xerces.xni.XNIException; |
13 | 16 | import org.apache.xerces.xs.StringList; |
14 | 17 | import org.apache.xerces.xs.XSModel; |
15 | 18 | import org.apache.xerces.xs.XSNamespaceItem; |
@@ -250,23 +253,42 @@ public FileInfo getFileFromURL(File targetFolder, String url, String extension, |
250 | 253 | */ |
251 | 254 | private void retrieveSchemasForImports(String rootURI, File rootFolder, HttpClient.Version httpVersion) { |
252 | 255 | XMLSchemaLoader xsdLoader = new XMLSchemaLoader(); |
253 | | - XSModel xsdModel = xsdLoader.loadURI(rootURI); |
254 | | - XSNamespaceItemList xsdNamespaceItemList = xsdModel.getNamespaceItems(); |
255 | 256 | Set<String> documentLocations = new HashSet<>(); |
256 | | - for (int i=0; i<xsdNamespaceItemList.getLength(); i++) { |
257 | | - XSNamespaceItem xsdItem = (XSNamespaceItem) xsdNamespaceItemList.get(i); |
258 | | - StringList sl = xsdItem.getDocumentLocations(); |
259 | | - for(int k=0; k<sl.getLength(); k++) { |
260 | | - if(!documentLocations.contains(sl.item(k))) { |
261 | | - String currentLocation = (String)sl.get(k); |
| 257 | + // Use a custom resolver as this will handle XSDs as well as DTDs. |
| 258 | + xsdLoader.setEntityResolver(new XMLCatalogResolver() { |
| 259 | + @Override |
| 260 | + public String resolveIdentifier(XMLResourceIdentifier resourceIdentifier) throws IOException, XNIException { |
| 261 | + String expandedLocation = resourceIdentifier.getExpandedSystemId(); |
| 262 | + if (expandedLocation != null && !documentLocations.contains(expandedLocation)) { |
262 | 263 | try { |
263 | | - getFileFromURL(rootFolder, currentLocation, httpVersion); |
264 | | - documentLocations.add(currentLocation); |
| 264 | + getFileFromURL(rootFolder, expandedLocation, httpVersion); |
| 265 | + documentLocations.add(expandedLocation); |
265 | 266 | } catch (IOException e) { |
266 | 267 | throw new ValidatorException("validator.label.exception.loadingRemoteSchemas", e); |
267 | 268 | } |
268 | 269 | } |
| 270 | + return super.resolveIdentifier(resourceIdentifier); |
| 271 | + } |
| 272 | + }); |
| 273 | + try { |
| 274 | + // Iterate also over the namespaces XSD imports and includes to ensure we haven't missed anything from the custom resolver. |
| 275 | + XSModel xsdModel = xsdLoader.loadURI(rootURI); |
| 276 | + XSNamespaceItemList xsdNamespaceItemList = xsdModel.getNamespaceItems(); |
| 277 | + for (int i=0; i<xsdNamespaceItemList.getLength(); i++) { |
| 278 | + XSNamespaceItem xsdItem = (XSNamespaceItem) xsdNamespaceItemList.get(i); |
| 279 | + StringList sl = xsdItem.getDocumentLocations(); |
| 280 | + for (int k=0; k<sl.getLength(); k++) { |
| 281 | + if (!documentLocations.contains(sl.item(k))) { |
| 282 | + String currentLocation = (String)sl.get(k); |
| 283 | + getFileFromURL(rootFolder, currentLocation, httpVersion); |
| 284 | + documentLocations.add(currentLocation); |
| 285 | + } |
| 286 | + } |
269 | 287 | } |
| 288 | + } catch (ValidatorException e) { |
| 289 | + throw e; |
| 290 | + } catch (Exception e) { |
| 291 | + throw new ValidatorException("validator.label.exception.loadingRemoteSchemas", e); |
270 | 292 | } |
271 | 293 | } |
272 | 294 |
|
|
0 commit comments