Debian Codegraph @

net/sf/saxon/style/XSLAttributeSet.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.style;
9
10 import net.sf.saxon.expr.Component;
11 import net.sf.saxon.expr.Expression;
12 import net.sf.saxon.expr.instruct.AttributeSet;
13 import net.sf.saxon.expr.instruct.SlotManager;
14 import net.sf.saxon.expr.instruct.UseAttributeSet;
15 import net.sf.saxon.expr.parser.Optimizer;
16 import net.sf.saxon.lib.NamespaceConstant;
17 import net.sf.saxon.om.*;
18 import net.sf.saxon.pattern.NodeKindTest;
19 import net.sf.saxon.trace.LocationKind;
20 import net.sf.saxon.trans.SymbolicName;
21 import net.sf.saxon.trans.Visibility;
22 import net.sf.saxon.trans.XPathException;
23 import net.sf.saxon.tree.AttributeLocation;
24 import net.sf.saxon.tree.iter.AxisIterator;
25 import net.sf.saxon.value.Whitespace;
26
27 import java.util.ArrayList;
28 import java.util.List;
29
30 /**
31 * An xsl:attribute-set element in the stylesheet. <br>
32 */
33
34 public class XSLAttributeSet extends StyleElement implements StylesheetComponent {
35
36 private String nameAtt;
37 // the name of the attribute set as written
38
39 /*@Nullable*/ private String useAtt;
40 // the value of the use-attribute-sets attribute, as supplied
41
42 private String visibilityAtt;
43 // the value of the visibility attribute, trimmed
44
45 private SlotManager stackFrameMap;
46 // needed if variables are used
47
48 private List<ComponentDeclaration> attributeSetElements = new ArrayList<ComponentDeclaration>();
49 // list of Declarations of XSLAttributeSet objects referenced by this one, within the same package
50
51 private StructuredQName[] useAttributeSetNames;
52
53 private List<Expression> containedInstructions = new ArrayList<Expression>();
54 // the compiled form of this attribute set
55
56 private boolean validated = false;
57 private Visibility visibility;
58 private boolean streamable = false;
59
60 /**
61 * Get the corresponding Procedure object that results from the compilation of this
62 * StylesheetProcedure
63 */
64 public AttributeSet getActor() {
65 return (AttributeSet)getPrincipalStylesheetModule().getStylesheetPackage().getComponent(
66 new SymbolicName(StandardNames.XSL_ATTRIBUTE_SET, getObjectName())).getActor();
67 }
68
69 public SymbolicName getSymbolicName() {
70 return new SymbolicName(StandardNames.XSL_ATTRIBUTE_SET, getObjectName());
71 }
72
73 /**
74 * Check the compatibility of this component with another component that it is overriding
75 *
76 * @param component the overridden component
77 * @throws XPathException if the components are not compatible (differing signatures)
78 */
79
80 public void checkCompatibility(Component component) throws XPathException {
81 if (((AttributeSet)component.getActor()).isDeclaredStreamable() && !isDeclaredStreamable()) {
82 compileError("The overridden attribute set is declared streamable, " +
83 "so the overriding attribute set must also be declared streamable");
84 }
85 }
86
87 /**
88 * Ask whether this node is a declaration, that is, a permitted child of xsl:stylesheet
89 * (including xsl:include and xsl:import).
90 *
91 * @return true for this element
92 */
93
94 @Override
95 public boolean isDeclaration() {
96 return true;
97 }
98
99 /**
100 * Get the name of this attribute set
101 *
102 * @return the name of the attribute set, as a QName
103 */
104
105 public StructuredQName getAttributeSetName() {
106 return getObjectName();
107 }
108
109 /**
110 * Ask whether the attribute set is declared streamable
111 * @return true if the attribute streamable="yes" is present
112 */
113
114 public boolean isDeclaredStreamable() {
115 return streamable;
116 }
117
118
119 public void prepareAttributes() throws XPathException {
120 useAtt = null;
121 String streamableAtt = null;
122
123 AttributeCollection atts = getAttributeList();
124
125 for (int a = 0; a < atts.getLength(); a++) {
126 String f = atts.getQName(a);
127 if (f.equals("name")) {
128 nameAtt = Whitespace.trim(atts.getValue(a));
129 } else if (f.equals("use-attribute-sets")) {
130 useAtt = atts.getValue(a);
131 } else if (f.equals("streamable")) {
132 streamableAtt = atts.getValue(a);
133 } else if (f.equals("visibility")) {
134 visibilityAtt = Whitespace.trim(atts.getValue(a));
135 } else {
136 checkUnknownAttribute(atts.getNodeName(a));
137 }
138 }
139
140 if (nameAtt == null) {
141 reportAbsence("name");
142 setObjectName(new StructuredQName("", "", "attribute-set-error-name"));
143 return;
144 }
145
146 if (visibilityAtt == null) {
147 visibility = Visibility.PRIVATE;
148 } else {
149 visibility = interpretVisibilityValue(visibilityAtt, "");
150 }
151
152 if (streamableAtt != null) {
153 streamable = processStreamableAtt(streamableAtt);
154 }
155
156 try {
157 setObjectName(makeQName(nameAtt));
158 } catch (XPathException err) {
159 setObjectName(new StructuredQName("", "", "attribute-set-error-name"));
160 err.setLocation(new AttributeLocation(this, StructuredQName.fromClarkName("name")));
161 throw err;
162 }
163
164 }
165
166 /**
167 * Get a name identifying the object of the expression, for example a function name, template name,
168 * variable name, key name, element name, etc. This is used only where the name is known statically.
169 * If there is no name, the value will be null.
170 *
171 * @return the name of the object declared in this element, if any
172 */
173
174 public StructuredQName getObjectName() {
175 StructuredQName o = super.getObjectName();
176 if (o == null) {
177 try {
178 prepareAttributes();
179 o = getObjectName();
180 } catch (XPathException err) {
181 o = new StructuredQName("saxon", NamespaceConstant.SAXON, "badly-named-attribute-set");
182 setObjectName(o);
183 }
184 }
185 return o;
186 }
187
188 public void validate(ComponentDeclaration decl) throws XPathException {
189
190 if (validated) {
191 return;
192 }
193
194 checkTopLevel("XTSE0010", true);
195
196 stackFrameMap = getConfiguration().makeSlotManager();
197
198 AxisIterator kids = iterateAxis(AxisInfo.CHILD);
199 NodeInfo child;
200 while ((child = kids.next()) != null) {
201 if (child instanceof XSLAttribute) {
202 if (visibility == Visibility.ABSTRACT) {
203 compileError("An abstract attribute-set must contain no xsl:attribute instructions");
204 }
205 } else {
206 compileError("Only xsl:attribute is allowed within xsl:attribute-set", "XTSE0010");
207 }
208 }
209
210 if (useAtt != null) {
211 if (visibility == Visibility.ABSTRACT) {
212 compileError("An abstract attribute-set must have no @use-attribute-sets attribute");
213 }
214
215 // identify any attribute sets that this one refers to
216
217 useAttributeSetNames = getUsedAttributeSets(useAtt);
218 }
219
220 validated = true;
221 }
222
223 public StructuredQName[] getUseAttributeSetNames() {
224 return useAttributeSetNames;
225 }
226
227 public void index(ComponentDeclaration decl, PrincipalStylesheetModule top) throws XPathException {
228 top.indexAttributeSet(decl);
229 }
230
231
232 /**
233 * Check for circularity: specifically, check that this attribute set does not contain
234 * a direct or indirect reference to the one supplied as a parameter
235 *
236 * @param origin the place from which the search started
237 * @throws net.sf.saxon.trans.XPathException
238 * if an error is found
239 */
240
241 public void checkCircularity(XSLAttributeSet origin) throws XPathException {
242 if (this == origin) {
243 compileError("The definition of the attribute set is circular", "XTSE0720");
244 } else {
245 if (!validated) {
246 // if this attribute set isn't validated yet, we don't check it.
247 // The circularity will be detected when the last attribute set in the cycle
248 // gets validated
249 return;
250 }
251 if (attributeSetElements != null) {
252 for (ComponentDeclaration attributeSetElement : attributeSetElements) {
253 XSLAttributeSet element = (XSLAttributeSet) attributeSetElement.getSourceElement();
254 element.checkCircularity(origin);
255 if (streamable && !element.streamable) {
256 compileError("Attribute-set is declared streamable but references a non-streamable attribute set " +
257 element.getAttributeSetName().getDisplayName(), "XTSE3430");
258 }
259 }
260 }
261 }
262 }
263
264 /**
265 * Get the contained xsl:attribute instructions, in compiled form
266 * @return the list of contained instructions. This does not include any xsl:use-attribute-set references.
267 */
268
269 public List<Expression> getContainedInstructions() {
270 return containedInstructions;
271 }
272
273 /**
274 * Get details of stack frame
275 */
276
277 public SlotManager getSlotManager() {
278 return stackFrameMap;
279 }
280
281 /**
282 * Compile the attribute set
283 *
284 * @param compilation the current compilation episode
285 * @param decl this attribute set declaration
286 * @throws XPathException if a failure is detected
287 */
288 public void compileDeclaration(Compilation compilation, ComponentDeclaration decl) throws XPathException {
289
290 if (isActionCompleted(ACTION_COMPILE)) {
291 return;
292 }
293
294 if (useAtt != null) {
295
296 // identify any attribute sets that this one refers to
297
298 List<UseAttributeSet> invocations = UseAttributeSet.makeUseAttributeSetInstructions(useAttributeSetNames, this);
299 if (!invocations.isEmpty()) {
300 containedInstructions.add(UseAttributeSet.makeCompositeExpression(invocations));
301 }
302
303 // check for circularity, to the extent possible within a single package
304
305 for (StructuredQName name : useAttributeSetNames) {
306 getPrincipalStylesheetModule().getAttributeSets(name, attributeSetElements);
307 }
308
309 for (ComponentDeclaration attributeSetElement : attributeSetElements) {
310 ((XSLAttributeSet) attributeSetElement.getSourceElement()).checkCircularity(this);
311 }
312
313 // check for consistency of streamability attribute
314
315 if (streamable) {
316 for (ComponentDeclaration attributeSetElement : attributeSetElements) {
317 if (!((XSLAttributeSet) attributeSetElement.getSourceElement()).streamable) {
318 compileError("Attribute set is declared streamable, " +
319 "but references an attribute set that is not declared streamable", "XTSE0730");
320 }
321 }
322 }
323
324 }
325
326 XSLAttribute node;
327 AxisIterator iter = iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT);
328 while ((node = (XSLAttribute) iter.next()) != null) {
329 Expression inst = node.compile(compilation, decl);
330 inst.setRetainedStaticContext(makeRetainedStaticContext());
331 inst = inst.simplify();
332 if (compilation.getCompilerInfo().isCompileWithTracing()) {
333 inst = makeTraceInstruction(this, inst);
334 }
335 containedInstructions.add(inst);
336 }
337
338 setActionCompleted(ACTION_COMPILE);
339 }
340
341 /**
342 * Optimize the stylesheet construct
343 *
344 * @param declaration this attribute set declaration
345 */
346
347 public void optimize(ComponentDeclaration declaration) throws XPathException {
348 // Already done earlier
349 }
350
351
352 /**
353 * Get the type of construct. This will be a constant in
354 * class {@link LocationKind}. This method is part of
355 * the {@link net.sf.saxon.trace.InstructionInfo} interface
356 */
357
358 public int getConstructType() {
359 return StandardNames.XSL_ATTRIBUTE_SET;
360 }
361
362 /**
363 * Generate byte code if appropriate
364 *
365 * @param opt the optimizer
366 */
367 public void generateByteCode(Optimizer opt) {}
368
369 // private void checkStreamability() throws XPathException {
370 ////#ifdefined STREAM
371 // if (streamable) {
372 // ContextItemStaticInfo info = new ContextItemStaticInfo(AnyItemType.getInstance(), false, true);
373 // procedure.getBody().getStreamability(false, info, null);
374 // if (procedure.getBody().getSweep() != Sweep.MOTIONLESS) {
375 // compileError("The attribute set is declared streamable but it is not motionless", "XTSE3430");
376 // }
377 // }
378 ////#endif
379 // }
380
381
382
383 }
384