1 package net.sf.saxon.type; 2 3 import net.sf.saxon.Err; 4 import net.sf.saxon.ConversionContext; 5 import net.sf.saxon.expr.Expression; 6 import net.sf.saxon.expr.StaticContext; 7 import net.sf.saxon.functions.NormalizeSpace; 8 import net.sf.saxon.instruct.ValueOf; 9 import net.sf.saxon.om.*; 10 import net.sf.saxon.style.StandardNames; 11 import net.sf.saxon.trans.DynamicError; 12 import net.sf.saxon.trans.XPathException; 13 import net.sf.saxon.value.*; 14 15 import java.io.Serializable ; 16 17 21 22 public class BuiltInAtomicType implements AtomicType, Serializable { 23 24 int fingerprint; 25 int baseFingerprint = -1; 26 27 public BuiltInAtomicType() {} 28 29 public BuiltInAtomicType(int fingerprint) { 30 this.fingerprint = fingerprint; 31 } 32 33 37 38 public AtomicType getCommonAtomicType() { 39 return this; 40 } 41 42 45 public final int getValidationStatus() { 46 return VALIDATED; 47 } 48 49 55 56 public final int getBlock() { 57 return 0; 58 } 59 60 66 67 public final int getDerivationMethod() { 68 return SchemaType.DERIVATION_RESTRICTION; 69 } 70 71 78 79 public final boolean allowsDerivation(int derivation) { 80 return true; 81 } 82 83 public final void setBaseTypeFingerprint(int baseFingerprint) { 84 this.baseFingerprint = baseFingerprint; 85 } 86 87 92 93 public int getFingerprint() { 94 return fingerprint; 95 } 96 97 102 103 public int getNameCode() { 104 return fingerprint; 105 } 106 107 112 113 public String getDisplayName() { 114 return StandardNames.getDisplayName(fingerprint); 115 } 116 117 122 123 public final boolean isComplexType() { 124 return false; 125 } 126 127 135 136 public final SchemaType getBaseType() { 137 if (baseFingerprint == -1) { 138 return null; 139 } else { 140 return BuiltInSchemaFactory.getSchemaType(baseFingerprint); 141 } 142 } 143 144 150 151 public boolean matchesItem(Item item) { 152 if (item instanceof AtomicValue) { 153 AtomicValue value = (AtomicValue)item; 154 AtomicType type = (AtomicType)value.getItemType(); 155 if (type.getFingerprint()==this.getFingerprint()) { 156 return true; 159 } 160 return Type.isSubType(type, this); 161 } else { 162 return false; 163 } 164 } 165 166 174 175 public ItemType getSuperType() { 176 SchemaType base = getBaseType(); 177 if (base instanceof AnySimpleType) { 178 return AnyItemType.getInstance(); 179 } else { 180 return (ItemType)base; 181 } 182 } 183 184 192 193 public ItemType getPrimitiveItemType() { 194 if (Type.isPrimitiveType(getFingerprint())) { 195 return this; 196 } else { 197 ItemType s = (ItemType)getBaseType(); 198 if (s instanceof AtomicType) { 199 return s.getPrimitiveItemType(); 200 } else { 201 return this; 202 } 203 } 204 } 205 206 214 215 public int getPrimitiveType() { 216 int x = getFingerprint(); 217 if (Type.isPrimitiveType(x)) { 218 return x; 219 } else { 220 SchemaType s = getBaseType(); 221 if (s instanceof AtomicType) { 222 return ((AtomicType)s).getPrimitiveType(); 223 } else { 224 return this.getFingerprint(); 225 } 226 } 227 } 228 229 232 233 public boolean isAllowedInBasicXSLT() { 234 int fp = getFingerprint(); 235 return (Type.isPrimitiveType(fp) && fp != StandardNames.XS_NOTATION); 236 } 237 238 242 243 public String toString(NamePool pool) { 244 return getDisplayName(); 245 } 246 247 251 252 public AtomicType getAtomizedItemType() { 253 return this; 254 } 255 256 264 265 public SchemaType getKnownBaseType() { 266 return getBaseType(); 267 } 268 269 274 275 public boolean isSameType(SchemaType other) { 276 return (other.getFingerprint() == this.getFingerprint()); 277 } 278 279 public String getDescription() { 280 return getDisplayName(); 281 } 282 283 public String toString() { 284 return getDisplayName(); 285 } 286 287 294 295 public void isTypeDerivationOK(SchemaType type, int block) throws SchemaException, ValidationException { 296 } 298 299 304 305 public final boolean isSimpleType() { 306 return true; 307 } 308 309 313 314 public boolean isAtomicType() { 315 return true; 316 } 317 318 319 325 326 public boolean isListType() { 327 return false; 328 } 329 330 335 336 public boolean isUnionType() { 337 return false; 338 } 339 340 345 346 public int getWhitespaceAction() { 347 if (getPrimitiveType() == Type.STRING) { 348 if (Type.isSubType(this, 349 (ItemType)BuiltInSchemaFactory.getSchemaType(StandardNames.XS_TOKEN))) { 350 return Whitespace.COLLAPSE; 351 } else if (Type.isSubType(this, 352 (ItemType)BuiltInSchemaFactory.getSchemaType(StandardNames.XS_NORMALIZED_STRING))) { 353 return Whitespace.REPLACE; 354 } else { 355 return Whitespace.PRESERVE; 356 } 357 } else { 358 return Whitespace.COLLAPSE; 359 } 360 } 361 362 368 369 public CharSequence applyWhitespaceNormalization(CharSequence value) throws ValidationException { 370 int action = getWhitespaceAction(); 371 switch (action) { 372 case Whitespace.PRESERVE: 373 return value; 374 case Whitespace.REPLACE: 375 FastStringBuffer sb = new FastStringBuffer(value.length()); 376 for (int i = 0; i < value.length(); i++) { 377 if ("\n\r\t".indexOf(value.charAt(i)) >= 0) { 378 sb.append(' '); 379 } else { 380 sb.append(value.charAt(i)); 381 } 382 } 383 return sb; 384 case Whitespace.COLLAPSE: 385 return NormalizeSpace.normalize(value.toString()); 386 default: 387 throw new IllegalArgumentException ("Unknown whitespace facet value"); 388 } 389 } 390 391 396 public SchemaType getBuiltInBaseType() throws ValidationException { 397 BuiltInAtomicType base = this; 398 while ((base != null) && (base.getFingerprint() > 1023)) { 399 base = (BuiltInAtomicType)base.getBaseType(); 400 } 401 return base; 402 } 403 404 410 411 public boolean isNamespaceSensitive() { 412 BuiltInAtomicType base = this; 413 int fp = base.getFingerprint(); 414 while (fp > 1023) { 415 base = (BuiltInAtomicType)base.getBaseType(); 416 fp = base.getFingerprint(); 417 } 418 419 if (fp == StandardNames.XS_QNAME || fp == StandardNames.XS_NOTATION) { 420 return true; 421 } 422 return false; 423 } 424 425 438 439 public ValidationException validateContent(CharSequence value, NamespaceResolver nsResolver, ConversionContext conversion) { 440 int f = getFingerprint(); 441 if (f==StandardNames.XS_STRING || 442 f==StandardNames.XS_ANY_SIMPLE_TYPE || 443 f==StandardNames.XDT_UNTYPED_ATOMIC || 444 f==StandardNames.XDT_ANY_ATOMIC_TYPE) { 445 return null; 446 } 447 ValidationException result = null; 448 if (isNamespaceSensitive()) { 449 if (nsResolver == null) { 450 throw new UnsupportedOperationException ("Cannot validate a QName without a namespace resolver"); 451 } 452 try { 453 String [] parts = Name.getQNameParts(value.toString()); 454 String uri = nsResolver.getURIForPrefix(parts[0], true); 455 if (uri == null) { 456 result = new ValidationException("Namespace prefix " + Err.wrap(parts[0]) + 457 " has not been declared"); 458 } 459 new QNameValue(parts[0], uri, parts[1]); 460 } catch (QNameException err) { 461 result = new ValidationException("Invalid lexical QName " + Err.wrap(value)); 462 } catch (XPathException err) { 463 result = new ValidationException(err.getMessage()); 464 } 465 } else { 466 467 Value v = StringValue.convertStringToBuiltInType(value, this, true); 468 if (v instanceof ValidationErrorValue) { 469 result = new ValidationException("Value " + Err.wrap(value, Err.VALUE) + " is invalid for type " 470 + getDisplayName() + ". " + ((ValidationErrorValue)v).getException().getMessage()); 471 } 472 } 473 return result; 474 } 475 476 483 484 public final SequenceIterator getTypedValue(NodeInfo node) 485 throws XPathException { 486 try { 487 return getTypedValue(node.getStringValue(), new InscopeNamespaceResolver(node), node.getConfiguration()); 488 } catch (ValidationException err) { 489 throw new DynamicError("Internal error: value doesn't match its type annotation. " + err.getMessage()); 490 } 491 } 492 493 503 504 public Value atomize(NodeInfo node) throws XPathException { 505 if (fingerprint == StandardNames.XS_STRING) { 507 return StringValue.makeStringValue(node.getStringValueCS()); 508 } else if (fingerprint == StandardNames.XDT_UNTYPED_ATOMIC) { 509 return new UntypedAtomicValue(node.getStringValueCS()); 510 } else if (isNamespaceSensitive()) { 511 try { 512 NamespaceResolver resolver = new InscopeNamespaceResolver(node); 513 String [] parts = Name.getQNameParts(node.getStringValueCS()); 514 String uri = resolver.getURIForPrefix(parts[0], true); 515 if (uri == null) { 516 throw new ValidationException("Namespace prefix " + Err.wrap(parts[0]) + 517 " has not been declared"); 518 } 519 return new QNameValue(parts[0], uri, parts[1]); 520 } catch (QNameException err) { 521 throw new ValidationException("Invalid lexical QName " + Err.wrap(node.getStringValueCS())); 522 } catch (XPathException err) { 523 throw new ValidationException(err.getMessage()); 524 } 525 } 526 AtomicValue val = StringValue.convertStringToBuiltInType(node.getStringValueCS(), this, true); 527 if (val instanceof ValidationErrorValue) { 528 throw ((ValidationErrorValue)val).getException(); 529 } 530 return val; 531 } 532 533 545 546 public SequenceIterator getTypedValue(CharSequence value, NamespaceResolver resolver, ConversionContext conversion) 547 throws ValidationException { 548 if (fingerprint == StandardNames.XS_STRING) { 550 return SingletonIterator.makeIterator(StringValue.makeStringValue(value)); 551 } else if (fingerprint == StandardNames.XDT_UNTYPED_ATOMIC) { 552 return SingletonIterator.makeIterator(new UntypedAtomicValue(value)); 553 } else if (isNamespaceSensitive()) { 554 try { 555 String [] parts = Name.getQNameParts(value.toString()); 556 String uri = resolver.getURIForPrefix(parts[0], true); 557 if (uri == null) { 558 throw new ValidationException("Namespace prefix " + Err.wrap(parts[0]) + 559 " has not been declared"); 560 } 561 return SingletonIterator.makeIterator(new QNameValue(parts[0], uri, parts[1])); 562 } catch (QNameException err) { 563 throw new ValidationException("Invalid lexical QName " + Err.wrap(value)); 564 } catch (XPathException err) { 565 throw new ValidationException(err.getMessage()); 566 } 567 } 568 AtomicValue val = StringValue.convertStringToBuiltInType(value, this, true); 569 if (val instanceof ValidationErrorValue) { 570 throw ((ValidationErrorValue)val).getException(); 571 } 572 return SingletonIterator.makeIterator(val); 573 } 574 575 576 586 587 public AtomicValue makeDerivedValue(AtomicValue primValue, CharSequence lexicalValue, boolean validate) { 588 throw new UnsupportedOperationException ("makeDerivedValue is not supported for built-in types"); 589 } 590 591 602 603 public void analyzeContentExpression(Expression expression, int kind, StaticContext env) throws XPathException { 604 analyzeContentExpression(this, expression, env, kind); 605 } 606 607 617 618 public static void analyzeContentExpression(SimpleType simpleType, Expression expression, StaticContext env, int kind) 619 throws XPathException { 620 if (kind == Type.ELEMENT) { 621 expression.checkPermittedContents(simpleType, env, true); 622 } else if (kind == Type.ATTRIBUTE) { 632 if (expression instanceof ValueOf || expression instanceof Value) { 634 expression.checkPermittedContents(simpleType, env, true); 635 } 636 } 637 } 638 } 639 640 | Popular Tags |