1 16 17 package org.apache.xerces.impl.xs; 18 19 import org.apache.xerces.xs.StringList; 20 import org.apache.xerces.xs.XSAnnotation; 21 import org.apache.xerces.xs.XSConstants; 22 import org.apache.xerces.xs.XSNamespaceItem; 23 import org.apache.xerces.xs.XSWildcard; 24 import org.apache.xerces.impl.xs.util.StringListImpl; 25 26 37 public class XSWildcardDecl implements XSWildcard { 38 39 public static final String ABSENT = null; 40 41 public short fType = NSCONSTRAINT_ANY; 43 public short fProcessContents = PC_STRICT; 45 public String [] fNamespaceList; 49 50 public XSAnnotationImpl fAnnotation = null; 52 53 70 73 public boolean allowNamespace(String namespace) { 74 76 if (fType == NSCONSTRAINT_ANY) 78 return true; 79 80 if (fType == NSCONSTRAINT_NOT) { 87 boolean found = false; 88 int listNum = fNamespaceList.length; 89 for (int i = 0; i < listNum && !found; i++) { 90 if (namespace == fNamespaceList[i]) 91 found = true; 92 } 93 94 if (!found) 95 return true; 96 } 97 98 if (fType == NSCONSTRAINT_LIST) { 100 int listNum = fNamespaceList.length; 101 for (int i = 0; i < listNum; i++) { 102 if (namespace == fNamespaceList[i]) 103 return true; 104 } 105 } 106 107 return false; 109 } 110 111 114 public boolean isSubsetOf(XSWildcardDecl superWildcard) { 115 if (superWildcard == null) 117 return false; 118 119 122 if (superWildcard.fType == NSCONSTRAINT_ANY) { 124 return true; 125 } 126 127 if (fType == NSCONSTRAINT_NOT) { 133 if (superWildcard.fType == NSCONSTRAINT_NOT && 134 fNamespaceList[0] == superWildcard.fNamespaceList[0]) { 135 return true; 136 } 137 } 138 139 if (fType == NSCONSTRAINT_LIST) { 150 if (superWildcard.fType == NSCONSTRAINT_LIST && 151 subset2sets(fNamespaceList, superWildcard.fNamespaceList)) { 152 return true; 153 } 154 155 if (superWildcard.fType == NSCONSTRAINT_NOT && 156 !elementInSet(superWildcard.fNamespaceList[0], fNamespaceList) && 157 !elementInSet(ABSENT, fNamespaceList)) { 158 return true; 159 } 160 } 161 162 return false; 164 165 } 167 170 public boolean weakerProcessContents(XSWildcardDecl superWildcard) { 171 return fProcessContents == XSWildcardDecl.PC_LAX && 172 superWildcard.fProcessContents == XSWildcardDecl.PC_STRICT || 173 fProcessContents == XSWildcardDecl.PC_SKIP && 174 superWildcard.fProcessContents != XSWildcardDecl.PC_SKIP; 175 } 176 177 180 public XSWildcardDecl performUnionWith(XSWildcardDecl wildcard, 181 short processContents) { 182 if (wildcard == null) 184 return null; 185 186 190 XSWildcardDecl unionWildcard = new XSWildcardDecl(); 191 unionWildcard.fProcessContents = processContents; 192 193 if (areSame(wildcard)) { 195 unionWildcard.fType = fType; 196 unionWildcard.fNamespaceList = fNamespaceList; 197 } 198 199 else if ( (fType == NSCONSTRAINT_ANY) || (wildcard.fType == NSCONSTRAINT_ANY) ) { 201 unionWildcard.fType = NSCONSTRAINT_ANY; 202 } 203 204 else if ( (fType == NSCONSTRAINT_LIST) && (wildcard.fType == NSCONSTRAINT_LIST) ) { 207 unionWildcard.fType = NSCONSTRAINT_LIST; 208 unionWildcard.fNamespaceList = union2sets(fNamespaceList, wildcard.fNamespaceList); 209 } 210 211 else if (fType == NSCONSTRAINT_NOT && wildcard.fType == NSCONSTRAINT_NOT) { 218 unionWildcard.fType = NSCONSTRAINT_NOT; 219 unionWildcard.fNamespaceList = new String [2]; 220 unionWildcard.fNamespaceList[0] = ABSENT; 221 unionWildcard.fNamespaceList[1] = ABSENT; 222 } 223 224 else if ( ((fType == NSCONSTRAINT_NOT) && (wildcard.fType == NSCONSTRAINT_LIST)) || 247 ((fType == NSCONSTRAINT_LIST) && (wildcard.fType == NSCONSTRAINT_NOT)) ) { 248 String [] other = null; 249 String [] list = null; 250 251 if (fType == NSCONSTRAINT_NOT) { 252 other = fNamespaceList; 253 list = wildcard.fNamespaceList; 254 } 255 else { 256 other = wildcard.fNamespaceList; 257 list = fNamespaceList; 258 } 259 260 boolean foundAbsent = elementInSet(ABSENT, list); 261 262 if (other[0] != ABSENT) { 263 boolean foundNS = elementInSet(other[0], list); 264 if (foundNS && foundAbsent) { 265 unionWildcard.fType = NSCONSTRAINT_ANY; 266 } else if (foundNS && !foundAbsent) { 267 unionWildcard.fType = NSCONSTRAINT_NOT; 268 unionWildcard.fNamespaceList = new String [2]; 269 unionWildcard.fNamespaceList[0] = ABSENT; 270 unionWildcard.fNamespaceList[1] = ABSENT; 271 } else if (!foundNS && foundAbsent) { 272 return null; 273 } else { unionWildcard.fType = NSCONSTRAINT_NOT; 275 unionWildcard.fNamespaceList = other; 276 } 277 } else { if (foundAbsent) { 279 unionWildcard.fType = NSCONSTRAINT_ANY; 280 } else { unionWildcard.fType = NSCONSTRAINT_NOT; 282 unionWildcard.fNamespaceList = other; 283 } 284 } 285 } 286 287 return unionWildcard; 288 289 } 291 294 public XSWildcardDecl performIntersectionWith(XSWildcardDecl wildcard, 295 short processContents) { 296 if (wildcard == null) 298 return null; 299 300 304 XSWildcardDecl intersectWildcard = new XSWildcardDecl(); 305 intersectWildcard.fProcessContents = processContents; 306 307 if (areSame(wildcard)) { 309 intersectWildcard.fType = fType; 310 intersectWildcard.fNamespaceList = fNamespaceList; 311 } 312 313 else if ( (fType == NSCONSTRAINT_ANY) || (wildcard.fType == NSCONSTRAINT_ANY) ) { 315 XSWildcardDecl other = this; 317 318 if (fType == NSCONSTRAINT_ANY) 319 other = wildcard; 320 321 intersectWildcard.fType = other.fType; 322 intersectWildcard.fNamespaceList = other.fNamespaceList; 323 } 324 325 else if ( ((fType == NSCONSTRAINT_NOT) && (wildcard.fType == NSCONSTRAINT_LIST)) || 334 ((fType == NSCONSTRAINT_LIST) && (wildcard.fType == NSCONSTRAINT_NOT)) ) { 335 String [] list = null; 336 String [] other = null; 337 338 if (fType == NSCONSTRAINT_NOT) { 339 other = fNamespaceList; 340 list = wildcard.fNamespaceList; 341 } 342 else { 343 other = wildcard.fNamespaceList; 344 list = fNamespaceList; 345 } 346 347 int listSize = list.length; 348 String [] intersect = new String [listSize]; 349 int newSize = 0; 350 for (int i = 0; i < listSize; i++) { 351 if (list[i] != other[0] && list[i] != ABSENT) 352 intersect[newSize++] = list[i]; 353 } 354 355 intersectWildcard.fType = NSCONSTRAINT_LIST; 356 intersectWildcard.fNamespaceList = new String [newSize]; 357 System.arraycopy(intersect, 0, intersectWildcard.fNamespaceList, 0, newSize); 358 } 359 360 else if ( (fType == NSCONSTRAINT_LIST) && (wildcard.fType == NSCONSTRAINT_LIST) ) { 363 intersectWildcard.fType = NSCONSTRAINT_LIST; 364 intersectWildcard.fNamespaceList = intersect2sets(fNamespaceList, wildcard.fNamespaceList); 365 } 366 367 else if (fType == NSCONSTRAINT_NOT && wildcard.fType == NSCONSTRAINT_NOT) { 376 if (fNamespaceList[0] != ABSENT && wildcard.fNamespaceList[0] != ABSENT) 377 return null; 378 379 XSWildcardDecl other = this; 380 if (fNamespaceList[0] == ABSENT) 381 other = wildcard; 382 383 intersectWildcard.fType = other.fType; 384 intersectWildcard.fNamespaceList = other.fNamespaceList; 385 } 386 387 return intersectWildcard; 388 389 } 391 private boolean areSame(XSWildcardDecl wildcard) { 392 if (fType == wildcard.fType) { 393 if (fType == NSCONSTRAINT_ANY) 395 return true; 396 397 if (fType == NSCONSTRAINT_NOT) 401 return fNamespaceList[0] == wildcard.fNamespaceList[0]; 402 403 if (fNamespaceList.length == wildcard.fNamespaceList.length) { 407 for (int i=0; i<fNamespaceList.length; i++) { 408 if (!elementInSet(fNamespaceList[i], wildcard.fNamespaceList)) 409 return false; 410 } 411 return true; 412 } 413 } 414 415 return false; 416 } 418 String [] intersect2sets(String [] one, String [] theOther){ 419 String [] result = new String [Math.min(one.length,theOther.length)]; 420 421 int count = 0; 423 for (int i=0; i<one.length; i++) { 424 if (elementInSet(one[i], theOther)) 425 result[count++] = one[i]; 426 } 427 428 String [] result2 = new String [count]; 429 System.arraycopy(result, 0, result2, 0, count); 430 431 return result2; 432 } 433 434 String [] union2sets(String [] one, String [] theOther){ 435 String [] result1 = new String [one.length]; 436 437 int count = 0; 439 for (int i=0; i<one.length; i++) { 440 if (!elementInSet(one[i], theOther)) 441 result1[count++] = one[i]; 442 } 443 444 String [] result2 = new String [count+theOther.length]; 445 System.arraycopy(result1, 0, result2, 0, count); 446 System.arraycopy(theOther, 0, result2, count, theOther.length); 447 448 return result2; 449 } 450 451 boolean subset2sets(String [] subSet, String [] superSet){ 452 for (int i=0; i<subSet.length; i++) { 453 if (!elementInSet(subSet[i], superSet)) 454 return false; 455 } 456 457 return true; 458 } 459 460 boolean elementInSet(String ele, String [] set){ 461 boolean found = false; 462 for (int i=0; i<set.length && !found; i++) { 463 if (ele==set[i]) 464 found = true; 465 } 466 467 return found; 468 } 469 470 473 private String fDescription = null; 474 public String toString() { 475 if (fDescription == null) { 476 StringBuffer buffer = new StringBuffer (); 477 buffer.append("WC["); 478 switch (fType) { 479 case NSCONSTRAINT_ANY: 480 buffer.append(SchemaSymbols.ATTVAL_TWOPOUNDANY); 481 break; 482 case NSCONSTRAINT_NOT: 483 buffer.append(SchemaSymbols.ATTVAL_TWOPOUNDOTHER); 484 buffer.append(":\""); 485 if (fNamespaceList[0] != null) 486 buffer.append(fNamespaceList[0]); 487 buffer.append("\""); 488 break; 489 case NSCONSTRAINT_LIST: 490 if (fNamespaceList.length == 0) 491 break; 492 buffer.append("\""); 493 if (fNamespaceList[0] != null) 494 buffer.append(fNamespaceList[0]); 495 buffer.append("\""); 496 for (int i = 1; i < fNamespaceList.length; i++) { 497 buffer.append(",\""); 498 if (fNamespaceList[i] != null) 499 buffer.append(fNamespaceList[i]); 500 buffer.append("\""); 501 } 502 break; 503 } 504 buffer.append("]"); 505 fDescription = buffer.toString(); 506 } 507 508 return fDescription; 509 } 510 511 514 public short getType() { 515 return XSConstants.WILDCARD; 516 } 517 518 522 public String getName() { 523 return null; 524 } 525 526 531 public String getNamespace() { 532 return null; 533 } 534 535 538 public short getConstraintType() { 539 return fType; 540 } 541 542 548 public StringList getNsConstraintList() { 549 return new StringListImpl(fNamespaceList, fNamespaceList == null ? 0 : fNamespaceList.length); 550 } 551 552 556 public short getProcessContents() { 557 return fProcessContents; 558 } 559 560 563 public String getProcessContentsAsString() { 564 switch (fProcessContents) { 565 case XSWildcardDecl.PC_SKIP: return "skip"; 566 case XSWildcardDecl.PC_LAX: return "lax"; 567 case XSWildcardDecl.PC_STRICT: return "strict"; 568 default: return "invalid value"; 569 } 570 } 571 572 575 public XSAnnotation getAnnotation() { 576 return fAnnotation; 577 } 578 579 580 583 public XSNamespaceItem getNamespaceItem() { 584 return null; 586 } 587 588 } | Popular Tags |