1 57 58 package org.enhydra.apache.xerces.framework; 59 60 import org.enhydra.apache.xerces.utils.StringPool; 61 62 114 public class XMLContentSpec { 115 116 120 124 public static final int CONTENTSPECNODE_LEAF = 0; 125 126 127 public static final int CONTENTSPECNODE_ZERO_OR_ONE = 1; 128 129 130 public static final int CONTENTSPECNODE_ZERO_OR_MORE = 2; 131 132 133 public static final int CONTENTSPECNODE_ONE_OR_MORE = 3; 134 135 136 public static final int CONTENTSPECNODE_CHOICE = 4; 137 138 139 public static final int CONTENTSPECNODE_SEQ = 5; 140 141 151 public static final int CONTENTSPECNODE_ANY = 6; 152 153 160 public static final int CONTENTSPECNODE_ANY_OTHER = 7; 161 162 163 public static final int CONTENTSPECNODE_ANY_NS = 8; 164 165 166 public static final int CONTENTSPECNODE_ALL = 9; 167 168 169 public static final int CONTENTSPECNODE_ANY_LAX = 22; 170 171 public static final int CONTENTSPECNODE_ANY_OTHER_LAX = 23; 172 173 public static final int CONTENTSPECNODE_ANY_NS_LAX = 24; 174 175 176 177 public static final int CONTENTSPECNODE_ANY_SKIP = 38; 178 179 public static final int CONTENTSPECNODE_ANY_OTHER_SKIP = 39; 180 181 public static final int CONTENTSPECNODE_ANY_NS_SKIP = 40; 182 186 197 public int type; 198 199 203 public int value; 204 205 209 public int otherValue; 210 211 215 216 public XMLContentSpec() { 217 clear(); 218 } 219 220 221 public XMLContentSpec(int type, int value, int otherValue) { 222 setValues(type, value, otherValue); 223 } 224 225 228 public XMLContentSpec(XMLContentSpec contentSpec) { 229 setValues(contentSpec); 230 } 231 232 236 public XMLContentSpec(XMLContentSpec.Provider provider, 237 int contentSpecIndex) { 238 setValues(provider, contentSpecIndex); 239 } 240 241 245 246 public void clear() { 247 type = -1; 248 value = -1; 249 otherValue = -1; 250 } 251 252 253 public void setValues(int type, int value, int otherValue) { 254 this.type = type; 255 this.value = value; 256 this.otherValue = otherValue; 257 } 258 259 260 public void setValues(XMLContentSpec contentSpec) { 261 type = contentSpec.type; 262 value = contentSpec.value; 263 otherValue = contentSpec.otherValue; 264 } 265 266 271 public void setValues(XMLContentSpec.Provider provider, 272 int contentSpecIndex) { 273 if (!provider.getContentSpec(contentSpecIndex, this)) { 274 clear(); 275 } 276 } 277 278 282 288 public static String toString(XMLContentSpec.Provider provider, 289 StringPool stringPool, 290 int contentSpecIndex) { 291 292 XMLContentSpec contentSpec = new XMLContentSpec(); 294 295 if (provider.getContentSpec(contentSpecIndex, contentSpec)) { 296 297 StringBuffer str = new StringBuffer (); 299 int parentContentSpecType = contentSpec.type & 0x0f; 300 int nextContentSpec; 301 switch (parentContentSpecType) { 302 case XMLContentSpec.CONTENTSPECNODE_LEAF: { 303 str.append('('); 304 if (contentSpec.value == -1 && contentSpec.otherValue == -1) { 305 str.append("#PCDATA"); 306 } 307 else { 308 str.append(stringPool.toString(contentSpec.value)); 309 } 310 str.append(')'); 311 break; 312 } 313 case XMLContentSpec.CONTENTSPECNODE_ZERO_OR_ONE: { 314 provider.getContentSpec(contentSpec.value, contentSpec); 315 nextContentSpec = contentSpec.type; 316 317 if (nextContentSpec == XMLContentSpec.CONTENTSPECNODE_LEAF) { 318 str.append('('); 319 str.append(stringPool.toString(contentSpec.value)); 320 str.append(')'); 321 } else if( nextContentSpec == XMLContentSpec.CONTENTSPECNODE_ONE_OR_MORE || 322 nextContentSpec == XMLContentSpec.CONTENTSPECNODE_ZERO_OR_MORE || 323 nextContentSpec == XMLContentSpec.CONTENTSPECNODE_ZERO_OR_ONE ) { 324 str.append('(' ); 325 appendContentSpec(provider, stringPool, contentSpec, str, 326 true, parentContentSpecType ); 327 str.append(')'); 328 329 } else { 330 appendContentSpec(provider, stringPool, contentSpec, str, 331 true, parentContentSpecType ); 332 } 333 str.append('?'); 334 break; 335 } 336 case XMLContentSpec.CONTENTSPECNODE_ZERO_OR_MORE: { 337 provider.getContentSpec(contentSpec.value, contentSpec); 338 nextContentSpec = contentSpec.type; 339 340 if ( nextContentSpec == XMLContentSpec.CONTENTSPECNODE_LEAF) { 341 str.append('('); 342 if (contentSpec.value == -1 && contentSpec.otherValue == -1) { 343 str.append("#PCDATA"); 344 } 345 else if (contentSpec.otherValue != -1) { 346 str.append("##any:uri="+stringPool.toString(contentSpec.otherValue)); 347 } 348 else if (contentSpec.value == -1) { 349 str.append("##any"); 350 } 351 else { 352 appendContentSpec(provider, stringPool, contentSpec, str, 353 true, parentContentSpecType ); 354 } 355 str.append(')'); 356 357 } else if( nextContentSpec == XMLContentSpec.CONTENTSPECNODE_ONE_OR_MORE || 358 nextContentSpec == XMLContentSpec.CONTENTSPECNODE_ZERO_OR_MORE || 359 nextContentSpec == XMLContentSpec.CONTENTSPECNODE_ZERO_OR_ONE ) { 360 str.append('(' ); 361 appendContentSpec(provider, stringPool, contentSpec, str, 362 true, parentContentSpecType ); 363 str.append(')'); 364 } else { 365 appendContentSpec(provider, stringPool, contentSpec, str, 366 true, parentContentSpecType ); 367 368 } 370 str.append('*'); 371 break; 372 } 373 case XMLContentSpec.CONTENTSPECNODE_ONE_OR_MORE: { 374 provider.getContentSpec(contentSpec.value, contentSpec); 375 nextContentSpec = contentSpec.type; 376 377 if ( nextContentSpec == XMLContentSpec.CONTENTSPECNODE_LEAF) { 378 str.append('('); 379 if (contentSpec.value == -1 && contentSpec.otherValue == -1) { 380 str.append("#PCDATA"); 381 } 382 else if (contentSpec.otherValue != -1) { 383 str.append("##any:uri="+stringPool.toString(contentSpec.otherValue)); 384 } 385 else if (contentSpec.value == -1) { 386 str.append("##any"); 387 } 388 else { 389 str.append(stringPool.toString(contentSpec.value)); 390 } 391 str.append(')'); 392 } else if( nextContentSpec == XMLContentSpec.CONTENTSPECNODE_ONE_OR_MORE || 393 nextContentSpec == XMLContentSpec.CONTENTSPECNODE_ZERO_OR_MORE || 394 nextContentSpec == XMLContentSpec.CONTENTSPECNODE_ZERO_OR_ONE ) { 395 str.append('(' ); 396 appendContentSpec(provider, stringPool, contentSpec, str, 397 true, parentContentSpecType ); 398 str.append(')'); 399 } else { 400 appendContentSpec(provider, stringPool, contentSpec, str, 401 true, parentContentSpecType); 402 } 403 str.append('+'); 404 break; 405 } 406 case XMLContentSpec.CONTENTSPECNODE_ALL: 407 case XMLContentSpec.CONTENTSPECNODE_CHOICE: 408 case XMLContentSpec.CONTENTSPECNODE_SEQ: { 409 appendContentSpec(provider, stringPool, 410 contentSpec, str, true, 411 parentContentSpecType ); 412 break; 413 } 414 case XMLContentSpec.CONTENTSPECNODE_ANY: { 415 str.append("##any"); 416 break; 417 } 418 case XMLContentSpec.CONTENTSPECNODE_ANY_OTHER: { 419 str.append("##other:uri="); 420 str.append(stringPool.toString(contentSpec.otherValue)); 421 break; 422 } 423 case XMLContentSpec.CONTENTSPECNODE_ANY_NS: { 424 str.append("namespace:uri="); 425 str.append(stringPool.toString(contentSpec.otherValue)); 426 break; 427 } 428 default: { 429 str.append("???"); 430 } 431 432 } 434 return str.toString(); 436 } 437 438 return null; 440 441 } 443 447 448 public int hashCode() { 449 return type << 16 | 450 value << 8 | 451 otherValue; 452 } 453 454 455 public boolean equals(Object object) { 456 if (object != null && object instanceof XMLContentSpec) { 457 XMLContentSpec contentSpec = (XMLContentSpec)object; 458 return type == contentSpec.type && 459 value == contentSpec.value && 460 otherValue == contentSpec.otherValue; 461 } 462 return false; 463 } 464 465 469 475 private static void appendContentSpec(XMLContentSpec.Provider provider, 476 StringPool stringPool, 477 XMLContentSpec contentSpec, 478 StringBuffer str, 479 boolean parens, 480 int parentContentSpecType ) { 481 482 int thisContentSpec = contentSpec.type & 0x0f; 483 switch (thisContentSpec) { 484 case XMLContentSpec.CONTENTSPECNODE_LEAF: { 485 if (contentSpec.value == -1 && contentSpec.otherValue == -1) { 486 str.append("#PCDATA"); 487 } 488 else if (contentSpec.value == -1 && contentSpec.otherValue != -1) { 489 str.append("##any:uri="+stringPool.toString(contentSpec.otherValue)); 490 } 491 else if (contentSpec.value == -1) { 492 str.append("##any"); 493 } 494 else { 495 str.append(stringPool.toString(contentSpec.value)); 496 } 497 break; 498 } 499 case XMLContentSpec.CONTENTSPECNODE_ZERO_OR_ONE: { 500 if( parentContentSpecType == XMLContentSpec.CONTENTSPECNODE_ONE_OR_MORE || 501 parentContentSpecType == XMLContentSpec.CONTENTSPECNODE_ZERO_OR_MORE || 502 parentContentSpecType == XMLContentSpec.CONTENTSPECNODE_ZERO_OR_ONE ) { 503 provider.getContentSpec(contentSpec.value, contentSpec); 504 str.append('('); 505 appendContentSpec(provider, stringPool, 506 contentSpec, str, true, thisContentSpec ); 507 str.append(')'); 508 509 } 510 else { 511 provider.getContentSpec(contentSpec.value, contentSpec); 512 appendContentSpec(provider, stringPool, 513 contentSpec, str, true, thisContentSpec ); 514 } 515 str.append('?'); 516 break; 517 } 518 case XMLContentSpec.CONTENTSPECNODE_ZERO_OR_MORE: { 519 if( parentContentSpecType == XMLContentSpec.CONTENTSPECNODE_ONE_OR_MORE || 520 parentContentSpecType == XMLContentSpec.CONTENTSPECNODE_ZERO_OR_MORE || 521 parentContentSpecType == XMLContentSpec.CONTENTSPECNODE_ZERO_OR_ONE ) { 522 provider.getContentSpec(contentSpec.value, contentSpec); 523 str.append('('); 524 appendContentSpec(provider, stringPool, 525 contentSpec, str, true, thisContentSpec); 526 str.append(')' ); 527 } else{ 528 provider.getContentSpec(contentSpec.value, contentSpec); 529 appendContentSpec(provider, stringPool, 530 contentSpec, str, true, thisContentSpec); 531 } 532 str.append('*'); 533 break; 534 } 535 case XMLContentSpec.CONTENTSPECNODE_ONE_OR_MORE: { 536 if( parentContentSpecType == XMLContentSpec.CONTENTSPECNODE_ONE_OR_MORE || 537 parentContentSpecType == XMLContentSpec.CONTENTSPECNODE_ZERO_OR_MORE || 538 parentContentSpecType == XMLContentSpec.CONTENTSPECNODE_ZERO_OR_ONE ) { 539 540 str.append('('); 541 provider.getContentSpec(contentSpec.value, contentSpec); 542 appendContentSpec(provider, stringPool, 543 contentSpec, str, true, thisContentSpec); 544 str.append(')' ); 545 } else { 546 provider.getContentSpec(contentSpec.value, contentSpec); 547 appendContentSpec(provider, stringPool, 548 contentSpec, str, true, thisContentSpec); 549 } 550 str.append('+'); 551 break; 552 } 553 case XMLContentSpec.CONTENTSPECNODE_CHOICE: 554 case XMLContentSpec.CONTENTSPECNODE_SEQ: 555 case XMLContentSpec.CONTENTSPECNODE_ALL: { 556 int type = contentSpec.type; 557 if (parens) { 558 if (type == XMLContentSpec.CONTENTSPECNODE_ALL) 559 str.append("all("); 560 else 561 str.append('('); 562 } 563 int otherValue = contentSpec.otherValue; 564 provider.getContentSpec(contentSpec.value, contentSpec); 565 appendContentSpec(provider, stringPool, 566 contentSpec, str, contentSpec.type != type, thisContentSpec); 567 if (otherValue != -2) { 568 if (type == XMLContentSpec.CONTENTSPECNODE_CHOICE) { 569 str.append('|'); 570 } 571 else { 572 str.append(','); 573 } 574 581 provider.getContentSpec(otherValue, contentSpec); 582 appendContentSpec(provider, stringPool, 583 contentSpec, str, true, thisContentSpec); 584 } 585 if (parens) { 586 str.append(')'); 587 } 588 break; 589 } 590 case XMLContentSpec.CONTENTSPECNODE_ANY: { 591 str.append("##any"); 592 break; 593 } 594 case XMLContentSpec.CONTENTSPECNODE_ANY_OTHER: { 595 str.append("##other:uri="); 596 str.append(stringPool.toString(contentSpec.otherValue)); 597 break; 598 } 599 case XMLContentSpec.CONTENTSPECNODE_ANY_NS: { 600 str.append("namespace:uri="); 601 str.append(stringPool.toString(contentSpec.otherValue)); 602 break; 603 } 604 default: { 605 str.append("???"); 606 break; 607 } 608 609 } 611 } 613 617 625 public interface Provider { 626 627 631 642 public boolean getContentSpec(int contentSpecIndex, XMLContentSpec contentSpec); 643 644 } 646 } | Popular Tags |