| 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.XPathContext; |
| 11 | import net.sf.saxon.lib.ExtensionFunctionCall; |
| 12 | import net.sf.saxon.lib.ExtensionFunctionDefinition; |
| 13 | import net.sf.saxon.lib.NamespaceConstant; |
| 14 | import net.sf.saxon.om.Item; |
| 15 | import net.sf.saxon.om.NodeInfo; |
| 16 | import net.sf.saxon.om.Sequence; |
| 17 | import net.sf.saxon.om.StructuredQName; |
| 18 | import net.sf.saxon.trans.XPathException; |
| 19 | import net.sf.saxon.value.BooleanValue; |
| 20 | import net.sf.saxon.value.SequenceType; |
| 21 | |
| 22 | /** |
| 23 | * This class implements the saxon:is-idref() extension function, |
| 24 | * which is specially-recognized by the system because calls are generated by the optimizer. |
| 25 | * <p>The function signature is <code>saxon:is-idref() as boolean</code></p> |
| 26 | * <p>The result is true if the context item is a node having the idref property.</p> |
| 27 | */ |
| 28 | |
| 29 | public class IsIdRef extends ExtensionFunctionDefinition { |
| 30 | |
| 31 | private static final StructuredQName qName = |
| 32 | new StructuredQName("", NamespaceConstant.SAXON, "is-idref"); |
| 33 | |
| 34 | /** |
| 35 | * Get the function name, as a QName |
| 36 | * |
| 37 | * @return the QName of the function |
| 38 | */ |
| 39 | |
| 40 | public StructuredQName getFunctionQName() { |
| 41 | return qName; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get the minimum number of arguments required by the function |
| 46 | * |
| 47 | * @return the minimum number of arguments that must be supplied in a call to this function |
| 48 | */ |
| 49 | |
| 50 | public int getMinimumNumberOfArguments() { |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Get the maximum number of arguments allowed by the function |
| 56 | * |
| 57 | * @return the maximum number of arguments that may be supplied in a call to this function |
| 58 | */ |
| 59 | |
| 60 | public int getMaximumNumberOfArguments() { |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Get the required types for the arguments of this function, counting from zero |
| 66 | * |
| 67 | * @return the required types of the argument, as defined by the function signature. Normally |
| 68 | * this should be an array of size {@link #getMaximumNumberOfArguments()}; however for functions |
| 69 | * that allow a variable number of arguments, the array can be smaller than this, with the last |
| 70 | * entry in the array providing the required type for all the remaining arguments. |
| 71 | */ |
| 72 | |
| 73 | public SequenceType[] getArgumentTypes() { |
| 74 | return new SequenceType[]{}; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get the type of the result of the function |
| 79 | * |
| 80 | * @param suppliedArgumentTypes the static types of the arguments to the function. |
| 81 | * This is provided so that a more precise result type can be returned in the common |
| 82 | * case where the type of the result depends on the type of the first argument. The value |
| 83 | * will be null if the function call has no arguments. |
| 84 | * @return the return type of the function, as defined by its function signature |
| 85 | */ |
| 86 | |
| 87 | public SequenceType getResultType(SequenceType[] suppliedArgumentTypes) { |
| 88 | return SequenceType.SINGLE_BOOLEAN; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Create a call on this function. This method is called by the compiler when it identifies |
| 93 | * a function call that calls this function. |
| 94 | */ |
| 95 | |
| 96 | /*@NotNull*/ |
| 97 | public ExtensionFunctionCall makeCallExpression() { |
| 98 | return new IsIdRefCall(); |
| 99 | } |
| 100 | |
| 101 | private static class IsIdRefCall extends ExtensionFunctionCall { |
| 102 | |
| 103 | /** |
| 104 | * Evaluate this function call at run-time |
| 105 | * |
| 106 | * @param context The XPath dynamic evaluation context |
| 107 | * @param arguments The values of the arguments to the function call. Each argument value (which is in general |
| 108 | * a sequence) is supplied in the form of an iterator over the items in the sequence. If required, the |
| 109 | * supplied sequence can be materialized by calling, for example, <code>new SequenceExtent(arguments[i])</code>. |
| 110 | * If the argument is always a singleton, then the single item may be obtained by calling |
| 111 | * <code>arguments[i].next()</code>. The implementation is not obliged to read all the items in each |
| 112 | * <code>SequenceIterator</code> if they are not required to compute the result; but if any SequenceIterator is not read |
| 113 | * to completion, it is good practice to call its close() method. |
| 114 | * @return an iterator over the results of the function. If the result is a single item, it can be |
| 115 | * returned in the form of a {@link net.sf.saxon.tree.iter.SingletonIterator}. If the result is an empty sequence, |
| 116 | * the method should return <code>EmptyIterator.getInstance()</code> |
| 117 | * @throws net.sf.saxon.trans.XPathException |
| 118 | * if a dynamic error occurs during evaluation of the function. The Saxon run-time |
| 119 | * code will add information about the error location. |
| 120 | */ |
| 121 | |
| 122 | public BooleanValue call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 123 | Item contextItem = context.getContextItem(); |
| 124 | return BooleanValue.get(contextItem instanceof NodeInfo && ((NodeInfo)contextItem).isIdref()); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Get the effective boolean value of the expression. This returns false if the value |
| 129 | * is the empty sequence, a zero-length string, a number equal to zero, or the boolean |
| 130 | * false. Otherwise it returns true. |
| 131 | * |
| 132 | * @param context The context in which the expression is to be evaluated |
| 133 | * @return the effective boolean value |
| 134 | * @throws net.sf.saxon.trans.XPathException |
| 135 | * if any dynamic error occurs evaluating the |
| 136 | * expression |
| 137 | */ |
| 138 | |
| 139 | public boolean effectiveBooleanValue(XPathContext context, Sequence[] arguments) throws XPathException { |
| 140 | Item contextItem = context.getContextItem(); |
| 141 | return contextItem instanceof NodeInfo && ((NodeInfo) contextItem).isIdref(); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | } |
| 146 | |
| 147 | } |
| 148 |