Debian Codegraph @

net/sf/saxon/functions/ElementAvailable.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.functions;
9
10 import net.sf.saxon.expr.Expression;
11 import net.sf.saxon.expr.StaticProperty;
12 import net.sf.saxon.expr.StringLiteral;
13 import net.sf.saxon.expr.XPathContext;
14 import net.sf.saxon.lib.Feature;
15 import net.sf.saxon.lib.NamespaceConstant;
16 import net.sf.saxon.om.NameChecker;
17 import net.sf.saxon.om.Sequence;
18 import net.sf.saxon.om.StandardNames;
19 import net.sf.saxon.om.StructuredQName;
20 import net.sf.saxon.trans.XPathException;
21 import net.sf.saxon.value.BooleanValue;
22
23 /**
24 * This class supports the XSLT element-available function. Note that when running in a 2.0 processor,
25 * it only looks for XSLT 2.0 instructions; but when running in a 3.0 processor, it recognizes all
26 * elements in the XSLT namespace whether or not they are classified as instructions.
27 */
28
29 public class ElementAvailable extends SystemFunction {
30
31 public static boolean isXslt20Instruction(int fp) {
32 switch (fp) {
33 case StandardNames.XSL_ANALYZE_STRING:
34 case StandardNames.XSL_APPLY_IMPORTS:
35 case StandardNames.XSL_APPLY_TEMPLATES:
36 case StandardNames.XSL_ATTRIBUTE:
37 case StandardNames.XSL_CALL_TEMPLATE:
38 case StandardNames.XSL_CHOOSE:
39 case StandardNames.XSL_COMMENT:
40 case StandardNames.XSL_COPY:
41 case StandardNames.XSL_COPY_OF:
42 case StandardNames.XSL_DOCUMENT:
43 case StandardNames.XSL_ELEMENT:
44 case StandardNames.XSL_FALLBACK:
45 case StandardNames.XSL_FOR_EACH:
46 case StandardNames.XSL_FOR_EACH_GROUP:
47 case StandardNames.XSL_IF:
48 case StandardNames.XSL_MESSAGE:
49 case StandardNames.XSL_NAMESPACE:
50 case StandardNames.XSL_NEXT_MATCH:
51 case StandardNames.XSL_PERFORM_SORT:
52 case StandardNames.XSL_PROCESSING_INSTRUCTION:
53 case StandardNames.XSL_RESULT_DOCUMENT:
54 case StandardNames.XSL_SEQUENCE:
55 case StandardNames.XSL_TEXT:
56 case StandardNames.XSL_VALUE_OF:
57 case StandardNames.XSL_VARIABLE:
58 return true;
59 default:
60 return false;
61 }
62 }
63
64 public static boolean isXslt30Element(int fp) {
65 switch (fp) {
66 case StandardNames.XSL_ACCEPT:
67 case StandardNames.XSL_ACCUMULATOR:
68 case StandardNames.XSL_ACCUMULATOR_RULE:
69 case StandardNames.XSL_ANALYZE_STRING:
70 case StandardNames.XSL_APPLY_IMPORTS:
71 case StandardNames.XSL_APPLY_TEMPLATES:
72 case StandardNames.XSL_ASSERT:
73 case StandardNames.XSL_ATTRIBUTE:
74 case StandardNames.XSL_ATTRIBUTE_SET:
75 case StandardNames.XSL_BREAK:
76 case StandardNames.XSL_CALL_TEMPLATE:
77 case StandardNames.XSL_CATCH:
78 case StandardNames.XSL_CHARACTER_MAP:
79 case StandardNames.XSL_CHOOSE:
80 case StandardNames.XSL_COMMENT:
81 case StandardNames.XSL_CONTEXT_ITEM:
82 case StandardNames.XSL_COPY:
83 case StandardNames.XSL_COPY_OF:
84 case StandardNames.XSL_DECIMAL_FORMAT:
85 case StandardNames.XSL_DOCUMENT:
86 case StandardNames.XSL_ELEMENT:
87 case StandardNames.XSL_EVALUATE:
88 case StandardNames.XSL_EXPOSE:
89 case StandardNames.XSL_FALLBACK:
90 case StandardNames.XSL_FOR_EACH:
91 case StandardNames.XSL_FOR_EACH_GROUP:
92 case StandardNames.XSL_FORK:
93 case StandardNames.XSL_FUNCTION:
94 case StandardNames.XSL_GLOBAL_CONTEXT_ITEM:
95 case StandardNames.XSL_IF:
96 case StandardNames.XSL_IMPORT:
97 case StandardNames.XSL_IMPORT_SCHEMA:
98 case StandardNames.XSL_INCLUDE:
99 case StandardNames.XSL_ITERATE:
100 case StandardNames.XSL_KEY:
101 case StandardNames.XSL_MAP:
102 case StandardNames.XSL_MAP_ENTRY:
103 case StandardNames.XSL_MATCHING_SUBSTRING:
104 case StandardNames.XSL_MERGE:
105 case StandardNames.XSL_MERGE_ACTION:
106 case StandardNames.XSL_MERGE_KEY:
107 case StandardNames.XSL_MERGE_SOURCE:
108 case StandardNames.XSL_MESSAGE:
109 case StandardNames.XSL_MODE:
110 case StandardNames.XSL_NAMESPACE:
111 case StandardNames.XSL_NAMESPACE_ALIAS:
112 case StandardNames.XSL_NEXT_ITERATION:
113 case StandardNames.XSL_NEXT_MATCH:
114 case StandardNames.XSL_NON_MATCHING_SUBSTRING:
115 case StandardNames.XSL_NUMBER:
116 case StandardNames.XSL_ON_COMPLETION:
117 case StandardNames.XSL_ON_EMPTY:
118 case StandardNames.XSL_ON_NON_EMPTY:
119 case StandardNames.XSL_OTHERWISE:
120 case StandardNames.XSL_OUTPUT:
121 case StandardNames.XSL_OUTPUT_CHARACTER:
122 case StandardNames.XSL_OVERRIDE:
123 case StandardNames.XSL_PACKAGE:
124 case StandardNames.XSL_PARAM:
125 case StandardNames.XSL_PERFORM_SORT:
126 case StandardNames.XSL_PRESERVE_SPACE:
127 case StandardNames.XSL_PROCESSING_INSTRUCTION:
128 case StandardNames.XSL_RESULT_DOCUMENT:
129 case StandardNames.XSL_SEQUENCE:
130 case StandardNames.XSL_SORT:
131 case StandardNames.XSL_SOURCE_DOCUMENT:
132 case StandardNames.XSL_STRIP_SPACE:
133 case StandardNames.XSL_STYLESHEET:
134 case StandardNames.XSL_TEMPLATE:
135 case StandardNames.XSL_TEXT:
136 case StandardNames.XSL_TRANSFORM:
137 case StandardNames.XSL_TRY:
138 case StandardNames.XSL_USE_PACKAGE:
139 case StandardNames.XSL_VALUE_OF:
140 case StandardNames.XSL_VARIABLE:
141 case StandardNames.XSL_WHEN:
142 case StandardNames.XSL_WITH_PARAM:
143 case StandardNames.XSL_WHERE_POPULATED:
144 return true;
145 default:
146 return false;
147 }
148 }
149
150 public static boolean isSaxonJSElement(int fp) {
151 switch (fp) {
152 // case StandardNames.XSL_ACCEPT:
153 // case StandardNames.XSL_ACCUMULATOR:
154 // case StandardNames.XSL_ACCUMULATOR_RULE:
155 case StandardNames.XSL_ANALYZE_STRING:
156 case StandardNames.XSL_APPLY_IMPORTS:
157 case StandardNames.XSL_APPLY_TEMPLATES:
158 case StandardNames.XSL_ASSERT:
159 case StandardNames.XSL_ATTRIBUTE:
160 case StandardNames.XSL_ATTRIBUTE_SET:
161 // case StandardNames.XSL_BREAK:
162 case StandardNames.XSL_CALL_TEMPLATE:
163 case StandardNames.XSL_CATCH:
164 // case StandardNames.XSL_CHARACTER_MAP:
165 case StandardNames.XSL_CHOOSE:
166 case StandardNames.XSL_COMMENT:
167 // case StandardNames.XSL_CONTEXT_ITEM:
168 case StandardNames.XSL_COPY:
169 case StandardNames.XSL_COPY_OF:
170 case StandardNames.XSL_DECIMAL_FORMAT:
171 case StandardNames.XSL_DOCUMENT:
172 case StandardNames.XSL_ELEMENT:
173 // case StandardNames.XSL_EVALUATE:
174 // case StandardNames.XSL_EXPOSE:
175 case StandardNames.XSL_FALLBACK:
176 case StandardNames.XSL_FOR_EACH:
177 case StandardNames.XSL_FOR_EACH_GROUP:
178 // case StandardNames.XSL_FORK:
179 case StandardNames.XSL_FUNCTION:
180 // case StandardNames.XSL_GLOBAL_CONTEXT_ITEM:
181 case StandardNames.XSL_IF:
182 case StandardNames.XSL_IMPORT:
183 // case StandardNames.XSL_IMPORT_SCHEMA:
184 case StandardNames.XSL_INCLUDE:
185 // case StandardNames.XSL_ITERATE:
186 case StandardNames.XSL_KEY:
187 case StandardNames.XSL_MAP:
188 case StandardNames.XSL_MAP_ENTRY:
189 case StandardNames.XSL_MATCHING_SUBSTRING:
190 // case StandardNames.XSL_MERGE:
191 // case StandardNames.XSL_MERGE_ACTION:
192 // case StandardNames.XSL_MERGE_KEY:
193 // case StandardNames.XSL_MERGE_SOURCE:
194 case StandardNames.XSL_MESSAGE:
195 case StandardNames.XSL_MODE:
196 case StandardNames.XSL_NAMESPACE:
197 case StandardNames.XSL_NAMESPACE_ALIAS:
198 // case StandardNames.XSL_NEXT_ITERATION:
199 case StandardNames.XSL_NEXT_MATCH:
200 case StandardNames.XSL_NON_MATCHING_SUBSTRING:
201 case StandardNames.XSL_NUMBER:
202 // case StandardNames.XSL_ON_COMPLETION:
203 // case StandardNames.XSL_ON_EMPTY:
204 // case StandardNames.XSL_ON_NON_EMPTY:
205 case StandardNames.XSL_OTHERWISE:
206 // case StandardNames.XSL_OUTPUT:
207 // case StandardNames.XSL_OUTPUT_CHARACTER:
208 // case StandardNames.XSL_OVERRIDE:
209 // case StandardNames.XSL_PACKAGE:
210 case StandardNames.XSL_PARAM:
211 case StandardNames.XSL_PERFORM_SORT:
212 case StandardNames.XSL_PRESERVE_SPACE:
213 case StandardNames.XSL_PROCESSING_INSTRUCTION:
214 case StandardNames.XSL_RESULT_DOCUMENT:
215 case StandardNames.XSL_SEQUENCE:
216 case StandardNames.XSL_SORT:
217 // case StandardNames.XSL_STREAM:
218 case StandardNames.XSL_STRIP_SPACE:
219 case StandardNames.XSL_STYLESHEET:
220 case StandardNames.XSL_TEMPLATE:
221 case StandardNames.XSL_TEXT:
222 case StandardNames.XSL_TRANSFORM:
223 case StandardNames.XSL_TRY:
224 // case StandardNames.XSL_USE_PACKAGE:
225 case StandardNames.XSL_VALUE_OF:
226 case StandardNames.XSL_VARIABLE:
227 case StandardNames.XSL_WHEN:
228 case StandardNames.XSL_WITH_PARAM:
229 // case StandardNames.XSL_WHERE_POPULATED:
230 return true;
231 default:
232 return false;
233 }
234 }
235
236 /**
237 * Special-case for element-available('xsl:evaluate') which may be dynamically-disabled,
238 * and the spec says that this should be assessed at run-time. By indicating that the
239 * effect of the function depends on the run-time environment, early evaluation at compile
240 * time is suppressed.
241 */
242
243 @Override
244 public int getSpecialProperties(Expression[] arguments) {
245 try {
246 if (arguments[0] instanceof StringLiteral) {
247 String arg = ((StringLiteral) arguments[0]).getStringValue();
248 StructuredQName elem = getElementName(arg);
249 if (elem.hasURI(NamespaceConstant.XSLT) && elem.getLocalPart().equals("evaluate")) {
250 return super.getSpecialProperties(arguments) | StaticProperty.DEPENDS_ON_RUNTIME_ENVIRONMENT;
251 }
252 }
253 } catch (XPathException e) {
254 // drop through
255 }
256 return super.getSpecialProperties(arguments);
257 }
258
259 /**
260 * Determine whether a particular instruction is available. Returns true
261 * for XSLT instructions, Saxon extension instructions, and registered
262 * user-defined extension instructions. If the processor is an XSLT 3.0 processor,
263 * all XSLT *elements* are recognized; an XSLT 2.0 processor recognizes only
264 * XSLT 2.0 elements that are classified as *instructions*.
265 *
266 * @param lexicalName the lexical QName of the element
267 * @param edition the target edition that the stylesheet is to run under, e.g. "HE", "JS"
268 * @param context the XPath evaluation context
269 * @return true if the instruction is available, in the sense of the XSLT element-available() function
270 * @throws XPathException if a dynamic error occurs (e.g., a bad QName)
271 */
272
273 private boolean isElementAvailable(String lexicalName, String edition, XPathContext context) throws XPathException {
274
275 StructuredQName qName = getElementName(lexicalName);
276
277 if (qName.hasURI(NamespaceConstant.XSLT)) {
278 int fp = context.getConfiguration().getNamePool().getFingerprint(NamespaceConstant.XSLT, qName.getLocalPart());
279 boolean known = isXslt30Element(fp);
280 if ("JS".equals(edition)) {
281 known = known && isSaxonJSElement(fp);
282 }
283 if (fp == StandardNames.XSL_EVALUATE) {
284 known = known && !context.getConfiguration().getBooleanProperty(Feature.DISABLE_XSL_EVALUATE);
285 }
286 return known;
287 } else {
288 return context.getConfiguration().isExtensionElementAvailable(qName);
289 }
290 }
291
292 private StructuredQName getElementName(String lexicalName) throws XPathException {
293 try {
294 if (lexicalName.indexOf(':') < 0 && NameChecker.isValidNCName(lexicalName)) {
295 String uri = getRetainedStaticContext().getURIForPrefix("", true);
296 return new StructuredQName("", uri, lexicalName);
297 } else {
298 return StructuredQName.fromLexicalQName(lexicalName, false,
299 true, getRetainedStaticContext());
300 }
301 } catch (XPathException e) {
302 XPathException err = new XPathException("Invalid element name passed to element-available(): " + e.getMessage());
303 err.setErrorCode("XTDE1440");
304 throw err;
305 }
306 }
307
308 /**
309 * Evaluate the expression
310 *
311 * @param context the dynamic evaluation context
312 * @param arguments the values of the arguments, supplied as SequenceIterators
313 * @return the result of the evaluation, in the form of a SequenceIterator
314 * @throws net.sf.saxon.trans.XPathException
315 * if a dynamic error occurs during the evaluation of the expression
316 */
317 public BooleanValue call(XPathContext context, Sequence[] arguments) throws XPathException {
318 String lexicalQName = arguments[0].head().getStringValue();
319 boolean b = isElementAvailable(lexicalQName, getRetainedStaticContext().getPackageData().getTargetEdition(), context);
320 return BooleanValue.get(b);
321 }
322 }
323