Debian Codegraph @

net/sf/saxon/type/AnyType.java

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