Debian Codegraph @

net/sf/saxon/functions/Idref.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.*;
11 import net.sf.saxon.expr.sort.DocumentOrderIterator;
12 import net.sf.saxon.expr.sort.LocalOrderComparer;
13 import net.sf.saxon.om.*;
14 import net.sf.saxon.trans.KeyDefinitionSet;
15 import net.sf.saxon.trans.KeyManager;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.type.Type;
18 import net.sf.saxon.value.StringValue;
19
20
21 public class Idref extends SystemFunction {
22
23 /**
24 * Get the static properties of this expression (other than its type). The result is
25 * bit-significant. These properties are used for optimizations. In general, if
26 * property bit is set, it is true, but if it is unset, the value is unknown.
27 * @param arguments the actual arguments to the function call
28 */
29
30 public int getSpecialProperties(Expression[] arguments) {
31 int prop = StaticProperty.ORDERED_NODESET |
32 StaticProperty.SINGLE_DOCUMENT_NODESET |
33 StaticProperty.NO_NODES_NEWLY_CREATED;
34 if ((getArity() == 1) ||
35 arguments[1].hasSpecialProperty(StaticProperty.CONTEXT_DOCUMENT_NODESET)) {
36 prop |= StaticProperty.CONTEXT_DOCUMENT_NODESET;
37 }
38 return prop;
39 }
40
41 /**
42 * Get the result when multiple idref values are supplied. Note this is also called from
43 * compiled XQuery code.
44 *
45 * @param doc the document to be searched
46 * @param keys the idref values supplied
47 * @param context the dynamic execution context
48 * @return iterator over the result of the function
49 * @throws XPathException if a dynamic error occurs
50 */
51
52 public static SequenceIterator<NodeInfo> getIdrefMultiple(TreeInfo doc, SequenceIterator<StringValue> keys, XPathContext context)
53 throws XPathException {
54 IdrefMappingFunction map = new IdrefMappingFunction();
55 map.document = doc;
56 map.keyContext = context;
57 map.keyManager = context.getController().getExecutable().getTopLevelPackage().getKeyManager();
58 map.keySet = map.keyManager.getKeyDefinitionSet(StandardNames.getStructuredQName(StandardNames.XS_IDREFS));
59 SequenceIterator<? extends NodeInfo> allValues = new MappingIterator<>(keys, map);
60 return new DocumentOrderIterator(allValues, LocalOrderComparer.getInstance());
61 }
62
63 /**
64 * Evaluate the expression
65 *
66 * @param context the dynamic evaluation context
67 * @param arguments the values of the arguments, supplied as SequenceIterators
68 * @return the result of the evaluation, in the form of a SequenceIterator
69 * @throws net.sf.saxon.trans.XPathException
70 * if a dynamic error occurs during the evaluation of the expression
71 */
72 public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException {
73 NodeInfo start = arguments.length == 1 ? getContextNode(context) : (NodeInfo) arguments[1].head();
74 NodeInfo arg2 = start.getRoot();
75 if (arg2.getNodeKind() != Type.DOCUMENT) {
76 throw new XPathException("In the idref() function," +
77 " the tree being searched must be one whose root is a document node", "FODC0001", context);
78 }
79 return SequenceTool.toLazySequence(getIdrefMultiple(arg2.getTreeInfo(), arguments[0].iterate(), context));
80 }
81
82 private static class IdrefMappingFunction implements MappingFunction<StringValue, NodeInfo> {
83 public TreeInfo document;
84 public XPathContext keyContext;
85 public KeyManager keyManager;
86 public KeyDefinitionSet keySet;
87
88 /**
89 * Implement the MappingFunction interface
90 */
91
92 public SequenceIterator<NodeInfo> map(StringValue item) throws XPathException {
93 return keyManager.selectByKey(keySet, document, item, keyContext);
94 }
95 }
96
97 }
98