Debian Codegraph @

net/sf/saxon/functions/Innermost.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.Expression;
11 import net.sf.saxon.expr.StaticProperty;
12 import net.sf.saxon.expr.XPathContext;
13 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
14 import net.sf.saxon.expr.parser.ExpressionVisitor;
15 import net.sf.saxon.expr.sort.DocumentOrderIterator;
16 import net.sf.saxon.expr.sort.GlobalOrderComparer;
17 import net.sf.saxon.om.NodeInfo;
18 import net.sf.saxon.om.Sequence;
19 import net.sf.saxon.om.SequenceIterator;
20 import net.sf.saxon.om.SequenceTool;
21 import net.sf.saxon.trace.ExpressionPresenter;
22 import net.sf.saxon.trans.XPathException;
23 import net.sf.saxon.tree.util.Navigator;
24
25 import java.util.Properties;
26
27 /**
28 * This class implements the function fn:has-children(), which is a standard function in XPath 3.0
29 */
30
31 public class Innermost extends SystemFunction {
32
33 boolean presorted = false;
34
35
36 @Override
37 public int getSpecialProperties(Expression[] arguments) {
38 return StaticProperty.ORDERED_NODESET | StaticProperty.PEER_NODESET;
39 }
40
41 /**
42 * Allow the function to create an optimized call based on the values of the actual arguments
43 *
44 * @param visitor the expression visitor
45 * @param contextInfo information about the context item
46 * @param arguments the supplied arguments to the function call. Note: modifying the contents
47 * of this array should not be attempted, it is likely to have no effect.
48 * @return either a function call on this function, or an expression that delivers
49 * the same result, or null indicating that no optimization has taken place
50 * @throws XPathException if an error is detected
51 */
52 @Override
53 public Expression makeOptimizedFunctionCall(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, Expression... arguments) throws XPathException {
54 if ((arguments[0].getSpecialProperties() & StaticProperty.PEER_NODESET) != 0) {
55 return arguments[0];
56 }
57 if ((arguments[0].getSpecialProperties() & StaticProperty.ORDERED_NODESET) != 0) {
58 presorted = true;
59 }
60 return super.makeOptimizedFunctionCall(visitor, contextInfo, arguments);
61 }
62
63 /**
64 * Evaluate the expression in a dynamic call
65 *
66 * @param context the dynamic evaluation context
67 * @param arguments the values of the arguments, supplied as Sequences
68 * @return the result of the evaluation, in the form of a Sequence
69 * @throws net.sf.saxon.trans.XPathException
70 * if a dynamic error occurs during the evaluation of the expression
71 */
72 public Sequence<? extends NodeInfo> call(XPathContext context, Sequence[] arguments) throws XPathException {
73 return SequenceTool.toLazySequence(innermost(arguments[0].iterate()));
74 }
75
76 public SequenceIterator<? extends NodeInfo> innermost(SequenceIterator<? extends NodeInfo> in) throws XPathException {
77 if (!presorted) {
78 in = new DocumentOrderIterator(in, GlobalOrderComparer.getInstance());
79 }
80 return new InnermostIterator(in);
81 }
82
83
84 @Override
85 public void exportAttributes(ExpressionPresenter out) {
86 super.exportAttributes(out);
87 if (presorted) {
88 out.emitAttribute("flags", "p");
89 }
90 }
91
92 @Override
93 public void importAttributes(Properties attributes) throws XPathException {
94 super.importAttributes(attributes);
95 String flags = attributes.getProperty("flags");
96 if (flags != null && flags.contains("p")) {
97 presorted = true;
98 }
99 }
100
101
102 /**
103 * Inner class implementing the logic in the form of an iterator.
104 * <p>The principle behind the algorithm is the assertion that in a sorted input
105 * sequence, if a node A is immediately followed by a descendant B, then we can
106 * discard A; if it is immediately followed by a node B that is not a descendant,
107 * then we can emit A. So there is a single-node lookahead. The last node in the input
108 * sequence is always emitted. (Note, "descendant" is used loosely here to
109 * include attributes and namespaces.)</p>
110 */
111
112 private class InnermostIterator implements SequenceIterator<NodeInfo> {
113
114 SequenceIterator<? extends NodeInfo> in;
115 NodeInfo pending = null;
116 int position = 0;
117
118 /**
119 * Create an iterator which filters an input sequence to select only those nodes
120 * that have no ancestor in the sequence
121 *
122 * @param in the input sequence, which must be a sequence of nodes in document order with no duplicates
123 * @throws XPathException if an error occurs evaluating the input iterator
124 */
125
126 public InnermostIterator(SequenceIterator<? extends NodeInfo> in) throws XPathException {
127 this.in = in;
128 pending = in.next();
129 }
130
131 public NodeInfo next() throws XPathException {
132 if (pending == null) {
133 // we're done
134 position = -1;
135 return null;
136 } else {
137 while (true) {
138 NodeInfo next = in.next();
139 if (next == null) {
140 NodeInfo current = pending;
141 position++;
142 pending = null;
143 return current;
144 }
145 if (Navigator.isAncestorOrSelf(pending, next)) {
146 // discard the pending node
147 pending = next;
148 } else {
149 // emit the pending node
150 position++;
151 NodeInfo current = pending;
152 pending = next;
153 return current;
154 }
155 }
156 }
157 }
158
159 public void close() {
160 in.close();
161 }
162
163 public int getProperties() {
164 return 0;
165 }
166 }
167 }
168
169 // Copyright (c) 2012 Saxonica Limited.