1 57 58 package com.sun.org.apache.xerces.internal.impl.xs; 59 60 import com.sun.org.apache.xerces.internal.xs.StringList; 61 import com.sun.org.apache.xerces.internal.xs.XSAnnotation; 62 import com.sun.org.apache.xerces.internal.xs.XSConstants; 63 import com.sun.org.apache.xerces.internal.xs.XSNamespaceItem; 64 import com.sun.org.apache.xerces.internal.xs.XSWildcard; 65 import com.sun.org.apache.xerces.internal.impl.xs.util.StringListImpl; 66 67 76 public class XSWildcardDecl implements XSWildcard { 77 78 public static final String ABSENT = null; 79 80 public short fType = NSCONSTRAINT_ANY; 82 public short fProcessContents = PC_STRICT; 84 public String [] fNamespaceList; 88 89 public XSAnnotationImpl fAnnotation = null; 91 92 109 112 public boolean allowNamespace(String namespace) { 113 115 if (fType == NSCONSTRAINT_ANY) 117 return true; 118 119 if (fType == NSCONSTRAINT_NOT) { 126 boolean found = false; 127 int listNum = fNamespaceList.length; 128 for (int i = 0; i < listNum && !found; i++) { 129 if (namespace == fNamespaceList[i]) 130 found = true; 131 } 132 133 if (!found) 134 return true; 135 } 136 137 if (fType == NSCONSTRAINT_LIST) { 139 int listNum = fNamespaceList.length; 140 for (int i = 0; i < listNum; i++) { 141 if (namespace == fNamespaceList[i]) 142 return true; 143 } 144 } 145 146 return false; 148 } 149 150 153 public boolean isSubsetOf(XSWildcardDecl superWildcard) { 154 if (superWildcard == null) 156 return false; 157 158 161 if (superWildcard.fType == NSCONSTRAINT_ANY) { 163 return true; 164 } 165 166 if (fType == NSCONSTRAINT_NOT) { 172 if (superWildcard.fType == NSCONSTRAINT_NOT && 173 fNamespaceList[0] == superWildcard.fNamespaceList[0]) { 174 return true; 175 } 176 } 177 178 if (fType == NSCONSTRAINT_LIST) { 189 if (superWildcard.fType == NSCONSTRAINT_LIST && 190 subset2sets(fNamespaceList, superWildcard.fNamespaceList)) { 191 return true; 192 } 193 194 if (superWildcard.fType == NSCONSTRAINT_NOT && 195 !elementInSet(superWildcard.fNamespaceList[0], fNamespaceList) && 196 !elementInSet(ABSENT, fNamespaceList)) { 197 return true; 198 } 199 } 200 201 return false; 203 204 } 206 209 public boolean weakerProcessContents(XSWildcardDecl superWildcard) { 210 return fProcessContents == XSWildcardDecl.PC_LAX && 211 superWildcard.fProcessContents == XSWildcardDecl.PC_STRICT || 212 fProcessContents == XSWildcardDecl.PC_SKIP && 213 superWildcard.fProcessContents != XSWildcardDecl.PC_SKIP; 214 } 215 216 219 public XSWildcardDecl performUnionWith(XSWildcardDecl wildcard, 220 short processContents) { 221 if (wildcard == null) 223 return null; 224 225 229 XSWildcardDecl unionWildcard = new XSWildcardDecl(); 230 unionWildcard.fProcessContents = processContents; 231 232 if (areSame(wildcard)) { 234 unionWildcard.fType = fType; 235 unionWildcard.fNamespaceList = fNamespaceList; 236 } 237 238 else if ( (fType == NSCONSTRAINT_ANY) || (wildcard.fType == NSCONSTRAINT_ANY) ) { 240 unionWildcard.fType = NSCONSTRAINT_ANY; 241 } 242 243 else if ( (fType == NSCONSTRAINT_LIST) && (wildcard.fType == NSCONSTRAINT_LIST) ) { 246 unionWildcard.fType = NSCONSTRAINT_LIST; 247 unionWildcard.fNamespaceList = union2sets(fNamespaceList, wildcard.fNamespaceList); 248 } 249 250 else if (fType == NSCONSTRAINT_NOT && wildcard.fType == NSCONSTRAINT_NOT) { 257 unionWildcard.fType = NSCONSTRAINT_NOT; 258 unionWildcard.fNamespaceList = new String [2]; 259 unionWildcard.fNamespaceList[0] = ABSENT; 260 unionWildcard.fNamespaceList[1] = ABSENT; 261 } 262 263 else if ( ((fType == NSCONSTRAINT_NOT) && (wildcard.fType == NSCONSTRAINT_LIST)) || 286 ((fType == NSCONSTRAINT_LIST) && (wildcard.fType == NSCONSTRAINT_NOT)) ) { 287 String [] other = null; 288 String [] list = null; 289 290 if (fType == NSCONSTRAINT_NOT) { 291 other = fNamespaceList; 292 list = wildcard.fNamespaceList; 293 } 294 else { 295 other = wildcard.fNamespaceList; 296 list = fNamespaceList; 297 } 298 299 boolean foundAbsent = elementInSet(ABSENT, list); 300 301 if (other[0] != ABSENT) { 302 boolean foundNS = elementInSet(other[0], list); 303 if (foundNS && foundAbsent) { 304 unionWildcard.fType = NSCONSTRAINT_ANY; 305 } else if (foundNS && !foundAbsent) { 306 unionWildcard.fType = NSCONSTRAINT_NOT; 307 unionWildcard.fNamespaceList = new String [2]; 308 unionWildcard.fNamespaceList[0] = ABSENT; 309 unionWildcard.fNamespaceList[1] = ABSENT; 310 } else if (!foundNS && foundAbsent) { 311 return null; 312 } else { unionWildcard.fType = NSCONSTRAINT_NOT; 314 unionWildcard.fNamespaceList = other; 315 } 316 } else { if (foundAbsent) { 318 unionWildcard.fType = NSCONSTRAINT_ANY; 319 } else { unionWildcard.fType = NSCONSTRAINT_NOT; 321 unionWildcard.fNamespaceList = other; 322 } 323 } 324 } 325 326 return unionWildcard; 327 328 } 330 333 public XSWildcardDecl performIntersectionWith(XSWildcardDecl wildcard, 334 short processContents) { 335 if (wildcard == null) 337 return null; 338 339 343 XSWildcardDecl intersectWildcard = new XSWildcardDecl(); 344 intersectWildcard.fProcessContents = processContents; 345 346 if (areSame(wildcard)) { 348 intersectWildcard.fType = fType; 349 intersectWildcard.fNamespaceList = fNamespaceList; 350 } 351 352 else if ( (fType == NSCONSTRAINT_ANY) || (wildcard.fType == NSCONSTRAINT_ANY) ) { 354 XSWildcardDecl other = this; 356 357 if (fType == NSCONSTRAINT_ANY) 358 other = wildcard; 359 360 intersectWildcard.fType = other.fType; 361 intersectWildcard.fNamespaceList = other.fNamespaceList; 362 } 363 364 else if ( ((fType == NSCONSTRAINT_NOT) && (wildcard.fType == NSCONSTRAINT_LIST)) || 373 ((fType == NSCONSTRAINT_LIST) && (wildcard.fType == NSCONSTRAINT_NOT)) ) { 374 String [] list = null; 375 String [] other = null; 376 377 if (fType == NSCONSTRAINT_NOT) { 378 other = fNamespaceList; 379 list = wildcard.fNamespaceList; 380 } 381 else { 382 other = wildcard.fNamespaceList; 383 list = fNamespaceList; 384 } 385 386 int listSize = list.length; 387 String [] intersect = new String [listSize]; 388 int newSize = 0; 389 for (int i = 0; i < listSize; i++) { 390 if (list[i] != other[0] && list[i] != ABSENT) 391 intersect[newSize++] = list[i]; 392 } 393 394 intersectWildcard.fType = NSCONSTRAINT_LIST; 395 intersectWildcard.fNamespaceList = new String [newSize]; 396 System.arraycopy(intersect, 0, intersectWildcard.fNamespaceList, 0, newSize); 397 } 398 399 else if ( (fType == NSCONSTRAINT_LIST) && (wildcard.fType == NSCONSTRAINT_LIST) ) { 402 intersectWildcard.fType = NSCONSTRAINT_LIST; 403 intersectWildcard.fNamespaceList = intersect2sets(fNamespaceList, wildcard.fNamespaceList); 404 } 405 406 else if (fType == NSCONSTRAINT_NOT && wildcard.fType == NSCONSTRAINT_NOT) { 415 if (fNamespaceList[0] != ABSENT && wildcard.fNamespaceList[0] != ABSENT) 416 return null; 417 418 XSWildcardDecl other = this; 419 if (fNamespaceList[0] == ABSENT) 420 other = wildcard; 421 422 intersectWildcard.fType = other.fType; 423 intersectWildcard.fNamespaceList = other.fNamespaceList; 424 } 425 426 return intersectWildcard; 427 428 } 430 private boolean areSame(XSWildcardDecl wildcard) { 431 if (fType == wildcard.fType) { 432 if (fType == NSCONSTRAINT_ANY) 434 return true; 435 436 if (fType == NSCONSTRAINT_NOT) 440 return fNamespaceList[0] == wildcard.fNamespaceList[0]; 441 442 if (fNamespaceList.length == wildcard.fNamespaceList.length) { 446 for (int i=0; i<fNamespaceList.length; i++) { 447 if (!elementInSet(fNamespaceList[i], wildcard.fNamespaceList)) 448 return false; 449 } 450 return true; 451 } 452 } 453 454 return false; 455 } 457 String [] intersect2sets(String [] one, String [] theOther){ 458 String [] result = new String [Math.min(one.length,theOther.length)]; 459 460 int count = 0; 462 for (int i=0; i<one.length; i++) { 463 if (elementInSet(one[i], theOther)) 464 result[count++] = one[i]; 465 } 466 467 String [] result2 = new String [count]; 468 System.arraycopy(result, 0, result2, 0, count); 469 470 return result2; 471 } 472 473 String [] union2sets(String [] one, String [] theOther){ 474 String [] result1 = new String [one.length]; 475 476 int count = 0; 478 for (int i=0; i<one.length; i++) { 479 if (!elementInSet(one[i], theOther)) 480 result1[count++] = one[i]; 481 } 482 483 String [] result2 = new String [count+theOther.length]; 484 System.arraycopy(result1, 0, result2, 0, count); 485 System.arraycopy(theOther, 0, result2, count, theOther.length); 486 487 return result2; 488 } 489 490 boolean subset2sets(String [] subSet, String [] superSet){ 491 for (int i=0; i<subSet.length; i++) { 492 if (!elementInSet(subSet[i], superSet)) 493 return false; 494 } 495 496 return true; 497 } 498 499 boolean elementInSet(String ele, String [] set){ 500 boolean found = false; 501 for (int i=0; i<set.length && !found; i++) { 502 if (ele==set[i]) 503 found = true; 504 } 505 506 return found; 507 } 508 509 512 private String fDescription = null; 513 public String toString() { 514 if (fDescription == null) { 515 StringBuffer buffer = new StringBuffer (); 516 buffer.append("WC["); 517 switch (fType) { 518 case NSCONSTRAINT_ANY: 519 buffer.append(SchemaSymbols.ATTVAL_TWOPOUNDANY); 520 break; 521 case NSCONSTRAINT_NOT: 522 buffer.append(SchemaSymbols.ATTVAL_TWOPOUNDOTHER); 523 buffer.append(":\""); 524 if (fNamespaceList[0] != null) 525 buffer.append(fNamespaceList[0]); 526 buffer.append("\""); 527 break; 528 case NSCONSTRAINT_LIST: 529 if (fNamespaceList.length == 0) 530 break; 531 buffer.append("\""); 532 if (fNamespaceList[0] != null) 533 buffer.append(fNamespaceList[0]); 534 buffer.append("\""); 535 for (int i = 1; i < fNamespaceList.length; i++) { 536 buffer.append(",\""); 537 if (fNamespaceList[i] != null) 538 buffer.append(fNamespaceList[i]); 539 buffer.append("\""); 540 } 541 break; 542 } 543 buffer.append("]"); 544 fDescription = buffer.toString(); 545 } 546 547 return fDescription; 548 } 549 550 553 public short getType() { 554 return XSConstants.WILDCARD; 555 } 556 557 561 public String getName() { 562 return null; 563 } 564 565 570 public String getNamespace() { 571 return null; 572 } 573 574 577 public short getConstraintType() { 578 return fType; 579 } 580 581 587 public StringList getNsConstraintList() { 588 return new StringListImpl(fNamespaceList, fNamespaceList == null ? 0 : fNamespaceList.length); 589 } 590 591 595 public short getProcessContents() { 596 return fProcessContents; 597 } 598 599 602 public String getProcessContentsAsString() { 603 switch (fProcessContents) { 604 case XSWildcardDecl.PC_SKIP: return "skip"; 605 case XSWildcardDecl.PC_LAX: return "lax"; 606 case XSWildcardDecl.PC_STRICT: return "strict"; 607 default: return "invalid value"; 608 } 609 } 610 611 614 public XSAnnotation getAnnotation() { 615 return fAnnotation; 616 } 617 618 619 622 public XSNamespaceItem getNamespaceItem() { 623 return null; 625 } 626 627 } | Popular Tags |