| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.resource; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.expr.XPathContext; |
| 12 |
import net.sf.saxon.lib.CollectionFinder; |
| 13 |
import net.sf.saxon.lib.CollectionURIResolver; |
| 14 |
import net.sf.saxon.lib.ParseOptions; |
| 15 |
import net.sf.saxon.lib.ResourceCollection; |
| 16 |
import net.sf.saxon.om.*; |
| 17 |
import net.sf.saxon.trans.XPathException; |
| 18 |
import net.sf.saxon.value.AnyURIValue; |
| 19 |
|
| 20 |
import javax.xml.transform.Source; |
| 21 |
import javax.xml.transform.TransformerException; |
| 22 |
import javax.xml.transform.URIResolver; |
| 23 |
import java.util.ArrayList; |
| 24 |
import java.util.Iterator; |
| 25 |
import java.util.List; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class CollectionURIResolverWrapper implements CollectionFinder { |
| 34 |
|
| 35 |
private CollectionURIResolver collectionURIResolver = null; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public CollectionURIResolverWrapper(CollectionURIResolver cr) { |
| 43 |
collectionURIResolver = cr; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
public ResourceCollection findCollection(final XPathContext context, final String collectionURI) throws XPathException { |
| 48 |
return new ResourceCollection() { |
| 49 |
private SpaceStrippingRule whitespaceRules; |
| 50 |
|
| 51 |
public String getCollectionURI() { |
| 52 |
return collectionURI; |
| 53 |
} |
| 54 |
|
| 55 |
public Iterator<String> getResourceURIs(XPathContext context) throws XPathException { |
| 56 |
List<String> uris = new ArrayList<String>(); |
| 57 |
SequenceIterator<?> iter = collectionURIResolver.resolve(collectionURI, "", context); |
| 58 |
iter.forEachOrFail(item -> { |
| 59 |
if (item instanceof AnyURIValue) { |
| 60 |
uris.add(item.getStringValue()); |
| 61 |
} else if (item instanceof NodeInfo) { |
| 62 |
uris.add(((NodeInfo) item).getSystemId()); |
| 63 |
} else { |
| 64 |
throw new XPathException("Result of CollectionURIResolver must consist of xs:anyURI values and/or nodes"); |
| 65 |
} |
| 66 |
}); |
| 67 |
return uris.iterator(); |
| 68 |
} |
| 69 |
|
| 70 |
public Iterator<XmlResource> getResources(XPathContext context) throws XPathException { |
| 71 |
List<XmlResource> resources = new ArrayList<XmlResource>(); |
| 72 |
SequenceIterator<?> iter = collectionURIResolver.resolve(collectionURI, "", context); |
| 73 |
Configuration config = context.getConfiguration(); |
| 74 |
URIResolver uriResolver = config.getURIResolver(); |
| 75 |
ParseOptions options = new ParseOptions(config.getParseOptions()); |
| 76 |
options.setSpaceStrippingRule(whitespaceRules); |
| 77 |
iter.forEachOrFail(item -> { |
| 78 |
if (item instanceof AnyURIValue) { |
| 79 |
Source source; |
| 80 |
try { |
| 81 |
source = uriResolver.resolve(item.getStringValue(), ""); |
| 82 |
} catch (TransformerException e) { |
| 83 |
throw XPathException.makeXPathException(e); |
| 84 |
} |
| 85 |
TreeInfo treeInfo = config.buildDocumentTree(source, options); |
| 86 |
resources.add(new XmlResource(treeInfo.getRootNode())); |
| 87 |
} else if (item instanceof NodeInfo) { |
| 88 |
resources.add(new XmlResource((NodeInfo) item)); |
| 89 |
} else { |
| 90 |
throw new XPathException("Result of CollectionURIResolver must consist of xs:anyURI values and/or nodes"); |
| 91 |
} |
| 92 |
}); |
| 93 |
return resources.iterator(); |
| 94 |
} |
| 95 |
|
| 96 |
public boolean isStable(XPathContext context) { |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
public boolean stripWhitespace(SpaceStrippingRule rules) { |
| 101 |
whitespaceRules = rules; |
| 102 |
return true; |
| 103 |
} |
| 104 |
}; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
public CollectionURIResolver getCollectionURIResolver() { |
| 109 |
return collectionURIResolver; |
| 110 |
} |
| 111 |
} |