Debian Codegraph @

net/sf/saxon/style/XSLCallTemplate.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.instruct.CallTemplate;
12 import net.sf.saxon.expr.instruct.NamedTemplate;
13 import net.sf.saxon.lib.NamespaceConstant;
14 import net.sf.saxon.om.*;
15 import net.sf.saxon.trans.Err;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.tree.iter.AxisIterator;
18 import net.sf.saxon.type.Type;
19 import net.sf.saxon.value.SequenceType;
20 import net.sf.saxon.value.Whitespace;
21
22 import java.util.List;
23
24 /**
25 * An xsl:call-template element in the stylesheet
26 */
27
28 public class XSLCallTemplate extends StyleElement {
29
30 private static StructuredQName ERROR_TEMPLATE_NAME =
31 new StructuredQName("saxon", NamespaceConstant.SAXON, "error-template");
32
33 private StructuredQName calledTemplateName; // the name of the called template
34 private NamedTemplate template = null; // the template to be called (which may subsequently be overridden in another package)
35 private boolean useTailRecursion = false;
36
37 /**
38 * Determine whether this node is an instruction.
39 *
40 * @return true - it is an instruction
41 */
42
43 public boolean isInstruction() {
44 return true;
45 }
46
47 public void prepareAttributes() throws XPathException {
48
49 AttributeCollection atts = getAttributeList();
50
51 String nameAttribute = null;
52
53 for (int a = 0; a < atts.getLength(); a++) {
54 String f = atts.getQName(a);
55 if (f.equals("name")) {
56 nameAttribute = Whitespace.trim(atts.getValue(a));
57 } else {
58 checkUnknownAttribute(atts.getNodeName(a));
59 }
60 }
61
62 if (nameAttribute == null) {
63 calledTemplateName = ERROR_TEMPLATE_NAME;
64 reportAbsence("name");
65 return;
66 }
67
68 try {
69 calledTemplateName = makeQName(nameAttribute);
70 } catch (XPathException err) {
71 calledTemplateName = ERROR_TEMPLATE_NAME;
72 compileErrorInAttribute(err.getMessage(), err.getErrorCodeLocalPart(), "name");
73 }
74 }
75
76 public void validate(ComponentDeclaration decl) throws XPathException {
77 AxisIterator kids = iterateAxis(AxisInfo.CHILD);
78 while (true) {
79 NodeInfo child = kids.next();
80 if (child == null) {
81 break;
82 }
83 if (child instanceof XSLWithParam) {
84 // OK;
85 } else if (child instanceof XSLFallback && mayContainFallback()) {
86 // xsl:fallback is not allowed on xsl:call-template, but is allowed on saxon:call-template (cheat!)
87 } else if (child.getNodeKind() == Type.TEXT) {
88 // with xml:space=preserve, white space nodes may still be there
89 if (!Whitespace.isWhite(child.getStringValueCS())) {
90 compileError("No character data is allowed within xsl:call-template", "XTSE0010");
91 }
92 } else {
93 compileError("Child element " + Err.wrap(child.getDisplayName(), Err.ELEMENT) +
94 " is not allowed as a child of xsl:call-template", "XTSE0010");
95 }
96 }
97 if (!calledTemplateName.equals(ERROR_TEMPLATE_NAME)) {
98 template = findTemplate(calledTemplateName);
99 }
100 }
101
102 public void postValidate() throws XPathException {
103 // check that a parameter is supplied for each required parameter
104 // of the called template
105
106 if (template != null) {
107 checkParams();
108 } else {
109 throw new AssertionError("Target template not known");
110 }
111 }
112
113 private void checkParams() throws XPathException {
114 List<NamedTemplate.LocalParamInfo> declaredParams = template.getLocalParamDetails();
115 for (NamedTemplate.LocalParamInfo param : declaredParams) {
116 if (param.isRequired && !param.isTunnel) {
117 AxisIterator actualParams = iterateAxis(AxisInfo.CHILD);
118 boolean ok = false;
119 NodeInfo withParam;
120 while ((withParam = actualParams.next()) != null) {
121 if (withParam instanceof XSLWithParam &&
122 ((XSLWithParam) withParam).getVariableQName().equals(param.name)) {
123 ok = true;
124 break;
125 }
126 }
127 if (!ok) {
128 compileError("No value supplied for required parameter " +
129 Err.wrap(param.name.getDisplayName(), Err.VARIABLE), "XTSE0690");
130 }
131 }
132 }
133
134 // check that every supplied parameter is declared in the called
135 // template
136
137 AxisIterator actualParams = iterateAxis(AxisInfo.CHILD);
138 NodeInfo w;
139 while ((w = actualParams.next()) != null) {
140 if (w instanceof XSLWithParam && !((XSLWithParam) w).isTunnelParam()) {
141 XSLWithParam withParam = (XSLWithParam) w;
142 boolean ok = false;
143 for (NamedTemplate.LocalParamInfo param : declaredParams) {
144 if (param.name.equals(withParam.getVariableQName()) && !param.isTunnel) {
145 // Note: see bug 10534
146 ok = true;
147 SequenceType required = param.requiredType;
148 withParam.checkAgainstRequiredType(required);
149 break;
150 }
151 }
152 if (!ok && !xPath10ModeIsEnabled()) {
153 compileError("Parameter " +
154 withParam.getVariableQName().getDisplayName() +
155 " is not declared in the called template", "XTSE0680");
156
157 }
158 }
159 }
160 }
161
162 private NamedTemplate findTemplate(StructuredQName templateName) throws XPathException {
163 PrincipalStylesheetModule pack = getPrincipalStylesheetModule();
164 NamedTemplate template = pack.getNamedTemplate(templateName);
165 if (template == null) {
166 if (templateName.hasURI(NamespaceConstant.XSLT) && templateName.getLocalPart().equals("original")) {
167 // Handle xsl:original
168 return (NamedTemplate) getXslOriginal(StandardNames.XSL_TEMPLATE);
169 }
170 compileError("Cannot find a template named " + calledTemplateName, "XTSE0650");
171 }
172 return template;
173 }
174
175 /**
176 * Mark tail-recursive calls on templates and functions.
177 * For most instructions, this does nothing.
178 */
179
180 public boolean markTailCalls() {
181 useTailRecursion = true;
182 return true;
183 }
184
185
186 public Expression compile(Compilation exec, ComponentDeclaration decl) throws XPathException {
187 if (template == null) {
188 return null; // error already reported
189 }
190
191 CallTemplate call = new CallTemplate(template, calledTemplateName, useTailRecursion, isWithinDeclaredStreamableConstruct());
192 call.setLocation(allocateLocation());
193 call.setActualParameters(
194 getWithParamInstructions(call, exec, decl, false),
195 getWithParamInstructions(call, exec, decl, true));
196 return call;
197 }
198
199 }
200