Debian Codegraph @

net/sf/saxon/style/XSLIterate.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.instruct.IterateInstr;
11 import net.sf.saxon.expr.Expression;
12 import net.sf.saxon.expr.Literal;
13 import net.sf.saxon.expr.instruct.LocalParam;
14 import net.sf.saxon.expr.instruct.LocalParamBlock;
15 import net.sf.saxon.om.AttributeCollection;
16 import net.sf.saxon.om.AxisInfo;
17 import net.sf.saxon.om.NodeInfo;
18 import net.sf.saxon.om.SequenceIterator;
19 import net.sf.saxon.trans.SaxonErrorCode;
20 import net.sf.saxon.trans.XPathException;
21 import net.sf.saxon.tree.iter.ListIterator;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 /**
27 * Handler for xsl:iterate elements in stylesheet. <br>
28 */
29
30 public class XSLIterate extends StyleElement {
31
32 Expression select = null;
33 boolean compilable;
34
35 /**
36 * Determine whether this node is an instruction.
37 *
38 * @return true - it is an instruction
39 */
40
41 public boolean isInstruction() {
42 return true;
43 }
44
45 /**
46 * Specify that xsl:param is a permitted child
47 */
48
49 protected boolean isPermittedChild(StyleElement child) {
50 return child instanceof XSLLocalParam || child instanceof XSLOnCompletion;
51 }
52
53 /**
54 * Determine whether this type of element is allowed to contain a sequence constructor
55 *
56 * @return true: yes, it may contain a sequence constructor
57 */
58
59 public boolean mayContainSequenceConstructor() {
60 return true;
61 }
62
63 /**
64 * Determine whether this type of element is allowed to contain an xsl:param element
65 *
66 * @return true if this element is allowed to contain an xsl:param
67 */
68
69 protected boolean mayContainParam() {
70 return true;
71 }
72
73 public void prepareAttributes() throws XPathException {
74
75 AttributeCollection atts = getAttributeList();
76
77 String selectAtt = null;
78
79 for (int a = 0; a < atts.getLength(); a++) {
80 String f = atts.getQName(a);
81 if (f.equals("select")) {
82 selectAtt = atts.getValue(a);
83 select = makeExpression(selectAtt, a);
84 } else {
85 checkUnknownAttribute(atts.getNodeName(a));
86 }
87 }
88
89 if (selectAtt == null) {
90 reportAbsence("select");
91 }
92
93 }
94
95 public void setCompilable(boolean compilable) {
96 this.compilable = compilable;
97 }
98
99 public boolean isCompilable() {
100 return compilable;
101 }
102
103 public void validate(ComponentDeclaration decl) throws XPathException {
104 //checkParamComesFirst(false);
105 select = typeCheck("select", select);
106 if (!hasChildNodes()) {
107 compileWarning("An empty xsl:iterate instruction has no effect", SaxonErrorCode.SXWN9009);
108 }
109 }
110
111 /*@Nullable*/
112 public Expression compile(Compilation exec, ComponentDeclaration decl) throws XPathException {
113 SequenceIterator children = iterateAxis(AxisInfo.CHILD);
114 List<NodeInfo> nonFinallyChildren = new ArrayList<>();
115 Expression finallyExp = null;
116 List<XSLLocalParam> params = new ArrayList<>();
117 while (true) {
118 NodeInfo node = (NodeInfo) children.next();
119 if (node == null) {
120 break;
121 } else if (node instanceof XSLLocalParam) {
122 params.add((XSLLocalParam) node);
123 } else if (node instanceof XSLOnCompletion) {
124 finallyExp = ((XSLOnCompletion) node).compile(exec, decl);
125 } else {
126 nonFinallyChildren.add(node);
127 }
128 }
129 LocalParam[] compiledParams = new LocalParam[params.size()];
130 for (int i = 0; i < params.size(); i++) {
131 compiledParams[i] = (LocalParam) params.get(i).compile(exec, decl);
132 if (compiledParams[i].isImplicitlyRequiredParam()) {
133 // see spec bug 25158; Saxon bug 2041
134 compileError("The parameter must be given an initial value because () is not valid, given the declared type", "XTSE3520");
135 }
136 }
137 LocalParamBlock paramBlock = new LocalParamBlock(compiledParams);
138 Expression action = compileSequenceConstructor(exec, decl, new ListIterator<>(nonFinallyChildren), false);
139 if (action == null) {
140 // body of xsl:iterate is empty: it's a no-op.
141 return Literal.makeEmptySequence();
142 }
143 try {
144 action = action.simplify();
145 return new IterateInstr(select, paramBlock, action, finallyExp);
146 } catch (XPathException err) {
147 compileError(err);
148 return null;
149 }
150 }
151
152
153 }