Debian Codegraph @

net/sf/saxon/value/SequenceType.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.value;
9
10 import com.saxonica.ee.extfn.ExperimentalSequenceTypeMarshaller;
11 import com.saxonica.ee.extfn.ExperimentalXPathCompiler;
12 import net.sf.saxon.expr.StaticProperty;
13 import net.sf.saxon.expr.parser.RoleDiagnostic;
14 import net.sf.saxon.ma.map.MapItem;
15 import net.sf.saxon.om.GroundedValue;
16 import net.sf.saxon.om.Item;
17 import net.sf.saxon.om.Sequence;
18 import net.sf.saxon.om.SequenceIterator;
19 import net.sf.saxon.pattern.AnyNodeTest;
20 import net.sf.saxon.pattern.NodeKindTest;
21 import net.sf.saxon.trans.XPathException;
22 import net.sf.saxon.type.*;
23
24 import java.util.Optional;
25
26 /**
27 * SequenceType: a sequence type consists of a primary type, which indicates the type of item,
28 * and a cardinality, which indicates the number of occurrences permitted. Where the primary type
29 * is element or attribute, there may also be a content type, indicating the required type
30 * annotation on the element or attribute content.
31 */
32
33 public final class SequenceType {
34
35
36 private ItemType primaryType; // the primary type of the item, e.g. "element", "comment", or "integer"
37 private int cardinality; // the required cardinality
38
39 /**
40 * A type that allows any sequence of items
41 */
42
43 public static final SequenceType ANY_SEQUENCE =
44 AnyItemType.getInstance().zeroOrMore();
45
46 /**
47 * A type that allows exactly one item, of any kind
48 */
49
50 public static final SequenceType SINGLE_ITEM =
51 AnyItemType.getInstance().one();
52
53 /**
54 * A type that allows zero or one items, of any kind
55 */
56
57 public static final SequenceType OPTIONAL_ITEM =
58 AnyItemType.getInstance().zeroOrOne();
59
60 /**
61 * A type that allows exactly one atomic value
62 */
63
64 public static final SequenceType SINGLE_ATOMIC =
65 BuiltInAtomicType.ANY_ATOMIC.one();
66
67 /**
68 * A type that allows zero or one atomic values
69 */
70
71 public static final SequenceType OPTIONAL_ATOMIC =
72 BuiltInAtomicType.ANY_ATOMIC.zeroOrOne();
73 /*
74
75 * A type that allows zero or more atomic values
76 */
77
78 public static final SequenceType ATOMIC_SEQUENCE =
79 BuiltInAtomicType.ANY_ATOMIC.zeroOrMore();
80
81 /**
82 * A type that allows a single string
83 */
84
85 public static final SequenceType SINGLE_STRING =
86 BuiltInAtomicType.STRING.one();
87
88 /**
89 * A type that allows a single untyped atomic
90 */
91
92 public static final SequenceType SINGLE_UNTYPED_ATOMIC =
93 BuiltInAtomicType.UNTYPED_ATOMIC.one();
94
95 /**
96 * A type that allows a single optional string
97 */
98
99 public static final SequenceType OPTIONAL_STRING =
100 BuiltInAtomicType.STRING.zeroOrOne();
101
102 /**
103 * A type that allows a single boolean
104 */
105
106 public static final SequenceType SINGLE_BOOLEAN =
107 BuiltInAtomicType.BOOLEAN.one();
108
109 /**
110 * A type that allows a single optional boolean
111 */
112
113 public static final SequenceType OPTIONAL_BOOLEAN =
114 BuiltInAtomicType.BOOLEAN.zeroOrOne();
115
116 /**
117 * A type that allows a single integer
118 */
119
120 public static final SequenceType SINGLE_INTEGER =
121 BuiltInAtomicType.INTEGER.one();
122
123 /**
124 * A type that allows a single decimal
125 */
126
127 public static final SequenceType SINGLE_DECIMAL =
128 BuiltInAtomicType.DECIMAL.one();
129
130 /**
131 * A type that allows a single optional integer
132 */
133
134 public static final SequenceType OPTIONAL_INTEGER =
135 BuiltInAtomicType.INTEGER.zeroOrOne();
136
137
138 /**
139 * A type that allows a single short
140 */
141
142 public static final SequenceType SINGLE_SHORT =
143 BuiltInAtomicType.SHORT.one();
144
145 /**
146 * A type that allows a single optional short
147 */
148
149 public static final SequenceType OPTIONAL_SHORT =
150 BuiltInAtomicType.SHORT.zeroOrOne();
151
152 /**
153 * A type that allows a single short
154 */
155
156 public static final SequenceType SINGLE_BYTE =
157 BuiltInAtomicType.BYTE.one();
158
159 /**
160 * A type that allows a single optional byte
161 */
162
163 public static final SequenceType OPTIONAL_BYTE =
164 BuiltInAtomicType.BYTE.zeroOrOne();
165
166
167 /**
168 * A type that allows a single double
169 */
170
171 public static final SequenceType SINGLE_DOUBLE =
172 BuiltInAtomicType.DOUBLE.one();
173
174 /**
175 * A type that allows a single optional double
176 */
177
178 public static final SequenceType OPTIONAL_DOUBLE =
179 BuiltInAtomicType.DOUBLE.zeroOrOne();
180
181 /**
182 * A type that allows a single float
183 */
184
185 public static final SequenceType SINGLE_FLOAT =
186 BuiltInAtomicType.FLOAT.one();
187
188 /**
189 * A type that allows a single optional float
190 */
191
192 public static final SequenceType OPTIONAL_FLOAT =
193 BuiltInAtomicType.FLOAT.zeroOrOne();
194
195 /**
196 * A type that allows a single optional decimal
197 */
198
199 public static final SequenceType OPTIONAL_DECIMAL =
200 BuiltInAtomicType.DECIMAL.zeroOrOne();
201
202 /**
203 * A type that allows a single optional anyURI
204 */
205
206 public static final SequenceType OPTIONAL_ANY_URI =
207 BuiltInAtomicType.ANY_URI.zeroOrOne();
208
209 /**
210 * A type that allows a single optional date
211 */
212
213 public static final SequenceType OPTIONAL_DATE =
214 BuiltInAtomicType.DATE.zeroOrOne();
215
216 /**
217 * A type that allows a single optional time
218 */
219
220 public static final SequenceType OPTIONAL_TIME =
221 BuiltInAtomicType.TIME.zeroOrOne();
222
223 /**
224 * A type that allows a single optional gYear
225 */
226
227 public static final SequenceType OPTIONAL_G_YEAR =
228 BuiltInAtomicType.G_YEAR.zeroOrOne();
229
230 /**
231 * A type that allows a single optional gYearMonth
232 */
233
234 public static final SequenceType OPTIONAL_G_YEAR_MONTH =
235 BuiltInAtomicType.G_YEAR_MONTH.zeroOrOne();
236
237 /**
238 * A type that allows a single optional gMonth
239 */
240
241 public static final SequenceType OPTIONAL_G_MONTH =
242 BuiltInAtomicType.G_MONTH.zeroOrOne();
243
244 /**
245 * A type that allows a single optional gMonthDay
246 */
247
248 public static final SequenceType OPTIONAL_G_MONTH_DAY =
249 BuiltInAtomicType.G_MONTH_DAY.zeroOrOne();
250 /**
251 * A type that allows a single optional gDay
252 */
253
254 public static final SequenceType OPTIONAL_G_DAY =
255 BuiltInAtomicType.G_DAY.zeroOrOne();
256
257
258 /**
259 * A type that allows a single optional dateTime
260 */
261
262 public static final SequenceType OPTIONAL_DATE_TIME =
263 BuiltInAtomicType.DATE_TIME.zeroOrOne();
264 /**
265 * A type that allows a single optional duration
266 */
267
268 public static final SequenceType OPTIONAL_DURATION =
269 BuiltInAtomicType.DURATION.zeroOrOne();
270
271 /**
272 * A type that allows a single optional yearMonthDuration
273 */
274
275 public static final SequenceType OPTIONAL_YEAR_MONTH_DURATION =
276 BuiltInAtomicType.YEAR_MONTH_DURATION.zeroOrOne();
277
278 /**
279 * A type that allows a single optional dayTimeDuration
280 */
281
282 public static final SequenceType OPTIONAL_DAY_TIME_DURATION =
283 BuiltInAtomicType.DAY_TIME_DURATION.zeroOrOne();
284
285
286 /**
287 * A type that allows a single xs:QName
288 */
289
290 public static final SequenceType SINGLE_QNAME =
291 BuiltInAtomicType.QNAME.one();
292 /**
293 * A type that allows a single optional xs:QName
294 */
295
296 public static final SequenceType OPTIONAL_QNAME =
297 BuiltInAtomicType.QNAME.zeroOrOne();
298 /**
299 * A type that allows a single optional xs:NOTATION
300 */
301
302 public static final SequenceType OPTIONAL_NOTATION =
303 BuiltInAtomicType.NOTATION.zeroOrOne();
304
305 /**
306 * A type that allows a single optional xs:Base64Binary
307 */
308
309 public static final SequenceType OPTIONAL_BASE64_BINARY =
310 BuiltInAtomicType.BASE64_BINARY.zeroOrOne();
311
312 /**
313 * A type that allows a single optional xs:hexBinary
314 */
315
316 public static final SequenceType OPTIONAL_HEX_BINARY =
317 BuiltInAtomicType.HEX_BINARY.zeroOrOne();
318 /**
319 * A type that allows an optional numeric value
320 */
321
322 public static final SequenceType OPTIONAL_NUMERIC =
323 makeSequenceType(NumericType.getInstance(), StaticProperty.ALLOWS_ZERO_OR_ONE);
324
325 public static final SequenceType SINGLE_NUMERIC =
326 makeSequenceType(NumericType.getInstance(), StaticProperty.EXACTLY_ONE);
327
328 /**
329 * A type that allows zero or one nodes
330 */
331
332 public static final SequenceType OPTIONAL_NODE =
333 AnyNodeTest.getInstance().zeroOrOne();
334
335 /**
336 * A type that allows a single node
337 */
338
339 public static final SequenceType SINGLE_NODE =
340 AnyNodeTest.getInstance().one();
341
342 /**
343 * A type that allows a single document node
344 */
345
346 public static final SequenceType OPTIONAL_DOCUMENT_NODE =
347 NodeKindTest.DOCUMENT.zeroOrOne();
348
349 /**
350 * A type that allows a sequence of zero or more nodes
351 */
352
353 public static final SequenceType NODE_SEQUENCE =
354 AnyNodeTest.getInstance().zeroOrMore();
355
356 /**
357 * A type that allows a sequence of zero or more string values
358 */
359 public static final SequenceType STRING_SEQUENCE =
360 BuiltInAtomicType.STRING.zeroOrMore();
361
362 /**
363 * A type that allows a single function item
364 */
365
366 public static final SequenceType SINGLE_FUNCTION =
367 makeSequenceType(AnyFunctionType.ANY_FUNCTION, StaticProperty.EXACTLY_ONE);
368
369 /**
370 * A type that allows a sequence of zero or one function items
371 */
372 public static final SequenceType OPTIONAL_FUNCTION_ITEM =
373 makeSequenceType(AnyFunctionType.getInstance(), StaticProperty.ALLOWS_ZERO_OR_ONE);
374
375 /**
376 * A type that allows a sequence of zero or mode function items
377 */
378 public static final SequenceType FUNCTION_ITEM_SEQUENCE =
379 makeSequenceType(AnyFunctionType.getInstance(), StaticProperty.ALLOWS_ZERO_OR_MORE);
380
381
382 /**
383 * A type that only permits the empty sequence
384 */
385
386 public static final SequenceType EMPTY_SEQUENCE =
387 new SequenceType(ErrorType.getInstance(), StaticProperty.EMPTY);
388
389 /**
390 * A type that only permits a non-empty sequence
391 */
392
393 public static final SequenceType NON_EMPTY_SEQUENCE =
394 makeSequenceType(AnyItemType.getInstance(), StaticProperty.ALLOWS_ONE_OR_MORE);
395
396 /**
397 * A type that has no instances
398 */
399
400 public static final SequenceType VOID =
401 makeSequenceType(ErrorType.getInstance(), StaticProperty.ALLOWS_MANY);
402
403 /**
404 * Construct an instance of SequenceType.
405 *
406 * @param primaryType The item type
407 * @param cardinality The required cardinality
408 */
409 public SequenceType(ItemType primaryType, int cardinality) {
410 this.primaryType = primaryType;
411 if (primaryType instanceof ErrorType && Cardinality.allowsZero(cardinality)) {
412 this.cardinality = StaticProperty.EMPTY;
413 } else {
414 this.cardinality = cardinality;
415 }
416 }
417
418 /**
419 * Construct an instance of SequenceType. This is a factory method: it maintains a
420 * pool of SequenceType objects to reduce the amount of object creation.
421 *
422 * @param primaryType The item type
423 * @param cardinality The required cardinality. This must be one of the constants {@link StaticProperty#EXACTLY_ONE},
424 * {@link StaticProperty#ALLOWS_ONE_OR_MORE}, etc
425 * @return the SequenceType (either a newly created object, or an existing one from the cache)
426 */
427
428 public static SequenceType makeSequenceType(ItemType primaryType, int cardinality) {
429
430 if (primaryType instanceof ItemType.WithSequenceTypeCache) {
431 ItemType.WithSequenceTypeCache bat = (ItemType.WithSequenceTypeCache) primaryType;
432 switch (cardinality) {
433 case StaticProperty.EXACTLY_ONE:
434 return bat.one();
435 case StaticProperty.ALLOWS_ZERO_OR_ONE:
436 return bat.zeroOrOne();
437 case StaticProperty.ALLOWS_ZERO_OR_MORE:
438 return bat.zeroOrMore();
439 case StaticProperty.ALLOWS_ONE_OR_MORE:
440 return bat.oneOrMore();
441 default:
442 // fall through
443 }
444 }
445 if (cardinality == StaticProperty.ALLOWS_ZERO) {
446 return SequenceType.EMPTY_SEQUENCE;
447 }
448 return new SequenceType(primaryType, cardinality);
449 }
450
451 /**
452 * Get the "primary" part of this required type. E.g. for type element(*, xs:date) the "primary type" is element()
453 *
454 * @return The item type code of the primary type
455 */
456 public ItemType getPrimaryType() {
457 return primaryType;
458 }
459
460 /**
461 * Get the cardinality component of this SequenceType. This is one of the constants {@link StaticProperty#EXACTLY_ONE},
462 * {@link StaticProperty#ALLOWS_ONE_OR_MORE}, etc
463 *
464 * @return the required cardinality
465 * @see net.sf.saxon.value.Cardinality
466 */
467 public int getCardinality() {
468 return cardinality;
469 }
470
471 /**
472 * Determine whether a given value is a valid instance of this SequenceType
473 *
474 * @param value the value to be tested
475 * @param th the type hierarchy cache
476 * @return true if the value is a valid instance of this type
477 * @throws XPathException if a dynamic error occurs while evaluating the Sequence (this
478 * won't happen if the sequence is grounded)
479 */
480
481 public boolean matches(Sequence<?> value, TypeHierarchy th) throws XPathException {
482 int count = 0;
483 SequenceIterator<?> iter = value.iterate();
484 Item item;
485 while ((item = iter.next()) != null) {
486 count++;
487 if (!primaryType.matches(item, th)) {
488 return false;
489 }
490 }
491 return !(count == 0 && !Cardinality.allowsZero(cardinality)
492 || count > 1 && !Cardinality.allowsMany(cardinality));
493 }
494
495 /**
496 * Get extra diagnostic information about why a supplied item does not conform to this
497 * item type, if available. If extra information is returned, it should be in the form of a complete
498 * sentence, minus the closing full stop. No information should be returned for obvious cases.
499 *
500 * @param value the value which has been found not to match this sequence type
501 * @param th the TypeHierarchy cache
502 */
503
504 public Optional<String> explainMismatch(GroundedValue value, TypeHierarchy th) {
505 try {
506 int count = 0;
507 SequenceIterator iter = value.iterate();
508 Item item;
509 while ((item = iter.next()) != null) {
510 count++;
511 if (!primaryType.matches(item, th)) {
512 String s = "The " + RoleDiagnostic.ordinal(count) + " item is not an instance of the required type";
513 Optional<String> more = primaryType.explainMismatch(item, th);
514 if (more.isPresent()) {
515 s = count == 1 ? more.get() : s + ". " + more.get();
516 } else {
517 if (count == 1) {
518 return Optional.empty(); // no new information, so don't say anything
519 }
520 }
521 return Optional.of(s);
522 }
523 }
524 if (count == 0 && !Cardinality.allowsZero(cardinality)) {
525 return Optional.of("The type does not allow an empty sequence");
526 } else if (count > 1 && !Cardinality.allowsMany(cardinality)) {
527 return Optional.of("The type does not allow a sequence of more than one item");
528 }
529 return Optional.empty();
530 } catch (XPathException e) {
531 return Optional.empty();
532 }
533 }
534
535 /**
536 * Return a string representation of this SequenceType
537 *
538 * @return the string representation as an instance of the XPath
539 * SequenceType construct
540 */
541 public String toString() {
542 if (cardinality == StaticProperty.ALLOWS_ZERO) {
543 return "empty-sequence()";
544 } else {
545 return primaryType + Cardinality.getOccurrenceIndicator(cardinality);
546 }
547 }
548
549 /**
550 * Return a string representation of this SequenceType suitable for use in stylesheet
551 * export files. This differs from the result of toString() in that it will not contain
552 * any references to anonymous types. Note that it may also use the Saxon extended syntax
553 * for union types and tuple types.
554 *
555 * @return the string representation as an instance of the XPath SequenceType construct
556 */
557 public String toExportString() {
558 if (cardinality == StaticProperty.ALLOWS_ZERO) {
559 return "empty-sequence()";
560 } else {
561 return primaryType.toExportString() + Cardinality.getOccurrenceIndicator(cardinality);
562 }
563 }
564
565 public String toExportString2() {
566 String result = null;
567 //#if EE==true
568 MapItem map = ExperimentalXPathCompiler.makeMap(this);
569 result= ExperimentalSequenceTypeMarshaller.unmarshal(map).getStringValue();
570 //#endif
571 return result;
572 }
573
574 /**
575 * Returns a hash code value for the object.
576 */
577 public int hashCode() {
578 return primaryType.hashCode() ^ cardinality;
579 }
580
581 /**
582 * Indicates whether some other object is "equal to" this one.
583 */
584 public boolean equals(/*@NotNull*/ Object obj) {
585 return obj instanceof SequenceType &&
586 this.primaryType.equals(((SequenceType) obj).primaryType) &&
587 this.cardinality == ((SequenceType) obj).cardinality;
588 }
589
590 public boolean isSameType(SequenceType other, TypeHierarchy th) {
591 return cardinality == other.cardinality && th.relationship(primaryType, other.primaryType) == TypeHierarchy.SAME_TYPE;
592 }
593
594
595 }
596