| 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.dom; |
| 9 | |
| 10 | import net.sf.saxon.Configuration; |
| 11 | import net.sf.saxon.type.AnyType; |
| 12 | import net.sf.saxon.type.SchemaType; |
| 13 | import org.w3c.dom.TypeInfo; |
| 14 | |
| 15 | /** |
| 16 | * This class implements the DOM TypeInfo interface as a wrapper over the Saxon SchemaType |
| 17 | * interface. |
| 18 | */ |
| 19 | |
| 20 | public class TypeInfoImpl implements TypeInfo { |
| 21 | |
| 22 | private Configuration config; |
| 23 | private SchemaType schemaType; |
| 24 | |
| 25 | /** |
| 26 | * Construct a TypeInfo based on a SchemaType |
| 27 | */ |
| 28 | |
| 29 | public TypeInfoImpl(Configuration config, SchemaType type) { |
| 30 | this.config = config; |
| 31 | this.schemaType = type; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Get the local name of the type (a system-allocated name if anonymous). Needed to implement the |
| 36 | * DOM level 3 TypeInfo interface. |
| 37 | */ |
| 38 | |
| 39 | public String getTypeName() { |
| 40 | return schemaType.getStructuredQName().getLocalPart(); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Get the namespace name of the type (a system-allocated name if anonymous). Needed to implement the |
| 45 | * DOM level 3 TypeInfo interface. |
| 46 | */ |
| 47 | |
| 48 | public String getTypeNamespace() { |
| 49 | return schemaType.getStructuredQName().getURI(); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * This method returns true if there is a derivation between the reference type definition, that is the TypeInfo |
| 54 | * on which the method is being called, and the other type definition, that is the one passed as parameters. |
| 55 | * This method implements the DOM Level 3 TypeInfo interface. It must be called only on a valid type. |
| 56 | * |
| 57 | * @param typeNamespaceArg the namespace of the "other" type |
| 58 | * @param typeNameArg the local name of the "other" type |
| 59 | * @param derivationMethod the derivation method: zero or more of {@link SchemaType#DERIVATION_RESTRICTION}, |
| 60 | * {@link SchemaType#DERIVATION_EXTENSION}, {@link SchemaType#DERIVATION_LIST}, or {@link SchemaType#DERIVATION_UNION}. |
| 61 | * Zero means derived by any possible route. |
| 62 | */ |
| 63 | |
| 64 | public boolean isDerivedFrom(String typeNamespaceArg, |
| 65 | String typeNameArg, |
| 66 | int derivationMethod) throws IllegalStateException { |
| 67 | SchemaType base = schemaType.getBaseType(); |
| 68 | int fingerprint = config.getNamePool().allocateFingerprint(typeNamespaceArg, typeNameArg); |
| 69 | if (derivationMethod == 0 || (derivationMethod & schemaType.getDerivationMethod()) != 0) { |
| 70 | if (base.getFingerprint() == fingerprint) { |
| 71 | return true; |
| 72 | } else if (base instanceof AnyType) { |
| 73 | return false; |
| 74 | } else { |
| 75 | return new TypeInfoImpl(config, base).isDerivedFrom(typeNamespaceArg, typeNameArg, derivationMethod); |
| 76 | } |
| 77 | } |
| 78 | return false; |
| 79 | // Note: if derivationMethod is RESTRICTION, this interpretation requires every step to be derived |
| 80 | // by restriction. An alternative interpretation is that at least one step must be derived by restriction. |
| 81 | } |
| 82 | |
| 83 | } |
| 84 |