Debian Codegraph @

net/sf/saxon/expr/FirstItemExpression.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.Configuration;
11 import net.sf.saxon.expr.parser.ExpressionTool;
12 import net.sf.saxon.expr.parser.RebindingMap;
13 import net.sf.saxon.om.AxisInfo;
14 import net.sf.saxon.om.Item;
15 import net.sf.saxon.om.SequenceIterator;
16 import net.sf.saxon.pattern.*;
17 import net.sf.saxon.trans.XPathException;
18 import net.sf.saxon.type.ItemType;
19
20
21 /**
22 * A FirstItemExpression returns the first item in the sequence returned by a given
23 * base expression
24 */
25
26 public final class FirstItemExpression extends SingleItemFilter {
27
28 /**
29 * Private Constructor
30 *
31 * @param base A sequence expression denoting sequence whose first item is to be returned
32 */
33
34 private FirstItemExpression(Expression base) {
35 super(base);
36 }
37
38 /**
39 * Static factory method
40 *
41 * @param base A sequence expression denoting sequence whose first item is to be returned
42 * @return the FirstItemExpression, or an equivalent
43 */
44
45 public static Expression makeFirstItemExpression(Expression base) {
46 if (base instanceof FirstItemExpression) {
47 return base;
48 } else {
49 return new FirstItemExpression(base);
50 }
51 }
52
53 /**
54 * Copy an expression. This makes a deep copy.
55 *
56 * @return the copy of the original expression
57 * @param rebindings
58 */
59
60 /*@NotNull*/
61 public Expression copy(RebindingMap rebindings) {
62 Expression e2 = new FirstItemExpression(getBaseExpression().copy(rebindings));
63 ExpressionTool.copyLocationInfo(this, e2);
64 return e2;
65 }
66
67 /**
68 * Convert this expression to an equivalent XSLT pattern
69 *
70 * @param config the Saxon configuration
71 * @return the equivalent pattern
72 * @throws net.sf.saxon.trans.XPathException
73 * if conversion is not possible
74 */
75 @Override
76 public Pattern toPattern(Configuration config) throws XPathException {
77 Pattern basePattern = getBaseExpression().toPattern(config);
78 ItemType type = basePattern.getItemType();
79 if (type instanceof NodeTest) {
80 Expression baseExpr = getBaseExpression();
81 if (baseExpr instanceof AxisExpression &&
82 ((AxisExpression)baseExpr).getAxis() == AxisInfo.CHILD && basePattern instanceof NodeTestPattern) {
83 return new SimplePositionalPattern((NodeTest) type, 1);
84 } else {
85 return new GeneralNodePattern(this, (NodeTest) type);
86 }
87 } else {
88 // For a non-node pattern, the predicate [1] is always true
89 return basePattern;
90 }
91 }
92
93
94 /**
95 * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
96 * This method indicates which of these methods is provided directly. The other methods will always be available
97 * indirectly, using an implementation that relies on one of the other methods.
98 *
99 * @return the implementation method, for example {@link #ITERATE_METHOD} or {@link #EVALUATE_METHOD} or
100 * {@link #PROCESS_METHOD}
101 */
102 @Override
103 public int getImplementationMethod() {
104 return EVALUATE_METHOD;
105 }
106
107 /**
108 * Evaluate the expression
109 */
110
111 public Item evaluateItem(XPathContext context) throws XPathException {
112 SequenceIterator iter = getBaseExpression().iterate(context);
113 Item result = iter.next();
114 iter.close();
115 return result;
116 }
117
118 public String getExpressionName() {
119 return "first";
120 }
121
122 @Override
123 public String toShortString() {
124 return getBaseExpression().toShortString() + "[1]";
125 }
126
127 /**
128 * Get the (partial) name of a class that supports streaming of this kind of expression
129 *
130 * @return the partial name of a class that can be instantiated to provide streaming support in Saxon-EE,
131 * or null if there is no such class
132 */
133 @Override
134 public String getStreamerName() {
135 return "FirstItemExpression";
136 }
137
138 }
139