1 16 17 package org.apache.xerces.impl.xs; 18 19 import org.apache.xerces.impl.dv.XSSimpleType; 20 import org.apache.xerces.xs.*; 21 import org.apache.xerces.impl.xs.models.XSCMValidator; 22 import org.apache.xerces.impl.xs.models.CMBuilder; 23 import org.apache.xerces.impl.xs.util.XSObjectListImpl; 24 import org.apache.xerces.impl.dv.xs.XSSimpleTypeDecl; 25 import org.w3c.dom.TypeInfo ; 26 27 37 public class XSComplexTypeDecl implements XSComplexTypeDefinition, TypeInfo { 38 39 String fName = null; 41 42 String fTargetNamespace = null; 44 45 XSTypeDefinition fBaseType = null; 47 48 short fDerivedBy = XSConstants.DERIVATION_RESTRICTION; 50 51 short fFinal = XSConstants.DERIVATION_NONE; 53 54 short fBlock = XSConstants.DERIVATION_NONE; 56 57 short fMiscFlags = 0; 60 61 XSAttributeGroupDecl fAttrGrp = null; 63 64 short fContentType = CONTENTTYPE_EMPTY; 66 67 XSSimpleType fXSSimpleType = null; 69 70 XSParticleDecl fParticle = null; 72 73 XSCMValidator fCMValidator = null; 75 76 XSObjectListImpl fAnnotations = null; 78 79 static final int DERIVATION_ANY = 0; 81 static final int DERIVATION_RESTRICTION = 1; 82 static final int DERIVATION_EXTENSION = 2; 83 static final int DERIVATION_UNION = 4; 84 static final int DERIVATION_LIST = 8; 85 86 public XSComplexTypeDecl() { 87 } 89 90 public void setValues(String name, String targetNamespace, 91 XSTypeDefinition baseType, short derivedBy, short schemaFinal, 92 short block, short contentType, 93 boolean isAbstract, XSAttributeGroupDecl attrGrp, 94 XSSimpleType simpleType, XSParticleDecl particle, 95 XSObjectListImpl annotations) { 96 fTargetNamespace = targetNamespace; 97 fBaseType = baseType; 98 fDerivedBy = derivedBy; 99 fFinal = schemaFinal; 100 fBlock = block; 101 fContentType = contentType; 102 if(isAbstract) 103 fMiscFlags |= CT_IS_ABSTRACT; 104 fAttrGrp = attrGrp; 105 fXSSimpleType = simpleType; 106 fParticle = particle; 107 fAnnotations = annotations; 108 } 109 110 public void setName(String name) { 111 fName = name; 112 } 113 114 public short getTypeCategory() { 115 return COMPLEX_TYPE; 116 } 117 118 public String getTypeName() { 119 return fName; 120 } 121 122 public short getFinalSet(){ 123 return fFinal; 124 } 125 126 public String getTargetNamespace(){ 127 return fTargetNamespace; 128 } 129 130 private static final short CT_IS_ABSTRACT = 1; 132 private static final short CT_HAS_TYPE_ID = 2; 133 private static final short CT_IS_ANONYMOUS = 4; 134 135 137 public boolean containsTypeID () { 138 return((fMiscFlags & CT_HAS_TYPE_ID) != 0); 139 } 140 141 public void setIsAbstractType() { 142 fMiscFlags |= CT_IS_ABSTRACT; 143 } 144 public void setContainsTypeID() { 145 fMiscFlags |= CT_HAS_TYPE_ID; 146 } 147 public void setIsAnonymous() { 148 fMiscFlags |= CT_IS_ANONYMOUS; 149 } 150 151 public synchronized XSCMValidator getContentModel(CMBuilder cmBuilder) { 152 if (fCMValidator == null) 153 fCMValidator = cmBuilder.getContentModel(this); 154 155 return fCMValidator; 156 } 157 158 160 public XSAttributeGroupDecl getAttrGrp() { 162 return fAttrGrp; 163 } 164 165 public String toString() { 166 StringBuffer str = new StringBuffer (); 167 appendTypeInfo(str); 168 return str.toString(); 169 } 170 171 void appendTypeInfo(StringBuffer str) { 172 String contentType[] = {"EMPTY", "SIMPLE", "ELEMENT", "MIXED"}; 173 String derivedBy[] = {"EMPTY", "EXTENSION", "RESTRICTION"}; 174 175 str.append("Complex type name='" + fTargetNamespace + "," + getTypeName() + "', "); 176 if (fBaseType != null) 177 str.append(" base type name='" + fBaseType.getName() + "', "); 178 179 str.append(" content type='" + contentType[fContentType] + "', "); 180 str.append(" isAbstract='" + getAbstract() + "', "); 181 str.append(" hasTypeId='" + containsTypeID() + "', "); 182 str.append(" final='" + fFinal + "', "); 183 str.append(" block='" + fBlock + "', "); 184 if (fParticle != null) 185 str.append(" particle='" + fParticle.toString() + "', "); 186 str.append(" derivedBy='" + derivedBy[fDerivedBy] + "'. "); 187 188 } 189 190 public boolean derivedFromType(XSTypeDefinition ancestor, short derivationMethod) { 191 if (ancestor == null) 193 return false; 194 if (ancestor == SchemaGrammar.fAnyType) 196 return true; 197 XSTypeDefinition type = this; 199 while (type != ancestor && type != SchemaGrammar.fAnySimpleType && type != SchemaGrammar.fAnyType) { type = type.getBaseType(); 203 } 204 205 return type == ancestor; 206 } 207 208 public boolean derivedFrom(String ancestorNS, String ancestorName, short derivationMethod) { 209 if (ancestorName == null) 211 return false; 212 if (ancestorNS != null && 214 ancestorNS.equals(SchemaSymbols.URI_SCHEMAFORSCHEMA) && 215 ancestorName.equals(SchemaSymbols.ATTVAL_ANYTYPE)) { 216 return true; 217 } 218 219 XSTypeDefinition type = this; 221 while (!(ancestorName.equals(type.getName()) && 222 ((ancestorNS == null && type.getNamespace() == null) || 223 (ancestorNS != null && ancestorNS.equals(type.getNamespace())))) && type != SchemaGrammar.fAnySimpleType && type != SchemaGrammar.fAnyType) { type = (XSTypeDefinition)type.getBaseType(); 227 } 228 229 return type != SchemaGrammar.fAnySimpleType && 230 type != SchemaGrammar.fAnyType; 231 } 232 233 248 public boolean isDOMDerivedFrom(String ancestorNS, String ancestorName, 249 int derivationMethod) { 250 if (ancestorName == null) 252 return false; 253 254 if (ancestorNS != null 256 && ancestorNS.equals(SchemaSymbols.URI_SCHEMAFORSCHEMA) 257 && ancestorName.equals(SchemaSymbols.ATTVAL_ANYTYPE) 258 && (derivationMethod == DERIVATION_RESTRICTION 259 && derivationMethod == DERIVATION_EXTENSION)) { 260 return true; 261 } 262 263 if ((derivationMethod & DERIVATION_RESTRICTION) != 0) { 265 if (isDerivedByRestriction(ancestorNS, ancestorName, 266 derivationMethod, this)) { 267 return true; 268 } 269 } 270 271 if ((derivationMethod & DERIVATION_EXTENSION) != 0) { 273 if (isDerivedByExtension(ancestorNS, ancestorName, 274 derivationMethod, this)) { 275 return true; 276 } 277 } 278 279 if ((((derivationMethod & DERIVATION_LIST) != 0) || ((derivationMethod & DERIVATION_UNION) != 0)) 281 && ((derivationMethod & DERIVATION_RESTRICTION) == 0) 282 && ((derivationMethod & DERIVATION_EXTENSION) == 0)) { 283 284 if (ancestorNS.equals(SchemaSymbols.URI_SCHEMAFORSCHEMA) 285 && ancestorName.equals(SchemaSymbols.ATTVAL_ANYTYPE)) { 286 ancestorName = SchemaSymbols.ATTVAL_ANYSIMPLETYPE; 287 } 288 289 if(!(fName.equals(SchemaSymbols.ATTVAL_ANYTYPE) 290 && fTargetNamespace.equals(SchemaSymbols.URI_SCHEMAFORSCHEMA))){ 291 if (fBaseType != null && fBaseType instanceof XSSimpleTypeDecl) { 292 293 return ((XSSimpleTypeDecl) fBaseType).isDOMDerivedFrom(ancestorNS, 294 ancestorName, derivationMethod); 295 } else if (fBaseType != null 296 && fBaseType instanceof XSComplexTypeDecl) { 297 return ((XSComplexTypeDecl) fBaseType).isDOMDerivedFrom( 298 ancestorNS, ancestorName, derivationMethod); 299 } 300 } 301 } 302 303 if (((derivationMethod & DERIVATION_EXTENSION) == 0) 307 && (((derivationMethod & DERIVATION_RESTRICTION) == 0) 308 && ((derivationMethod & DERIVATION_LIST) == 0) 309 && ((derivationMethod & DERIVATION_UNION) == 0))) { 310 return isDerivedByAny(ancestorNS, ancestorName, derivationMethod, this); 311 } 312 313 return false; 314 } 315 316 333 private boolean isDerivedByAny(String ancestorNS, String ancestorName, 334 int derivationMethod, XSTypeDefinition type) { 335 XSTypeDefinition oldType = null; 336 boolean derivedFrom = false; 337 while (type != null && type != oldType) { 338 339 if ((ancestorName.equals(type.getName())) 341 && ((ancestorNS == null && type.getNamespace() == null) 342 || (ancestorNS != null && ancestorNS.equals(type.getNamespace())))) { 343 derivedFrom = true; 344 break; 345 } 346 347 if (isDerivedByRestriction(ancestorNS, ancestorName, 350 derivationMethod, type)) { 351 return true; 352 } else if (!isDerivedByExtension(ancestorNS, ancestorName, 353 derivationMethod, type)) { 354 return true; 355 } 356 oldType = type; 357 type = type.getBaseType(); 358 } 359 360 return derivedFrom; 361 } 362 363 379 private boolean isDerivedByRestriction(String ancestorNS, 380 String ancestorName, int derivationMethod, XSTypeDefinition type) { 381 382 XSTypeDefinition oldType = null; 383 while (type != null && type != oldType) { 384 385 if (ancestorNS != null 387 && ancestorNS.equals(SchemaSymbols.URI_SCHEMAFORSCHEMA) 388 && ancestorName.equals(SchemaSymbols.ATTVAL_ANYSIMPLETYPE)) { 389 return false; 390 } 391 392 if ((ancestorName.equals(type.getName())) 395 && (ancestorNS != null && ancestorNS.equals(type.getNamespace())) 396 || ((type.getNamespace() == null && ancestorNS == null))) { 397 398 return true; 399 } 400 401 if (type instanceof XSSimpleTypeDecl) { 403 if (ancestorNS.equals(SchemaSymbols.URI_SCHEMAFORSCHEMA) 404 && ancestorName.equals(SchemaSymbols.ATTVAL_ANYTYPE)) { 405 ancestorName = SchemaSymbols.ATTVAL_ANYSIMPLETYPE; 406 } 407 return ((XSSimpleTypeDecl) type).isDOMDerivedFrom(ancestorNS, 408 ancestorName, derivationMethod); 409 } else { 410 if (((XSComplexTypeDecl) type).getDerivationMethod() != XSConstants.DERIVATION_RESTRICTION) { 414 return false; 415 } 416 } 417 oldType = type; 418 type = type.getBaseType(); 419 420 } 421 422 return false; 423 } 424 425 441 private boolean isDerivedByExtension(String ancestorNS, 442 String ancestorName, int derivationMethod, XSTypeDefinition type) { 443 444 boolean extension = false; 445 XSTypeDefinition oldType = null; 446 while (type != null && type != oldType) { 447 if (ancestorNS != null 449 && ancestorNS.equals(SchemaSymbols.URI_SCHEMAFORSCHEMA) 450 && ancestorName.equals(SchemaSymbols.ATTVAL_ANYSIMPLETYPE) 451 && SchemaSymbols.URI_SCHEMAFORSCHEMA.equals(type.getNamespace()) 452 && SchemaSymbols.ATTVAL_ANYTYPE.equals(type.getName())) { 453 break; 454 } 455 456 if ((ancestorName.equals(type.getName())) 457 && ((ancestorNS == null && type.getNamespace() == null) 458 || (ancestorNS != null && ancestorNS.equals(type.getNamespace())))) { 459 return extension; 461 } 462 463 if (type instanceof XSSimpleTypeDecl) { 465 if (ancestorNS.equals(SchemaSymbols.URI_SCHEMAFORSCHEMA) 466 && ancestorName.equals(SchemaSymbols.ATTVAL_ANYTYPE)) { 467 ancestorName = SchemaSymbols.ATTVAL_ANYSIMPLETYPE; 468 } 469 470 if ((derivationMethod & DERIVATION_EXTENSION) != 0) { 474 return extension 475 & ((XSSimpleTypeDecl) type).isDOMDerivedFrom( 476 ancestorNS, ancestorName, 477 (derivationMethod & DERIVATION_RESTRICTION)); 478 } else { 479 return extension 480 & ((XSSimpleTypeDecl) type).isDOMDerivedFrom( 481 ancestorNS, ancestorName, derivationMethod); 482 } 483 484 } else { 485 if (((XSComplexTypeDecl) type).getDerivationMethod() == XSConstants.DERIVATION_EXTENSION) { 489 extension = extension | true; 490 } 491 } 492 oldType = type; 493 type = type.getBaseType(); 494 } 495 496 return false; 497 } 498 499 500 501 public void reset(){ 502 fName = null; 503 fTargetNamespace = null; 504 fBaseType = null; 505 fDerivedBy = XSConstants.DERIVATION_RESTRICTION; 506 fFinal = XSConstants.DERIVATION_NONE; 507 fBlock = XSConstants.DERIVATION_NONE; 508 509 fMiscFlags = 0; 510 511 fAttrGrp.reset(); 513 fContentType = CONTENTTYPE_EMPTY; 514 fXSSimpleType = null; 515 fParticle = null; 516 fCMValidator = null; 517 if(fAnnotations != null) { 518 fAnnotations.clear(); 520 } 521 fAnnotations = null; 522 } 523 524 527 public short getType() { 528 return XSConstants.TYPE_DEFINITION; 529 } 530 531 535 public String getName() { 536 return getAnonymous() ? null : fName; 537 } 538 539 544 public boolean getAnonymous() { 545 return((fMiscFlags & CT_IS_ANONYMOUS) != 0); 546 } 547 548 553 public String getNamespace() { 554 return fTargetNamespace; 555 } 556 557 561 public XSTypeDefinition getBaseType() { 562 return fBaseType; 563 } 564 565 569 public short getDerivationMethod() { 570 return fDerivedBy; 571 } 572 573 581 public boolean isFinal(short derivation) { 582 return (fFinal & derivation) != 0; 583 } 584 585 592 public short getFinal() { 593 return fFinal; 594 } 595 596 601 public boolean getAbstract() { 602 return((fMiscFlags & CT_IS_ABSTRACT) != 0); 603 } 604 605 608 public XSObjectList getAttributeUses() { 609 return fAttrGrp.getAttributeUses(); 610 } 611 612 615 public XSWildcard getAttributeWildcard() { 616 return fAttrGrp.getAttributeWildcard(); 617 } 618 619 624 public short getContentType() { 625 return fContentType; 626 } 627 628 632 public XSSimpleTypeDefinition getSimpleType() { 633 return fXSSimpleType; 634 } 635 636 640 public XSParticle getParticle() { 641 return fParticle; 642 } 643 644 651 public boolean isProhibitedSubstitution(short prohibited) { 652 return (fBlock & prohibited) != 0; 653 } 654 655 660 public short getProhibitedSubstitutions() { 661 return fBlock; 662 } 663 664 667 public XSObjectList getAnnotations() { 668 return fAnnotations; 669 } 670 671 674 public XSNamespaceItem getNamespaceItem() { 675 return null; 677 } 678 679 682 public XSAttributeUse getAttributeUse(String namespace, String name) { 683 return fAttrGrp.getAttributeUse(namespace, name); 684 } 685 686 public String getTypeNamespace() { 687 return getNamespace(); 688 } 689 690 public boolean isDerivedFrom(String typeNamespaceArg, String typeNameArg, int derivationMethod) { 691 return isDOMDerivedFrom(typeNamespaceArg, typeNameArg, derivationMethod); 692 } 693 694 } | Popular Tags |