Debian Codegraph @

net/sf/saxon/functions/registry/VendorFunctionSetHE.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.registry;
9
10 import net.sf.saxon.expr.Expression;
11 import net.sf.saxon.expr.Literal;
12 import net.sf.saxon.expr.XPathContext;
13 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
14 import net.sf.saxon.expr.parser.ExpressionVisitor;
15 import net.sf.saxon.expr.parser.XPathParser;
16 import net.sf.saxon.functions.ApplyFn;
17 import net.sf.saxon.functions.Doc_2;
18 import net.sf.saxon.functions.SystemFunction;
19 import net.sf.saxon.lib.NamespaceConstant;
20 import net.sf.saxon.ma.arrays.ArrayItemType;
21 import net.sf.saxon.ma.map.MapCreate;
22 import net.sf.saxon.ma.map.MapType;
23 import net.sf.saxon.ma.map.MapUntypedContains;
24 import net.sf.saxon.om.NamespaceBinding;
25 import net.sf.saxon.om.NodeInfo;
26 import net.sf.saxon.om.Sequence;
27 import net.sf.saxon.om.StructuredQName;
28 import net.sf.saxon.pattern.NodeKindTest;
29 import net.sf.saxon.trans.XPathException;
30 import net.sf.saxon.tree.tiny.TinyElementImpl;
31 import net.sf.saxon.type.*;
32 import net.sf.saxon.value.*;
33
34 import javax.xml.transform.SourceLocator;
35
36
37 /**
38 * Implementation of vendor functions in the Saxon namespace, available in Saxon-HE because they
39 * are used internally. This library is available in all Saxon versions.
40 */
41 public class VendorFunctionSetHE extends BuiltInFunctionSet {
42
43 private static VendorFunctionSetHE THE_INSTANCE = new VendorFunctionSetHE();
44
45 public static VendorFunctionSetHE getInstance() {
46 return THE_INSTANCE;
47 }
48
49 private VendorFunctionSetHE() {
50 init();
51 }
52
53 private void init() {
54
55 // Test whether supplied argument is equal to an integer
56 register("is-whole-number", 1, IsWholeNumberFn.class, BuiltInAtomicType.BOOLEAN, ONE, 0, 0)
57 .arg(0, NumericType.getInstance(), OPT, EMPTY);
58
59 // Evaluate the value of a try-catch variable such as $err:code
60 register("dynamic-error-info", 1, DynamicErrorInfoFn.class, AnyItemType.getInstance(), STAR, 0, FOCUS | LATE | SIDE)
61 .arg(0, BuiltInAtomicType.STRING, ONE, null);
62
63 // saxon:apply is the same as fn:apply, but does not require the HOF feature
64 register("apply", 2, ApplyFn.class, AnyItemType.getInstance(), STAR, 0, LATE)
65 .arg(0, AnyFunctionType.getInstance(), ONE, null)
66 .arg(1, ArrayItemType.ANY_ARRAY_TYPE, ONE, null);
67
68 // Create a map according to the semantics of the XPath map constructor and XSLT xsl:map instruction
69 register("create-map", 1, MapCreate.class, MapType.ANY_MAP_TYPE, ONE, 0, 0)
70 .arg(0, MapType.ANY_MAP_TYPE, STAR, null);
71
72 // Variant of the doc() function with an options parameter
73 register("doc", 2, Doc_2.class, NodeKindTest.DOCUMENT, ONE, 0, LATE)
74 .arg(0, BuiltInAtomicType.STRING, ONE, null)
75 .arg(1, MapType.ANY_MAP_TYPE, ONE, EMPTY)
76 .optionDetails(Doc_2.makeOptionsParameter());
77
78 // Ask whether the supplied element node has any local namespace declarations
79 register("has-local-namespaces", 1, HasLocalNamespaces.class, BuiltInAtomicType.BOOLEAN, ONE, 0, 0)
80 .arg(0, NodeKindTest.ELEMENT, ONE, null);
81
82 // Ask whether the supplied element node has consistent in scope namespaces throughout its subtree
83 register("has-uniform-namespaces", 1, HasUniformNamespaces.class, BuiltInAtomicType.BOOLEAN, ONE, 0, 0)
84 .arg(0, NodeKindTest.ELEMENT, ONE, null);
85
86 // Function analogous to map:contains except in the way it handles untyped key values
87 register("map-untyped-contains", 2, MapUntypedContains.class, BuiltInAtomicType.BOOLEAN, ONE, 0, 0)
88 .arg(0, MapType.ANY_MAP_TYPE, STAR, null)
89 .arg(1, BuiltInAtomicType.ANY_ATOMIC, ONE, null);
90
91 }
92
93 @Override
94 public String getNamespace() {
95 return NamespaceConstant.SAXON;
96 }
97
98 @Override
99 public String getConventionalPrefix() {
100 return "saxon";
101 }
102
103 /**
104 * Implement saxon:is-whole-number
105 */
106
107 public static class IsWholeNumberFn extends SystemFunction {
108 /**
109 * Allow the function to create an optimized call based on the values of the actual arguments
110 *
111 * @param visitor the expression visitor
112 * @param contextInfo information about the context item
113 * @param arguments the supplied arguments to the function call. Note: modifying the contents
114 * of this array should not be attempted, it is likely to have no effect.
115 * @return either a function call on this function, or an expression that delivers
116 * the same result, or null indicating that no optimization has taken place
117 * @throws XPathException if an error is detected
118 */
119 @Override
120 public Expression makeOptimizedFunctionCall(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, Expression... arguments) throws XPathException {
121 if (arguments[0].getItemType().getPrimitiveItemType() == BuiltInAtomicType.INTEGER) {
122 return Literal.makeLiteral(BooleanValue.TRUE);
123 }
124 return null;
125 }
126
127 public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException {
128 NumericValue val = (NumericValue) arguments[0].head();
129 return BooleanValue.get(val != null && val.isWholeNumber());
130 }
131
132 }
133
134 /**
135 * Implement saxon:has-local-namespaces. The function takes an element node as input and returns
136 * true if (a) the element is parentless, or (b) the parent is a document node, or (c) the
137 * element has namespace declarations or undeclarations that differ from those of the parent
138 * element (that is, if its in-scope namespace bindings are different from those of the parent
139 * element).
140 *
141 * <p>This function is provided for use by the XSLT-compiler-in-XSLT, so that it can decide
142 * efficiently whether to generate an "ns" element containing namespace bindings in the SEF file.</p>
143 */
144
145 public static class HasLocalNamespaces extends SystemFunction {
146
147 // TODO: this is experimental and not yet publicly documented
148
149 public BooleanValue call(XPathContext context, Sequence[] arguments) throws XPathException {
150 NodeInfo val = (NodeInfo) arguments[0].head();
151 NodeInfo parent = val.getParent();
152 return BooleanValue.get(
153 parent == null || parent.getNodeKind() == Type.DOCUMENT ||
154 val.getDeclaredNamespaces(NamespaceBinding.EMPTY_ARRAY).length > 0);
155 }
156
157 }
158
159 /**
160 * Implement saxon:has-uniform-namespaces. The function takes an element node as input and returns
161 * true if it can be guaranteed that all descendant elements have the same namespace context as
162 * the target element. (If the result is false, there may or may not be descendant elements
163 * with new namespace declarations or undeclarations.)
164 *
165 * <p>This function is provided for use by the XSLT-compiler-in-XSLT, so that it can decide
166 * efficiently whether to generate an "ns" element containing namespace bindings in the SEF file.</p>
167 *
168 * <p>Currently returns false for any non-TinyTree element.</p>
169 */
170
171 public static class HasUniformNamespaces extends SystemFunction {
172
173 // TODO: this is experimental and not yet publicly documented
174
175 public BooleanValue call(XPathContext context, Sequence[] arguments) throws XPathException {
176 NodeInfo val = (NodeInfo) arguments[0].head();
177 if (val instanceof TinyElementImpl) {
178 return BooleanValue.get(((TinyElementImpl)val).hasUniformNamespaces());
179 } else {
180 return BooleanValue.FALSE;
181 }
182 }
183
184 }
185
186
187 /**
188 * Implement saxon:dynamic-error-info
189 */
190
191 public static class DynamicErrorInfoFn extends SystemFunction {
192
193 /**
194 * Determine the special properties of this function. The general rule
195 * is that a system function call is non-creative unless more details
196 * are defined in a subclass.
197 *
198 * @param arguments the actual arguments supplied in a call to the function
199 */
200 @Override
201 public int getSpecialProperties(Expression[] arguments) {
202 return 0; // treat as creative to avoid loop-lifting: test case try-catch-err-code-variable-14
203 }
204
205 public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException {
206 String var = arguments[0].head().getStringValue();
207 @SuppressWarnings({"ThrowableResultOfMethodCallIgnored"})
208 XPathException error = context.getCurrentException();
209 if (error == null) {
210 return EmptySequence.getInstance();
211 }
212 SourceLocator locator = error.getLocator();
213 switch (var) {
214 case "code":
215 StructuredQName errorCodeQName = error.getErrorCodeQName();
216 if (errorCodeQName == null) {
217 errorCodeQName = new StructuredQName("saxon", NamespaceConstant.SAXON, "XXXX9999");
218 }
219 return new QNameValue(errorCodeQName, BuiltInAtomicType.QNAME);
220 case "description":
221 String s = error.getMessage();
222 if (error.getCause() != null) {
223 s += "(" + error.getCause().getMessage() + ")";
224 }
225 return new StringValue(s);
226 case "value":
227 Sequence<?> value = error.getErrorObject();
228 if (value == null) {
229 return EmptySequence.getInstance();
230 } else {
231 return value;
232 }
233 case "module":
234 String module = locator == null ? null : locator.getSystemId();
235 if (module == null) {
236 return EmptySequence.getInstance();
237 } else {
238 return new StringValue(module);
239 }
240 case "line-number":
241 int line = locator == null ? -1 : locator.getLineNumber();
242 if (line == -1) {
243 return EmptySequence.getInstance();
244 } else {
245 return new Int64Value(line);
246 }
247 case "column-number":
248 // Bug 4144
249 int column = -1;
250 if (locator == null) {
251 return EmptySequence.getInstance();
252 } else if (locator instanceof XPathParser.NestedLocation) {
253 column = ((XPathParser.NestedLocation)locator).getContainingLocation().getColumnNumber();
254 } else {
255 column = locator.getColumnNumber();
256 }
257 if (column == -1) {
258 return EmptySequence.getInstance();
259 } else {
260 return new Int64Value(column);
261 }
262 default:
263 return EmptySequence.getInstance();
264 }
265
266 }
267 }
268
269
270
271 }
272
273 // Copyright (c) 2018 Saxonica Limited.