Debian Codegraph @

net/sf/saxon/expr/instruct/ParentNodeConstructor.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.expr.parser.PathMap;
15 import net.sf.saxon.lib.ParseOptions;
16 import net.sf.saxon.lib.Validation;
17 import net.sf.saxon.pattern.MultipleNodeKindTest;
18 import net.sf.saxon.pattern.NodeKindTest;
19 import net.sf.saxon.trans.XPathException;
20 import net.sf.saxon.type.ItemType;
21 import net.sf.saxon.type.SchemaType;
22 import net.sf.saxon.type.TypeHierarchy;
23 import net.sf.saxon.type.Untyped;
24 import net.sf.saxon.value.SequenceType;
25
26 /**
27 * An abstract class to act as a common parent for instructions that create element nodes
28 * and document nodes.
29 */
30
31 public abstract class ParentNodeConstructor extends Instruction
32 implements ValidatingInstruction, InstructionWithComplexContent {
33
34 private final static OperandRole SAME_FOCUS_CONTENT =
35 new OperandRole(0, OperandUsage.ABSORPTION, SequenceType.ANY_SEQUENCE);
36
37 protected Operand contentOp;
38 private boolean lazyConstruction = false;
39 private ParseOptions validationOptions = null;
40 private String baseURI;
41
42 /**
43 * Flag set to true if validation=preserve and no schema type supplied for validation; also true
44 * when validation="strip" if there is no need to physically strip type annotations
45 */
46
47 protected boolean preservingTypes = true;
48
49 /**
50 * Create a document or element node constructor instruction
51 */
52
53 public ParentNodeConstructor() {
54 }
55
56 /**
57 * Set the static base URI of the instruction
58 *
59 * @param uri the static base URI
60 */
61
62 public void setBaseURI(String uri) {
63 baseURI = uri;
64 }
65
66 /**
67 * Indicate that lazy construction should (or should not) be used. Note that
68 * this request will be ignored if validation is required
69 *
70 * @param lazy set to true if lazy construction should be used
71 */
72
73 public void setLazyConstruction(boolean lazy) {
74 lazyConstruction = lazy;
75 }
76
77 /**
78 * Establish whether lazy construction is to be used
79 *
80 * @return true if lazy construction is to be used
81 */
82
83 public final boolean isLazyConstruction() {
84 return lazyConstruction;
85 }
86
87 /**
88 * Get the schema type chosen for validation; null if not defined
89 *
90 * @return the type to be used for validation. (For a document constructor, this is the required
91 * type of the document element)
92 */
93
94 public SchemaType getSchemaType() {
95 return validationOptions == null ? null : validationOptions.getTopLevelType();
96 }
97
98 /**
99 * Get the validation options
100 *
101 * @return the validation options for the content of the constructed node. May be null if no
102 * validation was requested.
103 */
104
105 public ParseOptions getValidationOptions() {
106 return validationOptions;
107 }
108
109 /**
110 * Set the validation mode for the new document or element node
111 *
112 * @param mode the validation mode, for example {@link Validation#STRICT}
113 * @param schemaType the required type (for validation by type). Null if not
114 * validating by type
115 */
116
117
118 public void setValidationAction(int mode, /*@Nullable*/ SchemaType schemaType) {
119 preservingTypes = mode == Validation.PRESERVE && schemaType == null;
120 if (!preservingTypes) {
121 if (validationOptions == null) {
122 validationOptions = new ParseOptions();
123 }
124 if (schemaType == Untyped.getInstance()) {
125 validationOptions.setSchemaValidationMode(Validation.SKIP);
126 } else {
127 validationOptions.setSchemaValidationMode(mode);
128 validationOptions.setTopLevelType(schemaType);
129 }
130 }
131 }
132
133
134 /**
135 * Get the validation mode for this instruction
136 *
137 * @return the validation mode, for example {@link Validation#STRICT} or {@link Validation#PRESERVE}
138 */
139 public int getValidationAction() {
140 return validationOptions == null ? Validation.PRESERVE : validationOptions.getSchemaValidationMode();
141 }
142
143 /**
144 * Set that the newly constructed node and everything underneath it will automatically be untyped,
145 * without any need to physically remove type annotations, even though validation=STRIP is set.
146 */
147
148 public void setNoNeedToStrip() {
149 preservingTypes = true;
150 }
151
152 /**
153 * Set the expression that constructs the content of the element
154 *
155 * @param content the content expression
156 */
157
158 public void setContentExpression(Expression content) {
159 if (contentOp == null) {
160 contentOp = new Operand(this, content, SAME_FOCUS_CONTENT);
161 } else {
162 contentOp.setChildExpression(content);
163 }
164 }
165
166 /**
167 * Get the expression that constructs the content of the element
168 *
169 * @return the content expression
170 */
171
172 public Expression getContentExpression() {
173 return contentOp == null ? null : contentOp.getChildExpression();
174 }
175
176 public Operand getContentOperand() {
177 return contentOp;
178 }
179
180 /**
181 * Get the cardinality of the sequence returned by evaluating this instruction
182 *
183 * @return the static cardinality
184 */
185
186 public int computeCardinality() {
187 return StaticProperty.EXACTLY_ONE;
188 }
189
190 /*@NotNull*/
191 public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
192 typeCheckChildren(visitor, contextInfo);
193 checkContentSequence(visitor.getStaticContext());
194 return this;
195 }
196
197 /**
198 * Ask whether common subexpressions found in the operands of this expression can
199 * be extracted and evaluated outside the expression itself. The result is irrelevant
200 * in the case of operands evaluated with a different focus, which will never be
201 * extracted in this way, even if they have no focus dependency.
202 *
203 * @return false for this kind of expression
204 */
205 @Override
206 public boolean allowExtractingCommonSubexpressions() {
207 return false;
208 }
209
210 /**
211 * Check that the child instructions don't violate any obvious constraints for this kind of node
212 *
213 * @param env the static context
214 * @throws XPathException if the check fails
215 */
216
217 protected abstract void checkContentSequence(StaticContext env) throws XPathException;
218
219 /*@NotNull*/
220 public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException {
221 optimizeChildren(visitor, contextItemType);
222 if (!Literal.isEmptySequence(getContentExpression())) {
223 if (getContentExpression() instanceof Block) {
224 setContentExpression(((Block) getContentExpression()).mergeAdjacentTextInstructions());
225 }
226 // This code removed 25 Aug 2016. We no longer introduce copy operations. But we could go
227 // further, and try to get rid of more unnecessary copy operations (whether streaming or not...)
228 // Reinstated 14 Oct 2016
229 if (visitor.isOptimizeForStreaming()) {
230 visitor.obtainOptimizer().makeCopyOperationsExplicit(this, contentOp);
231 }
232 }
233 if (visitor.getStaticContext().getPackageData().isSchemaAware()) {
234 TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy();
235 if (getValidationAction() == Validation.STRIP) {
236 if (getContentExpression().hasSpecialProperty(StaticProperty.ALL_NODES_UNTYPED) ||
237 th.relationship(getContentExpression().getItemType(), MultipleNodeKindTest.DOC_ELEM_ATTR) == TypeHierarchy.DISJOINT) {
238 // No need to strip type annotations if there are none needing to be stripped
239 setNoNeedToStrip();
240 }
241 }
242 } else {
243 setNoNeedToStrip();
244 }
245 return this;
246 }
247
248
249 /**
250 * Determine whether this instruction creates new nodes.
251 * This implementation returns true.
252 */
253
254 @Override
255 public final boolean mayCreateNewNodes() {
256 return true;
257 }
258
259 /**
260 * Ask whether it is guaranteed that every item in the result of this instruction
261 * is a newly created node
262 *
263 * @return true if result of the instruction is always either an empty sequence or
264 * a sequence consisting entirely of newly created nodes
265 */
266
267 @Override
268 public boolean alwaysCreatesNewNodes() {
269 return true;
270 }
271
272 public int getCardinality() {
273 return StaticProperty.EXACTLY_ONE;
274 }
275
276 /**
277 * Add a representation of this expression to a PathMap. The PathMap captures a map of the nodes visited
278 * by an expression in a source tree.
279 * <p>The default implementation of this method assumes that an expression does no navigation other than
280 * the navigation done by evaluating its subexpressions, and that the subexpressions are evaluated in the
281 * same context as the containing expression. The method must be overridden for any expression
282 * where these assumptions do not hold. For example, implementations exist for AxisExpression, ParentExpression,
283 * and RootExpression (because they perform navigation), and for the doc(), document(), and collection()
284 * functions because they create a new navigation root. Implementations also exist for PathExpression and
285 * FilterExpression because they have subexpressions that are evaluated in a different context from the
286 * calling expression.</p>
287 *
288 * @param pathMap the PathMap to which the expression should be added
289 * @param pathMapNodeSet the PathMapNodeSet to which the paths embodied in this expression should be added
290 * @return the pathMapNodeSet representing the points in the source document that are both reachable by this
291 * expression, and that represent possible results of this expression. For an expression that does
292 * navigation, it represents the end of the arc in the path map that describes the navigation route. For other
293 * expressions, it is the same as the input pathMapNode.
294 */
295
296 public PathMap.PathMapNodeSet addToPathMap(PathMap pathMap, PathMap.PathMapNodeSet pathMapNodeSet) {
297 PathMap.PathMapNodeSet result = super.addToPathMap(pathMap, pathMapNodeSet);
298 result.setReturnable(false);
299 TypeHierarchy th = getConfiguration().getTypeHierarchy();
300 ItemType type = getItemType();
301 if (th.relationship(type, NodeKindTest.ELEMENT) != TypeHierarchy.DISJOINT ||
302 th.relationship(type, NodeKindTest.DOCUMENT) != TypeHierarchy.DISJOINT) {
303 result.addDescendants();
304 }
305 return new PathMap.PathMapNodeSet(pathMap.makeNewRoot(this));
306 }
307
308 /**
309 * Determine whether this elementCreator performs validation or strips type annotations
310 *
311 * @return false if the instruction performs validation of the constructed output or if it strips
312 * type annotations, otherwise true
313 */
314
315 public boolean isPreservingTypes() {
316 return preservingTypes;
317 }
318
319 public boolean isLocal() {
320 return ExpressionTool.isLocalConstructor(this);
321 }
322 }
323
324