| 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.lib.ConversionRules; |
| 11 | import net.sf.saxon.om.Genre; |
| 12 | import net.sf.saxon.om.Item; |
| 13 | import net.sf.saxon.om.StructuredQName; |
| 14 | import net.sf.saxon.value.AtomicValue; |
| 15 | |
| 16 | import java.util.Optional; |
| 17 | |
| 18 | /** |
| 19 | * Interface for atomic types (these are either built-in atomic types |
| 20 | * or user-defined atomic types). An AtomicType is both an ItemType (a possible type |
| 21 | * for items in a sequence) and a SchemaType (a possible type for validating and |
| 22 | * annotating nodes). |
| 23 | */ |
| 24 | public interface AtomicType extends SimpleType, PlainType { |
| 25 | |
| 26 | /** |
| 27 | * Determine the Genre (top-level classification) of this type |
| 28 | * |
| 29 | * @return the Genre to which this type belongs, specifically {@link Genre#ATOMIC} |
| 30 | */ |
| 31 | @Override |
| 32 | default Genre getGenre() { |
| 33 | return Genre.ATOMIC; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Validate that a primitive atomic value is a valid instance of a type derived from the |
| 38 | * same primitive type. |
| 39 | * |
| 40 | * @param primValue the value in the value space of the primitive type. |
| 41 | * @param lexicalValue the value in the lexical space. If null, the string value of primValue |
| 42 | * is used. This value is checked against the pattern facet (if any) |
| 43 | * @param rules the conversion rules for this configuration |
| 44 | * @return null if the value is valid; otherwise, a ValidationFailure object indicating |
| 45 | * the nature of the error. |
| 46 | * @throws UnsupportedOperationException in the case of an external object type |
| 47 | */ |
| 48 | |
| 49 | ValidationFailure validate(AtomicValue primValue, CharSequence lexicalValue, ConversionRules rules); |
| 50 | |
| 51 | /** |
| 52 | * Determine whether the atomic type is ordered, that is, whether less-than and greater-than comparisons |
| 53 | * are permitted |
| 54 | * |
| 55 | * @param optimistic if true, the function takes an optimistic view, returning true if ordering comparisons |
| 56 | * are available for some subtype. This mainly affects xs:duration, where the function returns true if |
| 57 | * optimistic is true, false if it is false. |
| 58 | * @return true if ordering operations are permitted |
| 59 | */ |
| 60 | |
| 61 | boolean isOrdered(boolean optimistic); |
| 62 | |
| 63 | /** |
| 64 | * Determine whether the type is abstract, that is, whether it cannot have instances that are not also |
| 65 | * instances of some concrete subtype |
| 66 | * @return true if the type is abstract |
| 67 | */ |
| 68 | |
| 69 | boolean isAbstract(); |
| 70 | |
| 71 | /** |
| 72 | * Determine whether the atomic type is a primitive type. The primitive types are |
| 73 | * the 19 primitive types of XML Schema, plus xs:integer, xs:dayTimeDuration and xs:yearMonthDuration; |
| 74 | * xs:untypedAtomic; and all supertypes of these (xs:anyAtomicType, xs:numeric, ...) |
| 75 | * |
| 76 | * @return true if the type is considered primitive under the above rules |
| 77 | */ |
| 78 | |
| 79 | boolean isPrimitiveType(); |
| 80 | |
| 81 | /** |
| 82 | * Determine whether the atomic type is a built-in type. The built-in atomic types are the 41 atomic types |
| 83 | * defined in XML Schema, plus xs:dayTimeDuration and xs:yearMonthDuration, |
| 84 | * xs:untypedAtomic, and all supertypes of these (xs:anyAtomicType, xs:numeric, ...) |
| 85 | */ |
| 86 | |
| 87 | boolean isBuiltInType(); |
| 88 | |
| 89 | /** |
| 90 | * Get the name of this type as a StructuredQName, unless the type is anonymous, in which case |
| 91 | * return null |
| 92 | * |
| 93 | * @return the name of the atomic type, or null if the type is anonymous. |
| 94 | */ |
| 95 | |
| 96 | StructuredQName getTypeName(); |
| 97 | |
| 98 | |
| 99 | /** |
| 100 | * Get a StringConverter, an object which converts strings in the lexical space of this |
| 101 | * data type to instances (in the value space) of the data type. |
| 102 | * @param rules the conversion rules to be used |
| 103 | * @return a StringConverter to do the conversion, or null if no built-in converter is available. |
| 104 | */ |
| 105 | |
| 106 | StringConverter getStringConverter(ConversionRules rules); |
| 107 | |
| 108 | /** |
| 109 | * Get extra diagnostic information about why a supplied item does not conform to this |
| 110 | * item type, if available. If extra information is returned, it should be in the form of a complete |
| 111 | * sentence, minus the closing full stop. No information should be returned for obvious cases. |
| 112 | * |
| 113 | * @param item the item that doesn't match this type |
| 114 | * @param th the type hierarchy cache |
| 115 | * @return optionally, a message explaining why the item does not match the type |
| 116 | */ |
| 117 | @Override |
| 118 | default Optional<String> explainMismatch(Item item, TypeHierarchy th) { |
| 119 | if (item instanceof AtomicValue) { |
| 120 | return Optional.of("The supplied value is of type " + ((AtomicValue) item).getItemType()); |
| 121 | } else { |
| 122 | return Optional.of("The supplied value is " + item.getGenre().getDescription()); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 |