Debian Codegraph @

net/sf/saxon/expr/instruct/SimpleNodeConstructor.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.ContextItemStaticInfo;
12 import net.sf.saxon.expr.parser.ExpressionTool;
13 import net.sf.saxon.expr.parser.ExpressionVisitor;
14 import net.sf.saxon.functions.String_1;
15 import net.sf.saxon.om.Item;
16 import net.sf.saxon.om.NodeName;
17 import net.sf.saxon.om.SequenceIterator;
18 import net.sf.saxon.trans.XPathException;
19 import net.sf.saxon.tree.iter.SingletonIterator;
20 import net.sf.saxon.tree.util.Orphan;
21 import net.sf.saxon.type.BuiltInAtomicType;
22 import net.sf.saxon.type.TypeHierarchy;
23 import net.sf.saxon.value.Cardinality;
24
25 /**
26 * Common superclass for XSLT instructions whose content template produces a text
27 * value: xsl:attribute, xsl:comment, xsl:processing-instruction, xsl:namespace,
28 * and xsl:text, and their XQuery equivalents
29 */
30
31 public abstract class SimpleNodeConstructor extends Instruction {
32
33 protected Operand selectOp;
34
35 /**
36 * Default constructor used by subclasses
37 */
38
39 public SimpleNodeConstructor() {
40 Expression select = Literal.makeEmptySequence();
41 selectOp = new Operand(this, select, OperandRole.SINGLE_ATOMIC);
42 }
43
44 /**
45 * Set the select expression: the value of this expression determines the string-value of the node
46 *
47 * @param select the expression that computes the string value of the node
48 *
49 */
50
51 public void setSelect(Expression select) {
52 selectOp.setChildExpression(select);
53 }
54
55 /**
56 * Get the select expression, that is the expression that computes the content of the constructed node.
57 * Note that this may correspond either to a select attribute or a contained sequence constructor
58 * in the case of XSLT; and in XQuery it corresponds to whatever expression is contained in the node
59 * constructpr.
60 * @return the expression used to compute the content of the node.
61 */
62
63 public Expression getSelect() {
64 return selectOp.getChildExpression();
65 }
66
67 @Override
68 public Iterable<Operand> operands() {
69 return selectOp;
70 }
71
72 /**
73 * Ask whether this instruction creates new nodes.
74 * This implementation returns true.
75 */
76
77 @Override
78 public final boolean mayCreateNewNodes() {
79 return true;
80 }
81
82 /**
83 * Ask whether it is guaranteed that every item in the result of this instruction
84 * is a newly created node
85 *
86 * @return true if result of the instruction is always either an empty sequence or
87 * a sequence consisting entirely of newly created nodes
88 */
89
90 @Override
91 public boolean alwaysCreatesNewNodes() {
92 return true;
93 }
94
95 /**
96 * Get the cardinality of the sequence returned by evaluating this instruction
97 *
98 * @return the static cardinality
99 */
100
101 public int computeCardinality() {
102 return getSelect().getCardinality(); // may allow empty sequence
103 }
104
105 /**
106 * Get the static properties of this expression (other than its type). The result is
107 * bit-signficant. These properties are used for optimizations. In general, if
108 * property bit is set, it is true, but if it is unset, the value is unknown.
109 *
110 * @return a set of flags indicating static properties of this expression
111 */
112
113 public int computeSpecialProperties() {
114 return super.computeSpecialProperties() |
115 StaticProperty.SINGLE_DOCUMENT_NODESET;
116 }
117
118 /**
119 * Ask whether common subexpressions found in the operands of this expression can
120 * be extracted and evaluated outside the expression itself. The result is irrelevant
121 * in the case of operands evaluated with a different focus, which will never be
122 * extracted in this way, even if they have no focus dependency.
123 *
124 * @return false for this kind of expression
125 */
126 @Override
127 public boolean allowExtractingCommonSubexpressions() {
128 return false;
129 }
130
131 /**
132 * Method to perform type-checking specific to the kind of instruction
133 *
134 * @param visitor an expression visitor
135 * @param contextItemType the static type of the context item
136 * @throws XPathException if a type error is detected
137 */
138
139 public abstract void localTypeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException;
140
141 /**
142 * The typeCheck() method is called in XQuery, where node constructors
143 * are implemented as Expressions. In this case the required type for the
144 * select expression is a single string.
145 *
146 *
147 param visitor an expression visitor
148 * @param contextInfo information about the dynamic context
149 * @return the rewritten expression
150 * @throws XPathException if any static errors are found in this expression
151 * or any of its children
152 */
153
154 /*@NotNull*/
155 public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
156 typeCheckChildren(visitor, contextInfo);
157 localTypeCheck(visitor, contextInfo);
158
159 final TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy();
160 if (getSelect() instanceof ValueOf) {
161 Expression valSelect = ((ValueOf) getSelect()).getSelect();
162 if (th.isSubType(valSelect.getItemType(), BuiltInAtomicType.STRING) &&
163 !Cardinality.allowsMany(valSelect.getCardinality())) {
164 setSelect(valSelect);
165 }
166 }
167
168 // Don't bother converting untypedAtomic to string
169 if (getSelect().isCallOn(String_1.class)) {
170 SystemFunctionCall fn = (SystemFunctionCall) getSelect();
171 Expression arg = fn.getArg(0);
172 if (arg.getItemType() == BuiltInAtomicType.UNTYPED_ATOMIC && !Cardinality.allowsMany(arg.getCardinality())) {
173 setSelect(arg);
174 }
175 } else if (getSelect() instanceof CastExpression && ((CastExpression) getSelect()).getTargetType() == BuiltInAtomicType.STRING) {
176 Expression arg = ((CastExpression) getSelect()).getBaseExpression();
177 if (arg.getItemType() == BuiltInAtomicType.UNTYPED_ATOMIC && !Cardinality.allowsMany(arg.getCardinality())) {
178 setSelect(arg);
179 }
180 }
181 adoptChildExpression(getSelect());
182 return this;
183 }
184
185 /*@NotNull*/
186 public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException {
187 optimizeChildren(visitor, contextItemType);
188 if (getSelect().isCallOn(String_1.class)) {
189 SystemFunctionCall sf = (SystemFunctionCall) getSelect();
190 TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy();
191 if (th.isSubType(sf.getArg(0).getItemType(), BuiltInAtomicType.STRING) &&
192 !Cardinality.allowsMany(sf.getArg(0).getCardinality())) {
193 setSelect(sf.getArg(0));
194 }
195 }
196 return this;
197 }
198
199 /**
200 * Get the (partial) name of a class that supports streaming of this kind of expression
201 *
202 * @return the partial name of a class that can be instantiated to provide streaming support in Saxon-EE,
203 * or null if there is no such class
204 */
205 @Override
206 public String getStreamerName() {
207 return "SimpleNodeConstructor";
208 }
209
210 /**
211 * Process this instruction
212 *
213 * @param context the dynamic context of the transformation
214 * @return a TailCall to be executed by the caller, always null for this instruction
215 */
216
217 public TailCall processLeavingTail(XPathContext context) throws XPathException {
218 CharSequence value = getSelect().evaluateAsString(context);
219 try {
220 processValue(value, context);
221 } catch (XPathException e) {
222 e.maybeSetLocation(getLocation());
223 throw e;
224 }
225 return null;
226 }
227
228
229 /**
230 * Process the value of the node, to create the new node.
231 *
232 * @param value the string value of the new node
233 * @param context the dynamic evaluation context
234 * @throws XPathException if a dynamic error occurs
235 */
236
237 public abstract void processValue(CharSequence value, XPathContext context) throws XPathException;
238
239 /**
240 * Evaluate as an expression.
241 */
242
243 public Item<?> evaluateItem(XPathContext context) throws XPathException {
244 Item<?> contentItem = getSelect().evaluateItem(context);
245 String content;
246 if (contentItem == null) {
247 content = "";
248 } else {
249 content = contentItem.getStringValue();
250 content = checkContent(content, context);
251 }
252 Orphan o = new Orphan(context.getConfiguration());
253 o.setNodeKind((short) getItemType().getPrimitiveType());
254 o.setStringValue(content);
255 o.setNodeName(evaluateNodeName(context));
256 return o;
257 }
258
259 /**
260 * Check the content of the node, and adjust it if necessary. The checks depend on the node kind.
261 *
262 * @param data the supplied content
263 * @param context the dynamic context
264 * @return the original content, unless adjustments are needed
265 * @throws XPathException if the content is invalid
266 */
267
268 protected String checkContent(String data, XPathContext context) throws XPathException {
269 return data;
270 }
271
272 /**
273 * Run-time method to compute the name of the node being constructed. This is overridden
274 * for nodes that have a name. The default implementation returns -1, which is suitable for
275 * unnamed nodes such as comments
276 *
277 * @param context the XPath dynamic evaluation context
278 * @return the name pool nameCode identifying the name of the constructed node
279 * @throws XPathException if any failure occurs
280 */
281
282 public NodeName evaluateNodeName(XPathContext context) throws XPathException {
283 return null;
284 }
285
286 /*@NotNull*/
287 public SequenceIterator<?> iterate(XPathContext context) throws XPathException {
288 return SingletonIterator.makeIterator(evaluateItem(context));
289 }
290
291 public boolean isLocal() {
292 return ExpressionTool.isLocalConstructor(this);
293 }
294
295
296 }
297