Debian Codegraph @

net/sf/saxon/functions/CollectionFn.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.Configuration;
11 import net.sf.saxon.Controller;
12 import net.sf.saxon.expr.*;
13 import net.sf.saxon.lib.CollectionFinder;
14 import net.sf.saxon.lib.Feature;
15 import net.sf.saxon.lib.Resource;
16 import net.sf.saxon.lib.ResourceCollection;
17 import net.sf.saxon.om.*;
18 import net.sf.saxon.resource.AbstractResourceCollection;
19 import net.sf.saxon.style.StylesheetPackage;
20 import net.sf.saxon.trans.UncheckedXPathException;
21 import net.sf.saxon.trans.XPathException;
22 import net.sf.saxon.tree.wrapper.SpaceStrippedDocument;
23 import net.sf.saxon.type.Type;
24 import net.sf.saxon.value.ExternalObject;
25 import net.sf.saxon.value.ObjectValue;
26
27 import java.io.Closeable;
28 import java.io.IOException;
29 import java.net.URI;
30 import java.net.URISyntaxException;
31 import java.util.ArrayList;
32 import java.util.Iterator;
33
34 /**
35 * Implement the fn:collection() function. This is responsible for calling the
36 * registered {@link CollectionFinder}. For the effect of the default
37 * system-supplied CollectionURIResolver, see {@link net.sf.saxon.resource.StandardCollectionFinder}
38 */
39
40 public class CollectionFn extends SystemFunction implements Callable {
41
42 /**
43 * URI representing a collection that is always empty, regardless of any collection URI resolver
44 */
45 public static String EMPTY_COLLECTION_URI = "http://saxon.sf.net/collection/empty";
46
47 /**
48 * An empty collection
49 */
50
51 public final static ResourceCollection EMPTY_COLLECTION = new EmptyCollection(EMPTY_COLLECTION_URI);
52
53 /**
54 * Implementation of an empty collection
55 */
56
57 private static class EmptyCollection implements ResourceCollection {
58 private String collectionUri;
59
60 public EmptyCollection(String cUri) {
61 collectionUri = cUri;
62 }
63
64 public String getCollectionURI() {
65 return collectionUri;
66 }
67
68 public Iterator<String> getResourceURIs(XPathContext context) {
69 return new ArrayList<String>().iterator();
70 }
71
72 public Iterator<Resource> getResources(XPathContext context) {
73 return new ArrayList<Resource>().iterator();
74 }
75
76 public boolean isStable(XPathContext context) {
77 return true;
78 }
79
80 public boolean stripWhitespace(SpaceStrippingRule rules) {
81 return false;
82 }
83 }
84
85 public int getSpecialProperties(Expression[] arguments) {
86 // See redmine bug 1652. We cannot assume that the nodes will be in document order because we can't assume
87 // they will all be "new" documents. We can't even assume that they will be distinct.
88 return (super.getSpecialProperties(arguments) & ~StaticProperty.NO_NODES_NEWLY_CREATED) | StaticProperty.PEER_NODESET;
89 }
90
91 private String getAbsoluteCollectionURI(String href, XPathContext context) throws XPathException {
92 String absoluteURI;
93 if (href == null) {
94 absoluteURI = context.getConfiguration().getDefaultCollection();
95 } else {
96 URI uri;
97 try {
98 uri = new URI(href);
99 } catch (URISyntaxException e) {
100 href = IriToUri.iriToUri(href).toString();
101 try {
102 uri = new URI(href);
103 } catch (URISyntaxException e2) {
104 throw new XPathException(e2.getMessage(), "FODC0004");
105 }
106 }
107 try {
108 if (uri.isAbsolute()) {
109 absoluteURI = uri.toString();
110 } else {
111 String base = getRetainedStaticContext().getStaticBaseUriString();
112 if (base != null) {
113 absoluteURI = new URI(base).resolve(href).toString();
114 } else {
115 throw new XPathException("Relative collection URI cannot be resolved: no base URI available", "FODC0002");
116 }
117 }
118 } catch (URISyntaxException e) {
119 throw new XPathException(e.getMessage(), "FODC0004");
120 }
121 }
122 return absoluteURI;
123 }
124
125 /**
126 * Get an iterator of the Resources in a ResourceCollection, returned in the form of external objects wrapping
127 * a Resource object
128 * @param collection the resource collection
129 * @param context the XPath dynamic context
130 * @return a SequenceIterator delivering ObjectValue&lt;Resource> items
131 * @throws XPathException if a dynamic error occurs
132 */
133
134 private SequenceIterator<Item<?>> getSequenceIterator(
135 final ResourceCollection collection, final XPathContext context) throws XPathException {
136
137 final Iterator<? extends Resource> sources = collection.getResources(context);
138
139 return new SequenceIterator<Item<?>>() {
140
141 public Item next() throws XPathException {
142 try {
143 if (sources.hasNext()) {
144 return new ObjectValue<Resource>(sources.next());
145 } else {
146 return null;
147 }
148 } catch (Exception e) {
149 throw XPathException.makeXPathException(e);
150 }
151 }
152
153 public void close() {
154 if (sources instanceof Closeable) {
155 try {
156 ((Closeable)sources).close();
157 } catch (IOException e) {
158 throw new UncheckedXPathException(new XPathException(e));
159 }
160 }
161 }
162
163 public int getProperties() {
164 return 0;
165 }
166
167 };
168 }
169
170 /**
171 * Dynamic call on collection() function
172 *
173 * @param context the dynamic evaluation context
174 * @param arguments the values of the arguments, supplied as Sequences.
175 * @return the sequence of nodes forming the collection
176 * @throws XPathException if a dynamic error occurs
177 */
178
179 public Sequence<?> call(final XPathContext context, Sequence[] arguments) throws XPathException {
180 String href;
181 if (getArity() == 0) {
182 // No arguments supplied: this gets the default collection
183 href = context.getController().getDefaultCollection();
184 } else {
185 Item arg = arguments[0].head();
186 if (arg == null) {
187 href = context.getController().getDefaultCollection();
188 } else {
189 href = arg.getStringValue();
190 }
191 }
192
193 if (href == null) {
194 throw new XPathException("No default collection has been defined", "FODC0002");
195 }
196
197 String absoluteURI = getAbsoluteCollectionURI(href, context);
198
199 // See if the collection has been cached
200
201 PackageData packageData = getRetainedStaticContext().getPackageData();
202 SpaceStrippingRule whitespaceRule = NoElementsSpaceStrippingRule.getInstance();
203 String collectionKey = absoluteURI;
204 if (packageData.getHostLanguage() == Configuration.XSLT) {
205 whitespaceRule = ((StylesheetPackage) packageData).getSpaceStrippingRule();
206 if (whitespaceRule != NoElementsSpaceStrippingRule.getInstance()) {
207 collectionKey = ((StylesheetPackage) packageData).getPackageName() +
208 ((StylesheetPackage) packageData).getPackageVersion() +
209 " " +
210 absoluteURI;
211 }
212 }
213
214 GroundedValue<?> cachedCollection = (GroundedValue<?>)context.getController().getUserData("saxon:collections", collectionKey);
215 if (cachedCollection != null) {
216 return cachedCollection;
217 }
218
219 // Call the user-supplied CollectionFinder to get the ResourceCollection
220
221 CollectionFinder collectionFinder = context.getController().getCollectionFinder();
222 ResourceCollection collection = collectionFinder.findCollection(context, absoluteURI);
223 if (collection == null) {
224 collection = new EmptyCollection(EMPTY_COLLECTION_URI);
225 }
226
227 // In XSLT, worry about whitespace stripping
228
229 if (packageData instanceof StylesheetPackage && whitespaceRule != NoElementsSpaceStrippingRule.getInstance()) {
230 if (collection instanceof AbstractResourceCollection) {
231 boolean alreadyStripped = collection.stripWhitespace(whitespaceRule);
232 if (alreadyStripped) {
233 whitespaceRule = null;
234 }
235 }
236 }
237
238 // Get an iterator over the resources in the collection
239 SequenceIterator<?> sourceSeq = getSequenceIterator(collection, context);
240
241 // Get an iterator over the items representing the resources
242 SequenceIterator<?> result = context.getConfiguration()
243 .getMultithreadedItemMappingIterator(sourceSeq,
244 item1 -> ((ExternalObject<Resource>)item1).getObject().getItem(context));
245
246 // In XSLT, apply space-stripping to document nodes in the collection
247 if (whitespaceRule != null) {
248 final SpaceStrippingRule rule = whitespaceRule;
249 ItemMappingFunction<Item<? extends Item>, Item<? extends Item>> stripper = item -> {
250 if (item instanceof NodeInfo && ((NodeInfo) item).getNodeKind() == Type.DOCUMENT) {
251 TreeInfo treeInfo = ((NodeInfo) item).getTreeInfo();
252 if (treeInfo.getSpaceStrippingRule() != rule) {
253 return new SpaceStrippedDocument(treeInfo, rule).getRootNode();
254 }
255 }
256 return item;
257 };
258 result = new ItemMappingIterator(result, stripper);
259 }
260
261 // If the collection is stable, cache the result
262 if (collection.isStable(context) || context.getConfiguration().getBooleanProperty(Feature.STABLE_COLLECTION_URI)) {
263 Controller controller = context.getController();
264 DocumentPool docPool = controller.getDocumentPool();
265 cachedCollection = result.materialize();
266 SequenceIterator<?> iter = cachedCollection.iterate();
267 Item item;
268 while ((item = iter.next()) != null) {
269 if (item instanceof NodeInfo && ((NodeInfo)item).getNodeKind() == Type.DOCUMENT) {
270 String docUri = ((NodeInfo)item).getSystemId();
271 DocumentURI docKey = new DocumentURI(docUri);
272 TreeInfo info = item instanceof TreeInfo ? (TreeInfo)item : new GenericTreeInfo(controller.getConfiguration(), (NodeInfo)item);
273 docPool.add(info, docKey);
274 }
275 }
276 context.getController().setUserData("saxon:collections", collectionKey, cachedCollection);
277 return cachedCollection;
278 }
279
280 return new LazySequence<>(result);
281 }
282
283 }
284