Debian Codegraph @

net/sf/saxon/functions/UriCollection.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
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 * Implement the fn:uri-collection() function (new in XQuery 3.0/XSLT 3.0). This is responsible for calling the
28 * registered {@link net.sf.saxon.lib.CollectionURIResolver}. For the effect of the default
29 * system-supplied CollectionURIResolver, see {@link net.sf.saxon.lib.StandardCollectionURIResolver}
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 // Should not happen, we're calling user code so we check for it.
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 * Evaluate the expression
70 *
71 * @param context the dynamic evaluation context
72 * @param arguments the values of the arguments, supplied as SequenceIterators
73 * @return the result of the evaluation, in the form of a SequenceIterator
74 * @throws net.sf.saxon.trans.XPathException
75 * if a dynamic error occurs during the evaluation of the expression
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