Debian Codegraph @

net/sf/saxon/pattern/PatternThatSetsCurrent.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 net.sf.saxon.expr.*;
11 import net.sf.saxon.expr.flwor.LocalVariableBinding;
12 import net.sf.saxon.expr.instruct.SlotManager;
13 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
14 import net.sf.saxon.expr.parser.ExpressionTool;
15 import net.sf.saxon.expr.parser.ExpressionVisitor;
16 import net.sf.saxon.expr.parser.RebindingMap;
17 import net.sf.saxon.functions.Current;
18 import net.sf.saxon.om.Item;
19 import net.sf.saxon.om.NodeInfo;
20 import net.sf.saxon.trace.ExpressionPresenter;
21 import net.sf.saxon.trans.XPathException;
22 import net.sf.saxon.type.ItemType;
23 import net.sf.saxon.type.UType;
24 import net.sf.saxon.value.SequenceType;
25
26 /**
27 * This class represents a pattern that sets the value of current() to the
28 * node being matched, and then wraps another pattern that uses the value
29 * of current()
30 */
31 public class PatternThatSetsCurrent extends Pattern {
32
33 private LocalVariableBinding binding;
34 private Pattern wrappedPattern;
35
36 public PatternThatSetsCurrent(Pattern wrappedPattern) {
37 this(wrappedPattern, new LocalVariableBinding(Current.FN_CURRENT, SequenceType.SINGLE_ITEM));
38 }
39
40 public PatternThatSetsCurrent(Pattern wrappedPattern, LocalVariableBinding binding) {
41 this.wrappedPattern = wrappedPattern;
42 this.binding = binding;
43 binding.setRequiredType(SequenceType.makeSequenceType(wrappedPattern.getItemType(), StaticProperty.EXACTLY_ONE));
44 adoptChildExpression(wrappedPattern);
45 setPriority(wrappedPattern.getDefaultPriority());
46 }
47
48 /**
49 * Get the immediate sub-expressions of this expression, with information about the relationship
50 * of each expression to its parent expression. Default implementation
51 * works off the results of iterateSubExpressions()
52 * <p>If the expression is a Callable, then it is required that the order of the operands
53 * returned by this function is the same as the order of arguments supplied to the corresponding
54 * call() method.</p>
55 *
56 * @return an iterator containing the sub-expressions of this expression
57 */
58 @Override
59 public Iterable<Operand> operands() {
60 return new Operand(this, wrappedPattern, OperandRole.SINGLE_ATOMIC);
61 }
62
63 /**
64 * Get the binding of the item being matched by the pattern, that is, the binding that
65 * represents the value of the current() function
66 *
67 * @return the binding of the item being matched by the pattern
68 */
69
70 public LocalBinding getCurrentBinding() {
71 return binding;
72 }
73
74 /**
75 * Ask whether this expression is, or contains, the binding of a given variable
76 *
77 * @param binding the variable binding
78 * @return true if this expression is the variable binding (for example a ForExpression
79 * or LetExpression) or if it is a FLWOR expression that binds the variable in one of its
80 * clauses.
81 */
82 @Override
83 public boolean hasVariableBinding(Binding binding) {
84 return binding == this.binding;
85 }
86
87 /**
88 * Allocate slots to any variables used within the pattern
89 *
90 * @param slotManager the slot manager representing the stack frame for local variables
91 * @param nextFree the next slot that is free to be allocated
92 * @return the next slot that is free to be allocated
93 */
94 @Override
95 public int allocateSlots(SlotManager slotManager, int nextFree) {
96 slotManager.allocateSlotNumber(Current.FN_CURRENT);
97 binding.setSlotNumber(nextFree++);
98 return wrappedPattern.allocateSlots(slotManager, nextFree);
99 }
100
101 /**
102 * Determine whether this Pattern matches the given item. This is the main external interface
103 * for matching patterns: it sets current() to the node being tested
104 *
105 * @param item The item to be tested against the Pattern
106 * @param context The dynamic context. Only relevant if the pattern
107 * uses variables, or contains calls on functions such as document() or key().
108 * @return true if the item matches the Pattern, false otherwise
109 */
110 @Override
111 public boolean matches(Item<?> item, XPathContext context) throws XPathException {
112 context.setLocalVariable(binding.getLocalSlotNumber(), item);
113 return wrappedPattern.matches(item, context);
114 }
115
116 /**
117 * Get an ItemType that all the items matching this pattern must satisfy
118 *
119 * @return an ItemType, as specific as possible, which all the matching items satisfy
120 */
121 @Override
122 public ItemType getItemType() {
123 return wrappedPattern.getItemType();
124 }
125
126 /**
127 * Simplify the pattern by applying any context-independent optimisations.
128 * Default implementation does nothing.
129 *
130 */
131 @Override
132 public Pattern simplify() throws XPathException {
133 wrappedPattern = wrappedPattern.simplify();
134 return this;
135 }
136
137 /**
138 * Type-check the pattern.
139 *
140 * @param visitor the expression visitor
141 * @param contextItemType the type of the context item at the point where the pattern
142 * is defined. Set to null if it is known that the context item is undefined.
143 * @return the optimised Pattern
144 */
145 @Override
146 public Pattern typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException {
147 wrappedPattern = wrappedPattern.typeCheck(visitor, contextItemType);
148 return this;
149 }
150
151 /**
152 * Get a UType indicating which kinds of items this Pattern can match.
153 *
154 * @return a UType indicating all the primitive types of item that the pattern can match.
155 */
156 @Override
157 public UType getUType() {
158 return wrappedPattern.getUType();
159 }
160
161 /**
162 * Determine the name fingerprint of nodes to which this pattern applies. Used for
163 * optimisation.
164 *
165 * @return A fingerprint that the nodes must match, or -1 if it can match multiple fingerprints,
166 * or it if matches atomic values
167 */
168 @Override
169 public int getFingerprint() {
170 return wrappedPattern.getFingerprint();
171 }
172
173 /**
174 * Get the original pattern text
175 */
176 @Override
177 public String toString() {
178 return wrappedPattern.toString();
179 }
180
181 /**
182 * Get the host language (XSLT, XQuery, XPath) used to implement the code in this container
183 *
184 * @return typically {@link net.sf.saxon.Configuration#XSLT} or {@link net.sf.saxon.Configuration#XQUERY}
185 */
186 @Override
187 public int getHostLanguage() {
188 return wrappedPattern.getHostLanguage();
189 }
190
191 /**
192 * Test whether a pattern is motionless, that is, whether it can be evaluated against a node
193 * without repositioning the input stream. This is a necessary condition for patterns used
194 * as the match pattern of a streamed template rule.
195 *
196 * @return true if the pattern is motionless, that is, if it can be evaluated against a streamed
197 * node without changing the position in the streamed input file
198 */
199 @Override
200 public boolean isMotionless() {
201 return wrappedPattern.isMotionless();
202 }
203
204 /**
205 * Determine whether this pattern matches a given Node within the subtree rooted at a given
206 * anchor node. This method is used when the pattern is used for streaming.
207 *
208 * @param node The NodeInfo representing the Element or other node to be tested against the Pattern
209 * @param anchor The anchor node, which must match any AnchorPattern subpattern
210 * @param context The dynamic context. Only relevant if the pattern
211 * uses variables, or contains calls on functions such as document() or key().
212 * @return true if the node matches the Pattern, false otherwise
213 */
214 @Override
215 public boolean matchesBeneathAnchor(NodeInfo node, NodeInfo anchor, XPathContext context) throws XPathException {
216 return wrappedPattern.matchesBeneathAnchor(node, anchor, context);
217 }
218
219 /**
220 * Convert the pattern to a typed pattern, in which an element name is treated as
221 * schema-element(N)
222 *
223 * @param val either "strict" or "lax" depending on the value of xsl:mode/@typed
224 * @return either the original pattern unchanged, or a new pattern as the result of the
225 * conversion
226 * @throws net.sf.saxon.trans.XPathException if the pattern cannot be converted
227 */
228 @Override
229 public Pattern convertToTypedPattern(String val) throws XPathException {
230 Pattern w2 = wrappedPattern.convertToTypedPattern(val);
231 if (w2 == wrappedPattern) {
232 return this;
233 } else {
234 return new PatternThatSetsCurrent(w2);
235 }
236 }
237
238 /**
239 * Copy a pattern. This makes a deep copy.
240 *
241 * @return the copy of the original pattern
242 * @param rebindings variables that need to be re-bound
243 */
244
245 /*@NotNull*/
246 public Pattern copy(RebindingMap rebindings) {
247 LocalVariableBinding newCurrent = new LocalVariableBinding(Current.FN_CURRENT, SequenceType.SINGLE_ITEM);
248 rebindings.put(binding, newCurrent);
249 PatternThatSetsCurrent n = new PatternThatSetsCurrent(wrappedPattern.copy(rebindings), newCurrent);
250 ExpressionTool.copyLocationInfo(this, n);
251 return n;
252 }
253
254 public void export(ExpressionPresenter presenter) throws XPathException {
255 presenter.startElement("p.withCurrent");
256 wrappedPattern.export(presenter);
257 presenter.endElement();
258 }
259
260
261 public Pattern getWrappedPattern() {
262 return wrappedPattern;
263 }
264
265 }
266