1 23 24 package org.xquark.xquery.typing; 25 26 import java.util.List ; 27 28 import org.xquark.schema.SimpleType; 29 import org.xquark.schema.Type; 30 import org.xquark.schema.datatypes.PrimitiveType; 31 import org.xquark.xquery.parser.XQueryExpression; 32 33 public abstract class QType implements QTypeVisitable, Cloneable { 34 private static final String RCSRevision = "$Revision: 1.10 $"; 35 private static final String RCSName = "$Name: $"; 36 37 public static final byte OCC_0_0 = 0; 39 public static final byte OCC_0_1 = 1; 40 public static final byte OCC_1_1 = 2; 41 public static final byte OCC_0_N = 3; 42 public static final byte OCC_1_N = 4; 43 44 public static final byte ELEMENT = 1; 46 public static final byte ATOM = 2; 47 public static final byte ATTRIBUTE = 3; 48 public static final byte UNION = 4; 49 public static final byte SEQUENCE = 5; 50 public static final byte COMMENT = 6; 51 public static final byte PROCESSING = 7; 52 public static final byte CDATA = 8; 53 public static final byte DOCUMENT = 9; 54 public static final byte TEXT = 10; 55 56 58 public static final byte PRIMITIVE_anySimpleType = 0; 59 public static final byte PRIMITIVE_string = 1; 60 public static final byte PRIMITIVE_float = 2; 61 public static final byte PRIMITIVE_double = 3; 62 public static final byte PRIMITIVE_decimal = 4; 63 public static final byte PRIMITIVE_duration = 5; 64 public static final byte PRIMITIVE_yearMonthDuration = 6; 65 public static final byte PRIMITIVE_dayTimeDuration = 7; 66 public static final byte PRIMITIVE_dateTime = 8; 67 public static final byte PRIMITIVE_time = 9; 68 public static final byte PRIMITIVE_date = 10; 69 public static final byte PRIMITIVE_gYearMonth = 11; 70 public static final byte PRIMITIVE_gYear = 12; 71 public static final byte PRIMITIVE_gMonthDay = 13; 72 public static final byte PRIMITIVE_gDay = 14; 73 public static final byte PRIMITIVE_gMonth = 15; 74 public static final byte PRIMITIVE_boolean = 16; 75 public static final byte PRIMITIVE_base64Binary = 17; 76 public static final byte PRIMITIVE_hexBinary = 18; 77 public static final byte PRIMITIVE_anyURI = 19; 78 public static final byte PRIMITIVE_Qname = 20; 79 public static final byte PRIMITIVE_NOTATION = 21; 80 81 130 public static final boolean castTab[][] = { { 132 134 true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, false, false }, { 135 136 true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, false, false }, { 137 138 true, true, true, true, true, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false }, { 139 140 true, true, true, true, true, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false }, { 141 142 true, true, true, true, true, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false }, { 143 144 true, true, false, false, false, true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false }, { 145 146 true, true, false, false, false, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false }, { 147 148 true, true, false, false, false, true, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false }, { 149 150 true, true, false, false, false, false, false, false, true, true, true, true, true, true, true, true, false, false, false, false, false, false }, { 151 152 true, true, false, false, false, false, false, false, true, true, false, false, false, false, false, false, false, false, false, false, false, false }, { 153 154 true, true, false, false, false, false, false, false, true, false, true, true, true, true, true, true, false, false, false, false, false, false }, { 155 156 true, true, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false }, { 157 158 true, true, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false }, { 159 160 true, true, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false }, { 161 162 true, true, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false }, { 163 164 true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false }, { 165 166 true, true, true, true, true, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false }, { 167 168 true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, true, false, false, false, false }, { 169 170 true, true, true, true, true, false, false, false, false, false, false, false, false, false, false, false, true, false, true, false, false, false }, { 171 172 true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false }, { 173 174 false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false }, { 175 176 false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true } 177 }; 178 179 protected byte subclass = 0; 180 protected byte occurence = OCC_1_1; 181 184 public static byte computeOccurence(byte occ1, byte occ2) { 185 switch (occ1) { 186 case OCC_1_1 : 187 return occ2; 188 case OCC_0_1 : 189 switch (occ2) { 190 case OCC_1_1 : 191 case OCC_0_1 : 192 return OCC_0_1; 193 case OCC_1_N : 194 case OCC_0_N : 195 return OCC_0_N; 196 } 197 case OCC_1_N : 198 switch (occ2) { 199 case OCC_1_1 : 200 case OCC_1_N : 201 return OCC_1_N; 202 case OCC_0_1 : 203 case OCC_0_N : 204 return OCC_0_N; 205 } 206 default: 207 return occ1; 208 } 209 } 210 211 public static byte getOccurence(int min, int max) { 212 if (min > 0) { 213 if (max > 1) 214 return QType.OCC_1_N; 215 else 216 return QType.OCC_1_1; 217 } else { 218 if (max > 1) 219 return QType.OCC_0_N; 220 else 221 return QType.OCC_0_1; 222 } 223 } 224 225 public byte getOccurence() { 227 return occurence; 228 } 229 public QType changeOccurence(byte occurence) { 230 QType qtype = null; 231 try { 232 qtype = (QType) clone(); 233 } catch (CloneNotSupportedException cnse) {} 234 qtype.occurence = occurence; 235 return qtype; 236 } 237 public int getMaxOccurence() { 238 switch (occurence) { 239 case OCC_0_0 : 240 return 0; 241 case OCC_1_1 : 242 case OCC_0_1 : 243 return 1; 244 case OCC_0_N : 245 case OCC_1_N : 246 return Integer.MAX_VALUE; 247 } 248 return -1; 249 } 250 public int getMinOccurence() { 251 switch (occurence) { 252 case OCC_0_N : 253 case OCC_0_1 : 254 case OCC_0_0 : 255 return 0; 256 case OCC_1_1 : 257 case OCC_1_N : 258 return 1; 259 } 260 return -1; 261 } 262 263 public byte getSubClass() { 264 return subclass; 265 } 266 267 public SimpleType getSimpleType() { 272 return null; 273 } 274 public Type getType() { 275 return null; 276 } 277 public List getList() { 281 return null; 282 } 283 public XQueryExpression getName() { 284 return null; 285 } 286 290 abstract public Object clone() throws CloneNotSupportedException ; 291 292 public boolean isNumeric() { 300 return false; 301 } 302 303 public boolean isBoolean() { 311 return false; 312 } 313 314 public boolean isInteger() { 322 return false; 323 } 324 325 public boolean isString() { 333 return false; 334 } 335 336 public boolean isAtom() { 342 return false; 343 } 344 345 public boolean isText() { 352 return false; 353 } 354 355 public boolean isDate() { 363 return false; 364 } 365 366 public boolean isDuration() { 368 return false; 369 } 370 371 public boolean isAttribute() { 377 return false; 378 } 379 public boolean isOnlyAttribute() { 385 return isAttribute(); 386 } 387 388 public boolean isIDAttribute() { 394 return false; 395 } 396 397 public boolean isNode() { 407 return false; 408 } 409 410 public boolean isSimpleTypeNode() { 417 return false; 418 } 419 420 427 public boolean isMultiple() { 432 switch (getOccurence()) { 433 case OCC_0_0 : 434 case OCC_1_1 : 435 case OCC_0_1 : 436 return false; 437 case OCC_0_N : 438 case OCC_1_N : 439 return true; 440 } 441 return false; 442 } 443 444 public boolean isAtomic() { 450 return false; 451 } 452 453 public boolean isMixed() { 459 return false; 460 } 461 462 public boolean isElement() { 468 return false; 469 } 470 471 public boolean isDocument() { 475 return false; 476 } 477 478 public boolean isQName() { 481 return false; 482 } 483 484 public QType applyDATAFunction() throws TypeException { 485 return null; 486 } 487 488 public boolean canBeComparedTo(QType compType) { 489 return false; 490 } 491 492 protected boolean compare(Type type1, Type type2) { 493 if (type1 == null || type2 == null) 494 return true; 495 if (type1 == null && type2 != null) 496 return false; 497 if (type2 == null && type1 != null) 498 return false; 499 if (type1 == null || type2 == null || (type1.getName() == null && type2.getName() != null) || (type1.getName() != null && type2.getName() == null)) 500 return false; 501 return ((type1.getName() == null && type2.getName() == null) || (type1.getName() != null && type1.getName().equals(type2.getName()))) && ((type1.getNamespace() == null && type2.getNamespace() == null) || (type1.getNamespace() != null && type1.getNamespace().equals(type2.getNamespace()))); 502 } 503 504 505 protected boolean isNumeric(Type type) { 506 SimpleType stype = type.getValueType(); 507 if (stype == null) { 508 if (type.getName() != null && type.getName().equals(TypeVisitor.SC_ANYTYPE)) 509 return true; 510 return false; 511 } 512 switch (stype.getPrimitive().getType()) { 513 517 case PrimitiveType.DECIMAL : 518 case PrimitiveType.DOUBLE : 519 case PrimitiveType.FLOAT : 520 return true; 521 544 default : 545 break; 546 } 547 return false; 548 } 549 550 protected boolean isInteger(Type type) { 551 SimpleType stype = type.getValueType(); 552 if (stype == null) 553 return false; 554 switch (stype.getPrimitive().getType()) { 555 559 case PrimitiveType.DECIMAL : 560 if (stype.getPrimitive().getFractionDigits() == 0) 561 return true; 562 587 default : 588 break; 589 } 590 return false; 591 } 592 593 protected boolean isString(Type type) { 594 SimpleType stype = type.getValueType(); 595 if (stype == null) 596 return false; 597 switch (stype.getPrimitive().getType()) { 598 case PrimitiveType.STRING : 599 case PrimitiveType.ANYSIMPLETYPE : 600 return true; 601 628 default : 629 break; 630 } 631 return false; 632 } 633 634 protected boolean isDate(Type type) { 635 SimpleType stype = type.getValueType(); 636 if (stype == null) 637 return false; 638 switch (stype.getPrimitive().getType()) { 639 647 case PrimitiveType.DATE_TIME : 648 case PrimitiveType.DATE : 650 return true; 651 670 default : 671 break; 672 } 673 return false; 674 } 675 676 protected boolean isBoolean(Type type) { 677 SimpleType stype = type.getValueType(); 678 if (stype == null) 679 return false; 680 switch (stype.getPrimitive().getType()) { 681 case PrimitiveType.BOOLEAN : 683 return true; 684 710 default : 711 break; 712 } 713 return false; 714 } 715 716 protected boolean isQName(Type type) { 717 SimpleType stype = type.getValueType(); 718 if (stype == null) 719 return false; 720 switch (stype.getPrimitive().getType()) { 721 case PrimitiveType.STRING : 722 740 case PrimitiveType.QNAME : 741 return true; 742 752 default : 753 break; 754 } 755 return false; 756 } 757 758 protected boolean canBeCompared(int primitiveType1, int primitiveType2) { 759 if (primitiveType1 == PrimitiveType.ANYSIMPLETYPE || primitiveType2 == PrimitiveType.ANYSIMPLETYPE) 760 return true; 761 if (primitiveType1 == primitiveType2) 762 return true; 763 switch (primitiveType1) { 764 case PrimitiveType.DECIMAL : 767 case PrimitiveType.DOUBLE : 768 case PrimitiveType.FLOAT : 769 if (primitiveType2 == PrimitiveType.DECIMAL) 770 return true; 771 if (primitiveType2 == PrimitiveType.DOUBLE) 772 return true; 773 if (primitiveType2 == PrimitiveType.FLOAT) 774 return true; 775 break; 776 case PrimitiveType.DATE_TIME : 778 case PrimitiveType.TIME : 779 case PrimitiveType.DATE : 780 case PrimitiveType.GYEAR_MONTH : 781 case PrimitiveType.GYEAR : 782 case PrimitiveType.GMONTH_DAY : 783 case PrimitiveType.GDAY : 784 case PrimitiveType.GMONTH : 785 if (primitiveType2 == PrimitiveType.DATE_TIME) 786 return true; 787 if (primitiveType2 == PrimitiveType.TIME) 788 return true; 789 if (primitiveType2 == PrimitiveType.DATE) 790 return true; 791 if (primitiveType2 == PrimitiveType.GYEAR_MONTH) 792 return true; 793 if (primitiveType2 == PrimitiveType.GYEAR) 794 return true; 795 if (primitiveType2 == PrimitiveType.GMONTH_DAY) 796 return true; 797 if (primitiveType2 == PrimitiveType.GDAY) 798 return true; 799 if (primitiveType2 == PrimitiveType.GMONTH) 800 return true; 801 break; 802 default : 813 break; 814 } 815 return false; 816 } 817 818 public boolean canBeCastedInto(int castType) { 819 return false; 820 } 821 822 public int getCastType() { 823 return -1; 824 } 825 826 } 827 | Popular Tags |