Debian Codegraph @

net/sf/saxon/expr/instruct/NamedTemplate.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.Expression;
11 import net.sf.saxon.expr.XPathContext;
12 import net.sf.saxon.expr.parser.RoleDiagnostic;
13 import net.sf.saxon.om.Item;
14 import net.sf.saxon.om.StandardNames;
15 import net.sf.saxon.om.StructuredQName;
16 import net.sf.saxon.trace.ExpressionPresenter;
17 import net.sf.saxon.trace.LocationKind;
18 import net.sf.saxon.trans.SymbolicName;
19 import net.sf.saxon.trans.XPathException;
20 import net.sf.saxon.type.AnyItemType;
21 import net.sf.saxon.type.ItemType;
22 import net.sf.saxon.type.TypeHierarchy;
23 import net.sf.saxon.value.SequenceType;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
28 /**
29 * The runtime object corresponding to a named xsl:template element in the stylesheet.
30 * <p>Note that the Template object no longer has precedence information associated with it; this is now
31 * only in the Rule object that references this Template. This allows two rules to share the same template,
32 * with different precedences. This occurs when a stylesheet module is imported more than once, from different
33 * places, with different import precedences.</p>
34 *
35 * <p>From Saxon 9.7, the NamedTemplate and TemplateRule objects are separated. A NamedTemplate represents
36 * a template with a name attribute; a TemplateRule is a template with a match attribute. If an xsl:template
37 * declaration has both attributes, two objects are created.</p>
38 */
39
40 public class NamedTemplate extends Actor {
41
42 // The body of the template is represented by an expression,
43 // which is responsible for any type checking that's needed.
44
45 private StructuredQName templateName;
46 private boolean hasRequiredParams;
47 private boolean bodyIsTailCallReturner;
48 private SequenceType requiredType;
49 private ItemType requiredContextItemType = AnyItemType.getInstance();
50 private boolean mayOmitContextItem = true;
51 private boolean absentFocus = false;
52 private List<LocalParamInfo> localParamDetails = new ArrayList<>(4);
53
54 /**
55 * Create a named template
56 */
57
58 public NamedTemplate(StructuredQName templateName) {
59 setTemplateName(templateName);
60 }
61
62 /**
63 * Initialize the template
64 *
65 * @param templateName the name of the template (if any)
66 * performed by apply-imports
67 */
68
69 public void setTemplateName(StructuredQName templateName) {
70 this.templateName = templateName;
71 }
72
73 /**
74 * Set the required context item type. Used when there is an xsl:context-item child element
75 *
76 * @param type the required context item type
77 * @param mayBeOmitted true if the context item may be absent
78 * @param absentFocus true if the context item is treated as absent even if supplied (use=absent)
79 */
80
81 public void setContextItemRequirements(ItemType type, boolean mayBeOmitted, boolean absentFocus) {
82 requiredContextItemType = type;
83 mayOmitContextItem = mayBeOmitted;
84 this.absentFocus = absentFocus;
85 }
86
87 public int getComponentKind() {
88 return StandardNames.XSL_TEMPLATE;
89 }
90
91 public SymbolicName getSymbolicName() {
92 if (getTemplateName() == null) {
93 return null;
94 } else {
95 return new SymbolicName(StandardNames.XSL_TEMPLATE, getTemplateName());
96 }
97 }
98
99 /**
100 * Set the expression that forms the body of the template
101 *
102 * @param body the body of the template
103 */
104
105 public void setBody(Expression body) {
106 super.setBody(body);
107 bodyIsTailCallReturner = (body instanceof TailCallReturner);
108 }
109
110 /**
111 * Get the name of the template (if it is named)
112 *
113 * @return the template name, or null if unnamed
114 */
115
116 public StructuredQName getTemplateName() {
117 return templateName;
118 }
119
120 /**
121 * Get a name identifying the object of the expression, for example a function name, template name,
122 * variable name, key name, element name, etc. This is used only where the name is known statically.
123 */
124
125 public StructuredQName getObjectName() {
126 return templateName;
127 }
128
129 /**
130 * Set whether this template has one or more required parameters
131 *
132 * @param has true if the template has at least one required parameter
133 */
134
135 public void setHasRequiredParams(boolean has) {
136 hasRequiredParams = has;
137 }
138
139 /**
140 * Ask whether this template has one or more required parameters
141 *
142 * @return true if this template has at least one required parameter
143 */
144
145 public boolean hasRequiredParams() {
146 return hasRequiredParams;
147 }
148
149 /**
150 * Set the required type to be returned by this template
151 *
152 * @param type the required type as defined in the "as" attribute on the xsl:template element
153 */
154
155 public void setRequiredType(SequenceType type) {
156 requiredType = type;
157 }
158
159 /**
160 * Get the required type to be returned by this template
161 *
162 * @return the required type as defined in the "as" attribute on the xsl:template element
163 */
164
165 public SequenceType getRequiredType() {
166 if (requiredType == null) {
167 return SequenceType.ANY_SEQUENCE;
168 } else {
169 return requiredType;
170 }
171 }
172
173 public ItemType getRequiredContextItemType() {
174 return requiredContextItemType;
175 }
176
177 public boolean isMayOmitContextItem() {
178 return mayOmitContextItem;
179 }
180
181 public boolean isAbsentFocus() {
182 return absentFocus;
183 }
184
185
186 /**
187 * Get the local parameter with a given parameter id
188 *
189 * @param id the parameter id
190 * @return the local parameter with this id if found, otherwise null
191 */
192
193 /*@Nullable*/
194 public LocalParamInfo getLocalParamInfo(StructuredQName id) {
195 List<LocalParamInfo> params = getLocalParamDetails();
196 for (LocalParamInfo lp : params) {
197 if (lp.name.equals(id)) {
198 return lp;
199 }
200 }
201 return null;
202 }
203
204 /**
205 * Expand the template. Called when the template is invoked using xsl:call-template.
206 * Invoking a template by this method does not change the current template.
207 *
208 * @param context the XPath dynamic context
209 * @return null if the template exited normally; but if it was a tail call, details of the call
210 * that hasn't been made yet and needs to be made by the caller
211 * @throws net.sf.saxon.trans.XPathException if a dynamic error occurs while evaluating
212 * the template
213 */
214
215 public TailCall expand(XPathContext context) throws XPathException {
216 Item contextItem = context.getContextItem();
217 if (contextItem == null) {
218 if (!mayOmitContextItem) {
219 XPathException err =
220 new XPathException("The template requires a context item, but none has been supplied", "XTTE3090");
221 err.setLocation(getLocation());
222 err.setIsTypeError(true);
223 throw err;
224 }
225 } else {
226 TypeHierarchy th = context.getConfiguration().getTypeHierarchy();
227 if (requiredContextItemType != AnyItemType.getInstance() &&
228 !requiredContextItemType.matches(contextItem, th)) {
229 RoleDiagnostic role = new RoleDiagnostic(
230 RoleDiagnostic.MISC, "context item for the named template", 0);
231 String message = role.composeErrorMessage(requiredContextItemType, contextItem, th);
232 XPathException err = new XPathException(message, "XTTE0590");
233 err.setLocation(getLocation());
234 err.setIsTypeError(true);
235 throw err;
236 }
237 if (absentFocus) {
238 context = context.newMinorContext();
239 context.setCurrentIterator(null);
240 }
241 }
242 if (bodyIsTailCallReturner) {
243 return ((TailCallReturner) body).processLeavingTail(context);
244 } else if (body != null) {
245 body.process(context);
246 }
247 return null;
248 }
249
250
251 /**
252 * Get the type of construct. This will either be the fingerprint of a standard XSLT instruction name
253 * (values in {@link net.sf.saxon.om.StandardNames}: all less than 1024)
254 * or it will be a constant in class {@link LocationKind}.
255 */
256
257 public int getConstructType() {
258 return LocationKind.TEMPLATE;
259 }
260
261 /**
262 * Output diagnostic explanation to an ExpressionPresenter
263 */
264
265 public void export(ExpressionPresenter presenter) throws XPathException {
266 presenter.startElement("template");
267 presenter.emitAttribute("name", getTemplateName());
268 explainProperties(presenter);
269
270 presenter.emitAttribute("slots", "" + getStackFrameMap().getNumberOfVariables());
271
272 if (getBody() != null) {
273 presenter.setChildRole("body");
274 getBody().export(presenter);
275 }
276 presenter.endElement();
277 }
278
279 public void explainProperties(ExpressionPresenter presenter) throws XPathException {
280 if (getRequiredContextItemType() != AnyItemType.getInstance()) {
281 presenter.emitAttribute("cxt", getRequiredContextItemType().toExportString());
282 if ("JS".equals(((ExpressionPresenter.ExportOptions) presenter.getOptions()).target)) {
283 try {
284 int targetVersion = ((ExpressionPresenter.ExportOptions) presenter.getOptions()).targetVersion;
285 presenter.emitAttribute("jsTest", getRequiredContextItemType().generateJavaScriptItemTypeTest(AnyItemType.getInstance(), targetVersion));
286 } catch (XPathException e) {
287 e.maybeSetLocation(getLocation());
288 throw e;
289 }
290 }
291 }
292
293 String flags = "";
294 if (mayOmitContextItem) {
295 flags = "o";
296 }
297 if (!absentFocus) {
298 flags += "s";
299 }
300 presenter.emitAttribute("flags", flags);
301 if (getRequiredType() != SequenceType.ANY_SEQUENCE) {
302 presenter.emitAttribute("as", getRequiredType().toExportString());
303 }
304 presenter.emitAttribute("line", getLineNumber() + "");
305 presenter.emitAttribute("module", getSystemId());
306 }
307
308 public void setLocalParamDetails(List<LocalParamInfo> details) {
309 localParamDetails = details;
310 }
311
312 public List<LocalParamInfo> getLocalParamDetails() {
313 return localParamDetails;
314 }
315
316 public static class LocalParamInfo {
317 public StructuredQName name;
318 public SequenceType requiredType;
319 public boolean isRequired;
320 public boolean isTunnel;
321 }
322
323
324 }
325