Debian Codegraph @

net/sf/saxon/expr/instruct/WherePopulated.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.instruct;
9
10 import net.sf.saxon.expr.*;
11 import net.sf.saxon.expr.parser.RebindingMap;
12 import net.sf.saxon.ma.map.MapItem;
13 import net.sf.saxon.om.Item;
14 import net.sf.saxon.om.NodeInfo;
15 import net.sf.saxon.om.SequenceIterator;
16 import net.sf.saxon.trace.ExpressionPresenter;
17 import net.sf.saxon.trans.XPathException;
18 import net.sf.saxon.type.Type;
19 import net.sf.saxon.value.Base64BinaryValue;
20 import net.sf.saxon.value.HexBinaryValue;
21 import net.sf.saxon.value.StringValue;
22
23 /**
24 * A compiled xsl:where-populated instruction (formerly xsl:conditional-content).
25 */
26 public class WherePopulated extends UnaryExpression implements ItemMappingFunction<Item<?>, Item<?>> {
27
28
29 /**
30 * Create the instruction
31 */
32 public WherePopulated(Expression base) {
33 super(base);
34 }
35
36 /**
37 * Ask whether this expression is an instruction. In XSLT streamability analysis this
38 * is used to distinguish constructs corresponding to XSLT instructions from other constructs.
39 *
40 * @return true if this construct originates as an XSLT instruction
41 */
42 @Override
43 public boolean isInstruction() {
44 return true;
45 }
46
47 /**
48 * Get the usage (in terms of streamability analysis) of the single operand
49 *
50 * @return the operand usage
51 */
52 @Override
53 protected OperandRole getOperandRole() {
54 return new OperandRole(0, OperandUsage.TRANSMISSION);
55 }
56
57 /*@NotNull*/
58 public Expression copy(RebindingMap rebindings) {
59 return new WherePopulated(getBaseExpression().copy(rebindings));
60 }
61
62 /**
63 * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
64 * This method indicates which of these methods is provided directly. The other methods will always be available
65 * indirectly, using an implementation that relies on one of the other methods.
66 *
67 * @return the implementation method, for example {@link #ITERATE_METHOD} or {@link #EVALUATE_METHOD} or
68 * {@link #PROCESS_METHOD}
69 */
70 @Override
71 public int getImplementationMethod() {
72 return ITERATE_METHOD;
73 }
74
75 @Override
76 public int computeCardinality() {
77 return super.computeCardinality() | StaticProperty.ALLOWS_ZERO;
78 }
79
80 /**
81 * Return an Iterator to iterate over the values of a sequence. The value of every
82 * expression can be regarded as a sequence, so this method is supported for all
83 * expressions.
84 *
85 * @param context supplies the context for evaluation
86 * @return a SequenceIterator that can be used to iterate over the result
87 * of the expression
88 * @throws net.sf.saxon.trans.XPathException if any dynamic error occurs evaluating the
89 * expression
90 */
91 @Override
92 public SequenceIterator<?> iterate(XPathContext context) throws XPathException {
93 return new ItemMappingIterator(getBaseExpression().iterate(context), this);
94 }
95
96 /**
97 * Map one item to another item.
98 *
99 * @param item The input item to be mapped.
100 * @return either the output item, or null.
101 * @throws net.sf.saxon.trans.XPathException if a dynamic error occurs
102 */
103 public Item mapItem(Item item) throws XPathException {
104 return isDeemedEmpty(item) ? null : item;
105 }
106
107 /**
108 * Test whether an item is empty according to the definition in the spec
109 * for xsl:conditional-content
110 * @param item the item to be tested
111 * @return true if it is "empty", for example, an element without children
112 */
113
114 public static boolean isDeemedEmpty(Item item) {
115 if (item instanceof NodeInfo) {
116 int kind = ((NodeInfo) item).getNodeKind();
117 switch (kind) {
118 case Type.DOCUMENT:
119 case Type.ELEMENT:
120 return !((NodeInfo) item).hasChildNodes();
121 default:
122 return item.getStringValueCS().length() == 0;
123 }
124 } else if (item instanceof StringValue || item instanceof HexBinaryValue || item instanceof Base64BinaryValue) {
125 return item.getStringValueCS().length() == 0;
126 } else if (item instanceof MapItem) {
127 return ((MapItem)item).isEmpty();
128 } else {
129 return false;
130 }
131 }
132
133 /**
134 * Get a name identifying the kind of expression, in terms meaningful to a user.
135 *
136 * @return a name identifying the kind of expression, in terms meaningful to a user.
137 * The name will always be in the form of a lexical XML QName, and should match the name used
138 * in export() output displaying the expression.
139 */
140 @Override
141 public String getExpressionName() {
142 return "condCont";
143 }
144
145
146
147 public void export(ExpressionPresenter out) throws XPathException {
148 out.startElement("condCont", this);
149 getBaseExpression().export(out);
150 out.endElement();
151 }
152
153 /**
154 * Get the (partial) name of a class that supports streaming of this kind of expression
155 *
156 * @return the partial name of a class that can be instantiated to provide streaming support in Saxon-EE,
157 * or null if there is no such class
158 */
159 @Override
160 public String getStreamerName() {
161 return "WherePopulated";
162 }
163 }
164