Debian Codegraph @

net/sf/saxon/expr/instruct/UseAttributeSet.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.ExpressionVisitor;
13 import net.sf.saxon.expr.parser.RebindingMap;
14 import net.sf.saxon.lib.NamespaceConstant;
15 import net.sf.saxon.om.StandardNames;
16 import net.sf.saxon.om.StructuredQName;
17 import net.sf.saxon.pattern.NodeKindTest;
18 import net.sf.saxon.style.StyleElement;
19 import net.sf.saxon.trace.ExpressionPresenter;
20 import net.sf.saxon.trans.SymbolicName;
21 import net.sf.saxon.trans.XPathException;
22 import net.sf.saxon.type.ItemType;
23
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27
28 /**
29 * This instruction corresponds to a single QName appearing within a use-attribute-sets attribute on a
30 * literal result element, xsl:element, or xsl:copy instruction. Binding to a specific attribute set
31 * component is done in the same way as function calls or call-template, allowing attribute sets defined
32 * in one package to be overridden in another.
33 */
34 public class UseAttributeSet extends Instruction implements ComponentInvocation, ContextOriginator {
35
36 private StructuredQName targetName;
37 private AttributeSet target;
38 private boolean isDeclaredStreamable;
39 private int bindingSlot = -1;
40
41 /**
42 * Create a use-attribute-set expression
43 *
44 * @param name the name of the target attribute set
45 */
46
47 public UseAttributeSet(StructuredQName name, boolean streamable) {
48 this.targetName = name;
49 this.isDeclaredStreamable = streamable;
50 }
51
52 @Override
53 public boolean isInstruction() {
54 return false;
55 }
56
57 /**
58 * Make an expression whose effect is to expand the attribute sets named in an [xsl]use-attribute-sets
59 * attribute, for example on a literal result element
60 *
61 * @param targets the QNames contained in the use-attribute-sets attribute
62 * @param instruction the instruction on which the use-attribute-sets attribute appears
63 * @return the required expression
64 * @throws XPathException if an error occurs, for example no attribute set found with the required name
65 */
66
67 public static Expression makeUseAttributeSets(StructuredQName[] targets, StyleElement instruction) throws XPathException {
68 List<UseAttributeSet> list = makeUseAttributeSetInstructions(targets, instruction);
69 return makeCompositeExpression(list);
70 }
71
72 /**
73 * Make an list of expressions whose combined effect is to expand the attribute sets named in an
74 * [xsl]use-attribute-sets attribute, for example on a literal result element
75 *
76 * @param targets the QNames contained in the use-attribute-sets attribute
77 * @param instruction the instruction on which the use-attribute-sets attribute appears
78 * @return the required expression
79 * @throws XPathException if an error occurs, for example no attribute set found with the required name
80 */
81
82 public static List<UseAttributeSet> makeUseAttributeSetInstructions(StructuredQName[] targets, StyleElement instruction) throws XPathException {
83 List<UseAttributeSet> list = new ArrayList<>(targets.length);
84 for (StructuredQName name : targets) {
85 UseAttributeSet use = makeUseAttributeSet(name, instruction);
86 if (use != null) {
87 list.add(use);
88 }
89 }
90 return list;
91 }
92
93 /**
94 * Given a list of UseAttributeSet expressions, combine them into a single expression
95 *
96 * @param targets the list of expressions
97 * @return the combined expression
98 */
99
100 public static Expression makeCompositeExpression(List<UseAttributeSet> targets) {
101 if (targets.size() == 0) {
102 return Literal.makeEmptySequence();
103 } else if (targets.size() == 1) {
104 return targets.get(0);
105 } else {
106 return new Block(targets.toArray(new Expression[0]));
107 }
108 }
109
110 /**
111 * Make a UseAttributeSet expression corresponding to a single attribute-set name
112 *
113 * @param name the name of the attribute-set to be expanded
114 * @param instruction the containing instruction
115 * @return a UseAttributeSet expression whose effect is to expand this attribute-set
116 * @throws XPathException if a dynamic error occurs
117 */
118
119 private static UseAttributeSet makeUseAttributeSet(StructuredQName name, StyleElement instruction) throws XPathException {
120 AttributeSet target;
121 if (name.hasURI(NamespaceConstant.XSLT) && name.getLocalPart().equals("original")) {
122 target = (AttributeSet) instruction.getXslOriginal(StandardNames.XSL_ATTRIBUTE_SET);
123 } else {
124 Component invokee = instruction.getContainingPackage().getComponent(new SymbolicName(StandardNames.XSL_ATTRIBUTE_SET, name));
125 instruction.getPrincipalStylesheetModule().getAttributeSetDeclarations(name);
126 if (invokee == null) {
127 instruction.compileError("Unknown attribute set " + name.getEQName(), "XTSE0710");
128 return null; // to prevent compile warnings
129 }
130 target = (AttributeSet) invokee.getActor();
131 }
132 UseAttributeSet invocation = new UseAttributeSet(name, target.isDeclaredStreamable());
133 invocation.setTarget(target);
134 invocation.setBindingSlot(-1);
135 invocation.setRetainedStaticContext(instruction.makeRetainedStaticContext());
136 return invocation;
137 }
138
139 public boolean isDeclaredStreamable() {
140 return isDeclaredStreamable;
141 }
142
143 /**
144 * Set the attribute set to be used.
145 *
146 * @param target the attribute set to be used
147 */
148
149 public void setTarget(AttributeSet target) {
150 this.target = target;
151 }
152
153 /**
154 * Set the binding slot to be used. This is the offset within the binding vector of the containing
155 * component where the actual target component is to be found. The target template is not held directly
156 * in the invocation instruction/expression itself because it can be overridden in a using package.
157 *
158 * @param slot the offset in the binding vector of the containing package where the target component
159 * can be found.
160 */
161
162 public void setBindingSlot(int slot) {
163 bindingSlot = slot;
164 }
165
166 /**
167 * Get the binding slot to be used. This is the offset within the binding vector of the containing
168 * component where the actual target component is to be found.
169 *
170 * @return the offset in the binding vector of the containing package where the target component
171 * can be found.
172 */
173
174 public int getBindingSlot() {
175 return bindingSlot;
176 }
177
178 /**
179 * Get the symbolic name of the component that this invocation references
180 *
181 * @return the symbolic name of the target component
182 */
183
184 public SymbolicName getSymbolicName() {
185 return new SymbolicName(StandardNames.XSL_ATTRIBUTE_SET, targetName);
186 }
187
188 /**
189 * Get the target attribute set to which this instruction is provisionally bound
190 *
191 * @return the provisional attribute set (which might be overridden in another package)
192 */
193
194 public AttributeSet getTargetAttributeSet() {
195 return target;
196 }
197
198 public Component getFixedTarget() {
199 if (target != null && bindingSlot < 0) {
200 return target.getDeclaringComponent();
201 }
202 return null;
203 }
204
205
206 @Override
207 public Iterable<Operand> operands() {
208 return Collections.emptyList();
209 }
210
211 /**
212 * Perform optimisation of an expression and its subexpressions.
213 * <p>This method is called after all references to functions and variables have been resolved
214 * to the declaration of the function or variable, and after all type checking has been done.</p>
215 *
216 * @param visitor an expression visitor
217 * @param contextItemType the static type of "." at the point where this expression is invoked.
218 * The parameter is set to null if it is known statically that the context item will be undefined.
219 * If the type of the context item is not known statically, the argument is set to
220 * {@link net.sf.saxon.type.Type#ITEM_TYPE}
221 * @return the original expression, rewritten if appropriate to optimize execution
222 * @throws XPathException if an error is discovered during this phase
223 * (typically a type error)
224 */
225
226 /*@NotNull*/
227 public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException {
228 return this;
229 }
230
231
232 /**
233 * Copy an expression. This makes a deep copy.
234 *
235 * @param rebindings a mutable list of (old binding, new binding) pairs
236 * that is used to update the bindings held in any
237 * local variable references that are copied.
238 * @return the copy of the original expression
239 */
240
241 /*@NotNull*/
242 public Expression copy(RebindingMap rebindings) {
243 UseAttributeSet ua = new UseAttributeSet(targetName, isDeclaredStreamable);
244 ua.setTarget(target);
245 ua.setBindingSlot(bindingSlot);
246 return ua;
247 }
248
249 /**
250 * Perform type checking of an expression and its subexpressions.
251 * <p>This checks statically that the operands of the expression have
252 * the correct type; if necessary it generates code to do run-time type checking or type
253 * conversion. A static type error is reported only if execution cannot possibly succeed, that
254 * is, if a run-time type error is inevitable. The call may return a modified form of the expression.</p>
255 * <p>This method is called after all references to functions and variables have been resolved
256 * to the declaration of the function or variable. However, the types of such functions and
257 * variables may not be accurately known if they have not been explicitly declared.</p>
258 *
259 * @param visitor an expression visitor
260 * @param contextInfo information about the static type of the context item
261 * @return the original expression, rewritten to perform necessary
262 * run-time type checks, and to perform other type-related
263 * optimizations
264 * @throws XPathException if an error is discovered during this phase
265 * (typically a type error)
266 */
267
268 /*@NotNull*/
269 public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
270 return this;
271 }
272
273 /**
274 * Get the item type of the items returned by evaluating this instruction
275 *
276 * @return the static item type of the instruction
277 */
278
279 /*@NotNull*/
280 public ItemType getItemType() {
281 return NodeKindTest.ATTRIBUTE;
282 }
283
284
285 /**
286 * Determine the intrinsic dependencies of an expression, that is, those which are not derived
287 * from the dependencies of its subexpressions. For example, position() has an intrinsic dependency
288 * on the context position, while (position()+1) does not. The default implementation
289 * of the method returns 0, indicating "no dependencies".
290 *
291 * @return a set of bit-significant flags identifying the "intrinsic"
292 * dependencies. The flags are documented in class net.sf.saxon.value.StaticProperty
293 */
294
295 public int getIntrinsicDependencies() {
296 // we have to assume the worst; the target attribute set could be overridden by one that has focus dependencies
297 return StaticProperty.DEPENDS_ON_XSLT_CONTEXT | StaticProperty.DEPENDS_ON_FOCUS;
298 }
299
300 /**
301 * Get the target attribute sets of this instruction. Called from generated bytecode.
302 *
303 * @return the target attribute sets, as an array
304 */
305
306 public StructuredQName getTargetAttributeSetName() {
307 return targetName;
308 }
309
310 /**
311 * ProcessLeavingTail: called to do the real work of this instruction. This method
312 * must be implemented in each subclass. The results of the instruction are written
313 * to the current Receiver, which can be obtained via the Controller.
314 *
315 * @param context The dynamic context of the transformation, giving access to the current node,
316 * the current variables, etc.
317 * @return null if the instruction has completed execution; or a TailCall indicating
318 * a function call or template call that is delegated to the caller, to be made after the stack has
319 * been unwound so as to save stack space.
320 */
321
322 /*@Nullable*/
323 public TailCall processLeavingTail(XPathContext context) throws XPathException {
324 Component target;
325
326 if (bindingSlot < 0) {
327 target = getFixedTarget();
328 } else {
329 target = context.getTargetComponent(bindingSlot);
330 if (target.isHiddenAbstractComponent()) {
331 XPathException err = new XPathException("Cannot expand an abstract attribute set ("
332 + targetName.getDisplayName()
333 + ") with no implementation", "XTDE3052");
334 err.setLocation(getLocation());
335 throw err;
336 }
337 }
338 if (target == null) {
339 throw new AssertionError("Failed to locate attribute set " + getTargetAttributeSetName().getEQName());
340 }
341 AttributeSet as = (AttributeSet) target.getActor();
342 XPathContextMajor c2 = context.newContext();
343 c2.setCurrentComponent(target);
344 c2.setOrigin(this);
345 SlotManager sm = as.getStackFrameMap();
346 if (sm == null) {
347 sm = SlotManager.EMPTY;
348 }
349 c2.openStackFrame(sm);
350 as.expand(c2);
351 return null;
352 }
353
354 /**
355 * Get a name identifying the kind of expression, in terms meaningful to a user.
356 *
357 * @return a name identifying the kind of expression, in terms meaningful to a user.
358 * The name will always be in the form of a lexical XML QName, and should match the name used
359 * in explain() output displaying the expression.
360 */
361 @Override
362 public String getExpressionName() {
363 return "useAS";
364 }
365
366 /**
367 * Export of expression structure. The abstract expression tree
368 * is written to the supplied output destination.
369 */
370
371 public void export(ExpressionPresenter out) throws XPathException {
372 out.startElement("useAS", this);
373 out.emitAttribute("name", targetName);
374 out.emitAttribute("bSlot", "" + getBindingSlot());
375 if (isDeclaredStreamable()) {
376 out.emitAttribute("flags", "s");
377 }
378 out.endElement();
379 }
380
381 /**
382 * Test whether this UseAttributeSets expression is equal to another
383 *
384 * @param obj the other expression
385 */
386
387 public boolean equals(Object obj) {
388 if (!(obj instanceof UseAttributeSet)) {
389 return false;
390 }
391 return targetName.equals(((UseAttributeSet) obj).targetName);
392 }
393
394 /**
395 * Compute a hashcode
396 */
397
398 public int computeHashCode() {
399 return 0x86423719 ^ targetName.hashCode();
400 }
401
402
403 /**
404 * Get the (partial) name of a class that supports streaming of this kind of expression
405 *
406 * @return the partial name of a class that can be instantiated to provide streaming support in Saxon-EE,
407 * or null if there is no such class
408 */
409 @Override
410 public String getStreamerName() {
411 return "UseAttributeSet";
412 }
413 }
414