| 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 com.saxonica.ee.schema.UserComplexType; |
| 11 | import net.sf.saxon.expr.Expression; |
| 12 | import net.sf.saxon.expr.StaticProperty; |
| 13 | import net.sf.saxon.lib.NamespaceConstant; |
| 14 | import net.sf.saxon.om.*; |
| 15 | import net.sf.saxon.value.UntypedAtomicValue; |
| 16 | import net.sf.saxon.z.IntHashSet; |
| 17 | |
| 18 | import static net.sf.saxon.type.SchemaComponent.ValidationStatus.VALIDATED; |
| 19 | |
| 20 | /** |
| 21 | * This class has a singleton instance which represents the complex type xdt:untyped, |
| 22 | * used for elements that have not been validated. |
| 23 | */ |
| 24 | |
| 25 | public enum Untyped implements ComplexType { |
| 26 | |
| 27 | // Josh Bloch recommends using a single-element enum type to implement singletons |
| 28 | // (see Effective Java 2nd Edition, Item 3: Enforce the singleton property with a |
| 29 | // private constructor or an enum type). |
| 30 | |
| 31 | INSTANCE; |
| 32 | |
| 33 | /** |
| 34 | * Get the singular instance of this class |
| 35 | * |
| 36 | * @return the singular object representing xs:anyType |
| 37 | */ |
| 38 | |
| 39 | /*@NotNull*/ |
| 40 | public static Untyped getInstance() { |
| 41 | return INSTANCE; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get the validation status - always valid |
| 46 | */ |
| 47 | public ValidationStatus getValidationStatus() { |
| 48 | return VALIDATED; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Get the local name of this type |
| 53 | * |
| 54 | * @return the local name of this type definition, if it has one. Return null in the case of an |
| 55 | * anonymous type. |
| 56 | */ |
| 57 | |
| 58 | /*@NotNull*/ |
| 59 | public String getName() { |
| 60 | return "untyped"; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Get the name of this type as an EQName, that is, a string in the format Q{uri}local. |
| 65 | * |
| 66 | * @return an EQName identifying the type, specifically "Q{http://www.w3.org/2001/XMLSchema}untyped" |
| 67 | */ |
| 68 | public String getEQName() { |
| 69 | return "Q{" + NamespaceConstant.SCHEMA + "}untyped"; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Get the redefinition level. This is zero for a component that has not been redefined; |
| 74 | * for a redefinition of a level-0 component, it is 1; for a redefinition of a level-N |
| 75 | * component, it is N+1. This concept is used to support the notion of "pervasive" redefinition: |
| 76 | * if a component is redefined at several levels, the top level wins, but it is an error to have |
| 77 | * two versions of the component at the same redefinition level. |
| 78 | * |
| 79 | * @return the redefinition level |
| 80 | */ |
| 81 | |
| 82 | public int getRedefinitionLevel() { |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Get the target namespace of this type |
| 88 | * |
| 89 | * @return the target namespace of this type definition, if it has one. Return null in the case |
| 90 | * of an anonymous type, and in the case of a global type defined in a no-namespace schema. |
| 91 | */ |
| 92 | |
| 93 | public String getTargetNamespace() { |
| 94 | return NamespaceConstant.SCHEMA; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Get the variety of this complex type. This will be one of the values |
| 99 | * {@link #VARIETY_EMPTY}, {@link #VARIETY_MIXED}, {@link #VARIETY_SIMPLE}, or |
| 100 | * {@link #VARIETY_ELEMENT_ONLY} |
| 101 | */ |
| 102 | |
| 103 | public int getVariety() { |
| 104 | return VARIETY_MIXED; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Get the URI of the schema document containing the definition of this type |
| 109 | * |
| 110 | * @return null for a built-in type |
| 111 | */ |
| 112 | |
| 113 | /*@Nullable*/ |
| 114 | public String getSystemId() { |
| 115 | return null; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Returns the value of the 'block' attribute for this type, as a bit-signnificant |
| 120 | * integer with fields such as {@link SchemaType#DERIVATION_LIST} and {@link SchemaType#DERIVATION_EXTENSION} |
| 121 | * |
| 122 | * @return the value of the 'block' attribute for this type |
| 123 | */ |
| 124 | |
| 125 | public int getBlock() { |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Gets the integer code of the derivation method used to derive this type from its |
| 131 | * parent. Returns zero for primitive types. |
| 132 | * |
| 133 | * @return a numeric code representing the derivation method, for example {@link SchemaType#DERIVATION_RESTRICTION} |
| 134 | */ |
| 135 | |
| 136 | public int getDerivationMethod() { |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Determines whether derivation (of a particular kind) |
| 142 | * from this type is allowed, based on the "final" property |
| 143 | * |
| 144 | * @param derivation the kind of derivation, for example {@link SchemaType#DERIVATION_LIST} |
| 145 | * @return true if this kind of derivation is allowed |
| 146 | */ |
| 147 | |
| 148 | public boolean allowsDerivation(int derivation) { |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Get the types of derivation that are not permitted, by virtue of the "final" property. |
| 154 | * |
| 155 | * @return the types of derivation that are not permitted, as a bit-significant integer |
| 156 | * containing bits such as {@link net.sf.saxon.type.SchemaType#DERIVATION_EXTENSION} |
| 157 | */ |
| 158 | public int getFinalProhibitions() { |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Check that this type is validly derived from a given type |
| 164 | * |
| 165 | * @param type the type from which this type is derived |
| 166 | * @param block the derivations that are blocked by the relevant element declaration |
| 167 | */ |
| 168 | |
| 169 | public void checkTypeDerivationIsOK(SchemaType type, int block) { |
| 170 | |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Get the fingerprint of the name of this type |
| 175 | * |
| 176 | * @return the fingerprint. Returns an invented fingerprint for an anonymous type. |
| 177 | */ |
| 178 | |
| 179 | public int getFingerprint() { |
| 180 | return StandardNames.XS_UNTYPED; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Get the display name of the type: that is, a lexical QName with an arbitrary prefix |
| 185 | * |
| 186 | * @return a lexical QName identifying the type |
| 187 | */ |
| 188 | |
| 189 | /*@NotNull*/ |
| 190 | public String getDisplayName() { |
| 191 | return "xs:untyped"; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Get the name of the type as a StructuredQName |
| 196 | * |
| 197 | * @return a StructuredQName identifying the type. In the case of an anonymous type, an internally-generated |
| 198 | * name is returned |
| 199 | */ |
| 200 | public StructuredQName getStructuredQName() { |
| 201 | return NAME; |
| 202 | } |
| 203 | |
| 204 | public final static StructuredQName NAME = |
| 205 | new StructuredQName("xs", NamespaceConstant.SCHEMA, "untyped"); |
| 206 | |
| 207 | /** |
| 208 | * Test whether this SchemaType is a complex type |
| 209 | * |
| 210 | * @return true if this SchemaType is a complex type |
| 211 | */ |
| 212 | |
| 213 | public boolean isComplexType() { |
| 214 | return true; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Test whether this is an anonymous type |
| 219 | * |
| 220 | * @return true if this SchemaType is an anonymous type |
| 221 | */ |
| 222 | |
| 223 | public boolean isAnonymousType() { |
| 224 | return false; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Returns the base type that this type inherits from. This method can be used to get the |
| 229 | * base type of a type that is known to be valid. |
| 230 | * If this type is a Simpletype that is a built in primitive type then null is returned. |
| 231 | * |
| 232 | * @return the base type. |
| 233 | * @throws IllegalStateException if this type is not valid. |
| 234 | */ |
| 235 | |
| 236 | /*@NotNull*/ |
| 237 | public SchemaType getKnownBaseType() throws IllegalStateException { |
| 238 | return AnyType.getInstance(); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Test whether this is the same type as another type. They are considered to be the same type |
| 243 | * if they are derived from the same type definition in the original XML representation (which |
| 244 | * can happen when there are multiple includes of the same file) |
| 245 | */ |
| 246 | |
| 247 | public boolean isSameType(SchemaType other) { |
| 248 | return other == INSTANCE; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Returns the base type that this type inherits from. |
| 253 | * If this type is a Simpletype that is a built in primitive type then null is returned. |
| 254 | * |
| 255 | * @return the base type. |
| 256 | */ |
| 257 | |
| 258 | /*@NotNull*/ |
| 259 | public SchemaType getBaseType() { |
| 260 | return AnyType.getInstance(); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Test whether this ComplexType has been marked as abstract. |
| 265 | * |
| 266 | * @return false: this class is not abstract. |
| 267 | */ |
| 268 | |
| 269 | public boolean isAbstract() { |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Test whether this SchemaType is a simple type |
| 275 | * |
| 276 | * @return true if this SchemaType is a simple type |
| 277 | */ |
| 278 | |
| 279 | public boolean isSimpleType() { |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Test whether this SchemaType is an atomic type |
| 285 | * |
| 286 | * @return true if this SchemaType is an atomic type |
| 287 | */ |
| 288 | |
| 289 | public boolean isAtomicType() { |
| 290 | return false; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Ask whether this type is an ID type. This is defined to be any simple type |
| 295 | * who typed value may contain atomic values of type xs:ID: that is, it includes types derived |
| 296 | * from ID by restriction, list, or union. Note that for a node to be treated |
| 297 | * as an ID, its typed value must be a *single* atomic value of type ID; the type of the |
| 298 | * node, however, can still allow a list. |
| 299 | */ |
| 300 | |
| 301 | public boolean isIdType() { |
| 302 | return false; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Ask whether this type is an IDREF or IDREFS type. This is defined to be any simple type |
| 307 | * who typed value may contain atomic values of type xs:IDREF: that is, it includes types derived |
| 308 | * from IDREF or IDREFS by restriction, list, or union |
| 309 | */ |
| 310 | |
| 311 | public boolean isIdRefType() { |
| 312 | return false; |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Test whether this complex type has complex content |
| 317 | * |
| 318 | * @return true: this complex type has complex content |
| 319 | */ |
| 320 | public boolean isComplexContent() { |
| 321 | return true; |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Test whether this complex type has simple content |
| 326 | * |
| 327 | * @return false: this complex type has complex content |
| 328 | */ |
| 329 | |
| 330 | public boolean isSimpleContent() { |
| 331 | return false; |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Test whether this complex type has "all" content, that is, a content model |
| 336 | * using an xs:all compositor |
| 337 | * |
| 338 | * @return false: this complex type does not use an "all" compositor |
| 339 | */ |
| 340 | |
| 341 | public boolean isAllContent() { |
| 342 | return false; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * For a complex type with simple content, return the simple type of the content. |
| 347 | * Otherwise, return null. |
| 348 | * |
| 349 | * @return null: this complex type does not have simple content |
| 350 | */ |
| 351 | |
| 352 | /*@Nullable*/ |
| 353 | public SimpleType getSimpleContentType() { |
| 354 | return null; |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Test whether this complex type is derived by restriction |
| 359 | * |
| 360 | * @return true: this type is treated as a restriction of xs:anyType |
| 361 | */ |
| 362 | public boolean isRestricted() { |
| 363 | return true; |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Test whether the content type of this complex type is empty |
| 368 | * |
| 369 | * @return false: the content model is not empty |
| 370 | */ |
| 371 | |
| 372 | public boolean isEmptyContent() { |
| 373 | return false; |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Test whether the content model of this complexType allows empty content |
| 378 | * |
| 379 | * @return true: the content is allowed to be empty |
| 380 | */ |
| 381 | |
| 382 | public boolean isEmptiable() { |
| 383 | return true; |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Test whether this complex type allows mixed content |
| 388 | * |
| 389 | * @return true: mixed content is allowed |
| 390 | */ |
| 391 | |
| 392 | public boolean isMixedContent() { |
| 393 | return true; |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Get a description of this type for use in diagnostics |
| 398 | * |
| 399 | * @return the string "xs:anyType" |
| 400 | */ |
| 401 | |
| 402 | /*@NotNull*/ |
| 403 | public String getDescription() { |
| 404 | return "xs:untyped"; |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Analyze an expression to see whether the expression is capable of delivering a value of this |
| 409 | * type. |
| 410 | * |
| 411 | * @param expression the expression that delivers the content |
| 412 | * @param kind the node kind whose content is being delivered: {@link Type#ELEMENT}, |
| 413 | * {@link Type#ATTRIBUTE}, or {@link Type#DOCUMENT} |
| 414 | */ |
| 415 | |
| 416 | public void analyzeContentExpression(Expression expression, int kind) { |
| 417 | //return; |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * Get the typed value of a node that is annotated with this schema type. |
| 422 | * |
| 423 | * @param node the node whose typed value is required |
| 424 | * @return the typed value. |
| 425 | * @since 8.5 |
| 426 | */ |
| 427 | |
| 428 | /*@NotNull*/ |
| 429 | public AtomicSequence atomize(/*@NotNull*/ NodeInfo node) { |
| 430 | return new UntypedAtomicValue(node.getStringValueCS()); |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Find an element particle within this complex type definition having a given element name |
| 435 | * (identified by fingerprint), and return the schema type associated with that element particle. |
| 436 | * If there is no such particle, return null. If the fingerprint matches an element wildcard, |
| 437 | * return the type of the global element declaration with the given name if one exists, or AnyType |
| 438 | * if none exists and lax validation is permitted by the wildcard. |
| 439 | * @param elementName Identifies the name of the child element within this content model |
| 440 | * @param considerExtensions True if types derived from this type by extension are to be included in the search |
| 441 | */ |
| 442 | |
| 443 | /*@NotNull*/ |
| 444 | public SchemaType getElementParticleType(int elementName, boolean considerExtensions) { |
| 445 | return this; |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * Find an element particle within this complex type definition having a given element name |
| 450 | * (identified by fingerprint), and return the cardinality associated with that element particle, |
| 451 | * that is, the number of times the element can occur within this complex type. The value is one of |
| 452 | * {@link net.sf.saxon.expr.StaticProperty#EXACTLY_ONE}, {@link net.sf.saxon.expr.StaticProperty#ALLOWS_ZERO_OR_ONE}, |
| 453 | * {@link net.sf.saxon.expr.StaticProperty#ALLOWS_ZERO_OR_MORE}, {@link net.sf.saxon.expr.StaticProperty#ALLOWS_ONE_OR_MORE}, |
| 454 | * If there is no such particle, return zero. |
| 455 | * @param elementName Identifies the name of the child element within this content model |
| 456 | * @param considerExtensions True if types derived from this type by extension are to be included in the search |
| 457 | */ |
| 458 | |
| 459 | public int getElementParticleCardinality(int elementName, boolean considerExtensions) { |
| 460 | return StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 461 | } |
| 462 | |
| 463 | @Override |
| 464 | public int getElementParticleCardinality(StructuredQName elementName, boolean considerExtensions) { |
| 465 | return StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Find an attribute use within this complex type definition having a given attribute name |
| 470 | * (identified by fingerprint), and return the schema type associated with that attribute. |
| 471 | * If there is no such attribute use, return null. If the fingerprint matches an attribute wildcard, |
| 472 | * return the type of the global attribute declaration with the given name if one exists, or AnySimpleType |
| 473 | * if none exists and lax validation is permitted by the wildcard. |
| 474 | * |
| 475 | * @param attributeName Identifies the name of the child element within this content model |
| 476 | */ |
| 477 | |
| 478 | /*@NotNull*/ |
| 479 | public SimpleType getAttributeUseType(StructuredQName attributeName) { |
| 480 | return BuiltInAtomicType.UNTYPED_ATOMIC; |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * Find an attribute use within this complex type definition having a given attribute name |
| 485 | * (identified by fingerprint), and return the cardinality associated with that attribute, |
| 486 | * which will always be 0, 1, or 0-or-1. |
| 487 | * If there is no such attribute use, return null. If the fingerprint matches an attribute wildcard, |
| 488 | * return the type of the global attribute declaration with the given name if one exists, or AnySimpleType |
| 489 | * if none exists and lax validation is permitted by the wildcard. |
| 490 | * <p>If there are types derived from this type by extension, search those too.</p> |
| 491 | * |
| 492 | * @param attributeName Identifies the name of the child element within this content model |
| 493 | * @return the schema type associated with the attribute use identified by the fingerprint. |
| 494 | * If there is no such attribute use, return null. |
| 495 | */ |
| 496 | |
| 497 | public int getAttributeUseCardinality(StructuredQName attributeName) { |
| 498 | return StaticProperty.ALLOWS_ZERO_OR_ONE; |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * Return true if this type (or any known type derived from it by extension) allows the element |
| 503 | * to have one or more attributes. |
| 504 | * |
| 505 | * @return true if attributes are allowed |
| 506 | */ |
| 507 | |
| 508 | public boolean allowsAttributes() { |
| 509 | return true; |
| 510 | } |
| 511 | |
| 512 | |
| 513 | /** |
| 514 | * Get a list of all the names of elements that can appear as children of an element having this |
| 515 | * complex type, as integer fingerprints. If the list is unbounded (because of wildcards or the use |
| 516 | * of xs:anyType), return null. |
| 517 | * @param children a set, initially empty, which on return will hold the names of all permitted |
| 518 | * child elements; if the result contains the value XS_INVALID_NAME, this indicates that it is not possible to enumerate |
| 519 | * all the children, typically because of wildcards. In this case the other contents of the set should |
| 520 | * @param ignoreWildcards True if wildcard particles in the content model should be ignored |
| 521 | */ |
| 522 | |
| 523 | public void gatherAllPermittedChildren(IntHashSet children, boolean ignoreWildcards) { |
| 524 | children.add(-1); |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Get a list of all the names of elements that can appear as descendants of an element having this |
| 529 | * complex type, as integer fingerprints. If the list is unbounded (because of wildcards or the use |
| 530 | * of xs:anyType), return null. |
| 531 | * |
| 532 | * @param descendants an integer set, initially empty, which on return will hold the fingerprints of all permitted |
| 533 | * descendant elements; if the result contains the value XS_INVALID_NAME, this indicates that it is not possible to enumerate |
| 534 | * all the descendants, typically because of wildcards. In this case the other contents of the set should |
| 535 | * be ignored. |
| 536 | */ |
| 537 | |
| 538 | public void gatherAllPermittedDescendants(/*@NotNull*/ IntHashSet descendants) { |
| 539 | descendants.add(-1); |
| 540 | } |
| 541 | |
| 542 | /** |
| 543 | * Assuming an element is a permitted descendant in the content model of this type, determine |
| 544 | * the type of the element when it appears as a descendant. If it appears with more than one type, |
| 545 | * return xs:anyType. |
| 546 | * |
| 547 | * @param fingerprint the name of the required descendant element |
| 548 | * @return the type of the descendant element; null if the element cannot appear as a descendant; |
| 549 | * anyType if it can appear with several different types |
| 550 | */ |
| 551 | |
| 552 | /*@NotNull*/ |
| 553 | public SchemaType getDescendantElementType(int fingerprint) { |
| 554 | return this; |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * Assuming an element is a permitted descendant in the content model of this type, determine |
| 559 | * the cardinality of the element when it appears as a descendant. |
| 560 | * |
| 561 | * @param elementFingerprint the name of the required descendant element |
| 562 | * @return the cardinality of the descendant element within this complex type |
| 563 | */ |
| 564 | |
| 565 | public int getDescendantElementCardinality(int elementFingerprint) { |
| 566 | return StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * Ask whether this type (or any known type derived from it by extension) allows the element |
| 571 | * to have children that match a wildcard |
| 572 | * |
| 573 | * @return true if the content model of this type, or its extensions, contains an element wildcard |
| 574 | */ |
| 575 | |
| 576 | public boolean containsElementWildcard() { |
| 577 | return true; |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Ask whether there are any assertions defined on this complex type |
| 582 | * |
| 583 | * @return true if there are any assertions |
| 584 | */ |
| 585 | public boolean hasAssertions() { |
| 586 | return false; |
| 587 | } |
| 588 | |
| 589 | //#ifdefined SCHEMA |
| 590 | |
| 591 | /** |
| 592 | * Get the schema component in the form of a function item. This allows schema information |
| 593 | * to be made visible to XSLT or XQuery code. The function makes available the contents of the |
| 594 | * schema component as defined in the XSD specification. The function takes a string as argument |
| 595 | * representing a property name, and returns the corresponding property of the schema component. |
| 596 | * There is also a property "class" which returns the kind of schema component, for example |
| 597 | * "Attribute Declaration". |
| 598 | * |
| 599 | * @return the schema component represented as a function from property names to property values. |
| 600 | */ |
| 601 | public Function getComponentAsFunction() { |
| 602 | return UserComplexType.getComponentAsFunction(this); |
| 603 | } |
| 604 | //#endif |
| 605 | |
| 606 | } |
| 607 |