| 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.expr; |
| 9 | |
| 10 | import net.sf.saxon.Configuration; |
| 11 | import net.sf.saxon.expr.parser.*; |
| 12 | import net.sf.saxon.om.Item; |
| 13 | import net.sf.saxon.om.SequenceIterator; |
| 14 | import net.sf.saxon.pattern.AnchorPattern; |
| 15 | import net.sf.saxon.pattern.Pattern; |
| 16 | import net.sf.saxon.trace.ExpressionPresenter; |
| 17 | import net.sf.saxon.trans.XPathException; |
| 18 | import net.sf.saxon.tree.iter.SingletonIterator; |
| 19 | import net.sf.saxon.type.AnyItemType; |
| 20 | import net.sf.saxon.type.ErrorType; |
| 21 | import net.sf.saxon.type.ItemType; |
| 22 | import net.sf.saxon.type.UType; |
| 23 | |
| 24 | |
| 25 | /** |
| 26 | * This class represents the expression "(dot)", which always returns the context item. |
| 27 | * This may be a AtomicValue or a Node. |
| 28 | */ |
| 29 | |
| 30 | public class ContextItemExpression extends Expression { |
| 31 | |
| 32 | ContextItemStaticInfo staticInfo = ContextItemStaticInfo.DEFAULT; |
| 33 | String errorCodeForAbsentContext = "XPDY0002"; |
| 34 | |
| 35 | /** |
| 36 | * Create the expression |
| 37 | */ |
| 38 | |
| 39 | public ContextItemExpression() { |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Get a name identifying the kind of expression, in terms meaningful to a user. |
| 44 | * |
| 45 | * @return a name identifying the kind of expression, in terms meaningful to a user. |
| 46 | * The name will always be in the form of a lexical XML QName, and should match the name used |
| 47 | * in explain() output displaying the expression. |
| 48 | */ |
| 49 | |
| 50 | public String getExpressionName() { |
| 51 | return "dot"; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Create a clone copy of this expression |
| 56 | * |
| 57 | * @return a copy of this expression |
| 58 | * @param rebindings |
| 59 | */ |
| 60 | |
| 61 | /*@NotNull*/ |
| 62 | public Expression copy(RebindingMap rebindings) { |
| 63 | ContextItemExpression cie2 = new ContextItemExpression(); |
| 64 | cie2.staticInfo = staticInfo; |
| 65 | cie2.setErrorCodeForUndefinedContext(errorCodeForAbsentContext); |
| 66 | ExpressionTool.copyLocationInfo(this, cie2); |
| 67 | return cie2; |
| 68 | } |
| 69 | |
| 70 | public void setErrorCodeForUndefinedContext(String errorCode) { |
| 71 | errorCodeForAbsentContext = errorCode; |
| 72 | } |
| 73 | |
| 74 | public String getErrorCodeForUndefinedContext() { |
| 75 | return errorCodeForAbsentContext; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Set static information about the context item |
| 80 | * @param info static information about the context item |
| 81 | */ |
| 82 | |
| 83 | public void setStaticInfo(ContextItemStaticInfo info) { |
| 84 | staticInfo = info; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Type-check the expression. |
| 89 | */ |
| 90 | |
| 91 | /*@NotNull*/ |
| 92 | public Expression typeCheck(ExpressionVisitor visitor, /*@Nullable*/ ContextItemStaticInfo contextInfo) throws XPathException { |
| 93 | if (contextInfo.getItemType() == ErrorType.getInstance()) { |
| 94 | visitor.issueWarning("Evaluation will always fail: there is no context item", getLocation()); |
| 95 | ErrorExpression ee = new ErrorExpression("There is no context item", getErrorCodeForUndefinedContext(), true); |
| 96 | ee.setOriginalExpression(this); |
| 97 | ExpressionTool.copyLocationInfo(this, ee); |
| 98 | return ee; |
| 99 | } else { |
| 100 | staticInfo = contextInfo; |
| 101 | } |
| 102 | return this; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Perform optimisation of an expression and its subexpressions. |
| 107 | * <p>This method is called after all references to functions and variables have been resolved |
| 108 | * to the declaration of the function or variable, and after all type checking has been done.</p> |
| 109 | * |
| 110 | * @param visitor an expression visitor |
| 111 | * @param contextItemType the static type of "." at the point where this expression is invoked. |
| 112 | * The parameter is set to null if it is known statically that the context item will be undefined. |
| 113 | * If the type of the context item is not known statically, the argument is set to |
| 114 | * {@link net.sf.saxon.type.Type#ITEM_TYPE} |
| 115 | * @return the original expression, rewritten if appropriate to optimize execution |
| 116 | * @throws XPathException if an error is discovered during this phase |
| 117 | * (typically a type error) |
| 118 | */ |
| 119 | |
| 120 | /*@NotNull*/ |
| 121 | public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 122 | // In XSLT, we don't catch this error at the typeCheck() phase because it's done one XPath expression |
| 123 | // at a time. So we repeat the check here. |
| 124 | if (contextItemType == null) { |
| 125 | XPathException err = new XPathException("The context item is undefined at this point"); |
| 126 | err.setErrorCode(getErrorCodeForUndefinedContext()); |
| 127 | err.setIsTypeError(true); |
| 128 | err.setLocation(getLocation()); |
| 129 | throw err; |
| 130 | } |
| 131 | return this; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Determine the item type |
| 136 | */ |
| 137 | |
| 138 | /*@NotNull*/ |
| 139 | public ItemType getItemType() { |
| 140 | return staticInfo.getItemType(); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Get the static type of the expression as a UType, following precisely the type |
| 145 | * inference rules defined in the XSLT 3.0 specification. |
| 146 | * |
| 147 | * @param contextItemType the static type of the context item |
| 148 | * @return the static item type of the expression according to the XSLT 3.0 defined rules |
| 149 | */ |
| 150 | |
| 151 | public UType getStaticUType(UType contextItemType) { |
| 152 | return contextItemType; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | /** |
| 157 | * Ask whether the context item may possibly be undefined |
| 158 | * |
| 159 | * @return true if it might be undefined |
| 160 | */ |
| 161 | |
| 162 | public boolean isContextPossiblyUndefined() { |
| 163 | return staticInfo.isPossiblyAbsent(); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Get the static cardinality |
| 168 | */ |
| 169 | |
| 170 | public int computeCardinality() { |
| 171 | return StaticProperty.EXACTLY_ONE; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Determine the special properties of this expression |
| 176 | * |
| 177 | * @return the value {@link StaticProperty#NO_NODES_NEWLY_CREATED} |
| 178 | */ |
| 179 | |
| 180 | public int computeSpecialProperties() { |
| 181 | int p = super.computeSpecialProperties(); |
| 182 | return p | StaticProperty.NO_NODES_NEWLY_CREATED | StaticProperty.CONTEXT_DOCUMENT_NODESET; |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public int getImplementationMethod() { |
| 187 | return EVALUATE_METHOD; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Is this expression the same as another expression? |
| 192 | */ |
| 193 | |
| 194 | public boolean equals(Object other) { |
| 195 | return other instanceof ContextItemExpression; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * get HashCode for comparing two expressions |
| 200 | */ |
| 201 | |
| 202 | public int computeHashCode() { |
| 203 | return "ContextItemExpression".hashCode(); |
| 204 | } |
| 205 | |
| 206 | public int getIntrinsicDependencies() { |
| 207 | return StaticProperty.DEPENDS_ON_CONTEXT_ITEM; |
| 208 | } |
| 209 | |
| 210 | |
| 211 | /** |
| 212 | * Add a representation of this expression to a PathMap. The PathMap captures a map of the nodes visited |
| 213 | * by an expression in a source tree. |
| 214 | * <p>The default implementation of this method assumes that an expression does no navigation other than |
| 215 | * the navigation done by evaluating its subexpressions, and that the subexpressions are evaluated in the |
| 216 | * same context as the containing expression. The method must be overridden for any expression |
| 217 | * where these assumptions do not hold. For example, implementations exist for AxisExpression, ParentExpression, |
| 218 | * and RootExpression (because they perform navigation), and for the doc(), document(), and collection() |
| 219 | * functions because they create a new navigation root. Implementations also exist for PathExpression and |
| 220 | * FilterExpression because they have subexpressions that are evaluated in a different context from the |
| 221 | * calling expression.</p> |
| 222 | * |
| 223 | * @param pathMap the PathMap to which the expression should be added |
| 224 | * @param pathMapNodeSet the PathMapNodeSet to which the paths embodied in this expression should be added |
| 225 | * @return the pathMapNodeSet representing the points in the source document that are both reachable by this |
| 226 | * expression, and that represent possible results of this expression. For an expression that does |
| 227 | * navigation, it represents the end of the arc in the path map that describes the navigation route. For other |
| 228 | * expressions, it is the same as the input pathMapNode. |
| 229 | */ |
| 230 | |
| 231 | public PathMap.PathMapNodeSet addToPathMap(PathMap pathMap, PathMap.PathMapNodeSet pathMapNodeSet) { |
| 232 | if (pathMapNodeSet == null) { |
| 233 | pathMapNodeSet = new PathMap.PathMapNodeSet(pathMap.makeNewRoot(this)); |
| 234 | } |
| 235 | return pathMapNodeSet; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Determine whether the expression can be evaluated without reference to the part of the context |
| 240 | * document outside the subtree rooted at the context node. |
| 241 | * |
| 242 | * @return true if the expression has no dependencies on the context node, or if the only dependencies |
| 243 | * on the context node are downward selections using the self, child, descendant, attribute, and namespace |
| 244 | * axes. |
| 245 | */ |
| 246 | |
| 247 | public boolean isSubtreeExpression() { |
| 248 | return true; |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public int getNetCost() { |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Convert this expression to an equivalent XSLT pattern |
| 258 | * |
| 259 | * @param config the Saxon configuration |
| 260 | * @return the equivalent pattern |
| 261 | * @throws net.sf.saxon.trans.XPathException |
| 262 | * if conversion is not possible |
| 263 | */ |
| 264 | @Override |
| 265 | public Pattern toPattern(Configuration config) throws XPathException { |
| 266 | return AnchorPattern.getInstance(); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Get the (partial) name of a class that supports streaming of this kind of expression |
| 271 | * |
| 272 | * @return the partial name of a class that can be instantiated to provide streaming support in Saxon-EE, |
| 273 | * or null if there is no such class |
| 274 | */ |
| 275 | @Override |
| 276 | public String getStreamerName() { |
| 277 | return "ContextItemExpr"; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Iterate over the value of the expression |
| 282 | */ |
| 283 | |
| 284 | /*@NotNull*/ |
| 285 | public SequenceIterator<?> iterate(XPathContext context) throws XPathException { |
| 286 | Item<?> item = context.getContextItem(); |
| 287 | if (item == null) { |
| 288 | dynamicError("The context item is absent", getErrorCodeForUndefinedContext(), context); |
| 289 | } |
| 290 | return SingletonIterator.makeIterator(item); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Evaluate the expression |
| 295 | */ |
| 296 | |
| 297 | public Item evaluateItem(XPathContext context) throws XPathException { |
| 298 | Item item = context.getContextItem(); |
| 299 | if (item == null) { |
| 300 | dynamicError("The context item is absent", getErrorCodeForUndefinedContext(), context); |
| 301 | } |
| 302 | return item; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * The toString() method for an expression attempts to give a representation of the expression |
| 307 | * in an XPath-like form, but there is no guarantee that the syntax will actually be true XPath. |
| 308 | * In the case of XSLT instructions, the toString() method gives an abstracted view of the syntax |
| 309 | */ |
| 310 | |
| 311 | public String toString() { |
| 312 | return "."; |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Diagnostic print of expression structure. The abstract expression tree |
| 317 | * is written to the supplied output destination. |
| 318 | */ |
| 319 | |
| 320 | public void export(ExpressionPresenter destination) throws XPathException { |
| 321 | destination.startElement("dot", this); |
| 322 | ItemType type = getItemType(); |
| 323 | if (!(type == AnyItemType.getInstance())) { |
| 324 | destination.emitAttribute("type", type.toExportString()); |
| 325 | } |
| 326 | if (staticInfo.isPossiblyAbsent()) { |
| 327 | destination.emitAttribute("flags", "a"); |
| 328 | } |
| 329 | destination.endElement(); |
| 330 | } |
| 331 | |
| 332 | @Override |
| 333 | public String toShortString() { |
| 334 | return "."; |
| 335 | } |
| 336 | } |
| 337 |