Debian Codegraph @

net/sf/saxon/functions/Outermost.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:outermost(), which is a standard function in XPath 3.0
29 */
30
31 public class Outermost extends SystemFunction {
32
33 boolean presorted = false;
34
35 /**
36 * Allow the function to create an optimized call based on the values of the actual arguments
37 *
38 * @param visitor the expression visitor
39 * @param contextInfo information about the context item
40 * @param arguments the supplied arguments to the function call
41 * @return either a function call on this function, or an expression that delivers
42 * the same result, or null indicating that no optimization has taken place
43 * @throws net.sf.saxon.trans.XPathException if an error is detected
44 */
45 @Override
46 public Expression makeOptimizedFunctionCall(
47 ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, Expression... arguments) throws XPathException {
48 if ((arguments[0].getSpecialProperties() & StaticProperty.PEER_NODESET) != 0) {
49 return arguments[0];
50 }
51 presorted = (arguments[0].getSpecialProperties() & StaticProperty.ORDERED_NODESET) != 0;
52 return null;
53 }
54
55 @Override
56 public int getSpecialProperties(Expression[] arguments) {
57 return StaticProperty.ORDERED_NODESET | StaticProperty.PEER_NODESET;
58 }
59
60 /**
61 * Evaluate the expression in a dynamic call
62 *
63 * @param context the dynamic evaluation context
64 * @param arguments the values of the arguments, supplied as Sequences
65 * @return the result of the evaluation, in the form of a Sequence
66 * @throws net.sf.saxon.trans.XPathException
67 * if a dynamic error occurs during the evaluation of the expression
68 */
69 public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException {
70 SequenceIterator<?> in = arguments[0].iterate();
71 if (!presorted) {
72 in = new DocumentOrderIterator(in, GlobalOrderComparer.getInstance());
73 }
74 SequenceIterator<?> out = new OutermostIterator(in);
75 return SequenceTool.toLazySequence(out);
76 }
77
78
79 @Override
80 public void exportAttributes(ExpressionPresenter out) {
81 super.exportAttributes(out);
82 if (presorted) {
83 out.emitAttribute("flags", "p");
84 }
85 }
86
87 @Override
88 public void importAttributes(Properties attributes) throws XPathException {
89 super.importAttributes(attributes);
90 String flags = attributes.getProperty("flags");
91 if (flags != null && flags.contains("p")) {
92 presorted = true;
93 }
94 }
95
96
97 /**
98 * Inner class implementing the logic in the form of an iterator.
99 * <p>The principle behind the algorithm is the assertion that in a sorted input
100 * sequence, if a node has an ancestor within the sequence, then the most recently
101 * selected node will be an ancestor: so we only need to test against the most
102 * recently output node.</p>
103 */
104
105 private class OutermostIterator implements SequenceIterator {
106
107 SequenceIterator in;
108 NodeInfo current = null;
109 int position = 0;
110
111 /**
112 * Create an iterator which filters an input sequence to select only those nodes
113 * that have no ancestor in the sequence
114 *
115 * @param in the input sequence, which must be sequence of nodes in document order with no duplicates
116 */
117
118 public OutermostIterator(SequenceIterator in) {
119 this.in = in;
120 }
121
122 public NodeInfo next() throws XPathException {
123 while (true) {
124 NodeInfo next = (NodeInfo)in.next();
125 if (next == null) {
126 current = null;
127 position = -1;
128 return null;
129 }
130 if (current == null || !Navigator.isAncestorOrSelf(current, next)) {
131 current = next;
132 position++;
133 return current;
134 }
135 }
136 }
137
138 public void close() {
139 in.close();
140 }
141
142 public int getProperties() {
143 return 0;
144 }
145 }
146
147 public String getStreamerName() {
148 return "Outermost";
149 }
150 }
151
152 // Copyright (c) 2012-2015 Saxonica Limited.