Debian Codegraph @

net/sf/saxon/expr/flwor/WhereClause.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.expr.flwor;
9
10 import net.sf.saxon.expr.*;
11 import net.sf.saxon.expr.parser.*;
12 import net.sf.saxon.trace.ExpressionPresenter;
13 import net.sf.saxon.trans.XPathException;
14 import net.sf.saxon.tree.util.FastStringBuffer;
15 import net.sf.saxon.type.ItemType;
16
17 import java.util.List;
18
19 /**
20 * A "where" clause in a FLWOR expression
21 */
22 public class WhereClause extends Clause {
23
24 private Operand predicateOp;
25
26 public WhereClause(FLWORExpression flwor, Expression predicate) {
27 this.predicateOp = new Operand(flwor, predicate, OperandRole.INSPECT);
28 }
29
30 @Override
31 public void setRepeated(boolean repeated) {
32 super.setRepeated(repeated);
33 if (repeated) {
34 this.predicateOp.setOperandRole(OperandRole.REPEAT_INSPECT);
35 }
36 }
37
38 @Override
39 public int getClauseKey() {
40 return WHERE;
41 }
42
43 public Expression getPredicate() {
44 return predicateOp.getChildExpression();
45 }
46
47 public void setPredicate(Expression predicate) {
48 predicateOp.setChildExpression(predicate);
49 }
50
51 public WhereClause copy(FLWORExpression flwor, RebindingMap rebindings) {
52 WhereClause w2 = new WhereClause(flwor, getPredicate().copy(rebindings));
53 w2.setLocation(getLocation());
54 w2.setPackageData(getPackageData());
55 return w2;
56 }
57
58 /**
59 * Type-check the expression
60 */
61 @Override
62 public void typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
63 super.typeCheck(visitor, contextInfo);
64 }
65
66
67 /**
68 * Get a tuple stream that implements the functionality of this clause, taking its
69 * input from another tuple stream which this clause modifies
70 *
71 * @param base the input tuple stream
72 * @param context the dynamic evaluation context
73 * @return the output tuple stream
74 */
75 @Override
76 public TuplePull getPullStream(TuplePull base, XPathContext context) {
77 return new WhereClausePull(base, getPredicate());
78 }
79
80 @Override
81 public void gatherVariableReferences(final ExpressionVisitor visitor, Binding binding, List<VariableReference> references) {
82 ExpressionTool.gatherVariableReferences(getPredicate(), binding, references);
83 }
84
85 @Override
86 public void refineVariableType(ExpressionVisitor visitor, List<VariableReference> references, Expression returnExpr) {
87 final ItemType actualItemType = getPredicate().getItemType();
88 for (VariableReference ref : references) {
89 ref.refineVariableType(actualItemType, getPredicate().getCardinality(),
90 getPredicate() instanceof Literal ? ((Literal) getPredicate()).getValue() : null,
91 getPredicate().getSpecialProperties());
92 ExpressionTool.resetStaticProperties(returnExpr);
93 }
94 }
95
96 /**
97 * Get a push-mode tuple stream that implements the functionality of this clause, supplying its
98 * output to another tuple stream
99 *
100 * @param destination the output tuple stream
101 * @param context the dynamic evaluation context
102 * @return the push tuple stream that implements the functionality of this clause of the FLWOR
103 * expression
104 */
105 @Override
106 public TuplePush getPushStream(TuplePush destination, XPathContext context) {
107 return new WhereClausePush(destination, getPredicate());
108 }
109
110 /**
111 * Process the subexpressions of this clause
112 *
113 * @param processor the expression processor used to process the subexpressions
114 */
115 @Override
116 public void processOperands(OperandProcessor processor) throws XPathException {
117 processor.processOperand(predicateOp);
118 }
119
120 @Override
121 public void addToPathMap(PathMap pathMap, PathMap.PathMapNodeSet pathMapNodeSet) {
122 getPredicate().addToPathMap(pathMap, pathMapNodeSet);
123 }
124
125 /**
126 * Diagnostic print of expression structure. The abstract expression tree
127 * is written to the supplied output destination.
128 *
129 * @param out the expression presenter used to display the structure
130 */
131 @Override
132 public void explain(ExpressionPresenter out) throws XPathException {
133 out.startElement("where");
134 getPredicate().export(out);
135 out.endElement();
136 }
137
138 public String toShortString() {
139 FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C64);
140 fsb.append("where ");
141 fsb.append(getPredicate().toShortString());
142 return fsb.toString();
143 }
144
145 public String toString() {
146 FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C64);
147 fsb.append("where ");
148 fsb.append(getPredicate().toString());
149 return fsb.toString();
150 }
151 }
152