| 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.expr.Expression; |
| 11 | import net.sf.saxon.om.AtomicSequence; |
| 12 | import net.sf.saxon.om.NodeInfo; |
| 13 | import net.sf.saxon.om.StructuredQName; |
| 14 | import net.sf.saxon.trans.XPathException; |
| 15 | |
| 16 | /** |
| 17 | * SchemaType is an interface implemented by all schema types: simple and complex types, built-in and |
| 18 | * user-defined types. |
| 19 | * <p>There is a hierarchy of interfaces that extend SchemaType, representing the top levels of the schema |
| 20 | * type system: SimpleType and ComplexType, with SimpleType further subdivided into List, Union, and Atomic |
| 21 | * types.</p> |
| 22 | * <p>The implementations of these interfaces are organized into a different hierarchy: on the one side, |
| 23 | * built-in types such as AnyType, AnySimpleType, and the built-in atomic types and list types; on the other |
| 24 | * side, user-defined types defined in a schema.</p> |
| 25 | */ |
| 26 | |
| 27 | public interface SchemaType extends SchemaComponent { |
| 28 | |
| 29 | // DerivationMethods. These constants are copied from org.w3.dom.TypeInfo. They are redefined here to avoid |
| 30 | // creating a dependency on the TypeInfo class, which is only available when JAXP 1.3 is available. |
| 31 | |
| 32 | /** |
| 33 | * If the document's schema is an XML Schema [<a href='http://www.w3.org/TR/2001/REC-xmlschema-1-20010502/'>XML Schema Part 1</a>] |
| 34 | * , this constant represents the derivation by <a href='http://www.w3.org/TR/2001/REC-xmlschema-1-20010502/#key-typeRestriction'> |
| 35 | * restriction</a> if complex types are involved, or a <a href='http://www.w3.org/TR/2001/REC-xmlschema-1-20010502/#element-restriction'> |
| 36 | * restriction</a> if simple types are involved. |
| 37 | * <br> The reference type definition is derived by restriction from the |
| 38 | * other type definition if the other type definition is the same as the |
| 39 | * reference type definition, or if the other type definition can be |
| 40 | * reached recursively following the {base type definition} property |
| 41 | * from the reference type definition, and all the <em>derivation methods</em> involved are restriction. |
| 42 | */ |
| 43 | int DERIVATION_RESTRICTION = 0x00000001; |
| 44 | /** |
| 45 | * If the document's schema is an XML Schema [<a href='http://www.w3.org/TR/2001/REC-xmlschema-1-20010502/'>XML Schema Part 1</a>] |
| 46 | * , this constant represents the derivation by <a href='http://www.w3.org/TR/2001/REC-xmlschema-1-20010502/#key-typeExtension'> |
| 47 | * extension</a>. |
| 48 | * <br> The reference type definition is derived by extension from the |
| 49 | * other type definition if the other type definition can be reached |
| 50 | * recursively following the {base type definition} property from the |
| 51 | * reference type definition, and at least one of the <em>derivation methods</em> involved is an extension. |
| 52 | */ |
| 53 | int DERIVATION_EXTENSION = 0x00000002; |
| 54 | /** |
| 55 | * If the document's schema is an XML Schema [<a href='http://www.w3.org/TR/2001/REC-xmlschema-1-20010502/'>XML Schema Part 1</a>] |
| 56 | * , this constant represents the <a href='http://www.w3.org/TR/2001/REC-xmlschema-1-20010502/#element-union'> |
| 57 | * union</a> if simple types are involved. |
| 58 | * <br> The reference type definition is derived by union from the other |
| 59 | * type definition if there exists two type definitions T1 and T2 such |
| 60 | * as the reference type definition is derived from T1 by |
| 61 | * <code>DERIVATION_RESTRICTION</code> or |
| 62 | * <code>DERIVATION_EXTENSION</code>, T2 is derived from the other type |
| 63 | * definition by <code>DERIVATION_RESTRICTION</code>, T1 has {variety} <em>union</em>, and one of the {member type definitions} is T2. Note that T1 could be |
| 64 | * the same as the reference type definition, and T2 could be the same |
| 65 | * as the other type definition. |
| 66 | */ |
| 67 | int DERIVATION_UNION = 0x00000004; |
| 68 | /** |
| 69 | * If the document's schema is an XML Schema [<a href='http://www.w3.org/TR/2001/REC-xmlschema-1-20010502/'>XML Schema Part 1</a>] |
| 70 | * , this constant represents the <a href='http://www.w3.org/TR/2001/REC-xmlschema-1-20010502/#element-list'>list</a>. |
| 71 | * <br> The reference type definition is derived by list from the other |
| 72 | * type definition if there exists two type definitions T1 and T2 such |
| 73 | * as the reference type definition is derived from T1 by |
| 74 | * <code>DERIVATION_RESTRICTION</code> or |
| 75 | * <code>DERIVATION_EXTENSION</code>, T2 is derived from the other type |
| 76 | * definition by <code>DERIVATION_RESTRICTION</code>, T1 has {variety} <em>list</em>, and T2 is the {item type definition}. Note that T1 could be the same as |
| 77 | * the reference type definition, and T2 could be the same as the other |
| 78 | * type definition. |
| 79 | */ |
| 80 | int DERIVATION_LIST = 0x00000008; |
| 81 | |
| 82 | /** |
| 83 | * Derivation by substitution. |
| 84 | * This constant, unlike the others, is NOT defined in the DOM level 3 TypeInfo interface. |
| 85 | */ |
| 86 | |
| 87 | int DERIVE_BY_SUBSTITUTION = 16; |
| 88 | |
| 89 | /** |
| 90 | * Get the local name of this type |
| 91 | * |
| 92 | * @return the local name of this type definition, if it has one. Return null in the case of an |
| 93 | * anonymous type. |
| 94 | */ |
| 95 | |
| 96 | /*@Nullable*/ |
| 97 | String getName(); |
| 98 | |
| 99 | /** |
| 100 | * Get the target namespace of this type |
| 101 | * |
| 102 | * @return the target namespace of this type definition, if it has one. Return null in the case |
| 103 | * of an anonymous type, and in the case of a global type defined in a no-namespace schema. |
| 104 | */ |
| 105 | |
| 106 | /*@Nullable*/ |
| 107 | String getTargetNamespace(); |
| 108 | |
| 109 | /** |
| 110 | * Get the fingerprint of the name of this type |
| 111 | * |
| 112 | * @return the fingerprint. Returns an invented fingerprint for an anonymous type. |
| 113 | */ |
| 114 | |
| 115 | int getFingerprint(); |
| 116 | |
| 117 | /** |
| 118 | * Get the display name of the type: that is, a lexical QName with an arbitrary prefix |
| 119 | * |
| 120 | * @return a lexical QName identifying the type. In the case of an anonymous type, an internally-generated |
| 121 | * name is returned |
| 122 | */ |
| 123 | |
| 124 | String getDisplayName(); |
| 125 | |
| 126 | /** |
| 127 | * Get the name of the type as a StructuredQName |
| 128 | * @return a StructuredQName identifying the type. In the case of an anonymous type, an internally-generated |
| 129 | * name is returned |
| 130 | */ |
| 131 | |
| 132 | StructuredQName getStructuredQName(); |
| 133 | |
| 134 | /** |
| 135 | * Get the name of this type as an EQName, that is, a string in the format Q{uri}local. |
| 136 | * |
| 137 | * @return an EQName identifying the type. In the case of an anonymous type, an internally-generated |
| 138 | * name is returned |
| 139 | */ |
| 140 | |
| 141 | String getEQName(); |
| 142 | |
| 143 | /** |
| 144 | * Test whether this SchemaType is a complex type |
| 145 | * |
| 146 | * @return true if this SchemaType is a complex type |
| 147 | */ |
| 148 | |
| 149 | boolean isComplexType(); |
| 150 | |
| 151 | /** |
| 152 | * Test whether this SchemaType is a simple type |
| 153 | * |
| 154 | * @return true if this SchemaType is a simple type |
| 155 | */ |
| 156 | |
| 157 | boolean isSimpleType(); |
| 158 | |
| 159 | /** |
| 160 | * Test whether this SchemaType is an atomic type |
| 161 | * |
| 162 | * @return true if this SchemaType is an atomic type |
| 163 | */ |
| 164 | |
| 165 | boolean isAtomicType(); |
| 166 | |
| 167 | /** |
| 168 | * Test whether this is an anonymous type |
| 169 | * |
| 170 | * @return true if this SchemaType is an anonymous type |
| 171 | */ |
| 172 | |
| 173 | boolean isAnonymousType(); |
| 174 | |
| 175 | /** |
| 176 | * Returns the value of the 'block' attribute for this type, as a bit-significant |
| 177 | * integer with fields such as {@link SchemaType#DERIVATION_LIST} and {@link SchemaType#DERIVATION_EXTENSION}. |
| 178 | * This corresponds to the property "prohibited substitutions" in the schema component model. |
| 179 | * |
| 180 | * @return the value of the 'block' attribute for this type |
| 181 | */ |
| 182 | |
| 183 | int getBlock(); |
| 184 | |
| 185 | /** |
| 186 | * Returns the base type that this type inherits from. This method can be used to get the |
| 187 | * base type of a type that is known to be valid. |
| 188 | * If this type is a Simpletype that is a built in primitive type then null is returned. |
| 189 | * |
| 190 | * @return the base type, or null if this is xs:anyType (the root of the type hierarchy) |
| 191 | */ |
| 192 | |
| 193 | SchemaType getBaseType(); |
| 194 | |
| 195 | /** |
| 196 | * Get the nearest named type in the type hierarchy, that is, the nearest type that |
| 197 | * is not anonymous. (In practice, since types cannot be derived from anonymous types, |
| 198 | * this will either the type itself, or its immediate base type). |
| 199 | * @return the nearest type, found by following the {@code getBaseType()} relation |
| 200 | * recursively, that is not an anonymous type |
| 201 | */ |
| 202 | |
| 203 | default SchemaType getNearestNamedType() { |
| 204 | SchemaType type = this; |
| 205 | while (type.isAnonymousType()) { |
| 206 | type = type.getBaseType(); |
| 207 | } |
| 208 | return type; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Gets the integer code of the derivation method used to derive this type from its |
| 213 | * parent. Returns zero for primitive types. |
| 214 | * |
| 215 | * @return a numeric code representing the derivation method, for example {@link SchemaType#DERIVATION_RESTRICTION} |
| 216 | */ |
| 217 | |
| 218 | int getDerivationMethod(); |
| 219 | |
| 220 | /** |
| 221 | * Get the types of derivation that are not permitted, by virtue of the "final" property. |
| 222 | * |
| 223 | * @return the types of derivation that are not permitted, as a bit-significant integer |
| 224 | * containing bits such as {@link SchemaType#DERIVATION_EXTENSION} |
| 225 | */ |
| 226 | |
| 227 | int getFinalProhibitions(); |
| 228 | |
| 229 | /** |
| 230 | * Determines whether derivation (of a particular kind) |
| 231 | * from this type is allowed, based on the "final" property |
| 232 | * |
| 233 | * @param derivation the kind of derivation, for example {@link SchemaType#DERIVATION_LIST} |
| 234 | * @return true if this kind of derivation is allowed |
| 235 | */ |
| 236 | |
| 237 | boolean allowsDerivation(int derivation); |
| 238 | |
| 239 | /** |
| 240 | * Analyze an XPath expression to see whether the expression is capable of delivering a value of this |
| 241 | * type. This method is called during static analysis of a query or stylesheet to give compile-time |
| 242 | * warnings when "impossible" paths are used. |
| 243 | * |
| 244 | * @param expression the expression that delivers the content |
| 245 | * @param kind the node kind whose content is being delivered: {@link Type#ELEMENT}, |
| 246 | * {@link Type#ATTRIBUTE}, or {@link Type#DOCUMENT} |
| 247 | * @throws XPathException if the expression will never deliver a value of the correct type |
| 248 | */ |
| 249 | |
| 250 | void analyzeContentExpression(Expression expression, int kind) throws XPathException; |
| 251 | |
| 252 | /** |
| 253 | * Get the typed value of a node that is annotated with this schema type. |
| 254 | * |
| 255 | * @param node the node whose typed value is required |
| 256 | * @return the typed value. |
| 257 | * @throws net.sf.saxon.trans.XPathException |
| 258 | * if the node cannot be atomized, for example if this is a complex type |
| 259 | * with element-only content |
| 260 | * @since 8.5. Changed in 9.5 to return the new type AtomicSequence |
| 261 | */ |
| 262 | |
| 263 | AtomicSequence atomize(NodeInfo node) throws XPathException; |
| 264 | |
| 265 | /** |
| 266 | * Test whether this is the same type as another type. They are considered to be the same type |
| 267 | * if they are derived from the same type definition in the original XML representation (which |
| 268 | * can happen when there are multiple includes of the same file) |
| 269 | * |
| 270 | * @param other the other type |
| 271 | * @return true if this is the same type as other |
| 272 | */ |
| 273 | |
| 274 | boolean isSameType(SchemaType other); |
| 275 | |
| 276 | /** |
| 277 | * Get a description of this type for use in error messages. This is the same as the display name |
| 278 | * in the case of named types; for anonymous types it identifies the type by its position in a source |
| 279 | * schema document. |
| 280 | * |
| 281 | * @return text identifing the type, for use in a phrase such as "the type XXXX". |
| 282 | */ |
| 283 | |
| 284 | String getDescription(); |
| 285 | |
| 286 | /** |
| 287 | * Check that this type is validly derived from a given type, following the rules for the Schema Component |
| 288 | * Constraint "Is Type Derivation OK (Simple)" (3.14.6) or "Is Type Derivation OK (Complex)" (3.4.6) as |
| 289 | * appropriate. |
| 290 | * |
| 291 | * @param base the base type; the algorithm tests whether derivation from this type is permitted |
| 292 | * @param block the derivations that are blocked by the relevant element declaration |
| 293 | * @throws SchemaException if the derivation is not allowed |
| 294 | */ |
| 295 | |
| 296 | void checkTypeDerivationIsOK(SchemaType base, int block) throws SchemaException; |
| 297 | |
| 298 | /** |
| 299 | * Get the URI of the schema document where the type was originally defined. |
| 300 | * |
| 301 | * @return the URI of the schema document. Returns null if the information is unknown or if this |
| 302 | * is a built-in type |
| 303 | */ |
| 304 | |
| 305 | /*@Nullable*/ |
| 306 | String getSystemId(); |
| 307 | |
| 308 | /** |
| 309 | * Ask whether this type is an ID type. This is defined to be any simple type |
| 310 | * who typed value may contain atomic values of type xs:ID: that is, it includes types derived |
| 311 | * from ID by restriction, list, or union. Note that for a node to be treated |
| 312 | * as an ID in XSD 1.0, its typed value must be a *single* atomic value of type ID; the type of the |
| 313 | * node, however, can still allow a list. But in XSD 1.1, a list of IDs is permitted |
| 314 | * |
| 315 | * @return true if this type is an ID type |
| 316 | */ |
| 317 | |
| 318 | boolean isIdType() throws MissingComponentException; |
| 319 | |
| 320 | /** |
| 321 | * Ask whether this type is an IDREF or IDREFS type. This is defined to be any simple type |
| 322 | * who typed value may contain atomic values of type xs:IDREF: that is, it includes types derived |
| 323 | * from IDREF or IDREFS by restriction, list, or union |
| 324 | * |
| 325 | * @return true if this type is an IDREF type |
| 326 | */ |
| 327 | |
| 328 | boolean isIdRefType() throws MissingComponentException; |
| 329 | |
| 330 | } |