| 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.type; |
| 9 | |
| 10 | import net.sf.saxon.expr.StaticProperty; |
| 11 | import net.sf.saxon.om.Genre; |
| 12 | import net.sf.saxon.om.Item; |
| 13 | import net.sf.saxon.trans.XPathException; |
| 14 | import net.sf.saxon.value.SequenceType; |
| 15 | |
| 16 | |
| 17 | /** |
| 18 | * An implementation of ItemType that matches any item (node or atomic value) |
| 19 | */ |
| 20 | |
| 21 | public class AnyItemType implements ItemType.WithSequenceTypeCache { |
| 22 | |
| 23 | private SequenceType _one; |
| 24 | private SequenceType _oneOrMore; |
| 25 | private SequenceType _zeroOrOne; |
| 26 | private SequenceType _zeroOrMore; |
| 27 | |
| 28 | private AnyItemType() { |
| 29 | } |
| 30 | |
| 31 | /*@NotNull*/ private static AnyItemType theInstance = new AnyItemType(); |
| 32 | |
| 33 | /** |
| 34 | * Factory method to get the singleton instance |
| 35 | * @return the singleton instance |
| 36 | */ |
| 37 | |
| 38 | /*@NotNull*/ |
| 39 | public static AnyItemType getInstance() { |
| 40 | return theInstance; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Determine the Genre (top-level classification) of this type |
| 45 | * |
| 46 | * @return the Genre to which this type belongs, specifically {@link Genre#ANY} |
| 47 | */ |
| 48 | @Override |
| 49 | public Genre getGenre() { |
| 50 | return Genre.ANY; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Get the corresponding {@link net.sf.saxon.type.UType}. A UType is a union of primitive item |
| 55 | * types. |
| 56 | * |
| 57 | * @return the smallest UType that subsumes this item type |
| 58 | */ |
| 59 | public UType getUType() { |
| 60 | return UType.ANY; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Determine whether this item type is an atomic type |
| 65 | * |
| 66 | * @return true if this is ANY_ATOMIC_TYPE or a subtype thereof |
| 67 | */ |
| 68 | public boolean isAtomicType() { |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Get an alphabetic code representing the type, or at any rate, the nearest built-in type |
| 74 | * from which this type is derived. The codes are designed so that for any two built-in types |
| 75 | * A and B, alphaCode(A) is a prefix of alphaCode(B) if and only if A is a supertype of B. |
| 76 | * |
| 77 | * @return the alphacode for the nearest containing built-in type |
| 78 | */ |
| 79 | @Override |
| 80 | public String getAlphaCode() { |
| 81 | return ""; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Determine whether this item type is atomic (that is, whether it can ONLY match |
| 86 | * atomic values) |
| 87 | * |
| 88 | * @return false: this type can match nodes or atomic values |
| 89 | */ |
| 90 | |
| 91 | public boolean isPlainType() { |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Test whether a given item conforms to this type |
| 97 | * |
| 98 | * |
| 99 | * |
| 100 | * @param item The item to be tested |
| 101 | * @param th |
| 102 | * @return true if the item is an instance of this type; false otherwise |
| 103 | */ |
| 104 | public boolean matches(Item item, TypeHierarchy th) { |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Get the primitive item type corresponding to this item type. For item(), |
| 110 | * this is Type.ITEM. For node(), it is Type.NODE. For specific node kinds, |
| 111 | * it is the value representing the node kind, for example Type.ELEMENT. |
| 112 | * For anyAtomicValue it is Type.ATOMIC_VALUE. For numeric it is Type.NUMBER. |
| 113 | * For other atomic types it is the primitive type as defined in XML Schema, |
| 114 | * except that INTEGER is considered to be a primitive type. |
| 115 | */ |
| 116 | |
| 117 | /*@NotNull*/ |
| 118 | public ItemType getPrimitiveItemType() { |
| 119 | return this; |
| 120 | } |
| 121 | |
| 122 | public int getPrimitiveType() { |
| 123 | return Type.ITEM; |
| 124 | } |
| 125 | |
| 126 | /*@NotNull*/ |
| 127 | public AtomicType getAtomizedItemType() { |
| 128 | return BuiltInAtomicType.ANY_ATOMIC; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Ask whether values of this type are atomizable |
| 133 | * |
| 134 | * @return true unless it is known that these items will be elements with element-only |
| 135 | * content, in which case return false |
| 136 | * @param th The type hierarchy cache |
| 137 | */ |
| 138 | |
| 139 | public boolean isAtomizable(TypeHierarchy th) { |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | public double getDefaultPriority() { |
| 144 | return -2; |
| 145 | } |
| 146 | |
| 147 | /*@NotNull*/ |
| 148 | public String toString() { |
| 149 | return "item()"; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Returns a hash code value for the object. |
| 154 | */ |
| 155 | |
| 156 | public int hashCode() { |
| 157 | return "AnyItemType".hashCode(); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Generate Javascript code to test whether an item conforms to this item type |
| 162 | * |
| 163 | * @return a Javascript instruction or sequence of instructions, which can be used as the body |
| 164 | * of a Javascript function, and which returns a boolean indication whether the value of the |
| 165 | * variable "item" is an instance of this item type. |
| 166 | * @throws XPathException if JS code cannot be generated for this item type, for example because |
| 167 | * the test is schema-aware. |
| 168 | * @param knownToBe |
| 169 | * @param targetVersion |
| 170 | */ |
| 171 | @Override |
| 172 | public String generateJavaScriptItemTypeTest(ItemType knownToBe, int targetVersion) throws XPathException { |
| 173 | return "return true;"; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Generate Javascript code to convert a supplied Javascript value to this item type, |
| 178 | * if conversion is possible, or throw an error otherwise. |
| 179 | * |
| 180 | * @param errorCode the error to be thrown if conversion is not possible |
| 181 | * @param targetVersion |
| 182 | * @return a Javascript instruction or sequence of instructions, which can be used as the body |
| 183 | * of a Javascript function, and which returns the result of conversion to this type, or throws |
| 184 | * an error if conversion is not possible. The variable "val" will hold the supplied Javascript |
| 185 | * value. |
| 186 | */ |
| 187 | public String generateJavaScriptItemTypeAcceptor(String errorCode, int targetVersion) throws XPathException { |
| 188 | return "return val;"; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Get a sequence type representing exactly one instance of this atomic type |
| 193 | * |
| 194 | * @return a sequence type representing exactly one instance of this atomic type |
| 195 | * @since 9.8.0.2 |
| 196 | */ |
| 197 | |
| 198 | public SequenceType one() { |
| 199 | if (_one == null) { |
| 200 | _one = new SequenceType(this, StaticProperty.EXACTLY_ONE); |
| 201 | } |
| 202 | return _one; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Get a sequence type representing zero or one instances of this atomic type |
| 207 | * |
| 208 | * @return a sequence type representing zero or one instances of this atomic type |
| 209 | * @since 9.8.0.2 |
| 210 | */ |
| 211 | |
| 212 | public SequenceType zeroOrOne() { |
| 213 | if (_zeroOrOne == null) { |
| 214 | _zeroOrOne = new SequenceType(this, StaticProperty.ALLOWS_ZERO_OR_ONE); |
| 215 | } |
| 216 | return _zeroOrOne; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Get a sequence type representing one or more instances of this atomic type |
| 221 | * |
| 222 | * @return a sequence type representing one or more instances of this atomic type |
| 223 | * @since 9.8.0.2 |
| 224 | */ |
| 225 | |
| 226 | public SequenceType oneOrMore() { |
| 227 | if (_oneOrMore == null) { |
| 228 | _oneOrMore = new SequenceType(this, StaticProperty.ALLOWS_ONE_OR_MORE); |
| 229 | } |
| 230 | return _oneOrMore; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Get a sequence type representing one or more instances of this atomic type |
| 235 | * |
| 236 | * @return a sequence type representing one or more instances of this atomic type |
| 237 | * @since 9.8.0.2 |
| 238 | */ |
| 239 | |
| 240 | public SequenceType zeroOrMore() { |
| 241 | if (_zeroOrMore == null) { |
| 242 | _zeroOrMore = new SequenceType(this, StaticProperty.ALLOWS_ZERO_OR_MORE); |
| 243 | } |
| 244 | return _zeroOrMore; |
| 245 | } |
| 246 | |
| 247 | } |
| 248 |