| 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 | |
| 11 | import net.sf.saxon.lib.ConversionRules; |
| 12 | import net.sf.saxon.om.Item; |
| 13 | import net.sf.saxon.tree.util.FastStringBuffer; |
| 14 | import net.sf.saxon.value.AtomicValue; |
| 15 | import net.sf.saxon.value.SequenceType; |
| 16 | |
| 17 | import java.util.Optional; |
| 18 | |
| 19 | import static net.sf.saxon.om.Genre.ATOMIC; |
| 20 | |
| 21 | /** |
| 22 | * Interface representing a union type. This may be either a built-in union type (of which there are |
| 23 | * currently two, namely ErrorType and NumericType), or a user-defined union type. |
| 24 | */ |
| 25 | |
| 26 | public interface UnionType extends SimpleType, ItemType { |
| 27 | |
| 28 | /** |
| 29 | * Ask whether the union contains a list type among its member types |
| 30 | * |
| 31 | * @return true of one of the member types is a list type |
| 32 | */ |
| 33 | |
| 34 | boolean containsListType() throws MissingComponentException; |
| 35 | |
| 36 | /** |
| 37 | * Get the "plain" types in the transitive membership. Plain types are atomic types and union types that |
| 38 | * are defined directly in terms of other plain types, without adding any restriction facets. |
| 39 | * |
| 40 | * @return the atomic types and plain union types in the transitive membership of the union type. |
| 41 | */ |
| 42 | |
| 43 | Iterable<PlainType> getPlainMemberTypes() throws MissingComponentException; |
| 44 | |
| 45 | /** |
| 46 | * Get the result type of a cast operation to this union type, as a sequence type. |
| 47 | * |
| 48 | * @return the result type of casting, as precisely as possible. For example, if all the member types of |
| 49 | * the union are derived from the same primitive type, this will return that primitive type. |
| 50 | */ |
| 51 | |
| 52 | SequenceType getResultTypeOfCast(); |
| 53 | |
| 54 | /** |
| 55 | * Validate an atomic value, which is known to be an instance of one of the member types of the |
| 56 | * union, against any facets (pattern facets or enumeration facets) defined at the level of the |
| 57 | * union itself. |
| 58 | * @param value the Atomic Value to be checked. This must be an instance of a member type of the |
| 59 | * union |
| 60 | * @param rules the ConversionRules for the Configuration |
| 61 | * @return a ValidationFailure if the value is not valid; null if it is valid. |
| 62 | */ |
| 63 | |
| 64 | ValidationFailure checkAgainstFacets(AtomicValue value, ConversionRules rules); |
| 65 | |
| 66 | /** |
| 67 | * Get extra diagnostic information about why a supplied item does not conform to this |
| 68 | * item type, if available. If extra information is returned, it should be in the form of a complete |
| 69 | * sentence, minus the closing full stop. No information should be returned for obvious cases. |
| 70 | * |
| 71 | * @param item the item that doesn't match this type |
| 72 | * @param th the type hierarchy cache |
| 73 | * @return optionally, a message explaining why the item does not match the type |
| 74 | */ |
| 75 | @Override |
| 76 | default Optional<String> explainMismatch(Item item, TypeHierarchy th) { |
| 77 | if (item.getGenre() == ATOMIC) { |
| 78 | FastStringBuffer message = new FastStringBuffer(256); |
| 79 | message.append("The required type is a union type allowing any of "); |
| 80 | |
| 81 | String punctuation = "("; |
| 82 | try { |
| 83 | for (PlainType member: getPlainMemberTypes()) { |
| 84 | message.append(punctuation); |
| 85 | punctuation = ", "; |
| 86 | message.append(member.getTypeName().getDisplayName()); |
| 87 | } |
| 88 | } catch (MissingComponentException e) { |
| 89 | message.append("*member types unobtainable*"); |
| 90 | } |
| 91 | message.append("), but the supplied type "); |
| 92 | message.append(((AtomicValue)item).getItemType().getDisplayName()); |
| 93 | message.append(" is not any of these"); |
| 94 | return Optional.of(message.toString()); |
| 95 | } else { |
| 96 | return Optional.empty(); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 |