| 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.s9api; |
| 9 | |
| 10 | import net.sf.saxon.lib.ConversionRules; |
| 11 | import net.sf.saxon.om.Item; |
| 12 | import net.sf.saxon.trans.XPathException; |
| 13 | import net.sf.saxon.type.TypeHierarchy; |
| 14 | |
| 15 | /** |
| 16 | * An item type constructed by the {@link ItemTypeFactory} (as distinct from one that is predefined) |
| 17 | * <p>This class is not user-visible.</p> |
| 18 | */ |
| 19 | |
| 20 | class ConstructedItemType extends ItemType { |
| 21 | |
| 22 | private net.sf.saxon.type.ItemType underlyingType; |
| 23 | private Processor processor; |
| 24 | |
| 25 | /** |
| 26 | * Protected constructor |
| 27 | * |
| 28 | * @param underlyingType the Saxon internal item type. Must not be null. |
| 29 | * @param processor The s9api processor Must not be null |
| 30 | */ |
| 31 | |
| 32 | protected ConstructedItemType(/*@Nullable*/ net.sf.saxon.type.ItemType underlyingType, Processor processor) { |
| 33 | if (processor == null) { |
| 34 | throw new NullPointerException("processor"); |
| 35 | } |
| 36 | if (underlyingType == null) { |
| 37 | throw new NullPointerException("underlyingType"); |
| 38 | } |
| 39 | this.processor = processor; |
| 40 | this.underlyingType = underlyingType; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Get the conversion rules implemented by this type. The conversion rules reflect variations |
| 45 | * between different versions of the W3C specifications, for example XSD 1.1 allows "+INF" as |
| 46 | * a lexical representation of xs:double, while XSD 1.0 does not. |
| 47 | * |
| 48 | * @return the conversion rules |
| 49 | */ |
| 50 | |
| 51 | public ConversionRules getConversionRules() { |
| 52 | return processor.getUnderlyingConfiguration().getConversionRules(); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Determine whether this item type matches a given item. |
| 57 | * |
| 58 | * @param item the item to be tested against this item type |
| 59 | * @return true if the item matches this item type, false if it does not match. |
| 60 | * @throws net.sf.saxon.s9api.SaxonApiUncheckedException in the rare event that the item is a map or |
| 61 | * array containing a lazily evaluated sequence, and evaluation of the sequence fails with a dynamic |
| 62 | * error in the course of checking |
| 63 | */ |
| 64 | |
| 65 | public boolean matches(XdmItem item) throws SaxonApiUncheckedException { |
| 66 | try { |
| 67 | TypeHierarchy th = processor.getUnderlyingConfiguration().getTypeHierarchy(); |
| 68 | return underlyingType.matches((Item) item.getUnderlyingValue(), th); |
| 69 | } catch (XPathException e) { |
| 70 | throw new SaxonApiUncheckedException(e); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Determine whether this ItemType subsumes another ItemType. Specifically, |
| 76 | * <code>A.subsumes(B) is true if every value that matches the ItemType B also matches |
| 77 | * the ItemType A. |
| 78 | * |
| 79 | * @param other the other ItemType |
| 80 | * @return true if this ItemType subsumes the other ItemType. This includes the case where A and B |
| 81 | * represent the same ItemType. |
| 82 | * @since 9.1 |
| 83 | */ |
| 84 | |
| 85 | public boolean subsumes(ItemType other) { |
| 86 | TypeHierarchy th = processor.getUnderlyingConfiguration().getTypeHierarchy(); |
| 87 | return th.isSubType(other.getUnderlyingItemType(), underlyingType); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Method to get the underlying Saxon implementation object |
| 92 | * <p>This gives access to Saxon methods that may change from one release to another.</p> |
| 93 | * |
| 94 | * @return the underlying Saxon implementation object |
| 95 | */ |
| 96 | |
| 97 | public net.sf.saxon.type.ItemType getUnderlyingItemType() { |
| 98 | return underlyingType; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Get the underlying Processor |
| 103 | * |
| 104 | * @return the processor used to create this ItemType. This will be null if the ItemType is one of the three |
| 105 | * static constant item types {@link #ANY_ITEM}, {@link #ANY_NODE}, or {@link #ANY_ATOMIC_VALUE} |
| 106 | */ |
| 107 | |
| 108 | protected Processor getProcessor() { |
| 109 | return processor; |
| 110 | } |
| 111 | } |
| 112 |