Debian Codegraph @

net/sf/saxon/functions/SuperId.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.XPathException;
15 import net.sf.saxon.tree.iter.SingletonIterator;
16 import net.sf.saxon.type.Type;
17 import net.sf.saxon.value.AtomicValue;
18 import net.sf.saxon.value.Whitespace;
19
20
21 /**
22 * The XPath id() or element-with-id() function
23 * XPath 2.0 version: accepts any sequence as the first parameter; each item in the sequence
24 * is taken as an IDREFS value, that is, a space-separated list of ID values.
25 * Also accepts an optional second argument to identify the target document, this
26 * defaults to the context node.
27 */
28
29
30 public abstract class SuperId extends SystemFunction {
31
32 public static final int ID = 0;
33 public static final int ELEMENT_WITH_ID = 1;
34
35 public abstract int getOp();
36
37 /**
38 * Get the static properties of this expression (other than its type). The result is
39 * bit-signficant. These properties are used for optimizations. In general, if
40 * property bit is set, it is true, but if it is unset, the value is unknown.
41 * @param arguments the actual arguments to the call
42 */
43
44 public int getSpecialProperties(Expression[] arguments) {
45 int prop = StaticProperty.ORDERED_NODESET |
46 StaticProperty.SINGLE_DOCUMENT_NODESET |
47 StaticProperty.NO_NODES_NEWLY_CREATED;
48 if ((getArity() == 1) ||
49 (arguments[1].getSpecialProperties() & StaticProperty.CONTEXT_DOCUMENT_NODESET) != 0) {
50 prop |= StaticProperty.CONTEXT_DOCUMENT_NODESET;
51 }
52 return prop;
53 }
54
55 /**
56 * Get an iterator over the nodes that have an id equal to one of the values is a whitespace separated
57 * string
58 *
59 * @param doc The document to be searched
60 * @param idrefs a string containing zero or more whitespace-separated ID values to be found in the document
61 * @param operation either {@link #ID} or {@link #ELEMENT_WITH_ID}
62 * @return an iterator over the nodes whose ID is one of the specified values
63 * @throws XPathException if an error occurs
64 */
65
66 public static SequenceIterator<?> getIdSingle(TreeInfo doc, String idrefs, int operation) throws XPathException {
67 if (Whitespace.containsWhitespace(idrefs)) {
68 Whitespace.Tokenizer tokens = new Whitespace.Tokenizer(idrefs);
69 IdMappingFunction map = new IdMappingFunction();
70 map.document = doc;
71 map.operation = operation;
72 SequenceIterator result = new MappingIterator<>(tokens, map);
73 return new DocumentOrderIterator(result, LocalOrderComparer.getInstance());
74 } else {
75 return SingletonIterator.makeIterator(doc.selectID(idrefs, operation == ELEMENT_WITH_ID));
76 }
77 }
78
79 /**
80 * Get an iterator over the nodes that have an id equal to one of the values is a set of whitespace separated
81 * strings
82 *
83 * @param doc The document to be searched
84 * @param idrefs an iterator over a set of strings each of which is a string containing
85 * zero or more whitespace-separated ID values to be found in the document
86 * @param operation distinguishes id() and element-with-id()
87 * @return an iterator over the nodes whose ID is one of the specified values
88 * @throws XPathException if an error occurs
89 */
90
91 public static SequenceIterator getIdMultiple(
92 TreeInfo doc, SequenceIterator<?> idrefs, int operation) throws XPathException {
93 IdMappingFunction map = new IdMappingFunction();
94 map.document = doc;
95 map.operation = operation;
96 SequenceIterator<?> result = new MappingIterator<>(idrefs, map);
97 return new DocumentOrderIterator(result, LocalOrderComparer.getInstance());
98 }
99
100 /**
101 * Evaluate the expression
102 *
103 * @param context the dynamic evaluation context
104 * @param arguments the values of the arguments, supplied as SequenceIterators
105 * @return the result of the evaluation, in the form of a SequenceIterator
106 * @throws net.sf.saxon.trans.XPathException
107 * if a dynamic error occurs during the evaluation of the expression
108 */
109 public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException {
110 NodeInfo start = arguments.length == 1 ? getContextNode(context) : (NodeInfo) arguments[1].head();
111 NodeInfo arg1 = start.getRoot();
112 if (arg1.getNodeKind() != Type.DOCUMENT) {
113 throw new XPathException("In the " + getFunctionName().getLocalPart() + "() function," +
114 " the tree being searched must be one whose root is a document node", "FODC0001", context);
115 }
116 TreeInfo doc = arg1.getTreeInfo();
117 SequenceIterator<?> result;
118 if (arguments[0] instanceof AtomicValue) {
119 result = getIdSingle(doc, ((AtomicValue)arguments[0]).getStringValue(), getOp());
120 } else {
121 SequenceIterator<?> idrefs = arguments[0].iterate();
122 result = getIdMultiple(doc, idrefs, getOp());
123 }
124 return SequenceTool.toLazySequence(result);
125 }
126
127 private static class IdMappingFunction implements MappingFunction<Item<?>, Item<?>> {
128
129 public TreeInfo document;
130 private int operation;
131
132 /**
133 * Evaluate the function for a single string value
134 * (implements the MappingFunction interface)
135 */
136
137 public SequenceIterator<?> map(Item item) {
138
139 String idrefs = Whitespace.trim(item.getStringValueCS());
140
141 // If this value contains a space, we need to break it up into its
142 // separate tokens; if not, we can process it directly
143
144 if (Whitespace.containsWhitespace(idrefs)) {
145 Whitespace.Tokenizer tokens = new Whitespace.Tokenizer(idrefs);
146 IdMappingFunction submap = new IdMappingFunction();
147 submap.document = document;
148 submap.operation = operation;
149 return new MappingIterator<>(tokens, submap);
150
151 } else {
152 return SingletonIterator.makeIterator(document.selectID(idrefs, operation == ELEMENT_WITH_ID));
153 }
154 }
155 }
156
157 public static class Id extends SuperId {
158 @Override
159 public int getOp() {
160 return ID;
161 }
162 }
163
164 public static class ElementWithId extends SuperId {
165 @Override
166 public int getOp() {
167 return ELEMENT_WITH_ID;
168 }
169 }
170
171
172
173 }
174