| 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.UserSimpleType; |
| 11 | import net.sf.saxon.expr.Expression; |
| 12 | import net.sf.saxon.lib.ConversionRules; |
| 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.value.Whitespace; |
| 17 | |
| 18 | import static net.sf.saxon.type.SchemaComponent.ValidationStatus.VALIDATED; |
| 19 | |
| 20 | /** |
| 21 | * This class has a singleton instance which represents the XML Schema built-in type xs:anySimpleType |
| 22 | */ |
| 23 | |
| 24 | public enum AnySimpleType implements SimpleType { |
| 25 | |
| 26 | // Josh Bloch recommends using a single-element enum type to implement singletons |
| 27 | // (see Effective Java 2nd Edition, Item 3: Enforce the singleton property with a |
| 28 | // private constructor or an enum type). |
| 29 | |
| 30 | INSTANCE; |
| 31 | |
| 32 | /** |
| 33 | * Get the local name of this type |
| 34 | * |
| 35 | * @return the local name of this type definition, if it has one. Return null in the case of an |
| 36 | * anonymous type. |
| 37 | */ |
| 38 | |
| 39 | /*@NotNull*/ |
| 40 | public String getName() { |
| 41 | return "anySimpleType"; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get the target namespace of this type |
| 46 | * |
| 47 | * @return the target namespace of this type definition, if it has one. Return null in the case |
| 48 | * of an anonymous type, and in the case of a global type defined in a no-namespace schema. |
| 49 | */ |
| 50 | |
| 51 | public String getTargetNamespace() { |
| 52 | return NamespaceConstant.SCHEMA; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Get the name of this type as an EQName, that is, a string in the format Q{uri}local. |
| 57 | * |
| 58 | * @return an EQName identifying the type. In the case of an anonymous type, an internally-generated |
| 59 | * name is returned |
| 60 | */ |
| 61 | public String getEQName() { |
| 62 | return "Q{" + NamespaceConstant.SCHEMA + "}anySimpleType"; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Return true if this is an external object type, that is, a Saxon-defined type for external |
| 67 | * Java or .NET objects |
| 68 | */ |
| 69 | |
| 70 | public boolean isExternalType() { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Determine whether this is a built-in type or a user-defined type |
| 76 | */ |
| 77 | |
| 78 | public boolean isBuiltInType() { |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Ask whether this type is an ID type. This is defined to be any simple type |
| 84 | * who typed value may contain atomic values of type xs:ID: that is, it includes types derived |
| 85 | * from ID by restriction, list, or union. Note that for a node to be treated |
| 86 | * as an ID, its typed value must be a *single* atomic value of type ID; the type of the |
| 87 | * node, however, can still allow a list. |
| 88 | */ |
| 89 | |
| 90 | public boolean isIdType() { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Ask whether this type is an IDREF or IDREFS type. This is defined to be any simple type |
| 96 | * who typed value may contain atomic values of type xs:IDREF: that is, it includes types derived |
| 97 | * from IDREF or IDREFS by restriction, list, or union |
| 98 | */ |
| 99 | |
| 100 | public boolean isIdRefType() { |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Get the redefinition level. This is zero for a component that has not been redefined; |
| 106 | * for a redefinition of a level-0 component, it is 1; for a redefinition of a level-N |
| 107 | * component, it is N+1. This concept is used to support the notion of "pervasive" redefinition: |
| 108 | * if a component is redefined at several levels, the top level wins, but it is an error to have |
| 109 | * two versions of the component at the same redefinition level. |
| 110 | * |
| 111 | * @return the redefinition level |
| 112 | */ |
| 113 | |
| 114 | public int getRedefinitionLevel() { |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Get the URI of the schema document containing the definition of this type |
| 120 | * |
| 121 | * @return null for a built-in type |
| 122 | */ |
| 123 | |
| 124 | /*@Nullable*/ |
| 125 | public String getSystemId() { |
| 126 | return null; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Get the singular instance of this class |
| 131 | * |
| 132 | * @return the singular object representing xs:anyType |
| 133 | */ |
| 134 | |
| 135 | /*@NotNull*/ |
| 136 | public static AnySimpleType getInstance() { |
| 137 | return INSTANCE; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Get the validation status - always valid |
| 142 | */ |
| 143 | public ValidationStatus getValidationStatus() { |
| 144 | return VALIDATED; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Get the base type |
| 149 | * |
| 150 | * @return AnyType |
| 151 | */ |
| 152 | |
| 153 | public SchemaType getBaseType() { |
| 154 | return AnyType.getInstance(); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Test whether this SchemaType is a complex type |
| 159 | * |
| 160 | * @return true if this SchemaType is a complex type |
| 161 | */ |
| 162 | |
| 163 | public boolean isComplexType() { |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Test whether this SchemaType is a simple type |
| 169 | * |
| 170 | * @return true if this SchemaType is a simple type |
| 171 | */ |
| 172 | |
| 173 | public boolean isSimpleType() { |
| 174 | return true; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Get the fingerprint of the name of this type |
| 179 | * |
| 180 | * @return the fingerprint. |
| 181 | */ |
| 182 | |
| 183 | public int getFingerprint() { |
| 184 | return StandardNames.XS_ANY_SIMPLE_TYPE; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Get the name of the type as a StructuredQName |
| 189 | * |
| 190 | * @return a StructuredQName identifying the type. In the case of an anonymous type, an internally-generated |
| 191 | * name is returned |
| 192 | */ |
| 193 | public StructuredQName getStructuredQName() { |
| 194 | return NAME; |
| 195 | } |
| 196 | |
| 197 | public final static StructuredQName NAME = new StructuredQName("xs", NamespaceConstant.SCHEMA, "anySimpleType"); |
| 198 | |
| 199 | /** |
| 200 | * Get a description of this type for use in diagnostics |
| 201 | * |
| 202 | * @return the string "xs:anyType" |
| 203 | */ |
| 204 | |
| 205 | /*@NotNull*/ |
| 206 | public String getDescription() { |
| 207 | return "xs:anySimpleType"; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Get the display name of the type: that is, a lexical QName with an arbitrary prefix |
| 212 | * |
| 213 | * @return a lexical QName identifying the type |
| 214 | */ |
| 215 | |
| 216 | /*@NotNull*/ |
| 217 | public String getDisplayName() { |
| 218 | return "xs:anySimpleType"; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Test whether this is the same type as another type. They are considered to be the same type |
| 223 | * if they are derived from the same type definition in the original XML representation (which |
| 224 | * can happen when there are multiple includes of the same file) |
| 225 | */ |
| 226 | |
| 227 | public boolean isSameType(SchemaType other) { |
| 228 | return other == INSTANCE; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Get the typed value of a node that is annotated with this schema type. |
| 233 | * |
| 234 | * @param node the node whose typed value is required |
| 235 | * @return the typed value. |
| 236 | * @since 8.5 |
| 237 | */ |
| 238 | |
| 239 | /*@NotNull*/ |
| 240 | public AtomicSequence atomize(/*@NotNull*/ NodeInfo node) { |
| 241 | return new UntypedAtomicValue(node.getStringValueCS()); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Check that this type is validly derived from a given type |
| 246 | * |
| 247 | * @param type the type from which this type is derived |
| 248 | * @param block the derivations that are blocked by the relevant element declaration |
| 249 | * @throws SchemaException if the derivation is not allowed |
| 250 | */ |
| 251 | |
| 252 | public void checkTypeDerivationIsOK(SchemaType type, int block) throws SchemaException { |
| 253 | if (type == this) { |
| 254 | return; |
| 255 | } |
| 256 | throw new SchemaException("Cannot derive xs:anySimpleType from another type"); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Test whether this Simple Type is an atomic type |
| 261 | * |
| 262 | * @return false, this is not (necessarily) an atomic type |
| 263 | */ |
| 264 | |
| 265 | public boolean isAtomicType() { |
| 266 | return false; |
| 267 | } |
| 268 | |
| 269 | public boolean isAnonymousType() { |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | |
| 274 | /** |
| 275 | * Determine whether this is a list type |
| 276 | * |
| 277 | * @return false (it isn't a list type) |
| 278 | */ |
| 279 | public boolean isListType() { |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Determin whether this is a union type |
| 285 | * |
| 286 | * @return false (it isn't a union type) |
| 287 | */ |
| 288 | public boolean isUnionType() { |
| 289 | return false; |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Get the built-in ancestor of this type in the type hierarchy |
| 294 | * |
| 295 | * @return this type itself |
| 296 | */ |
| 297 | /*@NotNull*/ |
| 298 | public SchemaType getBuiltInBaseType() { |
| 299 | return this; |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Get the typed value corresponding to a given string value, assuming it is |
| 304 | * valid against this type |
| 305 | * |
| 306 | * @param value the string value |
| 307 | * @param resolver a namespace resolver used to resolve any namespace prefixes appearing |
| 308 | * in the content of values. Can supply null, in which case any namespace-sensitive content |
| 309 | * will be rejected. |
| 310 | * @param rules rules used for type conversion |
| 311 | * @return an iterator over the atomic sequence comprising the typed value. The objects |
| 312 | * returned by this SequenceIterator will all be of type {@link net.sf.saxon.value.AtomicValue} |
| 313 | */ |
| 314 | |
| 315 | /*@NotNull*/ |
| 316 | public AtomicSequence getTypedValue(CharSequence value, NamespaceResolver resolver, ConversionRules rules) { |
| 317 | return new UntypedAtomicValue(value); |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Check whether a given input string is valid according to this SimpleType |
| 322 | * |
| 323 | * @param value the input string to be checked |
| 324 | * @param nsResolver a namespace resolver used to resolve namespace prefixes if the type |
| 325 | * is namespace sensitive. The value supplied may be null; in this case any namespace-sensitive |
| 326 | * content will throw an UnsupportedOperationException. |
| 327 | * @param rules rules used for type conversion |
| 328 | * @return null if validation succeeds (which it always does for this implementation) |
| 329 | * @throws UnsupportedOperationException if the type is namespace-sensitive and no namespace |
| 330 | * resolver is supplied |
| 331 | */ |
| 332 | /*@Nullable*/ |
| 333 | public ValidationFailure validateContent(/*@NotNull*/ CharSequence value, NamespaceResolver nsResolver, /*@NotNull*/ ConversionRules rules) { |
| 334 | return null; |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Test whether this type represents namespace-sensitive content |
| 339 | * |
| 340 | * @return false |
| 341 | */ |
| 342 | public boolean isNamespaceSensitive() { |
| 343 | return false; |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Returns the value of the 'block' attribute for this type, as a bit-signnificant |
| 348 | * integer with fields such as {@link SchemaType#DERIVATION_LIST} and {@link SchemaType#DERIVATION_EXTENSION} |
| 349 | * |
| 350 | * @return the value of the 'block' attribute for this type |
| 351 | */ |
| 352 | |
| 353 | public int getBlock() { |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Gets the integer code of the derivation method used to derive this type from its |
| 359 | * parent. Returns zero for primitive types. |
| 360 | * |
| 361 | * @return a numeric code representing the derivation method, for example {@link SchemaType#DERIVATION_RESTRICTION} |
| 362 | */ |
| 363 | |
| 364 | public int getDerivationMethod() { |
| 365 | return SchemaType.DERIVATION_RESTRICTION; |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Determines whether derivation (of a particular kind) |
| 370 | * from this type is allowed, based on the "final" property |
| 371 | * |
| 372 | * @param derivation the kind of derivation, for example {@link SchemaType#DERIVATION_LIST} |
| 373 | * @return true if this kind of derivation is allowed |
| 374 | */ |
| 375 | |
| 376 | public boolean allowsDerivation(int derivation) { |
| 377 | return true; |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Get the types of derivation that are not permitted, by virtue of the "final" property. |
| 382 | * |
| 383 | * @return the types of derivation that are not permitted, as a bit-significant integer |
| 384 | * containing bits such as {@link net.sf.saxon.type.SchemaType#DERIVATION_EXTENSION} |
| 385 | */ |
| 386 | public int getFinalProhibitions() { |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Determine how values of this simple type are whitespace-normalized. |
| 392 | * |
| 393 | * @return one of {@link net.sf.saxon.value.Whitespace#PRESERVE}, {@link net.sf.saxon.value.Whitespace#COLLAPSE}, |
| 394 | * {@link net.sf.saxon.value.Whitespace#REPLACE}. |
| 395 | */ |
| 396 | |
| 397 | public int getWhitespaceAction() { |
| 398 | return Whitespace.PRESERVE; |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Analyze an expression to see whether the expression is capable of delivering a value of this |
| 403 | * type. |
| 404 | * |
| 405 | * @param expression the expression that delivers the content |
| 406 | * @param kind the node kind whose content is being delivered: {@link Type#ELEMENT}, |
| 407 | * {@link Type#ATTRIBUTE}, or {@link Type#DOCUMENT} |
| 408 | */ |
| 409 | |
| 410 | public void analyzeContentExpression(Expression expression, int kind) { |
| 411 | //return; |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Apply any pre-lexical facets, other than whitespace. At the moment the only such |
| 416 | * facet is saxon:preprocess |
| 417 | * |
| 418 | * @param input the value to be preprocessed |
| 419 | * @return the value after preprocessing |
| 420 | */ |
| 421 | |
| 422 | public CharSequence preprocess(CharSequence input) { |
| 423 | return input; |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Reverse any pre-lexical facets, other than whitespace. At the moment the only such |
| 428 | * facet is saxon:preprocess. This is called when converting a value of this type to |
| 429 | * a string |
| 430 | * |
| 431 | * @param input the value to be postprocessed: this is the "ordinary" result of converting |
| 432 | * the value to a string |
| 433 | * @return the value after postprocessing |
| 434 | */ |
| 435 | |
| 436 | public CharSequence postprocess(CharSequence input) throws ValidationException { |
| 437 | return input; |
| 438 | } |
| 439 | |
| 440 | |
| 441 | //#ifdefined SCHEMA |
| 442 | |
| 443 | /** |
| 444 | * Get the schema component in the form of a function item. This allows schema information |
| 445 | * to be made visible to XSLT or XQuery code. The function makes available the contents of the |
| 446 | * schema component as defined in the XSD specification. The function takes a string as argument |
| 447 | * representing a property name, and returns the corresponding property of the schema component. |
| 448 | * There is also a property "class" which returns the kind of schema component, for example |
| 449 | * "Attribute Declaration". |
| 450 | * |
| 451 | * @return the schema component represented as a function from property names to property values. |
| 452 | */ |
| 453 | public Function getComponentAsFunction() { |
| 454 | return UserSimpleType.getComponentAsFunction(this); |
| 455 | } |
| 456 | //#endif |
| 457 | } |
| 458 |