| 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.ma.map; |
| 9 | |
| 10 | import net.sf.saxon.expr.ContextOriginator; |
| 11 | import net.sf.saxon.expr.Literal; |
| 12 | import net.sf.saxon.expr.OperandRole; |
| 13 | import net.sf.saxon.expr.XPathContext; |
| 14 | import net.sf.saxon.expr.sort.AtomicComparer; |
| 15 | import net.sf.saxon.functions.DeepEqual; |
| 16 | import net.sf.saxon.om.*; |
| 17 | import net.sf.saxon.pattern.NodeKindTest; |
| 18 | import net.sf.saxon.pattern.NodeTest; |
| 19 | import net.sf.saxon.query.AnnotationList; |
| 20 | import net.sf.saxon.trace.ExpressionPresenter; |
| 21 | import net.sf.saxon.trans.XPathException; |
| 22 | import net.sf.saxon.tree.iter.AtomicIterator; |
| 23 | import net.sf.saxon.tree.util.FastStringBuffer; |
| 24 | import net.sf.saxon.type.*; |
| 25 | import net.sf.saxon.value.AtomicValue; |
| 26 | import net.sf.saxon.value.EmptySequence; |
| 27 | import net.sf.saxon.value.SequenceType; |
| 28 | |
| 29 | /** |
| 30 | * Interface supported by different implementations of an XDM map item |
| 31 | */ |
| 32 | public interface MapItem extends Function { |
| 33 | |
| 34 | /** |
| 35 | * Get an entry from the Map |
| 36 | * @param key the value of the key |
| 37 | * @return the value associated with the given key, or null if the key is not present in the map. |
| 38 | */ |
| 39 | |
| 40 | GroundedValue<?> get(AtomicValue key); |
| 41 | |
| 42 | /** |
| 43 | * Get the size of the map |
| 44 | * |
| 45 | * @return the number of keys/entries present in this map |
| 46 | */ |
| 47 | |
| 48 | int size(); |
| 49 | |
| 50 | /** |
| 51 | * Ask whether the map is empty |
| 52 | * |
| 53 | * @return true if and only if the size of the map is zero |
| 54 | */ |
| 55 | |
| 56 | boolean isEmpty(); |
| 57 | |
| 58 | /** |
| 59 | * Get the set of all key values in the map. |
| 60 | * |
| 61 | * @return a set containing all the key values present in the map, in unpredictable order |
| 62 | */ |
| 63 | |
| 64 | AtomicIterator keys(); |
| 65 | |
| 66 | /** |
| 67 | * Get the set of all key-value pairs in the map |
| 68 | * @return an iterable containing all the key-value pairs |
| 69 | */ |
| 70 | |
| 71 | Iterable<KeyValuePair> keyValuePairs(); |
| 72 | |
| 73 | /** |
| 74 | * Create a new map containing the existing entries in the map plus an additional entry, |
| 75 | * without modifying the original. If there is already an entry with the specified key, |
| 76 | * this entry is replaced by the new entry. |
| 77 | * |
| 78 | * @param key the key of the new entry |
| 79 | * @param value the value associated with the new entry |
| 80 | * @return the new map containing the additional entry |
| 81 | */ |
| 82 | |
| 83 | MapItem addEntry(AtomicValue key, GroundedValue<?> value); |
| 84 | |
| 85 | /** |
| 86 | * Remove an entry from the map |
| 87 | * |
| 88 | * |
| 89 | * @param key the key of the entry to be removed |
| 90 | * @return a new map in which the requested entry has been removed; or this map |
| 91 | * unchanged if the specified key was not present |
| 92 | */ |
| 93 | |
| 94 | MapItem remove(AtomicValue key); |
| 95 | |
| 96 | /** |
| 97 | * Ask whether the map conforms to a given map type |
| 98 | * @param keyType the required keyType |
| 99 | * @param valueType the required valueType |
| 100 | * @param th the type hierarchy cache for the configuration |
| 101 | * @return true if the map conforms to the required type |
| 102 | */ |
| 103 | boolean conforms(AtomicType keyType, SequenceType valueType, TypeHierarchy th); |
| 104 | |
| 105 | /** |
| 106 | * Get the type of the map. This method is used largely for diagnostics, to report |
| 107 | * the type of a map when it differs from the required type. |
| 108 | * @return the type of this map |
| 109 | */ |
| 110 | |
| 111 | ItemType getItemType(TypeHierarchy th); |
| 112 | |
| 113 | /** |
| 114 | * Get the lowest common item type of the keys in the map |
| 115 | * |
| 116 | * @return the most specific type to which all the keys belong. If the map is |
| 117 | * empty, return UType.VOID |
| 118 | */ |
| 119 | |
| 120 | UType getKeyUType(); |
| 121 | |
| 122 | /** |
| 123 | * Provide a short string showing the contents of the item, suitable |
| 124 | * for use in error messages |
| 125 | * |
| 126 | * @return a depiction of the item suitable for use in error messages |
| 127 | */ |
| 128 | @Override |
| 129 | default String toShortString() { |
| 130 | StringBuilder sb = new StringBuilder(); |
| 131 | sb.append("map{"); |
| 132 | int count = 0; |
| 133 | for (KeyValuePair kvp : keyValuePairs()) { |
| 134 | if (count++ > 2) { |
| 135 | sb.append(" ..."); |
| 136 | break; |
| 137 | } |
| 138 | sb.append(kvp.key.toShortString()); |
| 139 | sb.append(":"); |
| 140 | sb.append(kvp.value.toShortString()); |
| 141 | sb.append(", "); |
| 142 | } |
| 143 | sb.append("}"); |
| 144 | return sb.toString(); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Get the genre of this item |
| 149 | * |
| 150 | * @return the genre: specifically, Map. |
| 151 | */ |
| 152 | @Override |
| 153 | default Genre getGenre() { |
| 154 | return Genre.MAP; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Ask whether this function item is an array |
| 159 | * |
| 160 | * @return false (it is not an array) |
| 161 | */ |
| 162 | default boolean isArray() { |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Ask whether this function item is a map |
| 168 | * |
| 169 | * @return true (it is a map) |
| 170 | */ |
| 171 | default boolean isMap() { |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Get the function annotations (as defined in XQuery). Returns an empty |
| 177 | * list if there are no function annotations. |
| 178 | * |
| 179 | * @return the function annotations |
| 180 | */ |
| 181 | |
| 182 | @Override |
| 183 | default AnnotationList getAnnotations() { |
| 184 | return AnnotationList.EMPTY; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Atomize the item. |
| 189 | * |
| 190 | * @return the result of atomization |
| 191 | * @throws XPathException if atomization is not allowed for this kind of item |
| 192 | */ |
| 193 | default AtomicSequence atomize() throws XPathException { |
| 194 | throw new XPathException("Cannot atomize a map (" + toShortString() + ")", "FOTY0013"); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Ask whether all the items in a sequence are known to conform to a given item type |
| 199 | * |
| 200 | * @param value the sequence |
| 201 | * @param itemType the given item type |
| 202 | * @return true if all the items conform; false if not, or if the information cannot |
| 203 | * be efficiently determined |
| 204 | */ |
| 205 | |
| 206 | static boolean isKnownToConform(Sequence<?> value, ItemType itemType) { |
| 207 | // Problem is we don't have access to a TypeHierarchy object... |
| 208 | if (itemType == AnyItemType.getInstance()) { |
| 209 | return true; |
| 210 | } |
| 211 | try { |
| 212 | SequenceIterator<?> iter = value.iterate(); |
| 213 | Item item; |
| 214 | while ((item = iter.next()) != null) { |
| 215 | if (item instanceof AtomicValue) { |
| 216 | if (itemType instanceof AtomicType) { |
| 217 | if (!Type.isSubType(((AtomicValue) item).getItemType(), (AtomicType) itemType)) { |
| 218 | return false; |
| 219 | } |
| 220 | } else { |
| 221 | return false; |
| 222 | } |
| 223 | } else if (item instanceof NodeInfo) { |
| 224 | if (itemType instanceof NodeTest) { |
| 225 | if (!((NodeTest) itemType).matchesNode((NodeInfo) item)) { |
| 226 | return false; |
| 227 | } |
| 228 | } else { |
| 229 | return false; |
| 230 | } |
| 231 | } else { |
| 232 | // functions, maps, arrays: give up (this is only an optimization) |
| 233 | return false; |
| 234 | } |
| 235 | } |
| 236 | return true; |
| 237 | } catch (XPathException e) { |
| 238 | return false; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Get an item type to which all the values in a sequence are known to conform |
| 244 | * |
| 245 | * @param val the sequence |
| 246 | * @return the type of the first item in the sequence, provided that all subsequent |
| 247 | * values in the sequence are known to conform to this type; otherwise item(). |
| 248 | */ |
| 249 | |
| 250 | static ItemType getItemTypeOfSequence(Sequence<?> val) { |
| 251 | try { |
| 252 | Item first = val.head(); |
| 253 | if (first == null) { |
| 254 | return AnyItemType.getInstance(); |
| 255 | } else { |
| 256 | ItemType type; |
| 257 | if (first instanceof AtomicValue) { |
| 258 | type = ((AtomicValue) first).getItemType(); |
| 259 | } else if (first instanceof NodeInfo) { |
| 260 | type = NodeKindTest.makeNodeKindTest(((NodeInfo) first).getNodeKind()); |
| 261 | } else { |
| 262 | type = AnyFunctionType.getInstance(); |
| 263 | } |
| 264 | if (isKnownToConform(val, type)) { |
| 265 | return type; |
| 266 | } else { |
| 267 | return AnyItemType.getInstance(); |
| 268 | } |
| 269 | } |
| 270 | } catch (XPathException e) { |
| 271 | return AnyItemType.getInstance(); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Get the roles of the arguments, for the purposes of streaming |
| 277 | * |
| 278 | * @return an array of OperandRole objects, one for each argument |
| 279 | */ |
| 280 | default OperandRole[] getOperandRoles() { |
| 281 | return new OperandRole[]{OperandRole.SINGLE_ATOMIC}; |
| 282 | } |
| 283 | |
| 284 | |
| 285 | /** |
| 286 | * Get the item type of this item as a function item. Note that this returns the generic function |
| 287 | * type for maps, not a type related to this specific map. |
| 288 | * |
| 289 | * @return the function item's type |
| 290 | */ |
| 291 | default FunctionItemType getFunctionItemType(/*@Nullable*/) { |
| 292 | return MapType.ANY_MAP_TYPE; |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Get the name of the function, or null if it is anonymous |
| 297 | * |
| 298 | * @return the function name, or null for an anonymous inline function |
| 299 | */ |
| 300 | default StructuredQName getFunctionName() { |
| 301 | return null; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Get a description of this function for use in error messages. For named functions, the description |
| 306 | * is the function name (as a lexical QName). For others, it might be, for example, "inline function", |
| 307 | * or "partially-applied ends-with function". |
| 308 | * |
| 309 | * @return a description of the function for use in error messages |
| 310 | */ |
| 311 | default String getDescription() { |
| 312 | return "map"; |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Get the arity of the function |
| 317 | * |
| 318 | * @return the number of arguments in the function signature |
| 319 | */ |
| 320 | default int getArity() { |
| 321 | return 1; |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Prepare an XPathContext object for evaluating the function |
| 326 | * |
| 327 | * @param callingContext the XPathContext of the function calling expression |
| 328 | * @param originator |
| 329 | * @return a suitable context for evaluating the function (which may or may |
| 330 | * not be the same as the caller's context) |
| 331 | */ |
| 332 | default XPathContext makeNewContext(XPathContext callingContext, ContextOriginator originator) { |
| 333 | return callingContext; |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Invoke the function |
| 338 | * |
| 339 | * @param context the XPath dynamic evaluation context |
| 340 | * @param args the actual arguments to be supplied |
| 341 | * @return the result of invoking the function |
| 342 | * @throws XPathException if an error occurs evaluating |
| 343 | * the supplied argument |
| 344 | */ |
| 345 | default Sequence<?> call(XPathContext context, Sequence[] args) throws XPathException { |
| 346 | AtomicValue key = (AtomicValue) args[0].head(); |
| 347 | Sequence<?> value = get(key); |
| 348 | if (value == null) { |
| 349 | return EmptySequence.getInstance(); |
| 350 | } else { |
| 351 | return value; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Get the value of the item as a string. For nodes, this is the string value of the |
| 357 | * node as defined in the XPath 2.0 data model, except that all nodes are treated as being |
| 358 | * untyped: it is not an error to get the string value of a node with a complex type. |
| 359 | * For atomic values, the method returns the result of casting the atomic value to a string. |
| 360 | * <p>If the calling code can handle any CharSequence, the method {@link #getStringValueCS} should |
| 361 | * be used. If the caller requires a string, this method is preferred.</p> |
| 362 | * |
| 363 | * @return the string value of the item |
| 364 | * @throws UnsupportedOperationException if the item is a function item (an unchecked exception |
| 365 | * is used here to avoid introducing exception handling to a large number of paths where it is not |
| 366 | * needed) |
| 367 | * @see #getStringValueCS |
| 368 | * @since 8.4 |
| 369 | */ |
| 370 | default String getStringValue() { |
| 371 | throw new UnsupportedOperationException("A map has no string value"); |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Get the string value of the item as a CharSequence. This is in some cases more efficient than |
| 376 | * the version of the method that returns a String. The method satisfies the rule that |
| 377 | * <code>X.getStringValueCS().toString()</code> returns a string that is equal to |
| 378 | * <code>X.getStringValue()</code>. |
| 379 | * <p>Note that two CharSequence values of different types should not be compared using equals(), and |
| 380 | * for the same reason they should not be used as a key in a hash table.</p> |
| 381 | * <p>If the calling code can handle any CharSequence, this method should |
| 382 | * be used. If the caller requires a string, the {@link #getStringValue} method is preferred.</p> |
| 383 | * |
| 384 | * @return the string value of the item |
| 385 | * @throws UnsupportedOperationException if the item is a function item (an unchecked exception |
| 386 | * is used here to avoid introducing exception handling to a large number of paths where it is not |
| 387 | * needed) |
| 388 | * @see #getStringValue |
| 389 | * @since 8.4 |
| 390 | */ |
| 391 | default CharSequence getStringValueCS() { |
| 392 | throw new UnsupportedOperationException("A map has no string value"); |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Get the typed value of the item. |
| 397 | * <p>For a node, this is the typed value as defined in the XPath 2.0 data model. Since a node |
| 398 | * may have a list-valued data type, the typed value is in general a sequence, and it is returned |
| 399 | * in the form of a SequenceIterator.</p> |
| 400 | * <p>If the node has not been validated against a schema, the typed value |
| 401 | * will be the same as the string value, either as an instance of xs:string or as an instance |
| 402 | * of xs:untypedAtomic, depending on the node kind.</p> |
| 403 | * <p>For an atomic value, this method returns an iterator over a singleton sequence containing |
| 404 | * the atomic value itself.</p> |
| 405 | * |
| 406 | * @return an iterator over the items in the typed value of the node or atomic value. The |
| 407 | * items returned by this iterator will always be atomic values. |
| 408 | * @throws XPathException where no typed value is available, for example in the case of |
| 409 | * an element with complex content |
| 410 | * @since 8.4 |
| 411 | */ |
| 412 | default SequenceIterator getTypedValue() throws XPathException { |
| 413 | throw new XPathException("A map has no typed value"); |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Test whether this FunctionItem is deep-equal to another function item, |
| 418 | * under the rules of the deep-equal function |
| 419 | * |
| 420 | * @param other the other function item |
| 421 | */ |
| 422 | default boolean deepEquals(Function other, XPathContext context, AtomicComparer comparer, int flags) throws XPathException { |
| 423 | if (other instanceof MapItem && |
| 424 | ((MapItem) other).size() == size()) { |
| 425 | AtomicIterator keys = keys(); |
| 426 | AtomicValue key; |
| 427 | while ((key = keys.next()) != null) { |
| 428 | Sequence<?> thisValue = get(key); |
| 429 | Sequence<?> otherValue = ((MapItem) other).get(key); |
| 430 | if (otherValue == null) { |
| 431 | return false; |
| 432 | } |
| 433 | if (!DeepEqual.deepEqual(otherValue.iterate(), |
| 434 | thisValue.iterate(), comparer, context, flags)) { |
| 435 | return false; |
| 436 | } |
| 437 | } |
| 438 | return true; |
| 439 | } |
| 440 | return false; |
| 441 | } |
| 442 | |
| 443 | /*@Nullable*/ |
| 444 | default MapItem itemAt(int n) { |
| 445 | return n == 0 ? this : null; |
| 446 | } |
| 447 | |
| 448 | default boolean effectiveBooleanValue() throws XPathException { |
| 449 | throw new XPathException("A map item has no effective boolean value"); |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * Returns a string representation of the object. |
| 454 | * |
| 455 | * @return a string representation of the object. |
| 456 | */ |
| 457 | |
| 458 | static String mapToString(MapItem map) { |
| 459 | FastStringBuffer buffer = new FastStringBuffer(256); |
| 460 | buffer.append("map{"); |
| 461 | for (KeyValuePair pair : map.keyValuePairs()) { |
| 462 | if (buffer.length() > 4) { |
| 463 | buffer.append(","); |
| 464 | } |
| 465 | buffer.append(pair.key.toString()); |
| 466 | buffer.append(":"); |
| 467 | buffer.append(pair.value.toString()); |
| 468 | } |
| 469 | buffer.append("}"); |
| 470 | return buffer.toString(); |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Export information about this function item to the export() or explain() output |
| 475 | */ |
| 476 | default void export(ExpressionPresenter out) throws XPathException { |
| 477 | out.startElement("map"); |
| 478 | out.emitAttribute("size", "" + size()); |
| 479 | for (KeyValuePair kvp : keyValuePairs()) { |
| 480 | Literal.exportAtomicValue(kvp.key, out); |
| 481 | Literal.exportValue(kvp.value, out); |
| 482 | } |
| 483 | out.endElement(); |
| 484 | } |
| 485 | |
| 486 | default boolean isTrustedResultType() { |
| 487 | return true; |
| 488 | } |
| 489 | |
| 490 | |
| 491 | } |
| 492 | |
| 493 | // Copyright (c) 2011-2016 Saxonica Limited. |