Debian Codegraph @

net/sf/saxon/type/BuiltInAtomicType.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 com.saxonica.ee.schema.UserSimpleType;
11 import net.sf.saxon.expr.Expression;
12 import net.sf.saxon.expr.Literal;
13 import net.sf.saxon.expr.StaticProperty;
14 import net.sf.saxon.expr.instruct.ValueOf;
15 import net.sf.saxon.lib.ConversionRules;
16 import net.sf.saxon.lib.NamespaceConstant;
17 import net.sf.saxon.om.*;
18 import net.sf.saxon.trans.XPathException;
19 import net.sf.saxon.value.*;
20
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.Set;
25
26 import static net.sf.saxon.type.SchemaComponent.ValidationStatus.VALIDATED;
27
28 /**
29 * This class represents a built-in atomic type, which may be either a primitive type
30 * (such as xs:decimal or xs:anyURI) or a derived type (such as xs:ID or xs:dayTimeDuration).
31 */
32
33 public class BuiltInAtomicType implements AtomicType, ItemType.WithSequenceTypeCache {
34
35 private int fingerprint;
36 private int baseFingerprint;
37 private int primitiveFingerprint;
38 private UType uType;
39 private String alphaCode;
40 private boolean ordered = false;
41 public StringConverter stringConverter; // may be null for types where conversion rules can vary
42 private SequenceType _one;
43 private SequenceType _oneOrMore;
44 private SequenceType _zeroOrOne;
45 private SequenceType _zeroOrMore;
46
47 /**
48 * Table of all built in atomic types, indexed by the alphacode, eg. ADI = integer
49 */
50
51 private static Map<String, BuiltInAtomicType> lookupByAlphaCode = new HashMap<>(100);
52
53 public final static BuiltInAtomicType ANY_ATOMIC =
54 makeAtomicType(StandardNames.XS_ANY_ATOMIC_TYPE, AnySimpleType.getInstance(), "A", true);
55
56 public final static BuiltInAtomicType STRING =
57 makeAtomicType(StandardNames.XS_STRING, ANY_ATOMIC, "AS", true);
58
59 public final static BuiltInAtomicType BOOLEAN =
60 makeAtomicType(StandardNames.XS_BOOLEAN, ANY_ATOMIC, "AB", true);
61
62 public final static BuiltInAtomicType DURATION =
63 makeAtomicType(StandardNames.XS_DURATION, ANY_ATOMIC, "AR", false);
64
65 public final static BuiltInAtomicType DATE_TIME =
66 makeAtomicType(StandardNames.XS_DATE_TIME, ANY_ATOMIC, "AM", true);
67
68 public final static BuiltInAtomicType DATE =
69 makeAtomicType(StandardNames.XS_DATE, ANY_ATOMIC, "AA", true);
70
71 public final static BuiltInAtomicType TIME =
72 makeAtomicType(StandardNames.XS_TIME, ANY_ATOMIC, "AT", true);
73
74 public final static BuiltInAtomicType G_YEAR_MONTH =
75 makeAtomicType(StandardNames.XS_G_YEAR_MONTH, ANY_ATOMIC, "AH", false);
76
77 public final static BuiltInAtomicType G_MONTH =
78 makeAtomicType(StandardNames.XS_G_MONTH, ANY_ATOMIC, "AI", false);
79
80 public final static BuiltInAtomicType G_MONTH_DAY =
81 makeAtomicType(StandardNames.XS_G_MONTH_DAY, ANY_ATOMIC, "AJ", false);
82
83 public final static BuiltInAtomicType G_YEAR =
84 makeAtomicType(StandardNames.XS_G_YEAR, ANY_ATOMIC, "AG", false);
85
86 public final static BuiltInAtomicType G_DAY =
87 makeAtomicType(StandardNames.XS_G_DAY, ANY_ATOMIC, "AK", false);
88
89 public final static BuiltInAtomicType HEX_BINARY =
90 makeAtomicType(StandardNames.XS_HEX_BINARY, ANY_ATOMIC, "AX", true);
91
92 public final static BuiltInAtomicType BASE64_BINARY =
93 makeAtomicType(StandardNames.XS_BASE64_BINARY, ANY_ATOMIC, "A2", true);
94
95 public final static BuiltInAtomicType ANY_URI =
96 makeAtomicType(StandardNames.XS_ANY_URI, ANY_ATOMIC, "AU", true);
97
98 public final static BuiltInAtomicType QNAME =
99 makeAtomicType(StandardNames.XS_QNAME, ANY_ATOMIC, "AQ", false);
100
101 public final static BuiltInAtomicType NOTATION =
102 makeAtomicType(StandardNames.XS_NOTATION, ANY_ATOMIC, "AN", false);
103
104 public final static BuiltInAtomicType UNTYPED_ATOMIC =
105 makeAtomicType(StandardNames.XS_UNTYPED_ATOMIC, ANY_ATOMIC, "AZ", true);
106
107 public final static BuiltInAtomicType DECIMAL =
108 makeAtomicType(StandardNames.XS_DECIMAL, ANY_ATOMIC, "AD", true);
109
110 public final static BuiltInAtomicType FLOAT =
111 makeAtomicType(StandardNames.XS_FLOAT, ANY_ATOMIC, "AF", true);
112
113 public final static BuiltInAtomicType DOUBLE =
114 makeAtomicType(StandardNames.XS_DOUBLE, ANY_ATOMIC, "AO", true);
115
116 public final static BuiltInAtomicType INTEGER =
117 makeAtomicType(StandardNames.XS_INTEGER, DECIMAL, "ADI", true);
118
119 public final static BuiltInAtomicType NON_POSITIVE_INTEGER =
120 makeAtomicType(StandardNames.XS_NON_POSITIVE_INTEGER, INTEGER, "ADIN", true);
121
122 public final static BuiltInAtomicType NEGATIVE_INTEGER =
123 makeAtomicType(StandardNames.XS_NEGATIVE_INTEGER, NON_POSITIVE_INTEGER, "ADINN", true);
124
125 public final static BuiltInAtomicType LONG =
126 makeAtomicType(StandardNames.XS_LONG, INTEGER, "ADIL", true);
127
128 public final static BuiltInAtomicType INT =
129 makeAtomicType(StandardNames.XS_INT, LONG, "ADILI", true);
130
131 public final static BuiltInAtomicType SHORT =
132 makeAtomicType(StandardNames.XS_SHORT, INT, "ADILIS", true);
133
134 public final static BuiltInAtomicType BYTE =
135 makeAtomicType(StandardNames.XS_BYTE, SHORT, "ADILISB", true);
136
137 public final static BuiltInAtomicType NON_NEGATIVE_INTEGER =
138 makeAtomicType(StandardNames.XS_NON_NEGATIVE_INTEGER, INTEGER, "ADIP", true);
139
140 public final static BuiltInAtomicType POSITIVE_INTEGER =
141 makeAtomicType(StandardNames.XS_POSITIVE_INTEGER, NON_NEGATIVE_INTEGER, "ADIPP", true);
142
143 public final static BuiltInAtomicType UNSIGNED_LONG =
144 makeAtomicType(StandardNames.XS_UNSIGNED_LONG, NON_NEGATIVE_INTEGER, "ADIPL", true);
145
146 public final static BuiltInAtomicType UNSIGNED_INT =
147 makeAtomicType(StandardNames.XS_UNSIGNED_INT, UNSIGNED_LONG, "ADIPLI", true);
148
149 public final static BuiltInAtomicType UNSIGNED_SHORT =
150 makeAtomicType(StandardNames.XS_UNSIGNED_SHORT, UNSIGNED_INT, "ADIPLIS", true);
151
152 public final static BuiltInAtomicType UNSIGNED_BYTE =
153 makeAtomicType(StandardNames.XS_UNSIGNED_BYTE, UNSIGNED_SHORT, "ADIPLISB", true);
154
155 public final static BuiltInAtomicType YEAR_MONTH_DURATION =
156 makeAtomicType(StandardNames.XS_YEAR_MONTH_DURATION, DURATION, "ARY", true);
157
158 public final static BuiltInAtomicType DAY_TIME_DURATION =
159 makeAtomicType(StandardNames.XS_DAY_TIME_DURATION, DURATION, "ARD", true);
160
161 public final static BuiltInAtomicType NORMALIZED_STRING =
162 makeAtomicType(StandardNames.XS_NORMALIZED_STRING, STRING, "ASN", true);
163
164 public final static BuiltInAtomicType TOKEN =
165 makeAtomicType(StandardNames.XS_TOKEN, NORMALIZED_STRING, "ASNT", true);
166
167 public final static BuiltInAtomicType LANGUAGE =
168 makeAtomicType(StandardNames.XS_LANGUAGE, TOKEN, "ASNTL", true);
169
170 public final static BuiltInAtomicType NAME =
171 makeAtomicType(StandardNames.XS_NAME, TOKEN, "ASNTN", true);
172
173 public final static BuiltInAtomicType NMTOKEN =
174 makeAtomicType(StandardNames.XS_NMTOKEN, TOKEN, "ASNTK", true);
175
176 public final static BuiltInAtomicType NCNAME =
177 makeAtomicType(StandardNames.XS_NCNAME, NAME, "ASNTNC", true);
178
179 public final static BuiltInAtomicType ID =
180 makeAtomicType(StandardNames.XS_ID, NCNAME, "ASNTNCI", true);
181
182 public final static BuiltInAtomicType IDREF =
183 makeAtomicType(StandardNames.XS_IDREF, NCNAME, "ASNTNCR", true);
184
185 public final static BuiltInAtomicType ENTITY =
186 makeAtomicType(StandardNames.XS_ENTITY, NCNAME, "ASNTNCE", true);
187
188 public final static BuiltInAtomicType DATE_TIME_STAMP =
189 makeAtomicType(StandardNames.XS_DATE_TIME_STAMP, DATE_TIME, "AMP", true);
190
191 static {
192 // See bug 2524
193 ANY_ATOMIC .stringConverter = StringConverter.StringToString.INSTANCE;
194 STRING .stringConverter = StringConverter.StringToString.INSTANCE;
195 LANGUAGE .stringConverter = StringConverter.StringToLanguage.INSTANCE;
196 NORMALIZED_STRING .stringConverter = StringConverter.StringToNormalizedString.INSTANCE;
197 TOKEN .stringConverter = StringConverter.StringToToken.INSTANCE;
198 NCNAME .stringConverter = StringConverter.StringToNCName.TO_NCNAME;
199 NAME .stringConverter = StringConverter.StringToName.INSTANCE;
200 NMTOKEN .stringConverter = StringConverter.StringToNMTOKEN.INSTANCE;
201 ID .stringConverter = StringConverter.StringToNCName.TO_ID;
202 IDREF .stringConverter = StringConverter.StringToNCName.TO_IDREF;
203 ENTITY .stringConverter = StringConverter.StringToNCName.TO_ENTITY;
204 DECIMAL .stringConverter = StringConverter.StringToDecimal.INSTANCE;
205 INTEGER .stringConverter = StringConverter.StringToInteger.INSTANCE;
206 DURATION .stringConverter = StringConverter.StringToDuration.INSTANCE;
207 G_MONTH .stringConverter = StringConverter.StringToGMonth.INSTANCE;
208 G_MONTH_DAY .stringConverter = StringConverter.StringToGMonthDay.INSTANCE;
209 G_DAY .stringConverter = StringConverter.StringToGDay.INSTANCE;
210 DAY_TIME_DURATION .stringConverter = StringConverter.StringToDayTimeDuration.INSTANCE;
211 YEAR_MONTH_DURATION .stringConverter = StringConverter.StringToYearMonthDuration.INSTANCE;
212 TIME .stringConverter = StringConverter.StringToTime.INSTANCE;
213 BOOLEAN .stringConverter = StringConverter.StringToBoolean.INSTANCE;
214 HEX_BINARY .stringConverter = StringConverter.StringToHexBinary.INSTANCE;
215 BASE64_BINARY .stringConverter = StringConverter.StringToBase64Binary.INSTANCE;
216 UNTYPED_ATOMIC .stringConverter = StringConverter.StringToUntypedAtomic.INSTANCE;
217
218 NON_POSITIVE_INTEGER.stringConverter = new StringConverter.StringToIntegerSubtype(NON_POSITIVE_INTEGER);
219 NEGATIVE_INTEGER .stringConverter = new StringConverter.StringToIntegerSubtype(NEGATIVE_INTEGER);
220 LONG .stringConverter = new StringConverter.StringToIntegerSubtype(LONG);
221 INT .stringConverter = new StringConverter.StringToIntegerSubtype(INT);
222 SHORT .stringConverter = new StringConverter.StringToIntegerSubtype(SHORT);
223 BYTE .stringConverter = new StringConverter.StringToIntegerSubtype(BYTE);
224 NON_NEGATIVE_INTEGER.stringConverter = new StringConverter.StringToIntegerSubtype(NON_NEGATIVE_INTEGER);
225 POSITIVE_INTEGER .stringConverter = new StringConverter.StringToIntegerSubtype(POSITIVE_INTEGER);
226 UNSIGNED_LONG .stringConverter = new StringConverter.StringToIntegerSubtype(UNSIGNED_LONG);
227 UNSIGNED_INT .stringConverter = new StringConverter.StringToIntegerSubtype(UNSIGNED_INT);
228 UNSIGNED_SHORT .stringConverter = new StringConverter.StringToIntegerSubtype(UNSIGNED_SHORT);
229 UNSIGNED_BYTE .stringConverter = new StringConverter.StringToIntegerSubtype(UNSIGNED_BYTE);
230
231 // We were getting an IntelliJ warning here about potential class loading deadlock. See bug #2524. Have moved the
232 // static initializers here, and removed the dependency on static initialization in StringConverter, which hopefully
233 // solves the problem.
234 }
235
236 private BuiltInAtomicType(int fingerprint) {
237 this.fingerprint = fingerprint;
238 }
239
240
241 /**
242 * Get the local name of this type
243 *
244 * @return the local name of this type definition, if it has one. Return null in the case of an
245 * anonymous type.
246 */
247
248 public String getName() {
249 return StandardNames.getLocalName(fingerprint);
250 }
251
252 /**
253 * Get the corresponding {@link net.sf.saxon.type.UType}. A UType is a union of primitive item
254 * types.
255 *
256 * @return the smallest UType that subsumes this item type
257 */
258 public UType getUType() {
259 return uType;
260 }
261
262 /**
263 * Get the target namespace of this type
264 *
265 * @return the target namespace of this type definition, if it has one. Return null in the case
266 * of an anonymous type, and in the case of a global type defined in a no-namespace schema.
267 */
268
269 public String getTargetNamespace() {
270 return NamespaceConstant.SCHEMA;
271 }
272
273 /**
274 * Get the name of this type as an EQName, that is, a string in the format Q{uri}local.
275 *
276 * @return an EQName identifying the type.
277 */
278 public String getEQName() {
279 return "Q{" + NamespaceConstant.SCHEMA + "}" + getName();
280 }
281
282 /**
283 * Determine whether the type is abstract, that is, whether it cannot have instances that are not also
284 * instances of some concrete subtype
285 */
286
287 public boolean isAbstract() {
288 switch (fingerprint) {
289 case StandardNames.XS_NOTATION:
290 case StandardNames.XS_ANY_ATOMIC_TYPE:
291 case StandardNames.XS_NUMERIC:
292 case StandardNames.XS_ANY_SIMPLE_TYPE:
293 return true;
294 default:
295 return false;
296 }
297 }
298
299 /**
300 * Return true if this is an external object type, that is, a Saxon-defined type for external
301 * Java or .NET objects
302 */
303
304 public boolean isExternalType() {
305 return false;
306 }
307
308 /**
309 * Determine whether this is a built-in type or a user-defined type
310 */
311
312 public boolean isBuiltInType() {
313 return true;
314 }
315
316 /**
317 * Get the name of this type as a StructuredQName, unless the type is anonymous, in which case
318 * return null
319 *
320 * @return the name of the atomic type, or null if the type is anonymous.
321 */
322
323 /*@NotNull*/
324 public StructuredQName getTypeName() {
325 return new StructuredQName(
326 StandardNames.getPrefix(fingerprint),
327 StandardNames.getURI(fingerprint),
328 StandardNames.getLocalName(fingerprint)
329 );
330 }
331
332 /**
333 * Get an alphabetic code representing the type, or at any rate, the nearest built-in type
334 * from which this type is derived. The codes are designed so that for any two built-in types
335 * A and B, alphaCode(A) is a prefix of alphaCode(B) if and only if A is a supertype of B.
336 *
337 * @return the alphacode for the nearest containing built-in type
338 */
339 @Override
340 public String getAlphaCode() {
341 return alphaCode;
342 }
343
344 /**
345 * Get a sequence type representing exactly one instance of this atomic type
346 * @return a sequence type representing exactly one instance of this atomic type
347 * @since 9.8.0.2
348 */
349
350 public SequenceType one() {
351 if (_one == null) {
352 _one = new SequenceType(this, StaticProperty.EXACTLY_ONE);
353 }
354 return _one;
355 }
356
357 /**
358 * Get a sequence type representing zero or one instances of this atomic type
359 *
360 * @return a sequence type representing zero or one instances of this atomic type
361 * @since 9.8.0.2
362 */
363
364 public SequenceType zeroOrOne() {
365 if (_zeroOrOne == null) {
366 _zeroOrOne = new SequenceType(this, StaticProperty.ALLOWS_ZERO_OR_ONE);
367 }
368 return _zeroOrOne;
369 }
370
371 /**
372 * Get a sequence type representing one or more instances of this atomic type
373 *
374 * @return a sequence type representing one or more instances of this atomic type
375 * @since 9.8.0.2
376 */
377
378 public SequenceType oneOrMore() {
379 if (_oneOrMore == null) {
380 _oneOrMore = new SequenceType(this, StaticProperty.ALLOWS_ONE_OR_MORE);
381 }
382 return _oneOrMore;
383 }
384
385 /**
386 * Get a sequence type representing one or more instances of this atomic type
387 *
388 * @return a sequence type representing one or more instances of this atomic type
389 * @since 9.8.0.2
390 */
391
392 public SequenceType zeroOrMore() {
393 if (_zeroOrMore == null) {
394 _zeroOrMore = new SequenceType(this, StaticProperty.ALLOWS_ZERO_OR_MORE);
395 }
396 return _zeroOrMore;
397 }
398
399 /**
400 * Get the redefinition level. This is zero for a component that has not been redefined;
401 * for a redefinition of a level-0 component, it is 1; for a redefinition of a level-N
402 * component, it is N+1. This concept is used to support the notion of "pervasive" redefinition:
403 * if a component is redefined at several levels, the top level wins, but it is an error to have
404 * two versions of the component at the same redefinition level.
405 *
406 * @return the redefinition level
407 */
408
409 public int getRedefinitionLevel() {
410 return 0;
411 }
412
413 /**
414 * Determine whether the atomic type is ordered, that is, whether less-than and greater-than comparisons
415 * are permitted
416 *
417 * @param optimistic if true, the function takes an optimistic view, returning true if ordering comparisons
418 * are available for some subtype. This mainly affects xs:duration, where the function returns true if
419 * optimistic is true, false if it is false.
420 * @return true if ordering operations are permitted
421 */
422
423 public boolean isOrdered(boolean optimistic) {
424 return ordered || (optimistic && (this == DURATION || this == ANY_ATOMIC));
425 }
426
427
428 /**
429 * Get the URI of the schema document where the type was originally defined.
430 *
431 * @return the URI of the schema document. Returns null if the information is unknown or if this
432 * is a built-in type
433 */
434
435 /*@Nullable*/
436 public String getSystemId() {
437 return null;
438 }
439
440 /**
441 * Determine whether the atomic type is numeric
442 *
443 * @return true if the type is a built-in numeric type
444 */
445
446 public boolean isPrimitiveNumeric() {
447 switch (getFingerprint()) {
448 case StandardNames.XS_INTEGER:
449 case StandardNames.XS_DECIMAL:
450 case StandardNames.XS_DOUBLE:
451 case StandardNames.XS_FLOAT:
452 return true;
453 default:
454 return false;
455 }
456 }
457
458 /**
459 * Get the validation status - always valid
460 */
461 public final ValidationStatus getValidationStatus() {
462 return VALIDATED;
463 }
464
465 /**
466 * Returns the value of the 'block' attribute for this type, as a bit-significant
467 * integer with fields such as {@link SchemaType#DERIVATION_LIST} and {@link SchemaType#DERIVATION_EXTENSION}
468 *
469 * @return the value of the 'block' attribute for this type
470 */
471
472 public final int getBlock() {
473 return 0;
474 }
475
476 /**
477 * Gets the integer code of the derivation method used to derive this type from its
478 * parent. Returns zero for primitive types.
479 *
480 * @return a numeric code representing the derivation method, for example {@link SchemaType#DERIVATION_RESTRICTION}
481 */
482
483 public final int getDerivationMethod() {
484 return SchemaType.DERIVATION_RESTRICTION;
485 }
486
487 /**
488 * Determines whether derivation (of a particular kind)
489 * from this type is allowed, based on the "final" property
490 *
491 * @param derivation the kind of derivation, for example {@link SchemaType#DERIVATION_LIST}
492 * @return true if this kind of derivation is allowed
493 */
494
495 public final boolean allowsDerivation(int derivation) {
496 return true;
497 }
498
499 /**
500 * Get the types of derivation that are not permitted, by virtue of the "final" property.
501 *
502 * @return the types of derivation that are not permitted, as a bit-significant integer
503 * containing bits such as {@link net.sf.saxon.type.SchemaType#DERIVATION_EXTENSION}
504 */
505 public int getFinalProhibitions() {
506 return 0;
507 }
508
509 /**
510 * Set the base type of this type
511 *
512 * @param baseFingerprint the namepool fingerprint of the name of the base type
513 */
514
515 public final void setBaseTypeFingerprint(int baseFingerprint) {
516 this.baseFingerprint = baseFingerprint;
517 }
518
519 /**
520 * Get the fingerprint of the name of this type
521 *
522 * @return the fingerprint. Returns an invented fingerprint for an anonymous type.
523 */
524
525 public final int getFingerprint() {
526 return fingerprint;
527 }
528
529 /**
530 * Get the name of the type as a QName
531 *
532 * @return a StructuredQName containing the name of the type. The conventional prefix "xs" is used
533 * to represent the XML Schema namespace
534 */
535
536 /*@NotNull*/
537 public final StructuredQName getStructuredQName() {
538 return new StructuredQName("xs", NamespaceConstant.SCHEMA, StandardNames.getLocalName(fingerprint));
539 }
540
541 /**
542 * Get the display name of the type: that is, a lexical QName with an arbitrary prefix
543 *
544 * @return a lexical QName identifying the type
545 */
546
547 public String getDisplayName() {
548 return StandardNames.getDisplayName(fingerprint);
549 }
550
551
552 /**
553 * Ask whether the atomic type is a primitive type. The primitive types are
554 * the 19 primitive types of XML Schema, plus xs:integer, xs:dayTimeDuration and xs:yearMonthDuration;
555 * xs:untypedAtomic; and all supertypes of these (xs:anyAtomicType, xs:numeric, ...)
556 *
557 * @return true if the type is considered primitive under the above rules
558 */
559
560 public final boolean isPrimitiveType() {
561 return Type.isPrimitiveAtomicType(fingerprint);
562 }
563
564 /**
565 * Ask whether this SchemaType is a complex type
566 *
567 * @return true if this SchemaType is a complex type
568 */
569
570 public final boolean isComplexType() {
571 return false;
572 }
573
574 /**
575 * Ask whether this is an anonymous type
576 *
577 * @return true if this SchemaType is an anonymous type
578 */
579
580 public final boolean isAnonymousType() {
581 return false;
582 }
583
584 /**
585 * Ask whether this is a plain type (a type whose instances are always atomic values)
586 *
587 * @return true
588 */
589
590 public boolean isPlainType() {
591 return true;
592 }
593
594 /**
595 * Returns the base type that this type inherits from. This method can be used to get the
596 * base type of a type that is known to be valid.
597 * If this type is a Simpletype that is a built in primitive type then null is returned.
598 *
599 * @return the base type.
600 * @throws IllegalStateException if this type is not valid.
601 */
602
603 /*@Nullable*/
604 public final SchemaType getBaseType() {
605 if (baseFingerprint == -1) {
606 return null;
607 } else {
608 return BuiltInType.getSchemaType(baseFingerprint);
609 }
610 }
611
612 /**
613 * Test whether a given item conforms to this type
614 *
615 *
616 *
617 * @param item The item to be tested
618 * @param th The type hierarchy cache
619 * @return true if the item is an instance of this type; false otherwise
620 */
621 public boolean matches(Item item, TypeHierarchy th) {
622 return item instanceof AtomicValue && Type.isSubType(((AtomicValue) item).getItemType(), this);
623 }
624
625 /**
626 * Get the primitive item type corresponding to this item type. For item(),
627 * this is Type.ITEM. For node(), it is Type.NODE. For specific node kinds,
628 * it is the value representing the node kind, for example Type.ELEMENT.
629 * For anyAtomicValue it is Type.ATOMIC_VALUE. For numeric it is Type.NUMBER.
630 * For other atomic types it is the primitive type as defined in XML Schema,
631 * except that INTEGER is considered to be a primitive type.
632 */
633
634 /*@NotNull*/
635 public BuiltInAtomicType getPrimitiveItemType() {
636 if (isPrimitiveType()) {
637 return this;
638 } else {
639 ItemType s = (ItemType) getBaseType();
640 assert s != null;
641 if (s.isPlainType()) {
642 return (BuiltInAtomicType)s.getPrimitiveItemType();
643 } else {
644 return this;
645 }
646 }
647 }
648
649 /**
650 * Get the primitive type corresponding to this item type. For item(),
651 * this is Type.ITEM. For node(), it is Type.NODE. For specific node kinds,
652 * it is the value representing the node kind, for example Type.ELEMENT.
653 * For anyAtomicValue it is Type.ATOMIC_VALUE. For numeric it is Type.NUMBER.
654 * For other atomic types it is the primitive type as defined in XML Schema,
655 * except that INTEGER is considered to be a primitive type.
656 */
657
658 public int getPrimitiveType() {
659 return primitiveFingerprint;
660 }
661
662 /**
663 * Determine whether this type is supported when using XSD 1.0
664 *
665 * @return true if this type is permitted in XSD 1.0
666 */
667
668 public boolean isAllowedInXSD10() {
669 return getFingerprint() != StandardNames.XS_DATE_TIME_STAMP;
670 }
671
672 public String toString() {
673 return getDisplayName();
674 }
675
676 /**
677 * Get the item type of the atomic values that will be produced when an item
678 * of this type is atomized
679 */
680
681 /*@NotNull*/
682 public AtomicType getAtomizedItemType() {
683 return this;
684 }
685
686 /**
687 * Ask whether values of this type are atomizable
688 *
689 * @return true unless it is known that these items will be elements with element-only
690 * content, in which case return false
691 * @param th The type hierarchy cache
692 */
693
694 public boolean isAtomizable(TypeHierarchy th) {
695 return true;
696 }
697
698 /**
699 * Returns the base type that this type inherits from. This method can be used to get the
700 * base type of a type that is known to be valid.
701 * If this type is a Simpletype that is a built in primitive type then null is returned.
702 *
703 * @return the base type.
704 * @throws IllegalStateException if this type is not valid.
705 */
706
707 /*@Nullable*/
708 public SchemaType getKnownBaseType() {
709 return getBaseType();
710 }
711
712 /**
713 * Test whether this is the same type as another type. They are considered to be the same type
714 * if they are derived from the same type definition in the original XML representation (which
715 * can happen when there are multiple includes of the same file)
716 */
717
718 public boolean isSameType(SchemaType other) {
719 return other.getFingerprint() == getFingerprint();
720 }
721
722 public String getDescription() {
723 return getDisplayName();
724 }
725
726
727 /**
728 * Check that this type is validly derived from a given type
729 *
730 * @param type the type from which this type is derived
731 * @param block the derivations that are blocked by the relevant element declaration
732 * @throws SchemaException if the derivation is not allowed
733 */
734
735 public void checkTypeDerivationIsOK(SchemaType type, int block) throws SchemaException {
736 if (type == AnySimpleType.getInstance()) {
737 // OK
738 } else if (isSameType(type)) {
739 // OK
740 } else {
741 SchemaType base = getBaseType();
742 if (base == null) {
743 throw new SchemaException("The type " + getDescription() +
744 " is not validly derived from the type " + type.getDescription());
745 }
746 try {
747 base.checkTypeDerivationIsOK(type, block);
748 } catch (SchemaException se) {
749 throw new SchemaException("The type " + getDescription() +
750 " is not validly derived from the type " + type.getDescription());
751 }
752 }
753 }
754
755 /**
756 * Returns true if this SchemaType is a SimpleType
757 *
758 * @return true (always)
759 */
760
761 public final boolean isSimpleType() {
762 return true;
763 }
764
765 /**
766 * Test whether this Simple Type is an atomic type
767 *
768 * @return true, this is an atomic type
769 */
770
771 public boolean isAtomicType() {
772 return true;
773 }
774
775 /**
776 * Ask whether this type is an ID type. This is defined to be any simple type
777 * who typed value may contain atomic values of type xs:ID: that is, it includes types derived
778 * from ID by restriction, list, or union. Note that for a node to be treated
779 * as an ID, its typed value must be a *single* atomic value of type ID; the type of the
780 * node, however, can still allow a list.
781 */
782
783 public boolean isIdType() {
784 return fingerprint == StandardNames.XS_ID;
785 }
786
787 /**
788 * Ask whether this type is an IDREF or IDREFS type. This is defined to be any simple type
789 * who typed value may contain atomic values of type xs:IDREF: that is, it includes types derived
790 * from IDREF or IDREFS by restriction, list, or union
791 */
792
793 public boolean isIdRefType() {
794 return fingerprint == StandardNames.XS_IDREF;
795 }
796
797 /**
798 * Returns true if this type is derived by list, or if it is derived by restriction
799 * from a list type, or if it is a union that contains a list as one of its members
800 *
801 * @return true if this is a list type
802 */
803
804 public boolean isListType() {
805 return false;
806 }
807
808 /**
809 * Return true if this type is a union type (that is, if its variety is union)
810 *
811 * @return true for a union type
812 */
813
814 public boolean isUnionType() {
815 return false;
816 }
817
818 /**
819 * Determine the whitespace normalization required for values of this type
820 *
821 * @return one of PRESERVE, REPLACE, COLLAPSE
822 */
823
824 public int getWhitespaceAction() {
825 switch (getFingerprint()) {
826 case StandardNames.XS_STRING:
827 return Whitespace.PRESERVE;
828 case StandardNames.XS_NORMALIZED_STRING:
829 return Whitespace.REPLACE;
830 default:
831 return Whitespace.COLLAPSE;
832 }
833 }
834
835 /**
836 * Returns the built-in base type this type is derived from.
837 *
838 * @return the first built-in type found when searching up the type hierarchy
839 */
840 /*@Nullable*/
841 public SchemaType getBuiltInBaseType() {
842 BuiltInAtomicType base = this;
843 while ((base != null) && (base.getFingerprint() > 1023)) {
844 base = (BuiltInAtomicType) base.getBaseType();
845 }
846 return base;
847 }
848
849 /**
850 * Test whether this simple type is namespace-sensitive, that is, whether
851 * it is derived from xs:QName or xs:NOTATION. Note that
852 * the result for xs:anyAtomicType is false, even though an instance might be a QName.
853 *
854 * @return true if this type is derived from xs:QName or xs:NOTATION
855 */
856
857 public boolean isNamespaceSensitive() {
858 BuiltInAtomicType base = this;
859 int fp = base.getFingerprint();
860 while (fp > 1023) {
861 base = (BuiltInAtomicType) base.getBaseType();
862 assert base != null;
863 fp = base.getFingerprint();
864 }
865
866 return fp == StandardNames.XS_QNAME || fp == StandardNames.XS_NOTATION;
867 }
868
869
870
871 /**
872 * Check whether a given input string is valid according to this SimpleType
873 *
874 * @param value the input string to be checked
875 * @param nsResolver a namespace resolver used to resolve namespace prefixes if the type
876 * is namespace sensitive. The value supplied may be null; in this case any namespace-sensitive
877 * content will throw an UnsupportedOperationException.
878 * @param rules conversion rules e.g for namespace-sensitive content
879 * @return XPathException if the value is invalid. Note that the exception is returned rather than being thrown.
880 * Returns null if the value is valid.
881 * @throws UnsupportedOperationException if the type is namespace-sensitive and no namespace
882 * resolver is supplied
883 */
884
885 /*@Nullable*/
886 public ValidationFailure validateContent(CharSequence value, /*@Nullable*/ NamespaceResolver nsResolver,
887 ConversionRules rules) {
888 int f = getFingerprint();
889 if (f == StandardNames.XS_STRING ||
890 f == StandardNames.XS_ANY_SIMPLE_TYPE ||
891 f == StandardNames.XS_UNTYPED_ATOMIC ||
892 f == StandardNames.XS_ANY_ATOMIC_TYPE) {
893 return null;
894 }
895 StringConverter converter = stringConverter;
896 if (converter == null) {
897 converter = getStringConverter(rules);
898 if (isNamespaceSensitive()) {
899 if (nsResolver == null) {
900 throw new UnsupportedOperationException("Cannot validate a QName without a namespace resolver");
901 }
902 converter = (StringConverter) converter.setNamespaceResolver(nsResolver);
903 ConversionResult result = converter.convertString(value);
904 if (result instanceof ValidationFailure) {
905 return (ValidationFailure) result;
906 }
907 if (fingerprint == StandardNames.XS_NOTATION) {
908 NotationValue nv = (NotationValue) result;
909 // This check added in 9.3. The XSLT spec says that this check should not be performed during
910 // validation. However, this appears to be based on an incorrect assumption: see spec bug 6952
911 if (!rules.isDeclaredNotation(nv.getNamespaceURI(), nv.getLocalName())) {
912 return new ValidationFailure("Notation {" + nv.getNamespaceURI() + "}" +
913 nv.getLocalName() + " is not declared in the schema");
914 }
915 }
916 return null;
917 }
918 }
919 return converter.validate(value);
920 }
921
922 /**
923 * Get a StringConverter, an object that converts strings in the lexical space of this
924 * data type to instances (in the value space) of the data type.
925 * @return a StringConverter to do the conversion. Note that in the case of namespace-sensitive
926 * types, the resulting converter needs to be supplied with a NamespaceResolver to handle prefix
927 * resolution.
928 */
929
930
931 public StringConverter getStringConverter(ConversionRules rules) {
932 if (stringConverter != null) {
933 return stringConverter;
934 }
935 switch (fingerprint) {
936 case StandardNames.XS_DOUBLE:
937 case StandardNames.XS_NUMERIC:
938 return rules.getStringToDoubleConverter();
939 case StandardNames.XS_FLOAT:
940 return new StringConverter.StringToFloat(rules);
941 case StandardNames.XS_DATE_TIME:
942 return new StringConverter.StringToDateTime(rules);
943 case StandardNames.XS_DATE_TIME_STAMP:
944 return new StringConverter.StringToDateTimeStamp(rules);
945 case StandardNames.XS_DATE:
946 return new StringConverter.StringToDate(rules);
947 case StandardNames.XS_G_YEAR:
948 return new StringConverter.StringToGYear(rules);
949 case StandardNames.XS_G_YEAR_MONTH:
950 return new StringConverter.StringToGYearMonth(rules);
951 case StandardNames.XS_ANY_URI:
952 return new StringConverter.StringToAnyURI(rules);
953 case StandardNames.XS_QNAME:
954 return new StringConverter.StringToQName(rules);
955 case StandardNames.XS_NOTATION:
956 return new StringConverter.StringToNotation(rules);
957 default:
958 throw new AssertionError("No string converter available for " + this);
959 }
960 }
961
962 /**
963 * Get the typed value of a node that is annotated with this schema type.
964 *
965 * @param node the node whose typed value is required
966 * @return the typed value.
967 * @since 8.5
968 */
969
970 public AtomicSequence atomize(NodeInfo node) throws XPathException {
971 // Fast path for common cases
972 CharSequence stringValue = node.getStringValueCS();
973 if (stringValue.length() == 0 && node.isNilled()) {
974 return AtomicArray.EMPTY_ATOMIC_ARRAY;
975 }
976 if (fingerprint == StandardNames.XS_STRING) {
977 return StringValue.makeStringValue(stringValue);
978 } else if (fingerprint == StandardNames.XS_UNTYPED_ATOMIC) {
979 return new UntypedAtomicValue(stringValue);
980 }
981 StringConverter converter = stringConverter;
982 if (converter == null) {
983 converter = getStringConverter(node.getConfiguration().getConversionRules());
984 if (isNamespaceSensitive()) {
985 converter = (StringConverter) converter.setNamespaceResolver(new InscopeNamespaceResolver(node));
986 }
987 }
988 return converter.convertString(stringValue).asAtomic();
989 }
990
991 /**
992 * Get the typed value corresponding to a given string value, assuming it is
993 * valid against this type (and that the containing node is not nilled)
994 *
995 * @param value the string value
996 * @param resolver a namespace resolver used to resolve any namespace prefixes appearing
997 * in the content of values. Can supply null, in which case any namespace-sensitive content
998 * will be rejected.
999 * @param rules the conversion rules to be used
1000 * @return an iterator over the atomic sequence comprising the typed value. The objects
1001 * returned by this SequenceIterator will all be of type {@link AtomicValue}
1002 * @throws ValidationException This method should be called only if it is known that the value is
1003 * valid. If the value is not valid, there is no guarantee that this method will perform validation,
1004 * but if it does detect a validity error, then it MAY throw a ValidationException.
1005 */
1006
1007 /*@NotNull*/
1008 public AtomicSequence getTypedValue(CharSequence value, NamespaceResolver resolver, ConversionRules rules)
1009 throws ValidationException {
1010 // Fast path for common cases
1011 if (fingerprint == StandardNames.XS_STRING) {
1012 return StringValue.makeStringValue(value);
1013 } else if (fingerprint == StandardNames.XS_UNTYPED_ATOMIC) {
1014 return new UntypedAtomicValue(value);
1015 }
1016 StringConverter converter = getStringConverter(rules);
1017 if (isNamespaceSensitive()) {
1018 converter = (StringConverter) converter.setNamespaceResolver(resolver);
1019 }
1020 return converter.convertString(value).asAtomic();
1021 }
1022
1023 /**
1024 * Two types are equal if they have the same fingerprint.
1025 * Note: it is normally safe to use ==, because we always use the static constants, one instance
1026 * for each built in atomic type. However, after serialization and deserialization a different instance
1027 * can appear.
1028 */
1029
1030 public boolean equals(Object obj) {
1031 return obj instanceof BuiltInAtomicType &&
1032 getFingerprint() == ((BuiltInAtomicType) obj).getFingerprint();
1033 }
1034
1035 /**
1036 * The fingerprint can be used as a hashcode
1037 */
1038
1039 public int hashCode() {
1040 return getFingerprint();
1041 }
1042
1043
1044 /**
1045 * Validate that a primitive atomic value is a valid instance of a type derived from the
1046 * same primitive type.
1047 *
1048 * @param primValue the value in the value space of the primitive type.
1049 * @param lexicalValue the value in the lexical space. If null, the string value of primValue
1050 * is used. This value is checked against the pattern facet (if any)
1051 * @param rules the conversion rules to be used
1052 * @return null if the value is valid; otherwise, a ValidationFailure object indicating
1053 * the nature of the error.
1054 * @throws UnsupportedOperationException in the case of an external object type
1055 */
1056
1057 /*@Nullable*/
1058 public ValidationFailure validate(AtomicValue primValue, CharSequence lexicalValue, ConversionRules rules) {
1059 switch (fingerprint) {
1060 case StandardNames.XS_NUMERIC:
1061 case StandardNames.XS_STRING:
1062 case StandardNames.XS_BOOLEAN:
1063 case StandardNames.XS_DURATION:
1064 case StandardNames.XS_DATE_TIME:
1065 case StandardNames.XS_DATE:
1066 case StandardNames.XS_TIME:
1067 case StandardNames.XS_G_YEAR_MONTH:
1068 case StandardNames.XS_G_MONTH:
1069 case StandardNames.XS_G_MONTH_DAY:
1070 case StandardNames.XS_G_YEAR:
1071 case StandardNames.XS_G_DAY:
1072 case StandardNames.XS_HEX_BINARY:
1073 case StandardNames.XS_BASE64_BINARY:
1074 case StandardNames.XS_ANY_URI:
1075 case StandardNames.XS_QNAME:
1076 case StandardNames.XS_NOTATION:
1077 case StandardNames.XS_UNTYPED_ATOMIC:
1078 case StandardNames.XS_DECIMAL:
1079 case StandardNames.XS_FLOAT:
1080 case StandardNames.XS_DOUBLE:
1081 case StandardNames.XS_INTEGER:
1082 return null;
1083 case StandardNames.XS_NON_POSITIVE_INTEGER:
1084 case StandardNames.XS_NEGATIVE_INTEGER:
1085 case StandardNames.XS_LONG:
1086 case StandardNames.XS_INT:
1087 case StandardNames.XS_SHORT:
1088 case StandardNames.XS_BYTE:
1089 case StandardNames.XS_NON_NEGATIVE_INTEGER:
1090 case StandardNames.XS_POSITIVE_INTEGER:
1091 case StandardNames.XS_UNSIGNED_LONG:
1092 case StandardNames.XS_UNSIGNED_INT:
1093 case StandardNames.XS_UNSIGNED_SHORT:
1094 case StandardNames.XS_UNSIGNED_BYTE:
1095 return ((IntegerValue) primValue).validateAgainstSubType(this);
1096 case StandardNames.XS_YEAR_MONTH_DURATION:
1097 case StandardNames.XS_DAY_TIME_DURATION:
1098 return null; // treated as primitive
1099 case StandardNames.XS_DATE_TIME_STAMP:
1100 return ((CalendarValue) primValue).getTimezoneInMinutes() == CalendarValue.NO_TIMEZONE
1101 ? new ValidationFailure("xs:dateTimeStamp value must have a timezone") : null;
1102 case StandardNames.XS_NORMALIZED_STRING:
1103 case StandardNames.XS_TOKEN:
1104 case StandardNames.XS_LANGUAGE:
1105 case StandardNames.XS_NAME:
1106 case StandardNames.XS_NMTOKEN:
1107 case StandardNames.XS_NCNAME:
1108 case StandardNames.XS_ID:
1109 case StandardNames.XS_IDREF:
1110 case StandardNames.XS_ENTITY:
1111 return stringConverter.validate(primValue.getStringValueCS());
1112 default:
1113 throw new IllegalArgumentException();
1114 }
1115 }
1116
1117 /**
1118 * Analyze an expression to see whether the expression is capable of delivering a value of this
1119 * type.
1120 *
1121 * @param expression the expression that delivers the content
1122 * @param kind the node kind whose content is being delivered: {@link Type#ELEMENT},
1123 * {@link Type#ATTRIBUTE}, or {@link Type#DOCUMENT}
1124 * @throws net.sf.saxon.trans.XPathException
1125 * if the expression will never deliver a value of the correct type
1126 */
1127
1128 public void analyzeContentExpression(Expression expression, int kind) throws XPathException {
1129 analyzeContentExpression(this, expression, kind);
1130 }
1131
1132 /**
1133 * Analyze an expression to see whether the expression is capable of delivering a value of this
1134 * type.
1135 *
1136 * @param simpleType the simple type against which the expression is to be checked
1137 * @param expression the expression that delivers the content
1138 * @param kind the node kind whose content is being delivered: {@link Type#ELEMENT},
1139 * {@link Type#ATTRIBUTE}, or {@link Type#DOCUMENT}
1140 * @throws net.sf.saxon.trans.XPathException
1141 * if the expression will never deliver a value of the correct type
1142 */
1143
1144 public static void analyzeContentExpression(SimpleType simpleType, Expression expression, int kind)
1145 throws XPathException {
1146 if (kind == Type.ELEMENT) {
1147 expression.checkPermittedContents(simpleType, true);
1148 // // if we are building the content of an element or document, no atomization will take
1149 // // place, and therefore the presence of any element or attribute nodes in the content will
1150 // // cause a validity error, since only simple content is allowed
1151 // if (Type.isSubType(itemType, NodeKindTest.makeNodeKindTest(Type.ELEMENT))) {
1152 // throw new XPathException("The content of an element with a simple type must not include any element nodes");
1153 // }
1154 // if (Type.isSubType(itemType, NodeKindTest.makeNodeKindTest(Type.ATTRIBUTE))) {
1155 // throw new XPathException("The content of an element with a simple type must not include any attribute nodes");
1156 // }
1157 } else if (kind == Type.ATTRIBUTE) {
1158 // for attributes, do a check only for text nodes and atomic values: anything else gets atomized
1159 if (expression instanceof ValueOf || expression instanceof Literal) {
1160 expression.checkPermittedContents(simpleType, true);
1161 }
1162 }
1163 }
1164
1165 /**
1166 * Internal factory method to create a BuiltInAtomicType. There is one instance for each of the
1167 * built-in atomic types
1168 *
1169 * @param fingerprint The name of the type
1170 * @param baseType The base type from which this type is derived
1171 * @param code Alphabetic code chosen to enable ordering of types according to the type hierarchy
1172 * @param ordered true if the type is ordered
1173 * @return the newly constructed built in atomic type
1174 */
1175 /*@NotNull*/
1176 private static BuiltInAtomicType makeAtomicType(int fingerprint, SimpleType baseType, String code, boolean ordered) {
1177 BuiltInAtomicType t = new BuiltInAtomicType(fingerprint);
1178 t.setBaseTypeFingerprint(baseType.getFingerprint());
1179 if (t.isPrimitiveType()) {
1180 t.primitiveFingerprint = fingerprint;
1181 } else {
1182 t.primitiveFingerprint = ((AtomicType) baseType).getPrimitiveType();
1183 }
1184 t.uType = UType.fromTypeCode(t.primitiveFingerprint);
1185 t.ordered = ordered;
1186 t.alphaCode = code;
1187 BuiltInType.register(fingerprint, t);
1188 lookupByAlphaCode.put(code, t);
1189 return t;
1190 }
1191
1192 public static BuiltInAtomicType getTypeWithAlphaCode(String alphaCode) {
1193 return lookupByAlphaCode.get(alphaCode);
1194 }
1195
1196 /**
1197 * Apply any pre-lexical facets, other than whitespace. At the moment the only such
1198 * facet is saxon:preprocess
1199 *
1200 * @param input the value to be preprocessed
1201 * @return the value after preprocessing
1202 */
1203
1204 public CharSequence preprocess(CharSequence input) {
1205 return input;
1206 }
1207
1208 /**
1209 * Reverse any pre-lexical facets, other than whitespace. At the moment the only such
1210 * facet is saxon:preprocess. This is called when converting a value of this type to
1211 * a string
1212 *
1213 * @param input the value to be postprocessed: this is the "ordinary" result of converting
1214 * the value to a string
1215 * @return the value after postprocessing
1216 */
1217
1218 public CharSequence postprocess(CharSequence input) {
1219 return input;
1220 }
1221
1222 /**
1223 * Get the list of plain types that are subsumed by this type
1224 *
1225 * @return for an atomic type, the type itself; for a plain union type, the list of plain types
1226 * in its transitive membership, in declaration order
1227 */
1228 /*@NotNull*/
1229 public Set<? extends PlainType> getPlainMemberTypes() {
1230 return Collections.singleton(this);
1231 }
1232
1233 /**
1234 * Ask whether a built-in type is a numeric type (integer, float, double)
1235 * @return true if the type is numeric
1236 */
1237
1238 public boolean isNumericType() {
1239 ItemType p = getPrimitiveItemType();
1240 return p == NumericType.getInstance() || p == DECIMAL ||
1241 p == DOUBLE || p == FLOAT ||
1242 p == INTEGER;
1243 }
1244
1245 /**
1246 * Generate Javascript code to test whether an item conforms to this item type
1247 * @param knownToBe a type that the item is known to conform to
1248 * @param targetVersion 1 or 2 depending on the version of Saxon-JS we are targeting
1249 * @return a Javascript instruction or sequence of instructions, which can be used as the body
1250 * of a Javascript function, and which returns a boolean indication whether the value of the
1251 * variable "item" is an instance of this item type.
1252 *
1253 */
1254 @Override
1255 public String generateJavaScriptItemTypeTest(ItemType knownToBe, int targetVersion) {
1256 if (this == NOTATION) {
1257 return "return false;";
1258 }
1259 if (targetVersion == 1) {
1260 return "return SaxonJS.U.Atomic." + getName() + ".matches(item);";
1261 } else if (targetVersion == 2) {
1262 return "return SaxonJS.U.hasAtomicType(item, '" + getName() + "');";
1263 } else {
1264 throw new IllegalArgumentException();
1265 }
1266 }
1267
1268 /**
1269 * Generate Javascript code to convert a supplied Javascript value to this item type,
1270 * if conversion is possible, or throw an error otherwise.
1271 *
1272 * @param errorCode the error to be thrown if conversion is not possible
1273 * @param targetVersion 1 or 2 depending on the version of Saxon-JS we are targeting
1274 * @return a Javascript instruction or sequence of instructions, which can be used as the body
1275 * of a Javascript function, and which returns the result of conversion to this type, or throws
1276 * an error if conversion is not possible. The variable "val" will hold the supplied Javascript
1277 * value.
1278 */
1279 public String generateJavaScriptItemTypeAcceptor(String errorCode, int targetVersion) {
1280 if (targetVersion == 1) {
1281 return "return SaxonJS.U.Atomic." + getName() + ".cast(val);";
1282 } else if (targetVersion == 2) {
1283 return "return SaxonJS.U.cast(val, '" + getName() + "');";
1284 } else {
1285 throw new IllegalArgumentException(targetVersion+"");
1286 }
1287 }
1288
1289 //#ifdefined SCHEMA
1290 public Function getComponentAsFunction() {
1291 return UserSimpleType.getComponentAsFunction(this);
1292 }
1293 //#endif
1294
1295 }
1296