1 57 package com.sun.org.apache.xerces.internal.xinclude; 58 59 60 import java.util.Enumeration ; 61 import java.util.StringTokenizer ; 62 import java.util.Stack ; 63 64 import com.sun.org.apache.xerces.internal.impl.Constants; 65 import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter; 66 import com.sun.org.apache.xerces.internal.impl.dtd.DTDGrammar; 67 import com.sun.org.apache.xerces.internal.util.ParserConfigurationSettings; 68 import com.sun.org.apache.xerces.internal.xni.Augmentations; 69 import com.sun.org.apache.xerces.internal.xni.NamespaceContext; 70 import com.sun.org.apache.xerces.internal.xni.QName; 71 import com.sun.org.apache.xerces.internal.xni.XMLAttributes; 72 import com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler; 73 import com.sun.org.apache.xerces.internal.xni.XMLLocator; 74 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier; 75 import com.sun.org.apache.xerces.internal.xni.XMLString; 76 import com.sun.org.apache.xerces.internal.xni.XNIException; 77 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription; 78 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool; 79 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager; 80 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException; 81 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentSource; 82 import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver; 83 import com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator; 84 87 public class XPointerElementHandler implements XPointerSchema { 88 89 90 92 93 protected static final String ERROR_REPORTER = 94 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY; 95 96 97 protected static final String GRAMMAR_POOL = 98 Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY; 99 100 101 protected static final String ENTITY_RESOLVER = 102 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY; 103 104 protected static final String XPOINTER_SCHEMA = 105 Constants.XERCES_PROPERTY_PREFIX + Constants.XPOINTER_SCHEMA_PROPERTY; 106 107 108 private static final String [] RECOGNIZED_FEATURES = { 109 }; 110 111 112 private static final Boolean [] FEATURE_DEFAULTS = { 113 }; 114 115 116 117 private static final String [] RECOGNIZED_PROPERTIES = 118 { ERROR_REPORTER, GRAMMAR_POOL, ENTITY_RESOLVER, XPOINTER_SCHEMA }; 119 120 121 private static final Object [] PROPERTY_DEFAULTS = { null, null, null,null }; 122 123 125 protected XMLDocumentHandler fDocumentHandler; 126 protected XMLDocumentSource fDocumentSource; 127 128 protected XIncludeHandler fParentXIncludeHandler; 129 130 protected XMLLocator fDocLocation; 131 protected XIncludeNamespaceSupport fNamespaceContext; 132 protected XMLErrorReporter fErrorReporter; 133 protected XMLGrammarPool fGrammarPool; 134 protected XMLGrammarDescription fGrammarDesc; 135 protected DTDGrammar fDTDGrammar; 136 protected XMLEntityResolver fEntityResolver; 137 protected ParserConfigurationSettings fSettings; 138 protected StringBuffer fPointer; 140 private int elemCount = 0; 141 142 143 private int fDepth; 146 147 private int fRootDepth; 151 152 private static final int INITIAL_SIZE = 8; 154 155 156 private boolean[] fSawInclude = new boolean[INITIAL_SIZE]; 161 162 163 private boolean[] fSawFallback = new boolean[INITIAL_SIZE]; 167 168 169 private int[] fState = new int[INITIAL_SIZE]; 171 172 QName foundElement = null; 173 boolean skip = false; 174 176 public XPointerElementHandler() { 177 178 179 fDepth = 0; 180 fRootDepth = 0; 181 fSawFallback[fDepth] = false; 182 fSawInclude[fDepth] = false; 183 fSchemaName="element"; 184 185 186 } 187 188 190 public void reset(){ 191 elemCount =0; 192 fPointerToken = null; 193 fCurrentTokenint=0; 194 fCurrentTokenString=null; 195 fCurrentTokenType=0 ; 196 fElementCount =0; 197 fCurrentToken =0; 198 includeElement = false; 199 foundElement = null; 200 skip = false; 201 fSubResourceIdentified=false; 202 } 203 204 public void reset(XMLComponentManager componentManager) 205 throws XNIException { 206 fNamespaceContext = null; 207 elemCount =0; 208 fDepth = 0; 209 fRootDepth = 0; 210 fPointerToken = null; 211 fCurrentTokenint=0; 212 fCurrentTokenString=null; 213 fCurrentTokenType=0 ; 214 foundElement = null; 215 includeElement = false; 216 skip = false; 217 fSubResourceIdentified=false; 218 219 220 221 222 try { 223 setErrorReporter( 224 (XMLErrorReporter)componentManager.getProperty(ERROR_REPORTER)); 225 } 226 catch (XMLConfigurationException e) { 227 fErrorReporter = null; 228 } 229 try { 230 fGrammarPool = 231 (XMLGrammarPool)componentManager.getProperty(GRAMMAR_POOL); 232 } 233 catch (XMLConfigurationException e) { 234 fGrammarPool = null; 235 } 236 try { 237 fEntityResolver = 238 (XMLEntityResolver)componentManager.getProperty( 239 ENTITY_RESOLVER); 240 } 241 catch (XMLConfigurationException e) { 242 fEntityResolver = null; 243 } 244 245 fSettings = new ParserConfigurationSettings(); 246 247 Enumeration xercesFeatures = Constants.getXercesFeatures(); 248 while (xercesFeatures.hasMoreElements()) { 249 String featureId = (String )xercesFeatures.nextElement(); 250 fSettings.addRecognizedFeatures(new String [] { featureId }); 251 try { 252 fSettings.setFeature( 253 featureId, 254 componentManager.getFeature(featureId)); 255 } 256 catch (XMLConfigurationException e) { 257 } 260 } 261 266 267 } 269 274 public String [] getRecognizedFeatures() { 275 return RECOGNIZED_FEATURES; 276 } 278 293 public void setFeature(String featureId, boolean state) 294 throws XMLConfigurationException { 295 if (fSettings != null) { 296 fSettings.setFeature(featureId, state); 297 } 298 299 } 301 306 public String [] getRecognizedProperties() { 307 return RECOGNIZED_PROPERTIES; 308 } 310 325 public void setProperty(String propertyId, Object value) 326 throws XMLConfigurationException { 327 if (propertyId.equals(ERROR_REPORTER)) { 328 setErrorReporter((XMLErrorReporter)value); 329 } 330 if (propertyId.equals(GRAMMAR_POOL)) { 331 fGrammarPool = (XMLGrammarPool)value; 332 } 333 if (propertyId.equals(ENTITY_RESOLVER)) { 334 fEntityResolver = (XMLEntityResolver)value; 335 } 336 337 } 339 348 public Boolean getFeatureDefault(String featureId) { 349 for (int i = 0; i < RECOGNIZED_FEATURES.length; i++) { 350 if (RECOGNIZED_FEATURES[i].equals(featureId)) { 351 return FEATURE_DEFAULTS[i]; 352 } 353 } 354 return null; 355 } 357 366 public Object getPropertyDefault(String propertyId) { 367 for (int i = 0; i < RECOGNIZED_PROPERTIES.length; i++) { 368 if (RECOGNIZED_PROPERTIES[i].equals(propertyId)) { 369 return PROPERTY_DEFAULTS[i]; 370 } 371 } 372 return null; 373 } 375 private void setErrorReporter(XMLErrorReporter reporter) { 376 fErrorReporter = reporter; 377 if (fErrorReporter != null) { 378 fErrorReporter.putMessageFormatter( 379 XIncludeMessageFormatter.XINCLUDE_DOMAIN, 380 new XIncludeMessageFormatter()); 381 } 382 } 383 385 386 387 389 public void setDocumentHandler(XMLDocumentHandler handler) { 390 fDocumentHandler = handler; 391 } 392 393 public XMLDocumentHandler getDocumentHandler() { 394 return fDocumentHandler; 395 } 396 397 399 400 401 402 404 String fSchemaName; 405 String fSchemaPointer; 406 boolean fSubResourceIdentified; 407 410 public void setXPointerSchemaName(String schemaName){ 411 fSchemaName = schemaName; 412 } 413 414 418 public String getXpointerSchemaName(){ 419 return fSchemaName; 420 } 421 422 426 public void setParent(Object parent){ 427 fParentXIncludeHandler = (XIncludeHandler)parent; 428 } 429 430 431 434 public Object getParent(){ 435 return fParentXIncludeHandler; 436 } 437 438 441 public void setXPointerSchemaPointer(String content){ 442 fSchemaPointer = content; 443 } 444 445 448 public String getXPointerSchemaPointer(){ 449 return fSchemaPointer; 450 } 451 452 public boolean isSubResourceIndentified(){ 453 return fSubResourceIdentified; 454 } 455 456 458 459 460 462 Stack fPointerToken = new Stack (); 463 int fCurrentTokenint=0; 464 String fCurrentTokenString=null; 465 int fCurrentTokenType=0 ; 467 public void getTokens(){ 468 fSchemaPointer = fSchemaPointer.substring(fSchemaPointer.indexOf("(")+1, fSchemaPointer.length()); 469 StringTokenizer st = new StringTokenizer (fSchemaPointer, "/"); 470 String tempToken; 471 Integer integerToken =null; 472 Stack tempPointerToken = new Stack (); 473 if(fPointerToken == null){ 474 fPointerToken = new Stack (); 475 } 476 while(st.hasMoreTokens()){ 477 tempToken=st.nextToken(); 478 try { 479 integerToken = Integer.valueOf(tempToken); 480 tempPointerToken.push(integerToken); 481 }catch(NumberFormatException e){ 482 tempPointerToken.push(tempToken); 483 } 484 } 485 while(!tempPointerToken.empty()){ 486 fPointerToken.push(tempPointerToken.pop()); 487 } 488 } 490 491 public boolean hasMoreToken(){ 492 if(fPointerToken.isEmpty()) 493 return false; 494 else 495 return true; 496 } 497 498 public boolean getNextToken(){ 499 Object currentToken; 500 if (!fPointerToken.isEmpty()){ 501 currentToken = fPointerToken.pop(); 502 if(currentToken instanceof Integer ){ 503 fCurrentTokenint = ((Integer )currentToken).intValue(); 504 fCurrentTokenType = 1; 505 } 506 else{ 507 fCurrentTokenString = ((String )currentToken).toString(); 508 fCurrentTokenType = 2; 509 } 510 return true; 511 } 512 else { 513 return false; 514 } 515 } 516 517 private boolean isIdAttribute(XMLAttributes attributes,Augmentations augs, int index) { 518 Object o = augs.getItem(Constants.ID_ATTRIBUTE); 519 if( o instanceof Boolean ) 520 return ((Boolean )o).booleanValue(); 521 return "ID".equals(attributes.getType(index)); 522 } 523 524 public boolean checkStringToken(QName element, XMLAttributes attributes){ 525 QName cacheQName = null; 526 String id =null; 527 String rawname =null; 528 QName attrName = new QName(); 529 String attrType = null; 530 String attrValue = null; 531 int attrCount = attributes.getLength(); 532 for (int i = 0; i < attrCount; i++) { 533 Augmentations aaugs = attributes.getAugmentations(i); 534 attributes.getName(i,attrName); 535 attrType = attributes.getType(i); 536 attrValue = attributes.getValue(i); 537 if(attrType != null && attrValue!= null && isIdAttribute(attributes,aaugs,i) && attrValue.equals(fCurrentTokenString)){ 538 if(hasMoreToken()){ 539 fCurrentTokenType = 0; 540 fCurrentTokenString = null; 541 return true; 542 } 543 else{ 544 foundElement = element; 545 includeElement = true; 546 fCurrentTokenType = 0; 547 fCurrentTokenString = null; 548 fSubResourceIdentified = true; 549 return true; 550 } 551 } 552 } 553 return false; 554 } 555 556 public boolean checkIntegerToken(QName element){ 557 if(!skip){ 558 fElementCount++; 559 if(fCurrentTokenint == fElementCount){ 560 if(hasMoreToken()){ 561 fElementCount=0; 562 fCurrentTokenType = 0; 563 return true; 564 } 565 else{ 566 foundElement = element; 567 includeElement = true; 568 fCurrentTokenType = 0; 569 fElementCount=0; 570 fSubResourceIdentified =true; 571 return true; 572 } 573 }else{ 574 addQName(element); 575 skip = true; 576 return false; 577 } 578 } 579 return false; 580 } 581 582 public void addQName(QName element){ 583 QName cacheQName = new QName(element); 584 ftempCurrentElement.push(cacheQName); 585 } 586 587 589 590 592 593 public void startDocument(XMLLocator locator, String encoding, 594 NamespaceContext namespaceContext, Augmentations augs) 595 throws XNIException { 596 597 getTokens(); 598 } 599 600 public void doctypeDecl(String rootElement, String publicId, String systemId, 601 Augmentations augs)throws XNIException { 602 } 603 604 public void xmlDecl(String version, String encoding, String standalone, 605 Augmentations augs) throws XNIException { 606 } 607 608 609 public void comment(XMLString text, Augmentations augs) 610 throws XNIException { 611 if (fDocumentHandler != null && includeElement) { 612 fDocumentHandler.comment(text, augs); 613 } 614 } 615 616 public void processingInstruction(String target, XMLString data, 617 Augmentations augs) throws XNIException { 618 if (fDocumentHandler != null && includeElement) { 619 fDocumentHandler.processingInstruction(target, data, augs); 620 621 } 622 } 623 624 Stack ftempCurrentElement = new Stack (); 625 int fElementCount =0; 626 int fCurrentToken ; 627 boolean includeElement; 628 629 630 public void startElement(QName element, XMLAttributes attributes, 631 Augmentations augs)throws XNIException { 632 633 boolean requiredToken=false; 634 if(fCurrentTokenType == 0) 635 getNextToken(); 636 if(fCurrentTokenType ==1) 637 requiredToken = checkIntegerToken(element); 638 else if (fCurrentTokenType ==2) 639 requiredToken = checkStringToken(element, attributes); 640 if(requiredToken && hasMoreToken()) 641 getNextToken(); 642 if(fDocumentHandler != null && includeElement){ 643 elemCount++; 644 fDocumentHandler.startElement(element, attributes, augs); 645 } 646 647 } 648 649 650 public void endElement(QName element, Augmentations augs) 651 throws XNIException { 652 if(includeElement && foundElement != null ){ 653 if(elemCount >0 )elemCount --; 654 fDocumentHandler.endElement(element, augs); 655 if(elemCount == 0)includeElement = false; 656 657 }else if(!ftempCurrentElement.empty()){ 658 QName name = (QName)ftempCurrentElement.peek(); 659 if(name.equals(element)){ 660 ftempCurrentElement.pop(); 661 skip = false; 662 } 663 } 664 } 665 666 public void emptyElement(QName element, XMLAttributes attributes, 667 Augmentations augs)throws XNIException { 668 if(fDocumentHandler != null && includeElement){ 669 fDocumentHandler.emptyElement(element, attributes, augs); 670 } 671 } 672 673 public void startGeneralEntity(String name, XMLResourceIdentifier resId, 674 String encoding, 675 Augmentations augs) 676 throws XNIException { 677 if (fDocumentHandler != null && includeElement) { 678 fDocumentHandler.startGeneralEntity(name, resId, encoding, augs); 679 } 680 } 681 682 public void textDecl(String version, String encoding, Augmentations augs) 683 throws XNIException { 684 if (fDocumentHandler != null && includeElement) { 685 fDocumentHandler.textDecl(version, encoding, augs); 686 } 687 } 688 689 public void endGeneralEntity(String name, Augmentations augs) 690 throws XNIException { 691 if (fDocumentHandler != null) { 692 fDocumentHandler.endGeneralEntity(name, augs); 693 } 694 } 695 696 public void characters(XMLString text, Augmentations augs) 697 throws XNIException { 698 if (fDocumentHandler != null && includeElement) { 699 fDocumentHandler.characters(text, augs); 700 } 701 } 702 703 public void ignorableWhitespace(XMLString text, Augmentations augs) 704 throws XNIException { 705 if (fDocumentHandler != null && includeElement) { 706 fDocumentHandler.ignorableWhitespace(text, augs); 707 } 708 } 709 710 public void startCDATA(Augmentations augs) throws XNIException { 711 if (fDocumentHandler != null && includeElement) { 712 fDocumentHandler.startCDATA(augs); 713 } 714 } 715 716 public void endCDATA(Augmentations augs) throws XNIException { 717 if (fDocumentHandler != null && includeElement) { 718 fDocumentHandler.endCDATA(augs); 719 } 720 } 721 722 public void endDocument(Augmentations augs) throws XNIException { 723 } 724 725 public void setDocumentSource(XMLDocumentSource source) { 726 fDocumentSource = source; 727 } 728 729 public XMLDocumentSource getDocumentSource() { 730 return fDocumentSource; 731 } 732 733 734 protected void reportFatalError(String key) { 735 this.reportFatalError(key, null); 736 } 737 738 protected void reportFatalError(String key, Object [] args) { 739 if (fErrorReporter != null) { 740 fErrorReporter.reportError( 741 fDocLocation, 742 XIncludeMessageFormatter.XINCLUDE_DOMAIN, 743 key, 744 args, 745 XMLErrorReporter.SEVERITY_FATAL_ERROR); 746 } 747 } 750 751 752 753 protected boolean isRootDocument() { 755 return this.fParentXIncludeHandler == null; 756 } 757 758 759 } | Popular Tags |