Debian Codegraph @

net/sf/saxon/style/XSLEvaluate.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.Block;
12 import net.sf.saxon.expr.instruct.Choose;
13 import net.sf.saxon.expr.parser.ExpressionVisitor;
14 import net.sf.saxon.expr.parser.RoleDiagnostic;
15 import net.sf.saxon.expr.parser.Token;
16 import net.sf.saxon.expr.parser.TypeChecker;
17 import net.sf.saxon.functions.SystemFunction;
18 import net.sf.saxon.lib.Feature;
19 import net.sf.saxon.lib.NamespaceConstant;
20 import net.sf.saxon.ma.map.MapType;
21 import net.sf.saxon.om.AttributeCollection;
22 import net.sf.saxon.om.AxisInfo;
23 import net.sf.saxon.om.NodeInfo;
24 import net.sf.saxon.pattern.AnyNodeTest;
25 import net.sf.saxon.trans.Err;
26 import net.sf.saxon.trans.XPathException;
27 import net.sf.saxon.tree.iter.AxisIterator;
28 import net.sf.saxon.type.AnyItemType;
29 import net.sf.saxon.type.ItemType;
30 import net.sf.saxon.type.Type;
31 import net.sf.saxon.value.BooleanValue;
32 import net.sf.saxon.value.SequenceType;
33 import net.sf.saxon.value.Whitespace;
34
35 import java.util.ArrayList;
36 import java.util.List;
37
38 /**
39 * Handler for xsl:evaluate elements in XSLT 3.0 stylesheet. <br>
40 */
41
42 public class XSLEvaluate extends StyleElement {
43
44 Expression xpath = null;
45 SequenceType requiredType = SequenceType.ANY_SEQUENCE;
46 Expression namespaceContext = null;
47 Expression contextItem = null;
48 Expression baseUri = null;
49 Expression schemaAware = null;
50 Expression withParams = null;
51 Expression options = null;
52 boolean hasFallbackChildren;
53
54 /**
55 * Determine whether this node is an instruction.
56 *
57 * @return true - it is an instruction
58 */
59
60 public boolean isInstruction() {
61 return true;
62 }
63
64 /**
65 * Specify that xsl:sort is a permitted child
66 */
67
68 protected boolean isPermittedChild(StyleElement child) {
69 return child instanceof XSLLocalParam;
70 }
71
72 /**
73 * Determine the type of item returned by this instruction (only relevant if
74 * it is an instruction).
75 *
76 * @return the item type returned
77 */
78
79 protected ItemType getReturnedItemType() {
80 return AnyItemType.getInstance();
81 }
82
83 /**
84 * Determine whether this type of element is allowed to contain a sequence constructor
85 *
86 * @return false: no, it may not contain a sequence constructor
87 */
88
89 public boolean mayContainSequenceConstructor() {
90 return false;
91 }
92
93
94 public void prepareAttributes() throws XPathException {
95
96 AttributeCollection atts = getAttributeList();
97
98 String xpathAtt = null;
99 String asAtt = null;
100 String contextItemAtt = null;
101 String baseUriAtt = null;
102 String namespaceContextAtt = null;
103 String schemaAwareAtt = null;
104 String withParamsAtt = null;
105
106 for (int a = 0; a < atts.getLength(); a++) {
107 String f = atts.getQName(a);
108 switch (f) {
109 case "xpath":
110 xpathAtt = atts.getValue(a);
111 xpath = makeExpression(xpathAtt, a);
112 break;
113 case "as":
114 asAtt = atts.getValue(a);
115 break;
116 case "context-item":
117 contextItemAtt = atts.getValue(a);
118 contextItem = makeExpression(contextItemAtt, a);
119 break;
120 case "base-uri":
121 baseUriAtt = atts.getValue(a);
122 baseUri = makeAttributeValueTemplate(baseUriAtt, a);
123 break;
124 case "namespace-context":
125 namespaceContextAtt = atts.getValue(a);
126 namespaceContext = makeExpression(namespaceContextAtt, a);
127 break;
128 case "schema-aware":
129 schemaAwareAtt = Whitespace.trim(atts.getValue(a));
130 schemaAware = makeAttributeValueTemplate(schemaAwareAtt, a);
131 break;
132 case "with-params":
133 withParamsAtt = atts.getValue(a);
134 withParams = makeExpression(withParamsAtt, a);
135 break;
136 default:
137 if (atts.getLocalName(a).equals("options") && atts.getURI(a).equals(NamespaceConstant.SAXON)) {
138 options = makeExpression(atts.getValue(a), a);
139 } else {
140 checkUnknownAttribute(atts.getNodeName(a));
141 }
142 break;
143 }
144 }
145
146 if (xpathAtt == null) {
147 reportAbsence("xpath");
148 }
149
150 if (asAtt != null) {
151 requiredType = makeSequenceType(asAtt);
152 try {
153 requiredType = makeSequenceType(asAtt);
154 } catch (XPathException e) {
155 compileErrorInAttribute(e.getMessage(), e.getErrorCodeLocalPart(), "as");
156 }
157 }
158
159 if (contextItemAtt == null) {
160 contextItem = Literal.makeEmptySequence();
161 }
162
163 if (schemaAwareAtt == null) {
164 schemaAware = new StringLiteral("no");
165 } else if (schemaAware instanceof StringLiteral) {
166 checkAttributeValue("schema-aware", schemaAwareAtt, true, StyleElement.YES_NO);
167 }
168
169 if (withParamsAtt == null) {
170 withParamsAtt = "map{}";
171 withParams = makeExpression(withParamsAtt, -1);
172 }
173
174 if (options == null) {
175 options = makeExpression("map{}", -1);
176 }
177 }
178
179
180 public void validate(ComponentDeclaration decl) throws XPathException {
181 getContainingPackage().setRetainUnusedFunctions();
182 if (xpath == null) {
183 xpath = new StringLiteral("''");
184 }
185 xpath = typeCheck("xpath", xpath);
186 baseUri = typeCheck("base-uri", baseUri);
187 contextItem = typeCheck("context-item", contextItem);
188 namespaceContext = typeCheck("namespace-context", namespaceContext);
189 schemaAware = typeCheck("schema-aware", schemaAware);
190 withParams = typeCheck("with-params", withParams);
191 options = typeCheck("options", options);
192
193 iterateAxis(AxisInfo.CHILD).forEachOrFail(child -> {
194 if (child instanceof XSLWithParam) {
195 // OK
196 } else if (child instanceof XSLFallback) {
197 hasFallbackChildren = true;
198 } else if (child.getNodeKind() == Type.TEXT) {
199 // with xml:space=preserve, white space nodes may still be there
200 if (!Whitespace.isWhite(child.getStringValueCS())) {
201 compileError("No character data is allowed within xsl:evaluate", "XTSE0010");
202 }
203 } else {
204 compileError("Child element " + Err.wrap(child.getDisplayName(), Err.ELEMENT) +
205 " is not allowed as a child of xsl:evaluate", "XTSE0010");
206 }
207 });
208
209 try {
210 ExpressionVisitor visitor = makeExpressionVisitor();
211 TypeChecker tc = getConfiguration().getTypeChecker(false);
212 RoleDiagnostic role = new RoleDiagnostic(RoleDiagnostic.INSTRUCTION, "xsl:evaluate/xpath", 0);
213 xpath = tc.staticTypeCheck(xpath, SequenceType.SINGLE_STRING, role, visitor);
214
215 role = new RoleDiagnostic(RoleDiagnostic.INSTRUCTION, "xsl:evaluate/context-item", 0);
216 role.setErrorCode("XTTE3210");
217 contextItem = tc.staticTypeCheck(contextItem, SequenceType.OPTIONAL_ITEM, role, visitor);
218
219 role = new RoleDiagnostic(RoleDiagnostic.INSTRUCTION, "xsl:evaluate/namespace-context", 0);
220 role.setErrorCode("XTTE3170");
221 if (namespaceContext != null) {
222 namespaceContext = tc.staticTypeCheck(namespaceContext, SequenceType.SINGLE_NODE, role, visitor);
223 }
224
225 role = new RoleDiagnostic(RoleDiagnostic.INSTRUCTION, "xsl:evaluate/with-params", 0);
226 role.setErrorCode("XTTE3170");
227 withParams = tc.staticTypeCheck(withParams,
228 SequenceType.makeSequenceType(MapType.ANY_MAP_TYPE, StaticProperty.EXACTLY_ONE), role, visitor);
229
230 role = new RoleDiagnostic(RoleDiagnostic.INSTRUCTION, "xsl:evaluate/saxon:options", 0);
231 options = tc.staticTypeCheck(options,
232 SequenceType.makeSequenceType(MapType.ANY_MAP_TYPE, StaticProperty.EXACTLY_ONE), role, visitor);
233
234 } catch (XPathException err) {
235 compileError(err);
236 }
237 }
238
239 public Expression getTargetExpression() {
240 return xpath;
241 }
242
243 public Expression getContextItemExpression() {
244 return contextItem;
245 }
246
247 public Expression getBaseUriExpression() {
248 return baseUri;
249 }
250
251 public Expression getNamespaceContextExpression() {
252 return namespaceContext;
253 }
254
255 public Expression getSchemaAwareExpression() {
256 return schemaAware;
257 }
258
259 public Expression getWithParamsExpression() {
260 return withParams;
261 }
262
263 public Expression getOptionsExpression() {
264 return options;
265 }
266
267 public SequenceType getRequiredType() {
268 return requiredType;
269 }
270
271 /*@Nullable*/
272 public Expression compile(Compilation exec, ComponentDeclaration decl) throws XPathException {
273 if (getConfiguration().getBooleanProperty(Feature.DISABLE_XSL_EVALUATE)) {
274 // If xsl:evaluate is statically disabled then we should execute any fallback children
275 validationError = new XPathException("xsl:evaluate is not available in this configuration", "XTDE3175");
276 return fallbackProcessing(exec, decl, this);
277 } else {
278 Expression evaluateExpr = getConfiguration().makeEvaluateInstruction(this, decl);
279 if (evaluateExpr instanceof ErrorExpression) {
280 return evaluateExpr;
281 }
282 // If there are any xsl:fallback children, we need to compile them, in case xsl:evaluate
283 // is dynamically disabled at run-time.
284 if (hasFallbackChildren) {
285 // Generate a conditional expression switched on the value of system-property('xsl:supports-dynamic-evaluation')
286 Expression[] conditions = new Expression[2];
287 Expression sysProp = SystemFunction.makeCall("system-property",
288 makeRetainedStaticContext(),
289 new StringLiteral("Q{" + NamespaceConstant.XSLT + "}supports-dynamic-evaluation"));
290 conditions[0] = new ValueComparison(sysProp, Token.FEQ, new StringLiteral("no"));
291 conditions[1] = Literal.makeLiteral(BooleanValue.TRUE);
292 Expression[] actions = new Expression[2];
293 List<Expression> fallbackExpressions = new ArrayList<>();
294 NodeInfo child;
295 AxisIterator kids = iterateChildren(AnyNodeTest.getInstance());
296 while ((child = kids.next()) != null) {
297 if (child instanceof XSLFallback) {
298 fallbackExpressions.add(((XSLFallback)child).compileSequenceConstructor(exec, decl, false));
299 }
300 }
301 actions[0] = new Block(fallbackExpressions.toArray(new Expression[0]));
302 actions[1] = evaluateExpr;
303 return new Choose(conditions, actions);
304 } else {
305 return evaluateExpr;
306 }
307 }
308 }
309
310
311 }