| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class CollectionFn extends SystemFunction implements Callable { |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public static String EMPTY_COLLECTION_URI = "http://saxon.sf.net/collection/empty"; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public final static ResourceCollection EMPTY_COLLECTION = new EmptyCollection(EMPTY_COLLECTION_URI); |
| 52 |
|
| 53 |
|
| 54 |
|
| 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 |
|
| 87 |
|
| 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 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 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 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public Sequence<?> call(final XPathContext context, Sequence[] arguments) throws XPathException { |
| 180 |
String href; |
| 181 |
if (getArity() == 0) { |
| 182 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 239 |
SequenceIterator<?> sourceSeq = getSequenceIterator(collection, context); |
| 240 |
|
| 241 |
|
| 242 |
SequenceIterator<?> result = context.getConfiguration() |
| 243 |
.getMultithreadedItemMappingIterator(sourceSeq, |
| 244 |
item1 -> ((ExternalObject<Resource>)item1).getObject().getItem(context)); |
| 245 |
|
| 246 |
|
| 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 |
|
| 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 |
|