| 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.s9api; |
| 9 | |
| 10 | import net.sf.saxon.om.Item; |
| 11 | import net.sf.saxon.om.NodeInfo; |
| 12 | import net.sf.saxon.s9api.streams.XdmStream; |
| 13 | import net.sf.saxon.value.AtomicValue; |
| 14 | |
| 15 | import java.util.Map; |
| 16 | import java.util.stream.Stream; |
| 17 | |
| 18 | /** |
| 19 | * The class XdmItem represents an item in a sequence, as defined by the XDM data model. |
| 20 | * An item is either an atomic value or a node. |
| 21 | * <p>An item is a member of a sequence, but it can also be considered as a sequence |
| 22 | * (of length one) in its own right. <tt>XdmItem</tt> is a subtype of <tt>XdmValue</tt> because every |
| 23 | * Item in the XDM data model is also a value.</p> |
| 24 | * <p>It cannot be assumed that every sequence of length one will be represented by |
| 25 | * an <tt>XdmItem</tt>. It is quite possible for an <tt>XdmValue</tt> that is not an <tt>XdmItem</tt> to hold |
| 26 | * a singleton sequence.</p> |
| 27 | * <p>Saxon provides two concrete subclasses of <code>XdmItem</code>, namely |
| 28 | * {@link XdmNode} and {@link XdmAtomicValue}. Users must not attempt to create |
| 29 | * additional subclasses.</p> |
| 30 | */ |
| 31 | |
| 32 | public abstract class XdmItem extends XdmValue { |
| 33 | |
| 34 | // internal protected constructor |
| 35 | |
| 36 | protected XdmItem() { |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Construct an XdmItem as a wrapper around an existing Saxon Item object |
| 41 | * |
| 42 | * @param item the Item object to be wrapped. This can be retrieved using the |
| 43 | * {@link #getUnderlyingValue} method. |
| 44 | * @throws NullPointerException if item is null |
| 45 | * @since 9.5 (previously a protected constructor) |
| 46 | */ |
| 47 | |
| 48 | public XdmItem(Item<?> item) { |
| 49 | super(); |
| 50 | setValue(item); |
| 51 | } |
| 52 | |
| 53 | // internal factory mathod to wrap an Item |
| 54 | |
| 55 | /*@Nullable*/ |
| 56 | protected static XdmItem wrapItem(Item<?> item) { |
| 57 | return item == null ? null : (XdmItem) XdmValue.wrap(item); |
| 58 | } |
| 59 | |
| 60 | protected static XdmNode wrapItem(NodeInfo item) { |
| 61 | return item == null ? null : (XdmNode) XdmValue.wrap(item); |
| 62 | } |
| 63 | |
| 64 | protected static XdmAtomicValue wrapItem(AtomicValue item) { |
| 65 | return item == null ? null : (XdmAtomicValue) XdmValue.wrap(item); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Get the underlying implementation object representing the value. This method allows |
| 70 | * access to lower-level Saxon functionality, including classes and methods that offer |
| 71 | * no guarantee of stability across releases. |
| 72 | * |
| 73 | * @return the underlying implementation object representing the value |
| 74 | * @since 9.8 (previously inherited from XdmValue which returns a Sequence) |
| 75 | */ |
| 76 | @Override |
| 77 | public Item<?> getUnderlyingValue() { |
| 78 | return (Item)super.getUnderlyingValue(); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get the string value of the item. For a node, this gets the string value |
| 83 | * of the node. For an atomic value, it has the same effect as casting the value |
| 84 | * to a string. In all cases the result is the same as applying the XPath string() |
| 85 | * function. |
| 86 | * <p>For atomic values, the result is the same as the result of calling |
| 87 | * <code>toString</code>. This is not the case for nodes, where <code>toString</code> |
| 88 | * returns an XML serialization of the node.</p> |
| 89 | * |
| 90 | * @return the result of converting the item to a string. |
| 91 | */ |
| 92 | |
| 93 | public String getStringValue() { |
| 94 | return getUnderlyingValue().getStringValue(); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Determine whether the item is an atomic value or some other type of item |
| 99 | * |
| 100 | * @return true if the item is an atomic value, false if it is a node or a function (including maps and arrays) |
| 101 | */ |
| 102 | |
| 103 | public boolean isAtomicValue() { |
| 104 | return getUnderlyingValue() instanceof AtomicValue; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Get the number of items in the sequence |
| 109 | * |
| 110 | * @return the number of items in the value. For an item (including a map or array) this is always 1 (one). |
| 111 | */ |
| 112 | |
| 113 | @Override |
| 114 | public int size() { |
| 115 | return 1; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * If this item is a map, return a corresponding Java Map. |
| 120 | * |
| 121 | * @return if this item is a map, return a mutable Map from atomic values to (sequence) values, containing the |
| 122 | * same entries as this map. Otherwise return null. |
| 123 | * @since 9.6. |
| 124 | */ |
| 125 | |
| 126 | public Map<XdmAtomicValue, XdmValue> asMap() { |
| 127 | return null; // Overridden in XdmMap. The method is retained on this interface for compatibility reasons. |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Get a stream comprising the items in this value |
| 132 | * |
| 133 | * @return a Stream over the items in this value |
| 134 | * @since 9.9 |
| 135 | */ |
| 136 | public XdmStream<? extends XdmItem> stream() { |
| 137 | return new XdmStream<>(Stream.of(this)); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | |
| 142 | /** |
| 143 | * Determine whether this item matches a given item type. |
| 144 | * |
| 145 | * @param type the item type to be tested against this item |
| 146 | * @return true if the item matches this item type, false if it does not match. |
| 147 | * @since 9.9 |
| 148 | */ |
| 149 | public boolean matches(ItemType type){ |
| 150 | return type.matches(this); |
| 151 | } |
| 152 | |
| 153 | } |
| 154 |