| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 31 |
|
| 32 |
|
| 33 |
public class ItemTypeFactory { |
| 34 |
|
| 35 |
private Processor processor; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public ItemTypeFactory(Processor processor) { |
| 46 |
this.processor = processor; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 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 |
|
| 91 |
|
| 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 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 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 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 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 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 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 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
public ItemType getElementTest( 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 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 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 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 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 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 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 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 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 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
public XdmItem getExternalObject(Object object) { |
| 499 |
return (XdmItem) XdmItem.wrap(new ObjectValue<>(object)); |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 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 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
public ItemType getArrayType(SequenceType memberType) { |
| 525 |
return new ConstructedItemType(new ArrayItemType(memberType.getUnderlyingSequenceType()), processor); |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 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 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 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 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
public ItemType exposeItemType(net.sf.saxon.type.ItemType it) { |
| 605 |
return new ConstructedItemType(it, processor); |
| 606 |
} |
| 607 |
|
| 608 |
} |
| 609 |
|