| 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.sxpath; |
| 9 | |
| 10 | import net.sf.saxon.expr.LocalBinding; |
| 11 | import net.sf.saxon.expr.VariableReference; |
| 12 | import net.sf.saxon.expr.XPathContext; |
| 13 | import net.sf.saxon.om.Item; |
| 14 | import net.sf.saxon.om.Sequence; |
| 15 | import net.sf.saxon.om.StructuredQName; |
| 16 | import net.sf.saxon.value.IntegerValue; |
| 17 | import net.sf.saxon.value.SequenceType; |
| 18 | |
| 19 | |
| 20 | /** |
| 21 | * An object representing an XPath variable for use in the standalone XPath API. The object |
| 22 | * can only be created by calling the declareVariable method of class {@link IndependentContext}. |
| 23 | * Note that once declared, this object is thread-safe: it does not hold the actual variable |
| 24 | * value, which means it can be used with any number of evaluations of a given XPath expression, |
| 25 | * in series or in parallel. |
| 26 | * <p>A variable can be given a value by calling |
| 27 | * {@link XPathDynamicContext#setVariable(XPathVariable, net.sf.saxon.om.Sequence)}. |
| 28 | * Note that the value of the variable is not held in the XPathVariable object, but in the |
| 29 | * XPathDynamicContext, which means that the XPathVariable itself can be used in multiple threads.</p> |
| 30 | */ |
| 31 | |
| 32 | public final class XPathVariable implements LocalBinding { |
| 33 | |
| 34 | private StructuredQName name; |
| 35 | private SequenceType requiredType = SequenceType.ANY_SEQUENCE; |
| 36 | private Sequence<?> defaultValue; |
| 37 | private int slotNumber; |
| 38 | |
| 39 | /** |
| 40 | * Private constructor: for use only by the protected factory method make() |
| 41 | */ |
| 42 | |
| 43 | private XPathVariable() { |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Factory method, for use by the declareVariable method of class IndependentContext |
| 48 | * |
| 49 | * @param name the name of the variable to create |
| 50 | * @return the constructed XPathVariable |
| 51 | */ |
| 52 | |
| 53 | protected static XPathVariable make(StructuredQName name) { |
| 54 | XPathVariable v = new XPathVariable(); |
| 55 | v.name = name; |
| 56 | return v; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Ask whether the binding is local or global. A global binding is one that has a fixed |
| 61 | * value for the life of a query or transformation; any other binding is local. An XPath |
| 62 | * variable is treated as a local variable (largely because it is held on the stack frame) |
| 63 | * |
| 64 | * @return false (always) |
| 65 | */ |
| 66 | |
| 67 | public boolean isGlobal() { |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Test whether it is permitted to assign to the variable using the saxon:assign |
| 73 | * extension element. This will only be for an XSLT global variable where the extra |
| 74 | * attribute saxon:assignable="yes" is present. |
| 75 | * |
| 76 | * @return false (always) |
| 77 | */ |
| 78 | |
| 79 | public final boolean isAssignable() { |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Set the required type of this variable. If no required type is specified, |
| 85 | * the type <code>item()*</code> is assumed. |
| 86 | * |
| 87 | * @param requiredType the required type |
| 88 | */ |
| 89 | |
| 90 | public void setRequiredType(SequenceType requiredType) { |
| 91 | this.requiredType = requiredType; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Get the required type of this variable. If no required type has been specified, |
| 96 | * the type <code>item()*</code> is returned. |
| 97 | * |
| 98 | * @return the required type of the variable |
| 99 | */ |
| 100 | |
| 101 | public SequenceType getRequiredType() { |
| 102 | return requiredType; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * If the variable is bound to an integer, get the minimum and maximum possible values. |
| 107 | * Return null if unknown or not applicable |
| 108 | */ |
| 109 | /*@Nullable*/ |
| 110 | public IntegerValue[] getIntegerBoundsForVariable() { |
| 111 | return null; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Set the slot number allocated to this variable. This method is for internal use. |
| 116 | * |
| 117 | * @param slotNumber the slot number to be allocated |
| 118 | */ |
| 119 | |
| 120 | public void setSlotNumber(int slotNumber) { |
| 121 | this.slotNumber = slotNumber; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * If this is a local variable held on the local stack frame, return the corresponding slot number. |
| 126 | * In other cases, return -1. |
| 127 | */ |
| 128 | |
| 129 | public int getLocalSlotNumber() { |
| 130 | return slotNumber; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Get the name of the variable as a QNameValue. |
| 135 | * |
| 136 | * @return the name of the variable, as a QNameValue |
| 137 | */ |
| 138 | |
| 139 | public StructuredQName getVariableQName() { |
| 140 | return name; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Method called by the XPath expression parser to register a reference to this variable. |
| 145 | * This method should not be called by users of the API. |
| 146 | */ |
| 147 | |
| 148 | public void addReference(VariableReference ref, boolean isLoopingReference) { |
| 149 | // no action |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Set a default value for the variable, to be used if no specific value is |
| 154 | * supplied when the expression is evaluated |
| 155 | * |
| 156 | * @param defaultValue the default value for the variable |
| 157 | */ |
| 158 | |
| 159 | public void setDefaultValue(Sequence<?> defaultValue) { |
| 160 | this.defaultValue = defaultValue; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Get the default value of the variable |
| 165 | * |
| 166 | * @return the default value if one has been registered, or null otherwise |
| 167 | */ |
| 168 | |
| 169 | public Sequence<?> getDefaultValue() { |
| 170 | return defaultValue; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Get the value of the variable. This method is used by the XPath execution engine |
| 175 | * to retrieve the value. Note that the value is not held within the variable itself, |
| 176 | * but within the dynamic context. |
| 177 | * |
| 178 | * @param context The dynamic evaluation context |
| 179 | * @return The value of the variable |
| 180 | */ |
| 181 | |
| 182 | public Sequence<?> evaluateVariable(XPathContext context) { |
| 183 | return context.evaluateLocalVariable(slotNumber); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Say that the bound value has the potential to be indexed |
| 188 | */ |
| 189 | public void setIndexedVariable() { } |
| 190 | |
| 191 | /** |
| 192 | * Ask whether the binding is to be indexed |
| 193 | * |
| 194 | * @return true if the variable value can be indexed |
| 195 | */ |
| 196 | public boolean isIndexedVariable() { |
| 197 | return false; |
| 198 | } |
| 199 | } |
| 200 |