Debian Codegraph @

net/sf/saxon/s9api/ItemTypeFactory.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.s9api;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.expr.parser.Token;
12 import net.sf.saxon.lib.NamespaceConstant;
13 import net.sf.saxon.ma.arrays.ArrayItem;
14 import net.sf.saxon.ma.arrays.ArrayItemType;
15 import net.sf.saxon.ma.map.MapItem;
16 import net.sf.saxon.ma.map.MapType;
17 import net.sf.saxon.om.Item;
18 import net.sf.saxon.om.NodeInfo;
19 import net.sf.saxon.om.StandardNames;
20 import net.sf.saxon.om.StructuredQName;
21 import net.sf.saxon.pattern.*;
22 import net.sf.saxon.type.*;
23 import net.sf.saxon.value.AtomicValue;
24 import net.sf.saxon.value.ExternalObject;
25 import net.sf.saxon.value.ObjectValue;
26
27 import java.util.Map;
28
29 /**
30 * This class is used for creating ItemType objects.
31 * <p>The <code>ItemTypeFactory</code> class is thread-safe.</p>
32 */
33 public class ItemTypeFactory {
34
35 private Processor processor;
36
37 /**
38 * Create an ItemTypeFactory
39 *
40 * @param processor the processor used by this ItemTypeFactory. This must be supplied
41 * in the case of user-defined types or types that reference element or attribute names;
42 * for built-in types it can be omitted.
43 */
44
45 public ItemTypeFactory(Processor processor) {
46 this.processor = processor;
47 }
48
49 /**
50 * Get an item type representing an atomic type. This may be a built-in type in the
51 * XML Schema namespace, or a user-defined atomic type. The resulting type will use
52 * the conversion rules configured for the supplied <code>Processor</code>.
53 * <p>It is undefined whether two calls supplying the same QName will return the same ItemType
54 * object.</p>
55 *
56 * @param name the name of the built-in or user-defined atomic type required
57 * @return an ItemType object representing this built-in or user-defined atomic type
58 * @throws SaxonApiException if the type name is not known, or if the type identified by the
59 * name is not an atomic type.
60 */
61
62 public ItemType getAtomicType(QName name) throws SaxonApiException {
63 String uri = name.getNamespaceURI();
64 String local = name.getLocalName();
65 if (NamespaceConstant.SCHEMA.equals(uri)) {
66 int fp = StandardNames.getFingerprint(uri, local);
67 Configuration config = processor.getUnderlyingConfiguration();
68 if (config.getXsdVersion() == Configuration.XSD10 && config.getXMLVersion() == Configuration.XML10) {
69 return getBuiltInAtomicType(fp);
70 } else {
71 return ItemType.BuiltInAtomicItemType.makeVariant(
72 (ItemType.BuiltInAtomicItemType) getBuiltInAtomicType(fp), config.getConversionRules());
73 }
74 } else {
75 Configuration config = processor.getUnderlyingConfiguration();
76 SchemaType type = config.getSchemaType(new StructuredQName("", name.getNamespaceURI(), local));
77 if (type == null || !type.isAtomicType()) {
78 throw new SaxonApiException("Unknown atomic type " + name.getClarkName());
79 }
80 return new ConstructedItemType((AtomicType) type, processor);
81 }
82 }
83
84
85 private ItemType getBuiltInAtomicType(int fp) throws SaxonApiException {
86 switch (fp) {
87 case StandardNames.XS_ANY_ATOMIC_TYPE:
88 return ItemType.ANY_ATOMIC_VALUE;
89
90 // case StandardNames.XS_NUMERIC:
91 // return ItemType.NUMERIC;
92
93 case StandardNames.XS_STRING:
94 return ItemType.STRING;
95
96 case StandardNames.XS_BOOLEAN:
97 return ItemType.BOOLEAN;
98
99 case StandardNames.XS_DURATION:
100 return ItemType.DURATION;
101
102 case StandardNames.XS_DATE_TIME:
103 return ItemType.DATE_TIME;
104
105 case StandardNames.XS_DATE:
106 return ItemType.DATE;
107
108 case StandardNames.XS_TIME:
109 return ItemType.TIME;
110
111 case StandardNames.XS_G_YEAR_MONTH:
112 return ItemType.G_YEAR_MONTH;
113
114 case StandardNames.XS_G_MONTH:
115 return ItemType.G_MONTH;
116
117 case StandardNames.XS_G_MONTH_DAY:
118 return ItemType.G_MONTH_DAY;
119
120 case StandardNames.XS_G_YEAR:
121 return ItemType.G_YEAR;
122
123 case StandardNames.XS_G_DAY:
124 return ItemType.G_DAY;
125
126 case StandardNames.XS_HEX_BINARY:
127 return ItemType.HEX_BINARY;
128
129 case StandardNames.XS_BASE64_BINARY:
130 return ItemType.BASE64_BINARY;
131
132 case StandardNames.XS_ANY_URI:
133 return ItemType.ANY_URI;
134
135 case StandardNames.XS_QNAME:
136 return ItemType.QNAME;
137
138 case StandardNames.XS_NOTATION:
139 return ItemType.NOTATION;
140
141 case StandardNames.XS_UNTYPED_ATOMIC:
142 return ItemType.UNTYPED_ATOMIC;
143
144 case StandardNames.XS_DECIMAL:
145 return ItemType.DECIMAL;
146
147 case StandardNames.XS_FLOAT:
148 return ItemType.FLOAT;
149
150 case StandardNames.XS_DOUBLE:
151 return ItemType.DOUBLE;
152
153 case StandardNames.XS_INTEGER:
154 return ItemType.INTEGER;
155
156 case StandardNames.XS_NON_POSITIVE_INTEGER:
157 return ItemType.NON_POSITIVE_INTEGER;
158
159 case StandardNames.XS_NEGATIVE_INTEGER:
160 return ItemType.NEGATIVE_INTEGER;
161
162 case StandardNames.XS_LONG:
163 return ItemType.LONG;
164
165 case StandardNames.XS_INT:
166 return ItemType.INT;
167
168 case StandardNames.XS_SHORT:
169 return ItemType.SHORT;
170
171 case StandardNames.XS_BYTE:
172 return ItemType.BYTE;
173
174 case StandardNames.XS_NON_NEGATIVE_INTEGER:
175 return ItemType.NON_NEGATIVE_INTEGER;
176
177 case StandardNames.XS_POSITIVE_INTEGER:
178 return ItemType.POSITIVE_INTEGER;
179
180 case StandardNames.XS_UNSIGNED_LONG:
181 return ItemType.UNSIGNED_LONG;
182
183 case StandardNames.XS_UNSIGNED_INT:
184 return ItemType.UNSIGNED_INT;
185
186 case StandardNames.XS_UNSIGNED_SHORT:
187 return ItemType.UNSIGNED_SHORT;
188
189 case StandardNames.XS_UNSIGNED_BYTE:
190 return ItemType.UNSIGNED_BYTE;
191
192 case StandardNames.XS_YEAR_MONTH_DURATION:
193 return ItemType.YEAR_MONTH_DURATION;
194
195 case StandardNames.XS_DAY_TIME_DURATION:
196 return ItemType.DAY_TIME_DURATION;
197
198 case StandardNames.XS_NORMALIZED_STRING:
199 return ItemType.NORMALIZED_STRING;
200
201 case StandardNames.XS_TOKEN:
202 return ItemType.TOKEN;
203
204 case StandardNames.XS_LANGUAGE:
205 return ItemType.LANGUAGE;
206
207 case StandardNames.XS_NAME:
208 return ItemType.NAME;
209
210 case StandardNames.XS_NMTOKEN:
211 return ItemType.NMTOKEN;
212
213 case StandardNames.XS_NCNAME:
214 return ItemType.NCNAME;
215
216 case StandardNames.XS_ID:
217 return ItemType.ID;
218
219 case StandardNames.XS_IDREF:
220 return ItemType.IDREF;
221
222 case StandardNames.XS_ENTITY:
223 return ItemType.ENTITY;
224
225 case StandardNames.XS_DATE_TIME_STAMP:
226 return ItemType.DATE_TIME_STAMP;
227
228 default:
229 throw new SaxonApiException("Unknown atomic type " +
230 processor.getUnderlyingConfiguration().getNamePool().getClarkName(fp));
231 }
232 }
233
234 /**
235 * Get an item type that matches any node of a specified kind.
236 * <p>This corresponds to the XPath syntactic forms element(), attribute(),
237 * document-node(), text(), comment(), processing-instruction(). It also provides
238 * an option, not available in the XPath syntax, that matches namespace nodes.</p>
239 * <p>It is undefined whether two calls supplying the same argument value will
240 * return the same ItemType object.</p>
241 *
242 * @param kind the kind of node for which a NodeTest is required
243 * @return an item type corresponding to the specified kind of node
244 */
245
246 public ItemType getNodeKindTest(XdmNodeKind kind) {
247 switch (kind) {
248 case DOCUMENT:
249 return ItemType.DOCUMENT_NODE;
250 case ELEMENT:
251 return ItemType.ELEMENT_NODE;
252 case ATTRIBUTE:
253 return ItemType.ATTRIBUTE_NODE;
254 case TEXT:
255 return ItemType.TEXT_NODE;
256 case COMMENT:
257 return ItemType.COMMENT_NODE;
258 case PROCESSING_INSTRUCTION:
259 return ItemType.PROCESSING_INSTRUCTION_NODE;
260 case NAMESPACE:
261 return ItemType.NAMESPACE_NODE;
262 default:
263 throw new IllegalArgumentException("XdmNodeKind");
264 }
265 }
266
267 /**
268 * Get an item type that matches nodes of a specified kind with a specified name.
269 * <p>This corresponds to the XPath syntactic forms element(name), attribute(name),
270 * and processing-instruction(name). In the case of processing-instruction, the supplied
271 * QName must have no namespace.</p>
272 * <p>It is undefined whether two calls supplying the same argument values will
273 * return the same ItemType object.</p>
274 *
275 * @param kind the kind of nodes that match
276 * @param name the name of the nodes that match
277 * @return an ItemType that matches nodes of a given node kind with a given name
278 * @throws IllegalArgumentException if the node kind is other than element, attribute, or
279 * processing instruction, or if the node kind is processing instruction and the name is in a namespace.
280 */
281
282 public ItemType getItemType(XdmNodeKind kind, QName name) {
283 int k = kind.getNumber();
284 if (k == Type.ELEMENT || k == Type.ATTRIBUTE || k == Type.PROCESSING_INSTRUCTION) {
285 if (k == Type.PROCESSING_INSTRUCTION && name.getNamespaceURI().isEmpty()) {
286 throw new IllegalArgumentException("The name of a processing instruction must not be in a namespace");
287 }
288 NameTest type = new NameTest(k,
289 name.getNamespaceURI(), name.getLocalName(), processor.getUnderlyingConfiguration().getNamePool());
290 return new ConstructedItemType(type, processor);
291 } else {
292 throw new IllegalArgumentException("Node kind must be element, attribute, or processing-instruction");
293 }
294 }
295
296 /**
297 * Make an ItemType representing an element declaration in the schema. This is the
298 * equivalent of the XPath syntax <code>schema-element(element-name)</code>
299 * <p>It is undefined whether two calls supplying the same argument values will
300 * return the same ItemType object.</p>
301 *
302 * @param name the element name
303 * @return the ItemType
304 * @throws SaxonApiException if the schema does not contain a global element declaration
305 * for the given name, or if the element declaration contains
306 * dangling references to other schema components
307 */
308
309 public ItemType getSchemaElementTest(QName name) throws SaxonApiException {
310 Configuration config = processor.getUnderlyingConfiguration();
311 SchemaDeclaration decl = config.getElementDeclaration(name.getStructuredQName());
312 if (decl == null) {
313 throw new SaxonApiException("No global declaration found for element " + name.getClarkName());
314 }
315 try {
316 NodeTest test = decl.makeSchemaNodeTest();
317 return new ConstructedItemType(test, processor);
318 } catch (MissingComponentException e) {
319 throw new SaxonApiException(e);
320 }
321 }
322
323 /**
324 * Make an ItemType that tests an element name and/or schema type. This is the
325 * equivalent of the XPath syntax <code>element(element-name, type)</code>
326 * <p>It is undefined whether two calls supplying the same argument values will
327 * return the same ItemType object.</p>
328 *
329 * @param name the element name, or null if there is no constraint on the name (equivalent to
330 * specifying <code>element(*, type)</code>)
331 * @param schemaType the name of the required schema type, or null if there is no constraint
332 * on the type (equivalent to specifying <code>element(name)</code>)
333 * @param nillable if a nilled element is allowed to match the type (equivalent to specifying
334 * "?" after the type name). The value is ignored if schemaType is null.
335 * @return the constructed ItemType
336 * @throws SaxonApiException if the schema does not contain a global element declaration
337 * for the given name
338 */
339
340 public ItemType getElementTest(/*@Nullable*/ QName name, QName schemaType, boolean nillable) throws SaxonApiException {
341 Configuration config = processor.getUnderlyingConfiguration();
342 NameTest nameTest = null;
343 ContentTypeTest contentTest = null;
344 if (name != null) {
345 int elementFP = config.getNamePool().allocateFingerprint(name.getNamespaceURI(), name.getLocalName());
346 nameTest = new NameTest(Type.ELEMENT, elementFP, config.getNamePool());
347 }
348 if (schemaType != null) {
349 SchemaType type = config.getSchemaType(
350 new StructuredQName("", schemaType.getNamespaceURI(), schemaType.getLocalName()));
351 if (type == null) {
352 throw new SaxonApiException("Unknown schema type " + schemaType.getClarkName());
353 }
354 contentTest = new ContentTypeTest(Type.ELEMENT, type, config, nillable);
355 }
356 if (contentTest == null) {
357 if (nameTest == null) {
358 return getNodeKindTest(XdmNodeKind.ELEMENT);
359 } else {
360 return new ConstructedItemType(nameTest, processor);
361 }
362 } else {
363 if (nameTest == null) {
364 return new ConstructedItemType(contentTest, processor);
365 } else {
366 CombinedNodeTest combo = new CombinedNodeTest(
367 nameTest,
368 Token.INTERSECT,
369 contentTest);
370 return new ConstructedItemType(combo, processor);
371 }
372 }
373 }
374
375 /**
376 * Get an ItemType representing an attribute declaration in the schema. This is the
377 * equivalent of the XPath syntax <code>schema-attribute(attribute-name)</code>
378 * <p>It is undefined whether two calls supplying the same argument values will
379 * return the same ItemType object.</p>
380 *
381 * @param name the attribute name
382 * @return the ItemType
383 * @throws SaxonApiException if the schema does not contain a global attribute declaration
384 * for the given name, or if the attribute declaration contains
385 * dangling references to other schema components
386 */
387
388 public ItemType getSchemaAttributeTest(QName name) throws SaxonApiException {
389 Configuration config = processor.getUnderlyingConfiguration();
390 StructuredQName nn = new StructuredQName("", name.getNamespaceURI(), name.getLocalName());
391 SchemaDeclaration decl = config.getAttributeDeclaration(nn);
392 if (decl == null) {
393 throw new SaxonApiException("No global declaration found for attribute " + name.getClarkName());
394 }
395 try {
396 NodeTest test = decl.makeSchemaNodeTest();
397 return new ConstructedItemType(test, processor);
398 } catch (MissingComponentException e) {
399 throw new SaxonApiException(e);
400 }
401 }
402
403 /**
404 * Get an ItemType that tests an attribute name and/or schema type. This is the
405 * equivalent of the XPath syntax <code>element(element-name, type)</code>
406 * <p>It is undefined whether two calls supplying the same argument values will
407 * return the same ItemType object.</p>
408 *
409 * @param name the element name, or null if there is no constraint on the name (equivalent to
410 * specifying <code>element(*, type)</code>)
411 * @param schemaType the name of the required schema type, or null of there is no constraint
412 * on the type (equivalent to specifying <code>element(name)</code>)
413 * @return the constructed ItemType
414 * @throws SaxonApiException if the schema does not contain a global element declaration
415 * for the given name
416 */
417
418 public ItemType getAttributeTest(QName name, QName schemaType) throws SaxonApiException {
419 NameTest nameTest = null;
420 ContentTypeTest contentTest = null;
421 Configuration config = processor.getUnderlyingConfiguration();
422 if (name != null) {
423 int attributeFP = config.getNamePool().allocateFingerprint(name.getNamespaceURI(), name.getLocalName());
424 nameTest = new NameTest(Type.ATTRIBUTE, attributeFP, config.getNamePool());
425 }
426 if (schemaType != null) {
427 SchemaType type = config.getSchemaType(
428 new StructuredQName("", schemaType.getNamespaceURI(), schemaType.getLocalName()));
429 if (type == null) {
430 throw new SaxonApiException("Unknown schema type " + schemaType.getClarkName());
431 }
432 contentTest = new ContentTypeTest(Type.ATTRIBUTE, type, config, false);
433 }
434 if (contentTest == null) {
435 if (nameTest == null) {
436 return getNodeKindTest(XdmNodeKind.ATTRIBUTE);
437 } else {
438 return new ConstructedItemType(nameTest, processor);
439 }
440 } else {
441 if (nameTest == null) {
442 return new ConstructedItemType(contentTest, processor);
443 } else {
444 CombinedNodeTest combo = new CombinedNodeTest(
445 nameTest,
446 Token.INTERSECT,
447 contentTest);
448 return new ConstructedItemType(combo, processor);
449 }
450 }
451 }
452
453 /**
454 * Make an ItemType representing a document-node() test with an embedded element test.
455 * This reflects the XPath syntax <code>document-node(element(N, T))</code> or
456 * <code>document-node(schema-element(N))</code>.
457 * <p>It is undefined whether two calls supplying the same argument values will
458 * return the same ItemType object.</p>
459 *
460 * @param elementTest the elementTest. An IllegalArgumentException is thrown if the supplied
461 * ItemTest is not an elementTest or schemaElementTest.
462 * @return a new ItemType representing the document test
463 */
464
465 public ItemType getDocumentTest(ItemType elementTest) {
466 net.sf.saxon.type.ItemType test = elementTest.getUnderlyingItemType();
467 if (test.getPrimitiveType() != Type.ELEMENT) {
468 throw new IllegalArgumentException("Supplied itemType is not an element test");
469 }
470 DocumentNodeTest docTest = new DocumentNodeTest((NodeTest) test);
471 return new ConstructedItemType(docTest, processor);
472 }
473
474 /**
475 * Get an ItemType representing the type of a Java object when used as an external object
476 * for use in conjunction with calls on extension/external functions.
477 *
478 * @param externalClass a Java class
479 * @return the ItemType representing the type of external objects of this class
480 */
481
482 public ItemType getExternalObjectType(Class externalClass) {
483 JavaExternalObjectType type = processor.getUnderlyingConfiguration().getJavaExternalObjectType(externalClass);
484 return new ConstructedItemType(type, processor);
485 }
486
487 /**
488 * Factory method to construct an "external object". This is an XDM value that wraps a Java
489 * object. Such values can be passed as parameters to stylesheets or queries, for use in conjunction
490 * with external (extension) functions.
491 * <p>Changed in Saxon 9.5 to return XdmItem rather than XdmAtomicValue, since wrapped
492 * external objects are now modelled as a separate type of item, rather than as an atomic value.</p>
493 *
494 * @param object the value to be wrapped as an external object. Must not be null.
495 * @return the object, wrapped as an XdmItem
496 */
497
498 public XdmItem getExternalObject(Object object) {
499 return (XdmItem) XdmItem.wrap(new ObjectValue<>(object));
500 }
501
502 /**
503 * Obtain a map type, that is a type for XDM maps with a given key type and value type
504 * @param keyType the type of the keys in the map
505 * @param valueType the type of the values in the map
506 * @return the required map type
507 */
508
509 public ItemType getMapType(ItemType keyType, SequenceType valueType) {
510 if (!(keyType.getUnderlyingItemType() instanceof AtomicType)) {
511 throw new IllegalArgumentException("Map key must be atomic");
512 }
513 return new ConstructedItemType(
514 new MapType((AtomicType)keyType.getUnderlyingItemType(), valueType.getUnderlyingSequenceType()), processor);
515 }
516
517 /**
518 * Obtain an array type, that is a type for XDM arrays with a given member type
519 *
520 * @param memberType the type of the members in the array
521 * @return the required array type
522 */
523
524 public ItemType getArrayType(SequenceType memberType) {
525 return new ConstructedItemType(new ArrayItemType(memberType.getUnderlyingSequenceType()), processor);
526 }
527
528 /**
529 * Factory method to construct a map item from a Java map
530 * @param map the input map. The contents of this map are shallow-copied, so subsequent changes to the
531 * Java map do not affect the XDM map. The keys in the map must be convertible to atomic values,
532 * and the values in the map must be convertible to XDM values.
533 * @return an XDM item representing the contents of the map. This is of type function item
534 * because XDM maps are functions (from the key to the value).
535 * @throws SaxonApiException if there are values that cannot be converted.
536 * @since 9.6
537 */
538
539 public XdmMap newMap(Map<?, ?> map) throws SaxonApiException {
540 try {
541 return XdmMap.makeMap(map);
542 } catch (IllegalArgumentException e) {
543 throw new SaxonApiException(e);
544 }
545 }
546
547 /**
548 * Get an ItemType representing the type of a supplied XdmItem.
549 *
550 * <p>If the supplied item is
551 * an atomic value, the returned ItemType will reflect the most specific atomic type of the
552 * item.</p>
553 *
554 * <p>If the supplied item is a node, the returned item type will reflect the node kind,
555 * and if the node has a name, then its name. It will not reflect the type annotation.</p>
556 *
557 * <p>For a map, the result is {@link net.sf.saxon.s9api.ItemType#ANY_MAP}. For an array, the result is
558 * {@link net.sf.saxon.s9api.ItemType#ANY_ARRAY}. For any other function, it is {@link net.sf.saxon.s9api.ItemType#ANY_FUNCTION}.</p>
559 *
560 * <p>If the item is an external object, a suitable item type object is constructed.</p>
561 *
562 * <p>Future versions of Saxon may return a more precise type.</p>
563 *
564 * @param item the supplied item whose type is required
565 * @return the type of the supplied item
566 */
567
568 public ItemType getItemType(XdmItem item) {
569 if (item.isAtomicValue()) {
570 AtomicValue value = (AtomicValue) item.getUnderlyingValue();
571 AtomicType type = value.getItemType();
572 return new ConstructedItemType(type, processor);
573 } else if (item.getUnderlyingValue() instanceof NodeInfo) {
574 NodeInfo node = (NodeInfo) item.getUnderlyingValue();
575 int kind = node.getNodeKind();
576 if (node.getLocalPart().isEmpty()) {
577 return new ConstructedItemType(NodeKindTest.makeNodeKindTest(kind), processor);
578 } else {
579 return new ConstructedItemType(new SameNameTest(node), processor);
580 }
581 } else {
582 Item it = item.getUnderlyingValue();
583 if (it instanceof MapItem) {
584 return ItemType.ANY_MAP;
585 } else if (it instanceof ArrayItem) {
586 return ItemType.ANY_ARRAY;
587 } else if (it instanceof ExternalObject) {
588 return new ConstructedItemType(ExternalObjectType.THE_INSTANCE, processor);
589 } else {
590 return ItemType.ANY_FUNCTION;
591 }
592 }
593 }
594
595 /**
596 * Obtain a s9api {@code ItemType} corresponding to an underlying instance of
597 * {@link net.sf.saxon.type.ItemType}
598 *
599 * @param it the supplied instance of {@link net.sf.saxon.type.ItemType}
600 * @return a corresponding instance of {@link ItemType}
601 * @since 10.0
602 */
603
604 public ItemType exposeItemType(net.sf.saxon.type.ItemType it) {
605 return new ConstructedItemType(it, processor);
606 }
607
608 }
609