Debian Codegraph @

net/sf/saxon/style/XSLApplyTemplates.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.*;
11 import net.sf.saxon.expr.instruct.ApplyTemplates;
12 import net.sf.saxon.expr.parser.RoleDiagnostic;
13 import net.sf.saxon.expr.sort.SortExpression;
14 import net.sf.saxon.expr.sort.SortKeyDefinitionList;
15 import net.sf.saxon.om.*;
16 import net.sf.saxon.pattern.AnyNodeTest;
17 import net.sf.saxon.pattern.NameTest;
18 import net.sf.saxon.trans.Err;
19 import net.sf.saxon.trans.Mode;
20 import net.sf.saxon.trans.SymbolicName;
21 import net.sf.saxon.trans.rules.RuleManager;
22 import net.sf.saxon.trans.XPathException;
23 import net.sf.saxon.tree.iter.AxisIterator;
24 import net.sf.saxon.type.Type;
25 import net.sf.saxon.value.Whitespace;
26
27 import java.util.HashMap;
28
29
30 /**
31 * An xsl:apply-templates element in the stylesheet
32 */
33
34 public class XSLApplyTemplates extends StyleElement {
35
36 /*@Nullable*/ private Expression select;
37 private StructuredQName modeName; // null if no name specified or if conventional values such as #current used
38 private boolean useCurrentMode = false;
39 private boolean useTailRecursion = false;
40 private boolean defaultedSelectExpression = true;
41 private Mode mode;
42 private String modeAttribute;
43
44 /**
45 * Determine whether this node is an instruction.
46 *
47 * @return true - it is an instruction
48 */
49
50 public boolean isInstruction() {
51 return true;
52 }
53
54
55 public void prepareAttributes() throws XPathException {
56
57 AttributeCollection atts = getAttributeList();
58
59 String selectAtt;
60
61 for (int a = 0; a < atts.getLength(); a++) {
62 String f = atts.getQName(a);
63 switch (f) {
64 case "mode":
65 modeAttribute = Whitespace.trim(atts.getValue(a));
66 break;
67 case "select":
68 selectAtt = atts.getValue(a);
69 select = makeExpression(selectAtt, a);
70 defaultedSelectExpression = false;
71 break;
72 default:
73 checkUnknownAttribute(atts.getNodeName(a));
74 break;
75 }
76 }
77
78 if (modeAttribute != null) {
79 switch (modeAttribute) {
80 case "#current":
81 useCurrentMode = true;
82 break;
83 case "#unnamed":
84 modeName = Mode.UNNAMED_MODE_NAME;
85 break;
86 case "#default":
87 // do nothing;
88 break;
89 default:
90 try {
91 modeName = makeQName(modeAttribute);
92 } catch (XPathException err) {
93 compileError("Mode name " + Err.wrap(modeAttribute) + " is not a valid QName",
94 err.getErrorCodeQName());
95 modeName = null;
96 }
97 break;
98 }
99 }
100 }
101
102 public void validate(ComponentDeclaration decl) throws XPathException {
103
104 // get the Mode object
105 if (useCurrentMode) {
106 // give a warning if we're not inside an xsl:template
107 if (iterateAxis(AxisInfo.ANCESTOR, new NameTest(Type.ELEMENT, StandardNames.XSL_TEMPLATE, getNamePool())).next() == null) {
108 issueWarning("Specifying mode=\"#current\" when not inside an xsl:template serves no useful purpose", this);
109 }
110 } else {
111 PrincipalStylesheetModule psm = getPrincipalStylesheetModule();
112 if (modeName == null) {
113 // XSLT 3.0 allows a default mode to be specified on a containing element
114 modeName = getDefaultMode();
115 if ((modeName == null || modeName.equals(Mode.UNNAMED_MODE_NAME)) &&
116 psm.isDeclaredModes() && !psm.getRuleManager().isUnnamedModeExplicit()) {
117 compileError("The unnamed mode must be explicitly declared in an xsl:mode declaration", "XTSE3085");
118 }
119 } else if (modeName.equals(Mode.UNNAMED_MODE_NAME) && psm.isDeclaredModes() && !psm.getRuleManager().isUnnamedModeExplicit()) {
120 compileError("The #unnamed mode must be explicitly declared in an xsl:mode declaration", "XTSE3085");
121 }
122
123 SymbolicName sName = new SymbolicName(StandardNames.XSL_MODE, modeName);
124 StylesheetPackage containingPackage = decl.getSourceElement().getContainingPackage();
125 HashMap<SymbolicName, Component> componentIndex = containingPackage.getComponentIndex();
126 // see if there is a mode with this name in a used package
127 Component existing = componentIndex.get(sName);
128 if (existing != null) {
129 mode = (Mode)existing.getActor();
130 }
131
132 if (mode == null) {
133 if (psm.isDeclaredModes()) {
134 compileError("Mode name " + modeName.getDisplayName() + " must be explicitly declared in an xsl:mode declaration", "XTSE3085");
135 }
136 mode = psm.getRuleManager().obtainMode(modeName, true);
137 }
138 }
139
140 // handle sorting if requested
141
142 AxisIterator kids = iterateAxis(AxisInfo.CHILD);
143 NodeInfo child;
144 while ((child = kids.next()) != null) {
145 if (child.getNodeKind() == Type.TEXT) {
146 // with xml:space=preserve, white space nodes may still be there
147 if (!Whitespace.isWhite(child.getStringValueCS())) {
148 compileError("No character data is allowed within xsl:apply-templates", "XTSE0010");
149 }
150 } else if (!(child instanceof XSLSort || child instanceof XSLWithParam)){
151 compileError("Invalid element " + Err.wrap(child.getDisplayName(), Err.ELEMENT) +
152 " within xsl:apply-templates", "XTSE0010");
153 }
154 }
155
156 if (select == null) {
157 Expression here = new ContextItemExpression();
158 RoleDiagnostic role =
159 new RoleDiagnostic(RoleDiagnostic.CONTEXT_ITEM, "", 0);
160 role.setErrorCode("XTTE0510");
161 here = new ItemChecker(here, AnyNodeTest.getInstance(), role);
162 select = new SimpleStepExpression(here, new AxisExpression(AxisInfo.CHILD, null));
163 //select = new AxisExpression(AxisInfo.CHILD, null);
164 select.setLocation(allocateLocation());
165 select.setRetainedStaticContext(makeRetainedStaticContext());
166 }
167
168 select = typeCheck("select", select);
169
170 }
171
172 /**
173 * Mark tail-recursive calls on templates and functions.
174 * For most instructions, this does nothing.
175 */
176
177 public boolean markTailCalls() {
178 useTailRecursion = true;
179 return true;
180 }
181
182
183 public Expression compile(Compilation compilation, ComponentDeclaration decl) throws XPathException {
184 SortKeyDefinitionList sortKeys = makeSortKeys(compilation, decl);
185 if (sortKeys != null) {
186 useTailRecursion = false;
187 }
188 assert select != null;
189 Expression sortedSequence = select;
190 if (sortKeys != null) {
191 sortedSequence = new SortExpression(select, sortKeys);
192 }
193 compileSequenceConstructor(compilation, decl, true);
194 RuleManager rm = compilation.getPrincipalStylesheetModule().getRuleManager();
195 ApplyTemplates app = new ApplyTemplates(
196 sortedSequence,
197 useCurrentMode,
198 useTailRecursion,
199 defaultedSelectExpression,
200 isWithinDeclaredStreamableConstruct(),
201 mode,
202 rm);
203 app.setActualParams(getWithParamInstructions(app, compilation, decl, false));
204 app.setTunnelParams(getWithParamInstructions(app, compilation, decl, true));
205 return app;
206 }
207
208 }
209