Debian Codegraph @

net/sf/saxon/style/XSLNextIteration.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.Expression;
11 import net.sf.saxon.expr.LocalVariableReference;
12 import net.sf.saxon.expr.VariableReference;
13 import net.sf.saxon.expr.instruct.LocalParam;
14 import net.sf.saxon.expr.instruct.NextIteration;
15 import net.sf.saxon.expr.instruct.WithParam;
16 import net.sf.saxon.om.AxisInfo;
17 import net.sf.saxon.om.NodeInfo;
18 import net.sf.saxon.om.StructuredQName;
19 import net.sf.saxon.trans.Err;
20 import net.sf.saxon.trans.XPathException;
21 import net.sf.saxon.tree.iter.AxisIterator;
22 import net.sf.saxon.type.Type;
23 import net.sf.saxon.value.SequenceType;
24 import net.sf.saxon.value.Whitespace;
25
26 /**
27 * An xsl:next-iteration element in the stylesheet
28 */
29
30 public class XSLNextIteration extends XSLBreakOrContinue {
31
32 public void validate(ComponentDeclaration decl) throws XPathException {
33 validatePosition();
34 if (xslIterate == null) {
35 compileError("xsl:next-iteration must be a descendant of an xsl:iterate instruction");
36 }
37 iterateAxis(AxisInfo.CHILD).forEachOrFail(child -> {
38 if (child instanceof XSLWithParam) {
39 if (((XSLWithParam) child).isTunnelParam()) {
40 compileError("An xsl:with-param element within xsl:iterate must not specify tunnel='yes'", "XTSE0020");
41 }
42 } else if (child.getNodeKind() == Type.TEXT) {
43 // with xml:space=preserve, white space nodes may still be there
44 if (!Whitespace.isWhite(child.getStringValueCS())) {
45 compileError("No character data is allowed within xsl:next-iteration", "XTSE0010");
46 }
47 } else {
48 compileError("Child element " + Err.wrap(child.getDisplayName(), Err.ELEMENT) +
49 " is not allowed as a child of xsl:next-iteration", "XTSE0010");
50 }
51 });
52 }
53
54 public void postValidate() throws XPathException {
55
56 if (xslIterate == null) {
57 return; // previous error already reported
58 }
59
60 // check that every supplied parameter is declared in the saxon:iterate instruction
61
62 AxisIterator actualParams = iterateAxis(AxisInfo.CHILD);
63 NodeInfo w;
64 while ((w = actualParams.next()) != null) {
65 if (w instanceof XSLWithParam) {
66 XSLWithParam withParam = (XSLWithParam) w;
67 AxisIterator formalParams = xslIterate.iterateAxis(AxisInfo.CHILD);
68 boolean ok = false;
69 NodeInfo param;
70 while ((param = formalParams.next()) != null) {
71 if (param instanceof XSLLocalParam &&
72 ((XSLLocalParam) param).getVariableQName().equals(withParam.getVariableQName())) {
73 ok = true;
74 SequenceType required = ((XSLLocalParam) param).getRequiredType();
75 withParam.checkAgainstRequiredType(required);
76 break;
77 }
78 }
79 if (!ok) {
80 compileError("Parameter " +
81 withParam.getVariableQName().getDisplayName() +
82 " is not declared in the containing xsl:iterate instruction", "XTSE3130");
83 }
84 }
85 }
86 }
87
88
89 public Expression compile(Compilation exec, ComponentDeclaration decl) throws XPathException {
90 NextIteration call = new NextIteration();
91 call.setRetainedStaticContext(makeRetainedStaticContext());
92 WithParam[] actualParams = getWithParamInstructions(call, exec, decl, false);
93 call.setParameters(actualParams);
94
95 // For all declared parameters of the xsl:iterate instruction that are not present in the
96 // actual parameters of the xsl:next-iteration, add an implicit <xsl:with-param name="p" select="$p"/>
97
98 if (xslIterate != null) {
99 AxisIterator declaredParams = xslIterate.iterateAxis(AxisInfo.CHILD);
100 NodeInfo param;
101 while ((param = declaredParams.next()) != null) {
102 if (param instanceof XSLLocalParam) {
103 XSLLocalParam pdecl = (XSLLocalParam) param;
104 StructuredQName paramName = pdecl.getVariableQName();
105 LocalParam lp = pdecl.getCompiledParam();
106 boolean found = false;
107 for (WithParam actualParam : actualParams) {
108 if (paramName.equals(actualParam.getVariableQName())) {
109 actualParam.setSlotNumber(lp.getSlotNumber());
110 found = true;
111 break;
112 }
113 }
114 if (!found) {
115 WithParam wp = new WithParam();
116 wp.setVariableQName(paramName);
117 VariableReference ref = new LocalVariableReference(lp);
118 wp.setSelectExpression(call, ref);
119 //wp.setParameterId(psm.allocateUniqueParameterNumber(paramName));
120 wp.setSlotNumber(lp.getSlotNumber());
121 ref.setStaticType(pdecl.getRequiredType(), null, 0);
122 WithParam[] p2 = new WithParam[actualParams.length + 1];
123 p2[0] = wp;
124 System.arraycopy(actualParams, 0, p2, 1, actualParams.length);
125 actualParams = p2;
126 }
127 }
128 }
129 }
130
131 return call;
132 }
133
134 }