Debian Codegraph @

net/sf/saxon/expr/TailExpression.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;
9
10 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
11 import net.sf.saxon.expr.parser.ExpressionTool;
12 import net.sf.saxon.expr.parser.ExpressionVisitor;
13 import net.sf.saxon.expr.parser.RebindingMap;
14 import net.sf.saxon.om.GroundedValue;
15 import net.sf.saxon.om.Item;
16 import net.sf.saxon.om.SequenceIterator;
17 import net.sf.saxon.trace.ExpressionPresenter;
18 import net.sf.saxon.trans.XPathException;
19 import net.sf.saxon.type.ItemType;
20
21 /**
22 * A TailExpression represents a FilterExpression of the form <code>EXPR[position() &gt; n]</code>
23 * Here n is usually 2, but we allow other values
24 */
25 public class TailExpression extends UnaryExpression {
26
27 int start; // 1-based offset of first item from base expression
28 // to be included
29
30 /**
31 * Construct a TailExpression, representing a filter expression of the form
32 * {@code $base[position() >= $start]}
33 *
34 * @param base the expression to be filtered
35 * @param start the position (1-based) of the first item to be included
36 */
37
38 public TailExpression(Expression base, int start) {
39 super(base);
40 this.start = start;
41 }
42
43 /*@NotNull*/
44 public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
45 getOperand().optimize(visitor, contextInfo);
46 if (getBaseExpression() instanceof Literal) {
47 GroundedValue<?> value =
48 iterate(visitor.getStaticContext().makeEarlyEvaluationContext()).materialize();
49 return Literal.makeLiteral(value, this);
50 }
51 return this;
52 }
53
54 /**
55 * Copy an expression. This makes a deep copy.
56 *
57 * @return the copy of the original expression
58 * @param rebindings variables that need to be re-bound
59 */
60
61 /*@NotNull*/
62 public Expression copy(RebindingMap rebindings) {
63 TailExpression exp = new TailExpression(getBaseExpression().copy(rebindings), start);
64 ExpressionTool.copyLocationInfo(this, exp);
65 return exp;
66 }
67
68 /**
69 * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
70 * This method indicates which of these methods is provided directly. The other methods will always be available
71 * indirectly, using an implementation that relies on one of the other methods.
72 *
73 * @return the implementation method, for example {@link #ITERATE_METHOD} or {@link #EVALUATE_METHOD} or
74 * {@link #PROCESS_METHOD}
75 */
76 @Override
77 public int getImplementationMethod() {
78 return ITERATE_METHOD;
79 }
80
81 /*@NotNull*/
82 public ItemType getItemType() {
83 return getBaseExpression().getItemType();
84 }
85
86 public int computeCardinality() {
87 return getBaseExpression().getCardinality() | StaticProperty.ALLOWS_ZERO;
88 }
89
90 @Override
91 protected OperandRole getOperandRole() {
92 return OperandRole.SAME_FOCUS_ACTION;
93 }
94
95 /**
96 * Get the start offset
97 *
98 * @return the one-based start offset (returns 2 if all but the first item is being selected)
99 */
100
101 public int getStart() {
102 return start;
103 }
104
105 /**
106 * Compare two expressions to see if they are equal
107 *
108 * @param other the other expression
109 * @return true if the expressions are equivalent
110 */
111
112 public boolean equals(Object other) {
113 return other instanceof TailExpression &&
114 getBaseExpression().isEqual(((TailExpression)other).getBaseExpression()) &&
115 start == ((TailExpression) other).start;
116 }
117
118 public int computeHashCode() {
119 return super.computeHashCode() ^ start;
120 }
121
122 /**
123 * Get the (partial) name of a class that supports streaming of this kind of expression
124 *
125 * @return the partial name of a class that can be instantiated to provide streaming support in Saxon-EE,
126 * or null if there is no such class
127 */
128 @Override
129 public String getStreamerName() {
130 return "TailExpression";
131 }
132
133 /*@NotNull*/
134 public SequenceIterator<?> iterate(XPathContext context) throws XPathException {
135 SequenceIterator<?> baseIter = getBaseExpression().iterate(context);
136 return TailIterator.make(baseIter, start);
137 }
138
139 /**
140 * Get a name identifying the kind of expression, in terms meaningful to a user.
141 *
142 * @return a name identifying the kind of expression, in terms meaningful to a user.
143 * The name will always be in the form of a lexical XML QName, and should match the name used
144 * in export() output displaying the expression.
145 */
146 @Override
147 public String getExpressionName() {
148 return "tail";
149 }
150
151
152
153 /**
154 * Diagnostic print of expression structure. The abstract expression tree
155 * is written to the supplied output destination.
156 */
157
158 public void export(ExpressionPresenter destination) throws XPathException {
159 destination.startElement("tail", this);
160 destination.emitAttribute("start", start + "");
161 getBaseExpression().export(destination);
162 destination.endElement();
163 }
164
165 /**
166 * <p>The toString() method for an expression attempts to give a representation of the expression
167 * in an XPath-like form.</p>
168 * <p>For subclasses of Expression that represent XPath expressions, the result should always be a string that
169 * parses as an XPath 3.0 expression.</p>
170 *
171 * @return a representation of the expression as a string
172 */
173 @Override
174 public String toString() {
175 if (start == 2) {
176 return "tail(" + getBaseExpression() + ")";
177 } else {
178 return ExpressionTool.parenthesize(getBaseExpression()) + "[position() ge " + start + "]";
179 }
180 }
181
182 /**
183 * Produce a short string identifying the expression for use in error messages
184 *
185 * @return a short string, sufficient to identify the expression
186 */
187 @Override
188 public String toShortString() {
189 if (start == 2) {
190 return "tail(" + getBaseExpression().toShortString() + ")";
191 } else {
192 return getBaseExpression().toShortString() + "[position() ge " + start + "]";
193 }
194 }
195 }
196