Debian Codegraph @

net/sf/saxon/type/ItemType.java

raw symbols SCIP
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.om.Genre;
11 import net.sf.saxon.om.Item;
12 import net.sf.saxon.trans.XPathException;
13 import net.sf.saxon.value.SequenceType;
14
15 import java.util.Optional;
16
17
18 /**
19 * ItemType is an interface that allows testing of whether an Item conforms to an
20 * expected type. ItemType represents the types in the type hierarchy in the XPath model,
21 * as distinct from the schema model: an item type is either item() (matches everything),
22 * a node type (matches nodes), an atomic type (matches atomic values), or empty()
23 * (matches nothing). Atomic types, represented by the class AtomicType, are also
24 * instances of SimpleType in the schema type hierarchy. Node Types, represented by
25 * the class NodeTest, are also Patterns as used in XSLT.
26 * <p>Saxon assumes that apart from {@link AnyItemType} (which corresponds to <code>item()</code>
27 * and matches anything), every ItemType will be either an {@link AtomicType}, a {@link net.sf.saxon.pattern.NodeTest},
28 * or a {@link FunctionItemType}. User-defined implementations of ItemType must therefore extend one of those
29 * three classes/interfaces.</p>
30 *
31 * @see AtomicType
32 * @see net.sf.saxon.pattern.NodeTest
33 * @see FunctionItemType
34 */
35
36 public interface ItemType {
37
38 /**
39 * Determine the Genre (top-level classification) of this type
40 * @return the Genre to which this type belongs, for example node or atomic value
41 */
42
43 Genre getGenre();
44
45 /**
46 * Determine whether this item type is an atomic type
47 *
48 * @return true if this is ANY_ATOMIC_TYPE or a subtype thereof
49 */
50
51 boolean isAtomicType();
52
53 /**
54 * Determine whether this item type is a plain type (that is, whether it can ONLY match
55 * atomic values)
56 *
57 * @return true if this is ANY_ATOMIC_TYPE or a subtype thereof, or a
58 * "plain" union type (that is, unions of atomic types that impose no further restrictions).
59 * Return false if this is a union type whose member types are not all known.
60 */
61
62 boolean isPlainType();
63
64 /**
65 * Ask whether this {@code ItemType} actually represents an item type in the XDM sense
66 * of the term. The only instances that aren't true item types are user-defined union types
67 * derived by restriction from other union types or containing list types in their transitive
68 * membership ("impure union types")
69 * @return true if this is a true item type in the XDM sense
70 */
71
72 default boolean isTrueItemType() {
73 return true;
74 }
75
76 /**
77 * Test whether a given item conforms to this type
78 *
79 *
80 * @param item The item to be tested
81 * @param th The type hierarchy cache. Currently used only when matching function items.
82 * @return true if the item is an instance of this type; false otherwise
83 */
84
85 boolean matches(Item item, TypeHierarchy th) throws XPathException;
86
87 /**
88 * Get the primitive item type corresponding to this item type. For item(),
89 * this is Type.ITEM. For node(), it is Type.NODE. For specific node kinds,
90 * it is the value representing the node kind, for example Type.ELEMENT.
91 * For anyAtomicValue and union types it is Type.ATOMIC_VALUE. For numeric it is Type.NUMBER.
92 * For other atomic types it is the primitive type as defined in XML Schema,
93 * except that integer, xs:dayTimeDuration, and xs:yearMonthDuration
94 * are considered to be primitive types.
95 *
96 * @return the corresponding primitive type
97 */
98
99 /*@NotNull*/
100 ItemType getPrimitiveItemType();
101
102 /**
103 * Get the primitive type corresponding to this item type. For item(),
104 * this is Type.ITEM. For node(), it is Type.NODE. For specific node kinds,
105 * it is the value representing the node kind, for example Type.ELEMENT.
106 * For anyAtomicValue it is BuiltInAtomicType.ANY_ATOMIC. For numeric it is Type.NUMBER.
107 * For other atomic types it is the primitive type as defined in XML Schema,
108 * except that INTEGER is considered to be a primitive type.
109 *
110 * @return the integer fingerprint of the corresponding primitive type
111 */
112
113 int getPrimitiveType();
114
115 /**
116 * Get the corresponding {@link UType}. A UType is a union of primitive item
117 * types.
118 * @return the smallest UType that subsumes this item type
119 */
120
121 UType getUType();
122
123 /**
124 * Get the item type of the atomic values that will be produced when an item
125 * of this type is atomized
126 *
127 * @return the best available item type of the atomic values that will be produced when an item
128 * of this type is atomized, or null if it is known that atomization will throw an error.
129 */
130
131 PlainType getAtomizedItemType();
132
133 /**
134 * Ask whether values of this type are atomizable
135 *
136 * @return true if some or all instances of this type can be successfully atomized; false
137 * if no instances of this type can be atomized
138 * @param th the type hierarchy cache
139 */
140
141 boolean isAtomizable(TypeHierarchy th);
142
143 /**
144 * Get an alphabetic code representing the type, or at any rate, the nearest built-in type
145 * from which this type is derived. The codes are designed so that for any two built-in types
146 * A and B, alphaCode(A) is a prefix of alphaCode(B) if and only if A is a supertype of B.
147 * @return the alphacode for the nearest containing built-in type
148 */
149
150 String getAlphaCode();
151
152 /**
153 * Return a string representation of this ItemType suitable for use in stylesheet
154 * export files. This differs from the result of toString() in that it will not contain
155 * any references to anonymous types. Note that it may also use the Saxon extended syntax
156 * for union types and tuple types. The default implementation returns the result of
157 * calling {@code toString()}.
158 *
159 * @return the string representation as an instance of the XPath SequenceType construct
160 */
161 default String toExportString() {
162 return toString();
163 }
164
165 /**
166 * Generate Javascript code to test whether an item conforms to this item type
167 * @param knownToBe An item type that the supplied item is known to conform to; the generated code
168 * can assume that the item is an instance of this type.
169 * @param targetVersion The version of Saxon-JS for which code is being generated. Currently either 1 or 2.
170 * @return a Javascript instruction or sequence of instructions, which can be used as the body
171 * of a Javascript function, and which returns a boolean indication whether the value of the
172 * variable "item" is an instance of this item type.
173 * @throws XPathException if JS code cannot be generated for this item type, for example because
174 * the test is schema-aware.
175 *
176 */
177
178 String generateJavaScriptItemTypeTest(ItemType knownToBe, int targetVersion) throws XPathException;
179
180
181 /**
182 * Generate Javascript code to convert a supplied Javascript value to this item type,
183 * if conversion is possible, or throw an error otherwise.
184 * @param errorCode the error to be thrown if conversion is not possible
185 * @param targetVersion the version of Saxon-JS for which code is being generated
186 * @return a Javascript instruction or sequence of instructions, which can be used as the body
187 * of a Javascript function, and which returns the result of conversion to this type, or throws
188 * an error if conversion is not possible. The variable "val" will hold the supplied Javascript
189 * value.
190 */
191
192 String generateJavaScriptItemTypeAcceptor(String errorCode, int targetVersion) throws XPathException;
193
194 /**
195 * Extension of the ItemType interface implemented by some item types, to provide
196 * a cache of SequenceType objects based on this item type, with different
197 * occurrence indicators.
198 */
199
200 interface WithSequenceTypeCache extends ItemType {
201 /**
202 * Get a sequence type representing exactly one instance of this atomic type
203 *
204 * @return a sequence type representing exactly one instance of this atomic type
205 * @since 9.8.0.2
206 */
207
208 SequenceType one();
209 /**
210 * Get a sequence type representing zero or one instances of this atomic type
211 *
212 * @return a sequence type representing zero or one instances of this atomic type
213 * @since 9.8.0.2
214 */
215
216 SequenceType zeroOrOne();
217
218 /**
219 * Get a sequence type representing one or more instances of this atomic type
220 *
221 * @return a sequence type representing one or more instances of this atomic type
222 * @since 9.8.0.2
223 */
224
225 SequenceType oneOrMore();
226
227 /**
228 * Get a sequence type representing one or more instances of this atomic type
229 *
230 * @return a sequence type representing one or more instances of this atomic type
231 * @since 9.8.0.2
232 */
233
234 SequenceType zeroOrMore();
235
236 }
237
238 String toString();
239
240 /**
241 * Get extra diagnostic information about why a supplied item does not conform to this
242 * item type, if available. If extra information is returned, it should be in the form of a complete
243 * sentence, minus the closing full stop. No information should be returned for obvious cases.
244 * @param item the item that doesn't match this type
245 * @param th the type hierarchy cache
246 * @return optionally, a message explaining why the item does not match the type
247 */
248
249 default Optional<String> explainMismatch(Item item, TypeHierarchy th) {
250 return Optional.empty();
251 }
252
253 }
254