| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.XPathContext; |
| 11 |
import net.sf.saxon.lib.ResourceCollection; |
| 12 |
import net.sf.saxon.om.Item; |
| 13 |
import net.sf.saxon.om.LazySequence; |
| 14 |
import net.sf.saxon.om.Sequence; |
| 15 |
import net.sf.saxon.om.SequenceIterator; |
| 16 |
import net.sf.saxon.trans.UncheckedXPathException; |
| 17 |
import net.sf.saxon.trans.XPathException; |
| 18 |
import net.sf.saxon.value.AnyURIValue; |
| 19 |
|
| 20 |
import java.io.Closeable; |
| 21 |
import java.io.IOException; |
| 22 |
import java.net.URI; |
| 23 |
import java.net.URISyntaxException; |
| 24 |
import java.util.Iterator; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public class UriCollection extends SystemFunction { |
| 33 |
|
| 34 |
private SequenceIterator<AnyURIValue> getUris(final String href, final XPathContext context) throws XPathException { |
| 35 |
ResourceCollection rCollection = context.getConfiguration().getCollectionFinder().findCollection(context, href); |
| 36 |
if (rCollection == null) { |
| 37 |
|
| 38 |
XPathException err = new XPathException("No collection has been defined for href: " + (href == null ? "" : href)); |
| 39 |
err.setErrorCode("FODC0002"); |
| 40 |
err.setXPathContext(context); |
| 41 |
throw err; |
| 42 |
|
| 43 |
} |
| 44 |
final Iterator<String> sources = rCollection.getResourceURIs(context); |
| 45 |
return new SequenceIterator<AnyURIValue>() { |
| 46 |
|
| 47 |
public AnyURIValue next() { |
| 48 |
if (sources.hasNext()) { |
| 49 |
return new AnyURIValue(sources.next()); |
| 50 |
} else { |
| 51 |
return null; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
public void close() { |
| 56 |
if (sources instanceof Closeable) { |
| 57 |
try { |
| 58 |
((Closeable) sources).close(); |
| 59 |
} catch (IOException e) { |
| 60 |
throw new UncheckedXPathException(new XPathException(e)); |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
}; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public Sequence<AnyURIValue> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 78 |
if (arguments.length == 0) { |
| 79 |
return getDefaultUriCollection(context); |
| 80 |
} else { |
| 81 |
Item arg = arguments[0].head(); |
| 82 |
if (arg == null) { |
| 83 |
return getDefaultUriCollection(context); |
| 84 |
} |
| 85 |
String href = arg.getStringValue(); |
| 86 |
URI hrefURI; |
| 87 |
try { |
| 88 |
hrefURI = new URI(href); |
| 89 |
} catch (URISyntaxException e) { |
| 90 |
throw new XPathException("Invalid URI passed to uri-collection: " + href, "FODC0004"); |
| 91 |
} |
| 92 |
if (!hrefURI.isAbsolute()) { |
| 93 |
URI staticBaseUri = getRetainedStaticContext().getStaticBaseUri(); |
| 94 |
if (staticBaseUri == null) { |
| 95 |
throw new XPathException("No base URI available for uri-collection", "FODC0002"); |
| 96 |
} |
| 97 |
hrefURI = staticBaseUri.resolve(hrefURI); |
| 98 |
} |
| 99 |
return new LazySequence(getUris(hrefURI.toString(), context)); |
| 100 |
} |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
private Sequence<AnyURIValue> getDefaultUriCollection(XPathContext context) throws XPathException { |
| 105 |
String href = context.getConfiguration().getDefaultCollection(); |
| 106 |
if (href == null) { |
| 107 |
throw new XPathException("No default collection has been defined", "FODC0002"); |
| 108 |
} else { |
| 109 |
return new LazySequence<>(getUris(href, context)); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
} |
| 114 |
|