Debian Codegraph @

net/sf/saxon/functions/FunctionAvailable.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.*;
11 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
12 import net.sf.saxon.expr.parser.ExpressionVisitor;
13 import net.sf.saxon.lib.NamespaceConstant;
14 import net.sf.saxon.om.NameChecker;
15 import net.sf.saxon.om.QNameParser;
16 import net.sf.saxon.om.Sequence;
17 import net.sf.saxon.om.StructuredQName;
18 import net.sf.saxon.style.StylesheetPackage;
19 import net.sf.saxon.trans.SymbolicName;
20 import net.sf.saxon.trans.XPathException;
21 import net.sf.saxon.value.BooleanValue;
22 import net.sf.saxon.value.NumericValue;
23
24 /**
25 * This class supports the XSLT element-available and function-available functions.
26 */
27
28 public class FunctionAvailable extends SystemFunction {
29
30 @Override
31 public Expression makeFunctionCall(Expression... arguments) {
32 PackageData pack = getRetainedStaticContext().getPackageData();
33 if (pack instanceof StylesheetPackage) {
34 ((StylesheetPackage) pack).setRetainUnusedFunctions();
35 }
36 return super.makeFunctionCall(arguments);
37 }
38
39 /**
40 * Allow the function to create an optimized call based on the values of the actual arguments
41 *
42 * @param visitor the expression visitor
43 * @param contextInfo information about the context item
44 * @param arguments the supplied arguments to the function call
45 * @return either a function call on this function, or an expression that delivers
46 * the same result, or null indicating that no optimization has taken place
47 * @throws net.sf.saxon.trans.XPathException if an error is detected
48 */
49 @Override
50 public Expression makeOptimizedFunctionCall(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, Expression... arguments) throws XPathException {
51
52 // Note, the LATE property is set in the function details to avoid the function being evaluated by the preEvaluate() call.
53 // This is because the full static context is needed, not the (smaller) RetainedStaticContext. Instead, pre-evaluation
54 // for calls with fixed arguments is done during the optimization phase, which makes the full static context available.
55
56 if (arguments[0] instanceof Literal && (arguments.length == 1 || arguments[1] instanceof Literal)) {
57 String lexicalQName = ((Literal) arguments[0]).getValue().getStringValue();
58 StaticContext env = visitor.getStaticContext();
59 boolean b = false;
60
61 QNameParser qp = new QNameParser(getRetainedStaticContext());
62 qp.setAcceptEQName(true);
63 qp.setDefaultNamespace(env.getDefaultFunctionNamespace());
64 qp.setErrorOnBadSyntax("XTDE1400");
65 qp.setErrorOnUnresolvedPrefix("XTDE1400");
66
67 StructuredQName functionName = qp.parse(lexicalQName);
68
69 int minArity = 0;
70 int maxArity = 20;
71 if (getArity() == 2) {
72 minArity = (int) ((NumericValue) arguments[1].evaluateItem(env.makeEarlyEvaluationContext())).longValue();
73 maxArity = minArity;
74 }
75
76 for (int i = minArity; i <= maxArity; i++) {
77 SymbolicName.F sn = new SymbolicName.F(functionName, i);
78 if (env.getFunctionLibrary().isAvailable(sn)) {
79 b = true;
80 break;
81 }
82 }
83
84 return Literal.makeLiteral(BooleanValue.get(b));
85 } else {
86 return null;
87 }
88 }
89
90 private boolean isFunctionAvailable(String lexicalName, String edition, int arity, XPathContext context) throws XPathException {
91 if (arity == -1) {
92 for (int i = 0; i < 20; i++) {
93 if (isFunctionAvailable(lexicalName, edition, i, context)) {
94 return true;
95 }
96 }
97 return false;
98 }
99 StructuredQName qName;
100 try {
101 if (NameChecker.isValidNCName(lexicalName)) {
102 // we're in XSLT, where the default namespace for functions can't be changed
103 String uri = NamespaceConstant.FN;
104 qName = new StructuredQName("", uri, lexicalName);
105 } else {
106 qName = StructuredQName.fromLexicalQName(lexicalName,
107 false, true,
108 getRetainedStaticContext());
109 }
110 } catch (XPathException e) {
111 e.setErrorCode("XTDE1400");
112 e.setXPathContext(context);
113 throw e;
114 }
115
116 final FunctionLibrary lib = context.getController().getExecutable().getFunctionLibrary();
117 SymbolicName.F sn = new SymbolicName.F(qName, arity);
118 // TODO: reinstate something along these lines. Removed because it doesn't build in HE 9.8
119 // if (known && qName.hasURI(NamespaceConstant.FN) && !context.getConfiguration().getEditionCode().equals(edition)) {
120 // // Target environment differs from compile-time environment: some functions might not be available
121 // BuiltInFunctionSet.Entry details = XPath31HOFunctionSet.getInstance().getFunctionDetails(qName.getLocalPart(), sn.getArity());
122 // if (details != null) {
123 // if (((details.applicability & BuiltInFunctionSet.HOF) != 0) && ("HE".equals(edition) || "JS".equals(edition))) {
124 // return false;
125 // }
126 // // TODO: some further functions are not available in Saxon-JS
127 // }
128 // }
129 return lib.isAvailable(sn);
130 }
131
132 /**
133 * Evaluate the expression
134 *
135 * @param context the dynamic evaluation context
136 * @param arguments the values of the arguments, supplied as SequenceIterators
137 * @return the result of the evaluation, in the form of a SequenceIterator
138 * @throws net.sf.saxon.trans.XPathException
139 * if a dynamic error occurs during the evaluation of the expression
140 */
141 public BooleanValue call(XPathContext context, Sequence[] arguments) throws XPathException {
142 String lexicalQName = arguments[0].head().getStringValue();
143 int arity = -1;
144 if (arguments.length == 2) {
145 arity = (int) ((NumericValue) arguments[1].head()).longValue();
146 }
147 return BooleanValue.get(
148 isFunctionAvailable(lexicalQName, getRetainedStaticContext().getPackageData().getTargetEdition(), arity, context));
149 }
150 }
151