Debian Codegraph @

net/sf/saxon/type/UType.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2013 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
11 import net.sf.saxon.om.*;
12 import net.sf.saxon.pattern.AnyNodeTest;
13 import net.sf.saxon.tree.iter.UnfailingIterator;
14 import net.sf.saxon.tree.util.FastStringBuffer;
15 import net.sf.saxon.value.AtomicValue;
16 import net.sf.saxon.value.ObjectValue;
17
18 import java.util.HashSet;
19 import java.util.Iterator;
20 import java.util.Set;
21
22
23 /**
24 * A UType is a union of primitive (atomic, node, or function) item types. It is represented as a simple
25 * integer, with bits representing which of the primitive types are present in the union.
26 */
27
28 public class UType {
29
30 public static final UType VOID = new UType(0);
31
32 public static final UType DOCUMENT = PrimitiveUType.DOCUMENT.toUType();
33 public static final UType ELEMENT = PrimitiveUType.ELEMENT.toUType();
34 public static final UType ATTRIBUTE = PrimitiveUType.ATTRIBUTE.toUType();
35 public static final UType TEXT = PrimitiveUType.TEXT.toUType();
36 public static final UType COMMENT = PrimitiveUType.COMMENT.toUType();
37 public static final UType PI = PrimitiveUType.PI.toUType();
38 public static final UType NAMESPACE = PrimitiveUType.NAMESPACE.toUType();
39
40 public static final UType FUNCTION = PrimitiveUType.FUNCTION.toUType();
41
42 public static final UType STRING = PrimitiveUType.STRING.toUType();
43 public static final UType BOOLEAN = PrimitiveUType.BOOLEAN.toUType();
44 public static final UType DECIMAL = PrimitiveUType.DECIMAL.toUType();
45 public static final UType FLOAT = PrimitiveUType.FLOAT.toUType();
46 public static final UType DOUBLE = PrimitiveUType.DOUBLE.toUType();
47 public static final UType DURATION = PrimitiveUType.DURATION.toUType();
48 public static final UType DATE_TIME = PrimitiveUType.DATE_TIME.toUType();
49 public static final UType TIME = PrimitiveUType.TIME.toUType();
50 public static final UType DATE = PrimitiveUType.DATE.toUType();
51 public static final UType G_YEAR_MONTH = PrimitiveUType.G_YEAR_MONTH.toUType();
52 public static final UType G_YEAR = PrimitiveUType.G_YEAR.toUType();
53 public static final UType G_MONTH_DAY = PrimitiveUType.G_MONTH_DAY.toUType();
54 public static final UType G_DAY = PrimitiveUType.G_DAY.toUType();
55 public static final UType G_MONTH = PrimitiveUType.G_MONTH.toUType();
56 public static final UType HEX_BINARY = PrimitiveUType.HEX_BINARY.toUType();
57 public static final UType BASE64_BINARY = PrimitiveUType.BASE64_BINARY.toUType();
58 public static final UType ANY_URI = PrimitiveUType.ANY_URI.toUType();
59 public static final UType QNAME = PrimitiveUType.QNAME.toUType();
60 public static final UType NOTATION = PrimitiveUType.NOTATION.toUType();
61
62 public static final UType UNTYPED_ATOMIC = PrimitiveUType.UNTYPED_ATOMIC.toUType();
63
64 public static final UType EXTENSION = PrimitiveUType.EXTENSION.toUType();
65
66 public static final UType NUMERIC = DOUBLE.union(FLOAT).union(DECIMAL);
67 public static final UType STRING_LIKE = STRING.union(ANY_URI).union(UNTYPED_ATOMIC);
68
69
70
71 public static final UType CHILD_NODE_KINDS = ELEMENT.union(TEXT).union(COMMENT).union(PI);
72 public static final UType PARENT_NODE_KINDS = DOCUMENT.union(ELEMENT);
73 public static final UType ELEMENT_OR_ATTRIBUTE = ELEMENT.union(ATTRIBUTE);
74 public static final UType ANY_NODE = CHILD_NODE_KINDS.union(DOCUMENT).union(ATTRIBUTE).union(NAMESPACE);
75
76 public static final UType ANY_ATOMIC = new UType(0x0FFFFF00);
77
78 public static final UType ANY = ANY_NODE.union(ANY_ATOMIC).union(FUNCTION).union(EXTENSION);
79
80
81 private int bits;
82
83 public UType(int bits) {
84 this.bits = bits;
85 }
86
87 /**
88 * Returns a hash code value for the object.
89 *
90 * @return a hash code value for this object.
91 * @see Object#equals(Object)
92 * @see java.util.Hashtable
93 */
94 @Override
95 public int hashCode() {
96 return bits;
97 }
98
99 /**
100 * Indicates whether some other object is "equal to" this one.
101 *
102 * @param obj the reference object with which to compare.
103 * @return <code>true</code> if this object is the same as the obj
104 * argument; <code>false</code> otherwise.
105 * @see #hashCode()
106 * @see java.util.Hashtable
107 */
108 @Override
109 public boolean equals(Object obj) {
110 return obj instanceof UType && bits == ((UType)obj).bits;
111 }
112
113 /**
114 * Form a UType as the union of two other UTypes
115 * @param other the other UType
116 * @return the UType representing the union of this UType and the other UType
117 */
118
119 public UType union(UType other) {
120 return new UType(bits | other.bits);
121 }
122
123 public UType intersection(UType other) {
124 return new UType(bits & other.bits);
125 }
126
127 public UType except(UType other) {
128 return new UType(bits &~ other.bits);
129 }
130
131
132 public static UType fromTypeCode(int code) {
133 switch (code) {
134 case Type.NODE:
135 return ANY_NODE;
136 case Type.ELEMENT:
137 return ELEMENT;
138 case Type.ATTRIBUTE:
139 return ATTRIBUTE;
140 case Type.TEXT:
141 case Type.WHITESPACE_TEXT:
142 return TEXT;
143 case Type.DOCUMENT:
144 return DOCUMENT;
145 case Type.COMMENT:
146 return COMMENT;
147 case Type.PROCESSING_INSTRUCTION:
148 return PI;
149 case Type.NAMESPACE:
150 return NAMESPACE;
151
152 case Type.FUNCTION:
153 return FUNCTION;
154
155 case Type.ITEM:
156 return ANY;
157 case StandardNames.XS_ANY_ATOMIC_TYPE:
158 return ANY_ATOMIC;
159
160 case StandardNames.XS_NUMERIC:
161 return NUMERIC;
162 case StandardNames.XS_STRING:
163 return STRING;
164 case StandardNames.XS_BOOLEAN:
165 return BOOLEAN;
166 case StandardNames.XS_DURATION:
167 return DURATION;
168 case StandardNames.XS_DATE_TIME:
169 return DATE_TIME;
170 case StandardNames.XS_DATE:
171 return DATE;
172 case StandardNames.XS_TIME:
173 return TIME;
174 case StandardNames.XS_G_YEAR_MONTH:
175 return G_YEAR_MONTH;
176 case StandardNames.XS_G_MONTH:
177 return G_MONTH;
178 case StandardNames.XS_G_MONTH_DAY:
179 return G_MONTH_DAY;
180 case StandardNames.XS_G_YEAR:
181 return G_YEAR;
182 case StandardNames.XS_G_DAY:
183 return G_DAY;
184 case StandardNames.XS_HEX_BINARY:
185 return HEX_BINARY;
186 case StandardNames.XS_BASE64_BINARY:
187 return BASE64_BINARY;
188 case StandardNames.XS_ANY_URI:
189 return ANY_URI;
190 case StandardNames.XS_QNAME:
191 return QNAME;
192 case StandardNames.XS_NOTATION:
193 return NOTATION;
194 case StandardNames.XS_UNTYPED_ATOMIC:
195 return UNTYPED_ATOMIC;
196 case StandardNames.XS_DECIMAL:
197 return DECIMAL;
198 case StandardNames.XS_FLOAT:
199 return FLOAT;
200 case StandardNames.XS_DOUBLE:
201 return DOUBLE;
202 case StandardNames.XS_INTEGER:
203 return DECIMAL;
204 case StandardNames.XS_NON_POSITIVE_INTEGER:
205 case StandardNames.XS_NEGATIVE_INTEGER:
206 case StandardNames.XS_LONG:
207 case StandardNames.XS_INT:
208 case StandardNames.XS_SHORT:
209 case StandardNames.XS_BYTE:
210 case StandardNames.XS_NON_NEGATIVE_INTEGER:
211 case StandardNames.XS_POSITIVE_INTEGER:
212 case StandardNames.XS_UNSIGNED_LONG:
213 case StandardNames.XS_UNSIGNED_INT:
214 case StandardNames.XS_UNSIGNED_SHORT:
215 case StandardNames.XS_UNSIGNED_BYTE:
216 return DECIMAL;
217 case StandardNames.XS_YEAR_MONTH_DURATION:
218 case StandardNames.XS_DAY_TIME_DURATION:
219 return DURATION;
220 case StandardNames.XS_DATE_TIME_STAMP:
221 return DATE_TIME;
222 case StandardNames.XS_NORMALIZED_STRING:
223 case StandardNames.XS_TOKEN:
224 case StandardNames.XS_LANGUAGE:
225 case StandardNames.XS_NAME:
226 case StandardNames.XS_NMTOKEN:
227 case StandardNames.XS_NCNAME:
228 case StandardNames.XS_ID:
229 case StandardNames.XS_IDREF:
230 case StandardNames.XS_ENTITY:
231 return STRING;
232 default:
233 throw new IllegalArgumentException(""+code);
234 }
235
236 }
237
238 /**
239 * Get a set containing all the primitive types in this UType
240 * @return a set of PrimitiveUTypes each of which represents exactly one primitive type
241 */
242
243 public Set<PrimitiveUType> decompose() {
244 Set<PrimitiveUType> result = new HashSet<PrimitiveUType>();
245 for (PrimitiveUType p : PrimitiveUType.values()) {
246 if ((bits & (1<<p.getBit())) != 0) {
247 result.add(p);
248 }
249 }
250 return result;
251 }
252
253 /**
254 * Produce a string representation of a UType
255 * @return the string representation
256 */
257
258 public String toString() {
259 Set<PrimitiveUType> components = decompose();
260 if (components.isEmpty()) {
261 return "U{}";
262 }
263 FastStringBuffer sb = new FastStringBuffer(FastStringBuffer.C256);
264 Iterator<PrimitiveUType> iter = components.iterator();
265 boolean started = false;
266 while (iter.hasNext()) {
267 if (started) {
268 sb.append("|");
269 }
270 started = true;
271 sb.append(iter.next().toString());
272 }
273 return sb.toString();
274 }
275
276 public String toStringWithIndefiniteArticle() {
277 String s = toString();
278 if ("aeiouxy".indexOf(s.charAt(0)) >= 0) {
279 return "an " + s + " node";
280 } else {
281 return "a " + s + " node";
282 }
283 }
284
285
286
287 /**
288 * Determine whether two UTypes have overlapping membership
289 * @param other the second UType
290 * @return true if the intersection between the two UTypes is non-empty
291 */
292
293 public boolean overlaps(UType other) {
294 return (bits & other.bits) != 0;
295 }
296
297 /**
298 * Ask whether one UType subsumes another
299 * @param other the second UType
300 * @return true if every item type allowed by this UType is also allowed by the other item type
301 */
302
303 public boolean subsumes (UType other) {
304 return (bits & other.bits) == other.bits;
305 }
306
307 /**
308 * Obtain (that is, create or get) an itemType that matches all items whose primitive type is one
309 * of the types present in this UType.
310 * @return a corresponding ItemType
311 */
312
313 public ItemType toItemType() {
314 Set<PrimitiveUType> p = decompose();
315 if (p.isEmpty()) {
316 return ErrorType.getInstance();
317 } else if (p.size() == 1) {
318 return p.toArray(new PrimitiveUType[1])[0].toItemType();
319 } else if (ANY_NODE.subsumes(this)) {
320 return AnyNodeTest.getInstance();
321 } else if (equals(NUMERIC)) {
322 return NumericType.getInstance();
323 } else if (ANY_ATOMIC.subsumes(this)) {
324 return BuiltInAtomicType.ANY_ATOMIC;
325 } else {
326 return AnyItemType.getInstance();
327 }
328 }
329
330 /**
331 * Ask whether a given Item is an instance of this UType
332 * @param item the item to be tested
333 * @return true if this UType matches the supplied item
334 */
335
336 public boolean matches(Item item) {
337 return subsumes(getUType(item));
338 }
339
340 /**
341 * Get the UType of an Item
342 * @param item the item whose UType is required
343 * @return the UType of the item
344 */
345
346 public static UType getUType(Item item) {
347 if (item instanceof NodeInfo) {
348 return fromTypeCode(((NodeInfo) item).getNodeKind());
349 } else if (item instanceof AtomicValue) {
350 return ((AtomicValue)item).getUType();
351 } else if (item instanceof Function) {
352 return UType.FUNCTION;
353 } else if (item instanceof ObjectValue) {
354 return UType.EXTENSION;
355 } else {
356 return UType.VOID;
357 }
358 }
359
360 /**
361 * Get the UType of a Sequence
362 * @param sequence the sequence whose UType is required
363 * @return the UType of the item
364 */
365
366 public static UType getUType(GroundedValue sequence) {
367 UnfailingIterator iter = sequence.iterate();
368 Item item;
369 UType u = UType.VOID;
370 while ((item = iter.next()) != null) {
371 u = u.union(getUType(item));
372 }
373 return u;
374 }
375
376 /**
377 * Determine whether two primitive atomic types are comparable under the rules for ValueComparisons
378 * (that is, untyped atomic values treated as strings)
379 *
380 * @param t1 the first type to compared.
381 * This must be a primitive atomic type as defined by {@link ItemType#getPrimitiveType}
382 * @param t2 the second type to compared.
383 * This must be a primitive atomic type as defined by {@link ItemType#getPrimitiveType}
384 * @param ordered true if testing for an ordering comparison (lt, gt, le, ge). False
385 * if testing for an equality comparison (eq, ne)
386 * @return true if the types are guaranteed comparable, as defined by the rules of the "eq" operator,
387 * or if we don't yet know (because some subtypes of the static type are comparable
388 * and others are not). False if they are definitely not comparable.
389 */
390
391 public static boolean isPossiblyComparable(UType t1, UType t2, boolean ordered) {
392 if (t1 == t2) {
393 return true; // short cut
394 }
395 if (t1 == UType.ANY_ATOMIC || t2 == UType.ANY_ATOMIC) {
396 return true; // meaning we don't actually know at this stage
397 }
398 if (t1 == UType.UNTYPED_ATOMIC || t1 == UType.ANY_URI) {
399 t1 = UType.STRING;
400 }
401 if (t2 == UType.UNTYPED_ATOMIC || t2 == UType.ANY_URI) {
402 t2 = UType.STRING;
403 }
404 if (NUMERIC.subsumes(t1)) {
405 t1 = NUMERIC;
406 }
407 if (NUMERIC.subsumes(t2)) {
408 t2 = NUMERIC;
409 }
410 return t1 == t2;
411 }
412
413 /**
414 * Determine whether two primitive atomic types are comparable under the rules for ValueComparisons
415 * (that is, untyped atomic values treated as strings), using the "eq" operator
416 *
417 * @param t1 the first type to compared.
418 * This must be a primitive atomic type as defined by {@link ItemType#getPrimitiveType}
419 * @param t2 the second type to compared.
420 * This must be a primitive atomic type as defined by {@link ItemType#getPrimitiveType}
421 * @return true if the types are comparable, as defined by the rules of the "eq" operator; false if they
422 * are not comparable, or if we don't yet know (because some subtypes of the static type are comparable
423 * and others are not)
424 */
425
426 public static boolean isGuaranteedComparable(UType t1, UType t2) {
427 if (t1 == t2) {
428 return true; // short cut
429 }
430 if (t1 == UType.UNTYPED_ATOMIC || t1 == UType.ANY_URI) {
431 t1 = UType.STRING;
432 }
433 if (t2 == UType.UNTYPED_ATOMIC || t2 == UType.ANY_URI) {
434 t2 = UType.STRING;
435 }
436 if (NUMERIC.subsumes(t1)) {
437 t1 = NUMERIC;
438 }
439 if (NUMERIC.subsumes(t2)) {
440 t2 = NUMERIC;
441 }
442 return t1.equals(t2);
443 }
444
445 /**
446 * Determine whether two primitive atomic types are comparable under the rules for GeneralComparisons
447 * for the "=" operator (that is, untyped atomic values treated as comparable to anything)
448 *
449 *
450 * @param t1 the first type to compared.
451 * This must be a primitive atomic type as defined by {@link net.sf.saxon.type.ItemType#getPrimitiveType}
452 * @param t2 the second type to compared.
453 * This must be a primitive atomic type as defined by {@link net.sf.saxon.type.ItemType#getPrimitiveType}
454 * @return true if the types are comparable, as defined by the rules of the "=" operator
455 */
456
457 public static boolean isGenerallyComparable(UType t1, UType t2) {
458 return t1 == UType.UNTYPED_ATOMIC ||
459 t2 == UType.UNTYPED_ATOMIC ||
460 isGuaranteedComparable(t1, t2);
461 }
462
463
464
465
466 }
467