Debian Codegraph @

net/sf/saxon/pattern/NodeSetPattern.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.pattern;
9
10 import com.saxonica.ee.stream.Posture;
11 import com.saxonica.ee.stream.Streamability;
12 import com.saxonica.ee.stream.Sweep;
13 import com.saxonica.ee.trans.ContextItemStaticInfoEE;
14 import net.sf.saxon.expr.*;
15 import net.sf.saxon.expr.instruct.SlotManager;
16 import net.sf.saxon.expr.parser.*;
17 import net.sf.saxon.om.Item;
18 import net.sf.saxon.om.NodeInfo;
19 import net.sf.saxon.om.SequenceIterator;
20 import net.sf.saxon.om.TreeInfo;
21 import net.sf.saxon.trace.ExpressionPresenter;
22 import net.sf.saxon.trans.XPathException;
23 import net.sf.saxon.tree.iter.ManualIterator;
24 import net.sf.saxon.type.ItemType;
25 import net.sf.saxon.type.UType;
26 import net.sf.saxon.value.SequenceType;
27
28 /**
29 * A NodeSetPattern is a pattern based on an expression that is evaluated to return a set of nodes;
30 * a node matches the pattern if it is a member of this node-set.
31 * <p>In XSLT 2.0 there are two forms of NodeSetPattern allowed, represented by calls on the id() and
32 * key() functions. In XSLT 3.0, additional forms are allowed, for example a variable reference, and
33 * a call to the doc() function. This class provides the general capability to use any expression
34 * at the head of a pattern. This is used also to support streaming, where streaming XPath expressions
35 * are mapped to patterns.</p>
36 */
37
38 public class NodeSetPattern extends Pattern {
39
40 protected Operand selectionOp;
41 protected ItemType itemType;
42
43 /**
44 * Create a node-set pattern.
45 *
46 * @param exp an expression that can be evaluated to return a node-set; a node matches the pattern
47 * if it is present in this node-set. The expression must not depend on the focus, though it can depend on
48 * other aspects of the dynamic context such as local or global variables.
49 */
50 public NodeSetPattern(Expression exp) {
51 selectionOp = new Operand(this, exp, OperandRole.NAVIGATE);
52 }
53
54 /**
55 * Get the immediate sub-expressions of this expression, with information about the relationship
56 * of each expression to its parent expression. Default implementation
57 * works off the results of iterateSubExpressions()
58 * <p>If the expression is a Callable, then it is required that the order of the operands
59 * returned by this function is the same as the order of arguments supplied to the corresponding
60 * call() method.</p>
61 *
62 * @return an iterator containing the sub-expressions of this expression
63 */
64 @Override
65 public Iterable<Operand> operands() {
66 return selectionOp;
67 }
68
69 /**
70 * Get the underlying expression
71 * @return the expression that returns all the selected nodes
72 */
73
74 public Expression getSelectionExpression() {
75 return selectionOp.getChildExpression();
76 }
77
78 /**
79 * Type-check the pattern.
80 * Default implementation does nothing. This is only needed for patterns that contain
81 * variable references or function calls.
82 *
83 * @return the optimised Pattern
84 */
85
86 public Pattern typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException {
87 selectionOp.setChildExpression(getSelectionExpression().typeCheck(visitor, contextItemType));
88 RoleDiagnostic role = new RoleDiagnostic(RoleDiagnostic.MATCH_PATTERN, getSelectionExpression().toString(), 0);
89 TypeChecker tc = visitor.getConfiguration().getTypeChecker(false);
90 Expression checked = null;
91 try {
92 checked = tc.staticTypeCheck(
93 getSelectionExpression(), SequenceType.NODE_SEQUENCE, role, visitor);
94 } catch (XPathException e) {
95 visitor.issueWarning("Pattern will never match anything. " + e.getMessage(), getLocation());
96 checked = Literal.makeEmptySequence();
97 }
98 selectionOp.setChildExpression(checked);
99 itemType = getSelectionExpression().getItemType();
100 return this;
101 }
102
103 /**
104 * Set the item type, that is, the type of nodes/items which the pattern will match
105 * @param type the item type that the pattern will match
106 */
107
108 public void setItemType(ItemType type) {
109 this.itemType = type;
110 }
111
112 /**
113 * Get the dependencies of the pattern. The only possible dependency for a pattern is
114 * on local variables. This is analyzed in those patterns where local variables may appear.
115 */
116
117 public int getDependencies() {
118 return getSelectionExpression().getDependencies();
119 }
120
121 //#ifdefined STREAM
122
123 /**
124 * Test whether a pattern is motionless, that is, whether it can be evaluated against a node
125 * without repositioning the input stream. This is a necessary condition for patterns used
126 * as the match pattern of a streamed template rule.
127 *
128 * @return true if the pattern is motionless, that is, if it can be evaluated against a streamed
129 * node without changing the position in the streamed input file
130 */
131 @Override
132 public boolean isMotionless() {
133 Expression select = getSelectionExpression();
134 Streamability.getStreamability(select, new ContextItemStaticInfoEE(getItemType(), false, Posture.STRIDING), null);
135 return Streamability.getSweep(select) == Sweep.MOTIONLESS;
136 }
137 //#endif
138
139 /**
140 * Allocate slots to any variables used within the pattern
141 *
142 * @param slotManager the slot manager representing the stack frame for local variables
143 * @param nextFree the next slot that is free to be allocated @return the next slot that is free to be allocated
144 */
145
146 public int allocateSlots(SlotManager slotManager, int nextFree) {
147 return ExpressionTool.allocateSlots(getSelectionExpression(), nextFree, slotManager);
148 }
149
150 /**
151 * Select nodes in a document using this PatternFinder.
152 *
153 * @param doc the document node at the root of a tree
154 * @param context the dynamic evaluation context
155 * @return an iterator over the selected nodes in the document.
156 */
157
158 public SequenceIterator<? extends NodeInfo> selectNodes(TreeInfo doc, XPathContext context) throws XPathException {
159 XPathContext c2 = context.newMinorContext();
160 ManualIterator<NodeInfo> mi = new ManualIterator<>(doc.getRootNode());
161 c2.setCurrentIterator(mi);
162 return (SequenceIterator<? extends NodeInfo>)getSelectionExpression().iterate(c2);
163 }
164
165 /**
166 * Determine whether this Pattern matches the given Node
167 *
168 * @param item The NodeInfo representing the Element or other node to be tested against the Pattern
169 * @return true if the node matches the Pattern, false otherwise
170 */
171
172 public boolean matches(Item<?> item, XPathContext context) throws XPathException {
173 if (item instanceof NodeInfo) {
174 SequenceIterator iter = getSelectionExpression().iterate(context);
175 return SingletonIntersectExpression.containsNode(iter, (NodeInfo)item);
176 } else {
177 return false;
178 }
179 }
180
181 /**
182 * Get a UType indicating which kinds of items this Pattern can match.
183 *
184 * @return a UType indicating all the primitive types of item that the pattern can match.
185 */
186 @Override
187 public UType getUType() {
188 return getItemType().getUType();
189 }
190
191 /**
192 * Get a NodeTest that all the nodes matching this pattern must satisfy
193 */
194
195 public ItemType getItemType() {
196 if (itemType == null) {
197 itemType = getSelectionExpression().getItemType();
198 }
199 if (itemType instanceof NodeTest) {
200 return itemType;
201 } else {
202 return AnyNodeTest.getInstance();
203 }
204 }
205
206 /**
207 * Determine whether this pattern is the same as another pattern
208 *
209 * @param other the other object
210 */
211
212 public boolean equals(Object other) {
213 return (other instanceof NodeSetPattern) &&
214 ((NodeSetPattern) other).getSelectionExpression().isEqual(getSelectionExpression());
215 }
216
217 /**
218 * Hashcode supporting equals()
219 */
220
221 public int computeHashCode() {
222 return 0x73108728 ^ getSelectionExpression().hashCode();
223 }
224
225 /**
226 * Copy a pattern. This makes a deep copy.
227 *
228 * @return the copy of the original pattern
229 * @param rebindings variable bindings to be changed if encountered
230 */
231
232 /*@NotNull*/
233 public Pattern copy(RebindingMap rebindings) {
234 NodeSetPattern n = new NodeSetPattern(getSelectionExpression().copy(rebindings));
235 ExpressionTool.copyLocationInfo(this, n);
236 return n;
237 }
238
239 public void export(ExpressionPresenter presenter) throws XPathException {
240 presenter.startElement("p.nodeSet");
241 if (itemType != null) {
242 presenter.emitAttribute("type", itemType.toExportString());
243 }
244 getSelectionExpression().export(presenter);
245 presenter.endElement();
246 }
247
248 }
249