1 /* 2 * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved. 3 */ 4 5 /* 6 * Licensed to the Apache Software Foundation (ASF) under one or more 7 * contributor license agreements. See the NOTICE file distributed with 8 * this work for additional information regarding copyright ownership. 9 * The ASF licenses this file to You under the Apache License, Version 2.0 10 * (the "License"); you may not use this file except in compliance with 11 * the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22 package com.sun.org.apache.xerces.internal.impl; 23 24 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter; 25 import com.sun.org.apache.xerces.internal.util.AugmentationsImpl; 26 import com.sun.org.apache.xerces.internal.util.XMLAttributesIteratorImpl; 27 import com.sun.org.apache.xerces.internal.util.XMLChar; 28 import com.sun.org.apache.xerces.internal.util.XMLStringBuffer; 29 import com.sun.org.apache.xerces.internal.util.XMLSymbols; 30 import com.sun.org.apache.xerces.internal.xni.QName; 31 import com.sun.org.apache.xerces.internal.xni.XMLAttributes; 32 import com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler; 33 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier; 34 import com.sun.org.apache.xerces.internal.xni.XMLString; 35 import com.sun.org.apache.xerces.internal.xni.XNIException; 36 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent; 37 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager; 38 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException; 39 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentScanner; 40 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource; 41 import com.sun.org.apache.xerces.internal.xni.Augmentations; 42 import com.sun.org.apache.xerces.internal.utils.SecuritySupport; 43 import com.sun.org.apache.xerces.internal.utils.XMLSecurityManager; 44 import com.sun.org.apache.xerces.internal.utils.XMLSecurityManager.Limit; 45 import com.sun.org.apache.xerces.internal.utils.XMLSecurityPropertyManager; 46 import com.sun.xml.internal.stream.XMLBufferListener; 47 import com.sun.xml.internal.stream.XMLEntityStorage; 48 import com.sun.xml.internal.stream.dtd.DTDGrammarUtil; 49 import java.io.EOFException; 50 import java.io.IOException; 51 import javax.xml.stream.XMLInputFactory; 52 import javax.xml.stream.XMLStreamConstants; 53 import javax.xml.stream.events.XMLEvent; 54 55 /** 56 * 57 * This class is responsible for scanning the structure and content 58 * of document fragments. 59 * 60 * This class has been modified as per the new design which is more suited to 61 * efficiently build pull parser. Lot of improvements have been done and 62 * the code has been added to support stax functionality/features. 63 * 64 * @author Neeraj Bajaj SUN Microsystems 65 * @author K.Venugopal SUN Microsystems 66 * @author Glenn Marcy, IBM 67 * @author Andy Clark, IBM 68 * @author Arnaud Le Hors, IBM 69 * @author Eric Ye, IBM 70 * @author Sunitha Reddy, SUN Microsystems 71 * 72 */ 73 public class XMLDocumentFragmentScannerImpl 74 extends XMLScanner 75 implements XMLDocumentScanner, XMLComponent, XMLEntityHandler, XMLBufferListener { 76 77 // 78 // Constants 79 // 80 81 protected int fElementAttributeLimit, fXMLNameLimit; 82 83 /** External subset resolver. **/ 84 protected ExternalSubsetResolver fExternalSubsetResolver; 85 86 // scanner states 87 88 //XXX this should be divided into more states. 89 /** Scanner state: start of markup. */ 90 protected static final int SCANNER_STATE_START_OF_MARKUP = 21; 91 92 /** Scanner state: content. */ 93 protected static final int SCANNER_STATE_CONTENT = 22; 94 95 /** Scanner state: processing instruction. */ 96 protected static final int SCANNER_STATE_PI = 23; 97 98 /** Scanner state: DOCTYPE. */ 99 protected static final int SCANNER_STATE_DOCTYPE = 24; 100 101 /** Scanner state: XML Declaration */ 102 protected static final int SCANNER_STATE_XML_DECL = 25; 103 104 /** Scanner state: root element. */ 105 protected static final int SCANNER_STATE_ROOT_ELEMENT = 26; 106 107 /** Scanner state: comment. */ 108 protected static final int SCANNER_STATE_COMMENT = 27; 109 110 /** Scanner state: reference. */ 111 protected static final int SCANNER_STATE_REFERENCE = 28; 112 113 // <book type="hard"> reading attribute name 'type' 114 protected static final int SCANNER_STATE_ATTRIBUTE = 29; 115 116 // <book type="hard"> //reading attribute value. 117 protected static final int SCANNER_STATE_ATTRIBUTE_VALUE = 30; 118 119 /** Scanner state: trailing misc. USED BY DOCUMENT_SCANNER_IMPL*/ 120 //protected static final int SCANNER_STATE_TRAILING_MISC = 32; 121 122 /** Scanner state: end of input. */ 123 protected static final int SCANNER_STATE_END_OF_INPUT = 33; 124 125 /** Scanner state: terminated. */ 126 protected static final int SCANNER_STATE_TERMINATED = 34; 127 128 /** Scanner state: CDATA section. */ 129 protected static final int SCANNER_STATE_CDATA = 35; 130 131 /** Scanner state: Text declaration. */ 132 protected static final int SCANNER_STATE_TEXT_DECL = 36; 133 134 /** Scanner state: Text declaration. */ 135 protected static final int SCANNER_STATE_CHARACTER_DATA = 37; 136 137 //<book type="hard">foo</book> 138 protected static final int SCANNER_STATE_START_ELEMENT_TAG = 38; 139 140 //<book type="hard">foo</book> reading </book> 141 protected static final int SCANNER_STATE_END_ELEMENT_TAG = 39; 142 143 protected static final int SCANNER_STATE_CHAR_REFERENCE = 40; 144 protected static final int SCANNER_STATE_BUILT_IN_REFS = 41; 145 146 // feature identifiers 147 148 149 /** Feature identifier: notify built-in refereces. */ 150 protected static final String NOTIFY_BUILTIN_REFS = 151 Constants.XERCES_FEATURE_PREFIX + Constants.NOTIFY_BUILTIN_REFS_FEATURE; 152 153 /** Property identifier: entity resolver. */ 154 protected static final String ENTITY_RESOLVER = 155 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY; 156 157 /** Feature identifier: standard uri conformant */ 158 protected static final String STANDARD_URI_CONFORMANT = 159 Constants.XERCES_FEATURE_PREFIX +Constants.STANDARD_URI_CONFORMANT_FEATURE; 160 161 /** Property identifier: Security property manager. */ 162 private static final String XML_SECURITY_PROPERTY_MANAGER = 163 Constants.XML_SECURITY_PROPERTY_MANAGER; 164 165 /** access external dtd: file protocol 166 * For DOM/SAX, the secure feature is set to true by default 167 */ 168 final static String EXTERNAL_ACCESS_DEFAULT = Constants.EXTERNAL_ACCESS_DEFAULT; 169 170 // recognized features and properties 171 172 /** Recognized features. */ 173 private static final String[] RECOGNIZED_FEATURES = { 174 NAMESPACES, 175 VALIDATION, 176 NOTIFY_BUILTIN_REFS, 177 NOTIFY_CHAR_REFS, 178 Constants.STAX_REPORT_CDATA_EVENT 179 }; 180 181 /** Feature defaults. */ 182 private static final Boolean[] FEATURE_DEFAULTS = { 183 Boolean.TRUE, 184 null, 185 Boolean.FALSE, 186 Boolean.FALSE, 187 Boolean.TRUE 188 }; 189 190 /** Recognized properties. */ 191 private static final String[] RECOGNIZED_PROPERTIES = { 192 SYMBOL_TABLE, 193 ERROR_REPORTER, 194 ENTITY_MANAGER, 195 XML_SECURITY_PROPERTY_MANAGER 196 }; 197 198 /** Property defaults. */ 199 private static final Object[] PROPERTY_DEFAULTS = { 200 null, 201 null, 202 null, 203 null 204 }; 205 206 private static final char [] cdata = {'[','C','D','A','T','A','['}; 207 static final char [] xmlDecl = {'<','?','x','m','l'}; 208 // private static final char [] endTag = {'<','/'}; 209 // debugging 210 211 /** Debug scanner state. */ 212 private static final boolean DEBUG_SCANNER_STATE = false; 213 214 /** Debug driver. */ 215 private static final boolean DEBUG_DISPATCHER = false; 216 217 /** Debug content driver scanning. */ 218 protected static final boolean DEBUG_START_END_ELEMENT = false; 219 220 221 /** Debug driver next */ 222 protected static final boolean DEBUG_NEXT = false ; 223 224 /** Debug driver next */ 225 protected static final boolean DEBUG = false; 226 protected static final boolean DEBUG_COALESCE = false; 227 // 228 // Data 229 // 230 231 // protected data 232 233 /** Document handler. */ 234 protected XMLDocumentHandler fDocumentHandler; 235 protected int fScannerLastState ; 236 237 /** Entity Storage */ 238 protected XMLEntityStorage fEntityStore; 239 240 /** Entity stack. */ 241 protected int[] fEntityStack = new int[4]; 242 243 /** Markup depth. */ 244 protected int fMarkupDepth; 245 246 //is the element empty 247 protected boolean fEmptyElement ; 248 249 //track if we are reading attributes, this is usefule while 250 //there is a callback 251 protected boolean fReadingAttributes = false; 252 253 /** Scanner state. */ 254 protected int fScannerState; 255 256 /** SubScanner state: inside scanContent method. */ 257 protected boolean fInScanContent = false; 258 protected boolean fLastSectionWasCData = false; 259 protected boolean fLastSectionWasEntityReference = false; 260 protected boolean fLastSectionWasCharacterData = false; 261 262 /** has external dtd */ 263 protected boolean fHasExternalDTD; 264 265 /** Standalone. */ 266 protected boolean fStandaloneSet; 267 protected boolean fStandalone; 268 protected String fVersion; 269 270 // element information 271 272 /** Current element. */ 273 protected QName fCurrentElement; 274 275 /** Element stack. */ 276 protected ElementStack fElementStack = new ElementStack(); 277 protected ElementStack2 fElementStack2 = new ElementStack2(); 278 279 // other info 280 281 /** Document system identifier. 282 * REVISIT: So what's this used for? - NG 283 * protected String fDocumentSystemId; 284 ******/ 285 286 protected String fPITarget ; 287 288 //xxx do we need to create an extra XMLString object... look for using fTempString for collecting all the data values 289 protected XMLString fPIData = new XMLString(); 290 291 // features 292 293 294 /** Notify built-in references. */ 295 protected boolean fNotifyBuiltInRefs = false; 296 297 //STAX related properties 298 //defaultValues. 299 protected boolean fSupportDTD = true; 300 protected boolean fReplaceEntityReferences = true; 301 protected boolean fSupportExternalEntities = false; 302 protected boolean fReportCdataEvent = false ; 303 protected boolean fIsCoalesce = false ; 304 protected String fDeclaredEncoding = null; 305 /** Xerces Feature: Disallow doctype declaration. */ 306 protected boolean fDisallowDoctype = false; 307 308 /** 309 * comma-delimited list of protocols that are allowed for the purpose 310 * of accessing external dtd or entity references 311 */ 312 protected String fAccessExternalDTD = EXTERNAL_ACCESS_DEFAULT; 313 314 /** 315 * standard uri conformant (strict uri). 316 * http://apache.org/xml/features/standard-uri-conformant 317 */ 318 protected boolean fStrictURI; 319 320 // drivers 321 322 /** Active driver. */ 323 protected Driver fDriver; 324 325 /** Content driver. */ 326 protected Driver fContentDriver = createContentDriver(); 327 328 // temporary variables 329 330 /** Element QName. */ 331 protected QName fElementQName = new QName(); 332 333 /** Attribute QName. */ 334 protected QName fAttributeQName = new QName(); 335 336 /** 337 * CHANGED: Using XMLAttributesIteratorImpl instead of XMLAttributesImpl. This class 338 * implements Iterator interface so we can directly give Attributes in the form of 339 * iterator. 340 */ 341 protected XMLAttributesIteratorImpl fAttributes = new XMLAttributesIteratorImpl(); 342 343 344 /** String. */ 345 protected XMLString fTempString = new XMLString(); 346 347 /** String. */ 348 protected XMLString fTempString2 = new XMLString(); 349 350 /** Array of 3 strings. */ 351 private String[] fStrings = new String[3]; 352 353 /** Making the buffer accesible to derived class -- String buffer. */ 354 protected XMLStringBuffer fStringBuffer = new XMLStringBuffer(); 355 356 /** Making the buffer accesible to derived class -- String buffer. */ 357 protected XMLStringBuffer fStringBuffer2 = new XMLStringBuffer(); 358 359 /** stores character data. */ 360 /** Making the buffer accesible to derived class -- stores PI data */ 361 protected XMLStringBuffer fContentBuffer = new XMLStringBuffer(); 362 363 /** Single character array. */ 364 private final char[] fSingleChar = new char[1]; 365 private String fCurrentEntityName = null; 366 367 // New members 368 protected boolean fScanToEnd = false; 369 370 protected DTDGrammarUtil dtdGrammarUtil= null; 371 372 protected boolean fAddDefaultAttr = false; 373 374 protected boolean foundBuiltInRefs = false; 375 376 /** Built-in reference character event */ 377 protected boolean builtInRefCharacterHandled = false; 378 379 //skip element algorithm 380 static final short MAX_DEPTH_LIMIT = 5 ; 381 static final short ELEMENT_ARRAY_LENGTH = 200 ; 382 static final short MAX_POINTER_AT_A_DEPTH = 4 ; 383 static final boolean DEBUG_SKIP_ALGORITHM = false; 384 //create a elemnet array of length equal to ELEMENT_ARRAY_LENGTH 385 String [] fElementArray = new String[ELEMENT_ARRAY_LENGTH] ; 386 //pointer location where last element was skipped 387 short fLastPointerLocation = 0 ; 388 short fElementPointer = 0 ; 389 //2D array to store pointer info 390 short [] [] fPointerInfo = new short[MAX_DEPTH_LIMIT] [MAX_POINTER_AT_A_DEPTH] ; 391 protected String fElementRawname ; 392 protected boolean fShouldSkip = false; 393 protected boolean fAdd = false ; 394 protected boolean fSkip = false; 395 396 /** Reusable Augmentations. */ 397 private Augmentations fTempAugmentations = null; 398 // 399 // Constructors 400 // 401 402 /** Default constructor. */ 403 public XMLDocumentFragmentScannerImpl() { 404 } // <init>() 405 406 // 407 // XMLDocumentScanner methods 408 // 409 410 /** 411 * Sets the input source. 412 * 413 * @param inputSource The input source. 414 * 415 * @throws IOException Thrown on i/o error. 416 */ 417 public void setInputSource(XMLInputSource inputSource) throws IOException { 418 fEntityManager.setEntityHandler(this); 419 fEntityManager.startEntity(false, "$fragment$", inputSource, false, true); 420 // fDocumentSystemId = fEntityManager.expandSystemId(inputSource.getSystemId()); 421 } // setInputSource(XMLInputSource) 422 423 /** 424 * Scans a document. 425 * 426 * @param complete True if the scanner should scan the document 427 * completely, pushing all events to the registered 428 * document handler. A value of false indicates that 429 * that the scanner should only scan the next portion 430 * of the document and return. A scanner instance is 431 * permitted to completely scan a document if it does 432 * not support this "pull" scanning model. 433 * 434 * @return True if there is more to scan, false otherwise. 435 */ 436 public boolean scanDocument(boolean complete) 437 throws IOException, XNIException { 438 439 // keep dispatching "events" 440 fEntityManager.setEntityHandler(this); 441 //System.out.println(" get Document Handler in NSDocumentHandler " + fDocumentHandler ); 442 443 int event = next(); 444 do { 445 switch (event) { 446 case XMLStreamConstants.START_DOCUMENT : 447 //fDocumentHandler.startDocument(fEntityManager.getEntityScanner(),fEntityManager.getEntityScanner().getVersion(),fNamespaceContext,null);// not able to get 448 break; 449 case XMLStreamConstants.START_ELEMENT : 450 //System.out.println(" in scann element"); 451 //fDocumentHandler.startElement(getElementQName(),fAttributes,null); 452 break; 453 case XMLStreamConstants.CHARACTERS : 454 fEntityScanner.checkNodeCount(fEntityScanner.fCurrentEntity); 455 fDocumentHandler.characters(getCharacterData(),null); 456 break; 457 case XMLStreamConstants.SPACE: 458 //check if getCharacterData() is the right function to retrieve ignorableWhitespace information. 459 //System.out.println("in the space"); 460 //fDocumentHandler.ignorableWhitespace(getCharacterData(), null); 461 break; 462 case XMLStreamConstants.ENTITY_REFERENCE : 463 fEntityScanner.checkNodeCount(fEntityScanner.fCurrentEntity); 464 //entity reference callback are given in startEntity 465 break; 466 case XMLStreamConstants.PROCESSING_INSTRUCTION : 467 fEntityScanner.checkNodeCount(fEntityScanner.fCurrentEntity); 468 fDocumentHandler.processingInstruction(getPITarget(),getPIData(),null); 469 break; 470 case XMLStreamConstants.COMMENT : 471 fEntityScanner.checkNodeCount(fEntityScanner.fCurrentEntity); 472 fDocumentHandler.comment(getCharacterData(),null); 473 break; 474 case XMLStreamConstants.DTD : 475 //all DTD related callbacks are handled in DTDScanner. 476 //1. Stax doesn't define DTD states as it does for XML Document. 477 //therefore we don't need to take care of anything here. So Just break; 478 break; 479 case XMLStreamConstants.CDATA: 480 fEntityScanner.checkNodeCount(fEntityScanner.fCurrentEntity); 481 fDocumentHandler.startCDATA(null); 482 //xxx: check if CDATA values comes from getCharacterData() function 483 fDocumentHandler.characters(getCharacterData(),null); 484 fDocumentHandler.endCDATA(null); 485 //System.out.println(" in CDATA of the XMLNSDocumentScannerImpl"); 486 break; 487 case XMLStreamConstants.NOTATION_DECLARATION : 488 break; 489 case XMLStreamConstants.ENTITY_DECLARATION : 490 break; 491 case XMLStreamConstants.NAMESPACE : 492 break; 493 case XMLStreamConstants.ATTRIBUTE : 494 break; 495 case XMLStreamConstants.END_ELEMENT : 496 //do not give callback here. 497 //this callback is given in scanEndElement function. 498 //fDocumentHandler.endElement(getElementQName(),null); 499 break; 500 default : 501 throw new InternalError("processing event: " + event); 502 503 } 504 //System.out.println("here in before calling next"); 505 event = next(); 506 //System.out.println("here in after calling next"); 507 } while (event!=XMLStreamConstants.END_DOCUMENT && complete); 508 509 if(event == XMLStreamConstants.END_DOCUMENT) { 510 fDocumentHandler.endDocument(null); 511 return false; 512 } 513 514 return true; 515 516 } // scanDocument(boolean):boolean 517 518 519 520 public com.sun.org.apache.xerces.internal.xni.QName getElementQName(){ 521 if(fScannerLastState == XMLEvent.END_ELEMENT){ 522 fElementQName.setValues(fElementStack.getLastPoppedElement()); 523 } 524 return fElementQName ; 525 } 526 527 /** return the next state on the input 528 * @return int 529 */ 530 531 public int next() throws IOException, XNIException { 532 return fDriver.next(); 533 } 534 535 // 536 // XMLComponent methods 537 // 538 539 /** 540 * Resets the component. The component can query the component manager 541 * about any features and properties that affect the operation of the 542 * component. 543 * 544 * @param componentManager The component manager. 545 * 546 * @throws SAXException Thrown by component on initialization error. 547 * For example, if a feature or property is 548 * required for the operation of the component, the 549 * component manager may throw a 550 * SAXNotRecognizedException or a 551 * SAXNotSupportedException. 552 */ 553 554 public void reset(XMLComponentManager componentManager) 555 throws XMLConfigurationException { 556 557 super.reset(componentManager); 558 559 // other settings 560 // fDocumentSystemId = null; 561 562 // sax features 563 //fAttributes.setNamespaces(fNamespaces); 564 565 // xerces features 566 fReportCdataEvent = componentManager.getFeature(Constants.STAX_REPORT_CDATA_EVENT, true); 567 fSecurityManager = (XMLSecurityManager)componentManager.getProperty(Constants.SECURITY_MANAGER, null); 568 fNotifyBuiltInRefs = componentManager.getFeature(NOTIFY_BUILTIN_REFS, false); 569 570 Object resolver = componentManager.getProperty(ENTITY_RESOLVER, null); 571 fExternalSubsetResolver = (resolver instanceof ExternalSubsetResolver) ? 572 (ExternalSubsetResolver) resolver : null; 573 574 //attribute 575 fReadingAttributes = false; 576 //xxx: external entities are supported in Xerces 577 // it would be good to define feature for this case 578 fSupportExternalEntities = true; 579 fReplaceEntityReferences = true; 580 fIsCoalesce = false; 581 582 // setup Driver 583 setScannerState(SCANNER_STATE_CONTENT); 584 setDriver(fContentDriver); 585 586 // JAXP 1.5 features and properties 587 XMLSecurityPropertyManager spm = (XMLSecurityPropertyManager) 588 componentManager.getProperty(XML_SECURITY_PROPERTY_MANAGER, null); 589 fAccessExternalDTD = spm.getValue(XMLSecurityPropertyManager.Property.ACCESS_EXTERNAL_DTD); 590 591 fStrictURI = componentManager.getFeature(STANDARD_URI_CONFORMANT, false); 592 593 resetCommon(); 594 //fEntityManager.test(); 595 } // reset(XMLComponentManager) 596 597 598 public void reset(PropertyManager propertyManager){ 599 600 super.reset(propertyManager); 601 602 // other settings 603 // fDocumentSystemId = null; 604 fNamespaces = ((Boolean)propertyManager.getProperty(XMLInputFactory.IS_NAMESPACE_AWARE)).booleanValue(); 605 fNotifyBuiltInRefs = false ; 606 607 //fElementStack2.clear(); 608 //fReplaceEntityReferences = true; 609 //fSupportExternalEntities = true; 610 Boolean bo = (Boolean)propertyManager.getProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES); 611 fReplaceEntityReferences = bo.booleanValue(); 612 bo = (Boolean)propertyManager.getProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES); 613 fSupportExternalEntities = bo.booleanValue(); 614 Boolean cdata = (Boolean)propertyManager.getProperty(Constants.ZEPHYR_PROPERTY_PREFIX + Constants.STAX_REPORT_CDATA_EVENT) ; 615 if(cdata != null) 616 fReportCdataEvent = cdata.booleanValue() ; 617 Boolean coalesce = (Boolean)propertyManager.getProperty(XMLInputFactory.IS_COALESCING) ; 618 if(coalesce != null) 619 fIsCoalesce = coalesce.booleanValue(); 620 fReportCdataEvent = fIsCoalesce ? false : (fReportCdataEvent && true) ; 621 //if fIsCoalesce is set to true, set the value of fReplaceEntityReferences to true, 622 //if fIsCoalesce is set to false, take the value of fReplaceEntityReferences as set by application 623 fReplaceEntityReferences = fIsCoalesce ? true : fReplaceEntityReferences; 624 // setup Driver 625 //we dont need to do this -- nb. 626 //setScannerState(SCANNER_STATE_CONTENT); 627 //setDriver(fContentDriver); 628 //fEntityManager.test(); 629 630 // JAXP 1.5 features and properties 631 XMLSecurityPropertyManager spm = (XMLSecurityPropertyManager) 632 propertyManager.getProperty(XML_SECURITY_PROPERTY_MANAGER); 633 fAccessExternalDTD = spm.getValue(XMLSecurityPropertyManager.Property.ACCESS_EXTERNAL_DTD); 634 635 fSecurityManager = (XMLSecurityManager)propertyManager.getProperty(Constants.SECURITY_MANAGER); 636 resetCommon(); 637 } // reset(XMLComponentManager) 638 639 void resetCommon() { 640 // initialize vars 641 fMarkupDepth = 0; 642 fCurrentElement = null; 643 fElementStack.clear(); 644 fHasExternalDTD = false; 645 fStandaloneSet = false; 646 fStandalone = false; 647 fInScanContent = false; 648 //skipping algorithm 649 fShouldSkip = false; 650 fAdd = false; 651 fSkip = false; 652 653 fEntityStore = fEntityManager.getEntityStore(); 654 dtdGrammarUtil = null; 655 656 if (fSecurityManager != null) { 657 fElementAttributeLimit = fSecurityManager.getLimit(XMLSecurityManager.Limit.ELEMENT_ATTRIBUTE_LIMIT); 658 fXMLNameLimit = fSecurityManager.getLimit(XMLSecurityManager.Limit.MAX_NAME_LIMIT); 659 } else { 660 fElementAttributeLimit = 0; 661 fXMLNameLimit = XMLSecurityManager.Limit.MAX_NAME_LIMIT.defaultValue(); 662 } 663 fLimitAnalyzer = fEntityManager.fLimitAnalyzer; 664 } 665 666 /** 667 * Returns a list of feature identifiers that are recognized by 668 * this component. This method may return null if no features 669 * are recognized by this component. 670 */ 671 public String[] getRecognizedFeatures() { 672 return (String[])(RECOGNIZED_FEATURES.clone()); 673 } // getRecognizedFeatures():String[] 674 675 /** 676 * Sets the state of a feature. This method is called by the component 677 * manager any time after reset when a feature changes state. 678 * <p> 679 * <strong>Note:</strong> Components should silently ignore features 680 * that do not affect the operation of the component. 681 * 682 * @param featureId The feature identifier. 683 * @param state The state of the feature. 684 * 685 * @throws SAXNotRecognizedException The component should not throw 686 * this exception. 687 * @throws SAXNotSupportedException The component should not throw 688 * this exception. 689 */ 690 public void setFeature(String featureId, boolean state) 691 throws XMLConfigurationException { 692 693 super.setFeature(featureId, state); 694 695 // Xerces properties 696 if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) { 697 String feature = featureId.substring(Constants.XERCES_FEATURE_PREFIX.length()); 698 if (feature.equals(Constants.NOTIFY_BUILTIN_REFS_FEATURE)) { 699 fNotifyBuiltInRefs = state; 700 } 701 } 702 703 } // setFeature(String,boolean) 704 705 /** 706 * Returns a list of property identifiers that are recognized by 707 * this component. This method may return null if no properties 708 * are recognized by this component. 709 */ 710 public String[] getRecognizedProperties() { 711 return (String[])(RECOGNIZED_PROPERTIES.clone()); 712 } // getRecognizedProperties():String[] 713 714 /** 715 * Sets the value of a property. This method is called by the component 716 * manager any time after reset when a property changes value. 717 * <p> 718 * <strong>Note:</strong> Components should silently ignore properties 719 * that do not affect the operation of the component. 720 * 721 * @param propertyId The property identifier. 722 * @param value The value of the property. 723 * 724 * @throws SAXNotRecognizedException The component should not throw 725 * this exception. 726 * @throws SAXNotSupportedException The component should not throw 727 * this exception. 728 */ 729 public void setProperty(String propertyId, Object value) 730 throws XMLConfigurationException { 731 732 super.setProperty(propertyId, value); 733 734 // Xerces properties 735 if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) { 736 final int suffixLength = propertyId.length() - Constants.XERCES_PROPERTY_PREFIX.length(); 737 if (suffixLength == Constants.ENTITY_MANAGER_PROPERTY.length() && 738 propertyId.endsWith(Constants.ENTITY_MANAGER_PROPERTY)) { 739 fEntityManager = (XMLEntityManager)value; 740 return; 741 } 742 if (suffixLength == Constants.ENTITY_RESOLVER_PROPERTY.length() && 743 propertyId.endsWith(Constants.ENTITY_RESOLVER_PROPERTY)) { 744 fExternalSubsetResolver = (value instanceof ExternalSubsetResolver) ? 745 (ExternalSubsetResolver) value : null; 746 return; 747 } 748 } 749 750 751 // Xerces properties 752 if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) { 753 String property = propertyId.substring(Constants.XERCES_PROPERTY_PREFIX.length()); 754 if (property.equals(Constants.ENTITY_MANAGER_PROPERTY)) { 755 fEntityManager = (XMLEntityManager)value; 756 } 757 return; 758 } 759 760 //JAXP 1.5 properties 761 if (propertyId.equals(XML_SECURITY_PROPERTY_MANAGER)) 762 { 763 XMLSecurityPropertyManager spm = (XMLSecurityPropertyManager)value; 764 fAccessExternalDTD = spm.getValue(XMLSecurityPropertyManager.Property.ACCESS_EXTERNAL_DTD); 765 } 766 767 } // setProperty(String,Object) 768 769 /** 770 * Returns the default state for a feature, or null if this 771 * component does not want to report a default value for this 772 * feature. 773 * 774 * @param featureId The feature identifier. 775 * 776 * @since Xerces 2.2.0 777 */ 778 public Boolean getFeatureDefault(String featureId) { 779 for (int i = 0; i < RECOGNIZED_FEATURES.length; i++) { 780 if (RECOGNIZED_FEATURES[i].equals(featureId)) { 781 return FEATURE_DEFAULTS[i]; 782 } 783 } 784 return null; 785 } // getFeatureDefault(String):Boolean 786 787 /** 788 * Returns the default state for a property, or null if this 789 * component does not want to report a default value for this 790 * property. 791 * 792 * @param propertyId The property identifier. 793 * 794 * @since Xerces 2.2.0 795 */ 796 public Object getPropertyDefault(String propertyId) { 797 for (int i = 0; i < RECOGNIZED_PROPERTIES.length; i++) { 798 if (RECOGNIZED_PROPERTIES[i].equals(propertyId)) { 799 return PROPERTY_DEFAULTS[i]; 800 } 801 } 802 return null; 803 } // getPropertyDefault(String):Object 804 805 // 806 // XMLDocumentSource methods 807 // 808 809 /** 810 * setDocumentHandler 811 * 812 * @param documentHandler 813 */ 814 public void setDocumentHandler(XMLDocumentHandler documentHandler) { 815 fDocumentHandler = documentHandler; 816 //System.out.println(" In Set DOCUMENT HANDLER" + fDocumentHandler + " scanner =" + this); 817 } // setDocumentHandler(XMLDocumentHandler) 818 819 820 /** Returns the document handler */ 821 public XMLDocumentHandler getDocumentHandler(){ 822 return fDocumentHandler; 823 } 824 825 // 826 // XMLEntityHandler methods 827 // 828 829 /** 830 * This method notifies of the start of an entity. The DTD has the 831 * pseudo-name of "[dtd]" parameter entity names start with '%'; and 832 * general entities are just specified by their name. 833 * 834 * @param name The name of the entity. 835 * @param identifier The resource identifier. 836 * @param encoding The auto-detected IANA encoding name of the entity 837 * stream. This value will be null in those situations 838 * where the entity encoding is not auto-detected (e.g. 839 * internal entities or a document entity that is 840 * parsed from a java.io.Reader). 841 * @param augs Additional information that may include infoset augmentations 842 * 843 * @throws XNIException Thrown by handler to signal an error. 844 */ 845 public void startEntity(String name, 846 XMLResourceIdentifier identifier, 847 String encoding, Augmentations augs) throws XNIException { 848 849 // keep track of this entity before fEntityDepth is increased 850 if (fEntityDepth == fEntityStack.length) { 851 int[] entityarray = new int[fEntityStack.length * 2]; 852 System.arraycopy(fEntityStack, 0, entityarray, 0, fEntityStack.length); 853 fEntityStack = entityarray; 854 } 855 fEntityStack[fEntityDepth] = fMarkupDepth; 856 857 super.startEntity(name, identifier, encoding, augs); 858 859 // WFC: entity declared in external subset in standalone doc 860 if(fStandalone && fEntityStore.isEntityDeclInExternalSubset(name)) { 861 reportFatalError("MSG_REFERENCE_TO_EXTERNALLY_DECLARED_ENTITY_WHEN_STANDALONE", 862 new Object[]{name}); 863 } 864 865 /** we are not calling the handlers yet.. */ 866 // call handler 867 if (fDocumentHandler != null && !fScanningAttribute) { 868 if (!name.equals("[xml]")) { 869 fDocumentHandler.startGeneralEntity(name, identifier, encoding, augs); 870 } 871 } 872 873 } // startEntity(String,XMLResourceIdentifier,String) 874 875 /** 876 * This method notifies the end of an entity. The DTD has the pseudo-name 877 * of "[dtd]" parameter entity names start with '%'; and general entities 878 * are just specified by their name. 879 * 880 * @param name The name of the entity. 881 * @param augs Additional information that may include infoset augmentations 882 * 883 * @throws XNIException Thrown by handler to signal an error. 884 */ 885 public void endEntity(String name, Augmentations augs) throws IOException, XNIException { 886 887 /** 888 * // flush possible pending output buffer - see scanContent 889 * if (fInScanContent && fStringBuffer.length != 0 890 * && fDocumentHandler != null) { 891 * fDocumentHandler.characters(fStringBuffer, null); 892 * fStringBuffer.length = 0; // make sure we know it's been flushed 893 * } 894 */ 895 super.endEntity(name, augs); 896 897 // make sure markup is properly balanced 898 if (fMarkupDepth != fEntityStack[fEntityDepth]) { 899 reportFatalError("MarkupEntityMismatch", null); 900 } 901 902 /**/ 903 // call handler 904 if (fDocumentHandler != null && !fScanningAttribute) { 905 if (!name.equals("[xml]")) { 906 fDocumentHandler.endGeneralEntity(name, augs); 907 } 908 } 909 910 911 } // endEntity(String) 912 913 // 914 // Protected methods 915 // 916 917 // Driver factory methods 918 919 /** Creates a content Driver. */ 920 protected Driver createContentDriver() { 921 return new FragmentContentDriver(); 922 } // createContentDriver():Driver 923 924 // scanning methods 925 926 /** 927 * Scans an XML or text declaration. 928 * <p> 929 * <pre> 930 * [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>' 931 * [24] VersionInfo ::= S 'version' Eq (' VersionNum ' | " VersionNum ") 932 * [80] EncodingDecl ::= S 'encoding' Eq ('"' EncName '"' | "'" EncName "'" ) 933 * [81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')* 934 * [32] SDDecl ::= S 'standalone' Eq (("'" ('yes' | 'no') "'") 935 * | ('"' ('yes' | 'no') '"')) 936 * 937 * [77] TextDecl ::= '<?xml' VersionInfo? EncodingDecl S? '?>' 938 * </pre> 939 * 940 * @param scanningTextDecl True if a text declaration is to 941 * be scanned instead of an XML 942 * declaration. 943 */ 944 protected void scanXMLDeclOrTextDecl(boolean scanningTextDecl) 945 throws IOException, XNIException { 946 947 // scan decl 948 super.scanXMLDeclOrTextDecl(scanningTextDecl, fStrings); 949 fMarkupDepth--; 950 951 // pseudo-attribute values 952 String version = fStrings[0]; 953 String encoding = fStrings[1]; 954 String standalone = fStrings[2]; 955 fDeclaredEncoding = encoding; 956 // set standalone 957 fStandaloneSet = standalone != null; 958 fStandalone = fStandaloneSet && standalone.equals("yes"); 959 ///xxx see where its used.. this is not used anywhere. it may be useful for entity to store this information 960 //but this information is only related with Document Entity. 961 fEntityManager.setStandalone(fStandalone); 962 963 964 // call handler 965 if (fDocumentHandler != null) { 966 if (scanningTextDecl) { 967 fDocumentHandler.textDecl(version, encoding, null); 968 } else { 969 fDocumentHandler.xmlDecl(version, encoding, standalone, null); 970 } 971 } 972 973 if(version != null){ 974 fEntityScanner.setVersion(version); 975 fEntityScanner.setXMLVersion(version); 976 } 977 // set encoding on reader, only if encoding was not specified by the application explicitly 978 if (encoding != null && !fEntityScanner.getCurrentEntity().isEncodingExternallySpecified()) { 979 fEntityScanner.setEncoding(encoding); 980 } 981 982 } // scanXMLDeclOrTextDecl(boolean) 983 984 public String getPITarget(){ 985 return fPITarget ; 986 } 987 988 public XMLStringBuffer getPIData(){ 989 return fContentBuffer ; 990 } 991 992 //XXX: why not this function behave as per the state of the parser? 993 public XMLString getCharacterData(){ 994 if(fUsebuffer){ 995 return fContentBuffer ; 996 }else{ 997 return fTempString; 998 } 999 1000 } 1001 1002 1003 /** 1004 * Scans a processing data. This is needed to handle the situation 1005 * where a document starts with a processing instruction whose 1006 * target name <em>starts with</em> "xml". (e.g. xmlfoo) 1007 * 1008 * @param target The PI target 1009 * @param data The XMLStringBuffer to fill in with the data 1010 */ 1011 protected void scanPIData(String target, XMLStringBuffer data) 1012 throws IOException, XNIException { 1013 1014 super.scanPIData(target, data); 1015 1016 //set the PI target and values 1017 fPITarget = target ; 1018 1019 fMarkupDepth--; 1020 1021 } // scanPIData(String) 1022 1023 /** 1024 * Scans a comment. 1025 * <p> 1026 * <pre> 1027 * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->' 1028 * </pre> 1029 * <p> 1030 * <strong>Note:</strong> Called after scanning past '<!--' 1031 */ 1032 protected void scanComment() throws IOException, XNIException { 1033 fContentBuffer.clear(); 1034 scanComment(fContentBuffer); 1035 //getTextCharacters can also be called for reading comments 1036 fUsebuffer = true; 1037 fMarkupDepth--; 1038 1039 } // scanComment() 1040 1041 //xxx value returned by this function may not remain valid if another event is scanned. 1042 public String getComment(){ 1043 return fContentBuffer.toString(); 1044 } 1045 1046 void addElement(String rawname){ 1047 if(fElementPointer < ELEMENT_ARRAY_LENGTH){ 1048 //storing element raw name in a linear list of array 1049 fElementArray[fElementPointer] = rawname ; 1050 //storing elemnetPointer for particular element depth 1051 1052 if(DEBUG_SKIP_ALGORITHM){ 1053 StringBuffer sb = new StringBuffer() ; 1054 sb.append(" Storing element information ") ; 1055 sb.append(" fElementPointer = " + fElementPointer) ; 1056 sb.append(" fElementRawname = " + fElementQName.rawname) ; 1057 sb.append(" fElementStack.fDepth = " + fElementStack.fDepth); 1058 System.out.println(sb.toString()) ; 1059 } 1060 1061 //store pointer information only when element depth is less MAX_DEPTH_LIMIT 1062 if(fElementStack.fDepth < MAX_DEPTH_LIMIT){ 1063 short column = storePointerForADepth(fElementPointer); 1064 if(column > 0){ 1065 short pointer = getElementPointer((short)fElementStack.fDepth, (short)(column - 1) ); 1066 //identity comparison shouldn't take much time and we can rely on this 1067 //since its guaranteed to have same object id for same string. 1068 if(rawname == fElementArray[pointer]){ 1069 fShouldSkip = true ; 1070 fLastPointerLocation = pointer ; 1071 //reset the things and return. 1072 resetPointer((short)fElementStack.fDepth , column) ; 1073 fElementArray[fElementPointer] = null ; 1074 return ; 1075 }else{ 1076 fShouldSkip = false ; 1077 } 1078 } 1079 } 1080 fElementPointer++ ; 1081 } 1082 } 1083 1084 1085 void resetPointer(short depth, short column){ 1086 fPointerInfo[depth] [column] = (short)0; 1087 } 1088 1089 //returns column information at which pointer was stored. 1090 short storePointerForADepth(short elementPointer){ 1091 short depth = (short) fElementStack.fDepth ; 1092 1093 //Stores element pointer locations at particular depth , only 4 pointer locations 1094 //are stored at particular depth for now. 1095 for(short i = 0 ; i < MAX_POINTER_AT_A_DEPTH ; i++){ 1096 1097 if(canStore(depth, i)){ 1098 fPointerInfo[depth][i] = elementPointer ; 1099 if(DEBUG_SKIP_ALGORITHM){ 1100 StringBuffer sb = new StringBuffer() ; 1101 sb.append(" Pointer information ") ; 1102 sb.append(" fElementPointer = " + fElementPointer) ; 1103 sb.append(" fElementStack.fDepth = " + fElementStack.fDepth); 1104 sb.append(" column = " + i ) ; 1105 System.out.println(sb.toString()) ; 1106 } 1107 return i; 1108 } 1109 //else 1110 //pointer was not stored because we reached the limit 1111 } 1112 return -1 ; 1113 } 1114 1115 boolean canStore(short depth, short column){ 1116 //colum = 0 , means first element at particular depth 1117 //column = 1, means second element at particular depth 1118 // calle should make sure that it doesn't call for value outside allowed co-ordinates 1119 return fPointerInfo[depth][column] == 0 ? true : false ; 1120 } 1121 1122 1123 short getElementPointer(short depth, short column){ 1124 //colum = 0 , means first element at particular depth 1125 //column = 1, means second element at particular depth 1126 // calle should make sure that it doesn't call for value outside allowed co-ordinates 1127 return fPointerInfo[depth][column] ; 1128 } 1129 1130 //this function assumes that string passed is not null and skips 1131 //the following string from the buffer this makes sure 1132 boolean skipFromTheBuffer(String rawname) throws IOException{ 1133 if(fEntityScanner.skipString(rawname)){ 1134 char c = (char)fEntityScanner.peekChar() ; 1135 //If the start element was completely skipped we should encounter either ' '(space), 1136 //or '/' (in case of empty element) or '>' 1137 if( c == ' ' || c == '/' || c == '>'){ 1138 fElementRawname = rawname ; 1139 return true ; 1140 } else{ 1141 return false; 1142 } 1143 } else 1144 return false ; 1145 } 1146 1147 boolean skipQElement(String rawname) throws IOException{ 1148 1149 final int c = fEntityScanner.getChar(rawname.length()); 1150 //if this character is still valid element name -- this means string can't match 1151 if(XMLChar.isName(c)){ 1152 return false; 1153 }else{ 1154 return fEntityScanner.skipString(rawname); 1155 } 1156 } 1157 1158 protected boolean skipElement() throws IOException { 1159 1160 if(!fShouldSkip) return false ; 1161 1162 if(fLastPointerLocation != 0){ 1163 //Look at the next element stored in the array list.. we might just get a match. 1164 String rawname = fElementArray[fLastPointerLocation + 1] ; 1165 if(rawname != null && skipFromTheBuffer(rawname)){ 1166 fLastPointerLocation++ ; 1167 if(DEBUG_SKIP_ALGORITHM){ 1168 System.out.println("Element " + fElementRawname + " was SKIPPED at pointer location = " + fLastPointerLocation); 1169 } 1170 return true ; 1171 } else{ 1172 //reset it back to zero... we haven't got the correct subset yet. 1173 fLastPointerLocation = 0 ; 1174 1175 } 1176 } 1177 //xxx: we can put some logic here as from what column it should start looking 1178 //for now we always start at 0 1179 //fallback to tolerant algorithm, it would look for differnt element stored at different 1180 //depth and get us the pointer location. 1181 return fShouldSkip && skipElement((short)0); 1182 1183 } 1184 1185 //start of the column at which it should try searching 1186 boolean skipElement(short column) throws IOException { 1187 short depth = (short)fElementStack.fDepth ; 1188 1189 if(depth > MAX_DEPTH_LIMIT){ 1190 return fShouldSkip = false ; 1191 } 1192 for(short i = column ; i < MAX_POINTER_AT_A_DEPTH ; i++){ 1193 short pointer = getElementPointer(depth , i ) ; 1194 1195 if(pointer == 0){ 1196 return fShouldSkip = false ; 1197 } 1198 1199 if(fElementArray[pointer] != null && skipFromTheBuffer(fElementArray[pointer])){ 1200 if(DEBUG_SKIP_ALGORITHM){ 1201 System.out.println(); 1202 System.out.println("Element " + fElementRawname + " was SKIPPED at depth = " + fElementStack.fDepth + " column = " + column ); 1203 System.out.println(); 1204 } 1205 fLastPointerLocation = pointer ; 1206 return fShouldSkip = true ; 1207 } 1208 } 1209 return fShouldSkip = false ; 1210 } 1211 1212 /** 1213 * Scans a start element. This method will handle the binding of 1214 * namespace information and notifying the handler of the start 1215 * of the element. 1216 * <p> 1217 * <pre> 1218 * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>' 1219 * [40] STag ::= '<' Name (S Attribute)* S? '>' 1220 * </pre> 1221 * <p> 1222 * <strong>Note:</strong> This method assumes that the leading 1223 * '<' character has been consumed. 1224 * <p> 1225 * <strong>Note:</strong> This method uses the fElementQName and 1226 * fAttributes variables. The contents of these variables will be 1227 * destroyed. The caller should copy important information out of 1228 * these variables before calling this method. 1229 * NB: Content in fAttributes is valid only till the state of the parser is XMLEvent.START_ELEMENT 1230 * 1231 * @return True if element is empty. (i.e. It matches 1232 * production [44]. 1233 */ 1234 // fElementQName will have the details of element just read.. 1235 // fAttributes will have the details of all the attributes. 1236 protected boolean scanStartElement() 1237 throws IOException, XNIException { 1238 1239 if (DEBUG_START_END_ELEMENT) System.out.println( this.getClass().toString() + ">>> scanStartElement()"); 1240 //when skipping is true and no more elements should be added 1241 if(fSkip && !fAdd){ 1242 //get the stored element -- if everything goes right this should match the 1243 //token in the buffer 1244 1245 QName name = fElementStack.getNext(); 1246 1247 if(DEBUG_SKIP_ALGORITHM){ 1248 System.out.println("Trying to skip String = " + name.rawname); 1249 } 1250 1251 //Be conservative -- if skipping fails -- stop. 1252 fSkip = fEntityScanner.skipString(name.rawname); 1253 1254 if(fSkip){ 1255 if(DEBUG_SKIP_ALGORITHM){ 1256 System.out.println("Element SUCESSFULLY skipped = " + name.rawname); 1257 } 1258 fElementStack.push(); 1259 fElementQName = name; 1260 }else{ 1261 //if skipping fails reposition the stack or fallback to normal way of processing 1262 fElementStack.reposition(); 1263 if(DEBUG_SKIP_ALGORITHM){ 1264 System.out.println("Element was NOT skipped, REPOSITIONING stack" ); 1265 } 1266 } 1267 } 1268 1269 //we are still at the stage of adding elements 1270 //the elements were not matched or 1271 //fSkip is not set to true 1272 if(!fSkip || fAdd){ 1273 //get the next element from the stack 1274 fElementQName = fElementStack.nextElement(); 1275 // name 1276 if (fNamespaces) { 1277 fEntityScanner.scanQName(fElementQName, NameType.ELEMENTSTART); 1278 } else { 1279 String name = fEntityScanner.scanName(NameType.ELEMENTSTART); 1280 fElementQName.setValues(null, name, name, null); 1281 } 1282 1283 if(DEBUG)System.out.println("Element scanned in start element is " + fElementQName.toString()); 1284 if(DEBUG_SKIP_ALGORITHM){ 1285 if(fAdd){ 1286 System.out.println("Elements are being ADDED -- elemet added is = " + fElementQName.rawname + " at count = " + fElementStack.fCount); 1287 } 1288 } 1289 1290 } 1291 1292 //when the elements are being added , we need to check if we are set for skipping the elements 1293 if(fAdd){ 1294 //this sets the value of fAdd variable 1295 fElementStack.matchElement(fElementQName); 1296 } 1297 1298 1299 //xxx: We dont need another pointer, fCurrentElement, we can use fElementQName 1300 fCurrentElement = fElementQName; 1301 1302 String rawname = fElementQName.rawname; 1303 1304 fEmptyElement = false; 1305 1306 fAttributes.removeAllAttributes(); 1307 1308 checkDepth(rawname); 1309 if(!seekCloseOfStartTag()){ 1310 fReadingAttributes = true; 1311 fAttributeCacheUsedCount =0; 1312 fStringBufferIndex =0; 1313 fAddDefaultAttr = true; 1314 do { 1315 scanAttribute(fAttributes); 1316 if (fSecurityManager != null && !fSecurityManager.isNoLimit(fElementAttributeLimit) && 1317 fAttributes.getLength() > fElementAttributeLimit){ 1318 fErrorReporter.reportError(XMLMessageFormatter.XML_DOMAIN, 1319 "ElementAttributeLimit", 1320 new Object[]{rawname, fElementAttributeLimit }, 1321 XMLErrorReporter.SEVERITY_FATAL_ERROR ); 1322 } 1323 1324 } while (!seekCloseOfStartTag()); 1325 fReadingAttributes=false; 1326 } 1327 1328 if (fEmptyElement) { 1329 //decrease the markup depth.. 1330 fMarkupDepth--; 1331 1332 // check that this element was opened in the same entity 1333 if (fMarkupDepth < fEntityStack[fEntityDepth - 1]) { 1334 reportFatalError("ElementEntityMismatch", 1335 new Object[]{fCurrentElement.rawname}); 1336 } 1337 // call handler 1338 if (fDocumentHandler != null) { 1339 fDocumentHandler.emptyElement(fElementQName, fAttributes, null); 1340 } 1341 1342 //We should not be popping out the context here in endELement becaause the namespace context is still 1343 //valid when parser is at the endElement state. 1344 //if (fNamespaces) { 1345 // fNamespaceContext.popContext(); 1346 //} 1347 1348 //pop the element off the stack.. 1349 fElementStack.popElement(); 1350 1351 } else { 1352 1353 if(dtdGrammarUtil != null) 1354 dtdGrammarUtil.startElement(fElementQName, fAttributes); 1355 if(fDocumentHandler != null){ 1356 //complete element and attributes are traversed in this function so we can send a callback 1357 //here. 1358 //<strong>we shouldn't be sending callback in scanDocument()</strong> 1359 fDocumentHandler.startElement(fElementQName, fAttributes, null); 1360 } 1361 } 1362 1363 1364 if (DEBUG_START_END_ELEMENT) System.out.println(this.getClass().toString() + "<<< scanStartElement(): "+fEmptyElement); 1365 return fEmptyElement; 1366 1367 } // scanStartElement():boolean 1368 1369 /** 1370 * Looks for the close of start tag, i.e. if it finds '>' or '/>' 1371 * Characters are consumed. 1372 */ 1373 protected boolean seekCloseOfStartTag() throws IOException, XNIException { 1374 // spaces 1375 boolean sawSpace = fEntityScanner.skipSpaces(); 1376 1377 // end tag? 1378 final int c = fEntityScanner.peekChar(); 1379 if (c == '>') { 1380 fEntityScanner.scanChar(null); 1381 return true; 1382 } else if (c == '/') { 1383 fEntityScanner.scanChar(null); 1384 if (!fEntityScanner.skipChar('>', NameType.ELEMENTEND)) { 1385 reportFatalError("ElementUnterminated", 1386 new Object[]{fElementQName.rawname}); 1387 } 1388 fEmptyElement = true; 1389 return true; 1390 } else if (!isValidNameStartChar(c) || !sawSpace) { 1391 // Second chance. Check if this character is a high 1392 // surrogate of a valid name start character. 1393 if (!isValidNameStartHighSurrogate(c) || !sawSpace) { 1394 reportFatalError("ElementUnterminated", 1395 new Object[]{fElementQName.rawname}); 1396 } 1397 } 1398 1399 return false; 1400 } 1401 1402 public boolean hasAttributes(){ 1403 return fAttributes.getLength() > 0 ? true : false ; 1404 } 1405 1406 1407 /** 1408 * Scans an attribute. 1409 * <p> 1410 * <pre> 1411 * [41] Attribute ::= Name Eq AttValue 1412 * </pre> 1413 * <p> 1414 * <strong>Note:</strong> This method assumes that the next 1415 * character on the stream is the first character of the attribute 1416 * name. 1417 * <p> 1418 * <strong>Note:</strong> This method uses the fAttributeQName and 1419 * fQName variables. The contents of these variables will be 1420 * destroyed. 1421 * 1422 * @param attributes The attributes list for the scanned attribute. 1423 */ 1424 1425 /** 1426 * protected void scanAttribute(AttributeIteratorImpl attributes) 1427 * throws IOException, XNIException { 1428 * if (DEBUG_START_END_ELEMENT) System.out.println(">>> scanAttribute()"); 1429 * 1430 * 1431 * // name 1432 * if (fNamespaces) { 1433 * fEntityScanner.scanQName(fAttributeQName); 1434 * } 1435 * else { 1436 * String name = fEntityScanner.scanName(); 1437 * fAttributeQName.setValues(null, name, name, null); 1438 * } 1439 * 1440 * // equals 1441 * fEntityScanner.skipSpaces(); 1442 * if (!fEntityScanner.skipChar('=')) { 1443 * reportFatalError("EqRequiredInAttribute", 1444 * new Object[]{fAttributeQName.rawname}); 1445 * } 1446 * fEntityScanner.skipSpaces(); 1447 * 1448 * 1449 * // content 1450 * int oldLen = attributes.getLength(); 1451 */ 1452 /**xxx there is one check of duplicate attribute that has been removed. 1453 * attributes.addAttribute(fAttributeQName, XMLSymbols.fCDATASymbol, null); 1454 * 1455 * // WFC: Unique Att Spec 1456 * if (oldLen == attributes.getLength()) { 1457 * reportFatalError("AttributeNotUnique", 1458 * new Object[]{fCurrentElement.rawname, 1459 * fAttributeQName.rawname}); 1460 * } 1461 */ 1462 1463 /* 1464 //REVISIT: one more case needs to be included: external PE and standalone is no 1465 boolean isVC = fHasExternalDTD && !fStandalone; 1466 scanAttributeValue(fTempString, fTempString2, 1467 fAttributeQName.rawname, attributes, 1468 oldLen, isVC); 1469 1470 //attributes.setValue(oldLen, fTempString.toString()); 1471 //attributes.setNonNormalizedValue(oldLen, fTempString2.toString()); 1472 //attributes.setSpecified(oldLen, true); 1473 1474 AttributeImpl attribute = new AttributeImpl(fAttributeQName.prefix,fAttributeQName.localpart,fAttributeQName.uri,fTempString.toString(),fTempString2.toString(),XMLSymbols.fCDATASymbol,true); 1475 fAttributes.addAttribute(attribute); 1476 if (DEBUG_START_END_ELEMENT) System.out.println("<<< scanAttribute()"); 1477 } // scanAttribute(XMLAttributes) 1478 1479 */ 1480 1481 /** return the attribute iterator implementation */ 1482 public XMLAttributesIteratorImpl getAttributeIterator(){ 1483 if(dtdGrammarUtil != null && fAddDefaultAttr){ 1484 dtdGrammarUtil.addDTDDefaultAttrs(fElementQName,fAttributes); 1485 fAddDefaultAttr = false; 1486 } 1487 return fAttributes; 1488 } 1489 1490 /** return if standalone is set */ 1491 public boolean standaloneSet(){ 1492 return fStandaloneSet; 1493 } 1494 /** return if the doucment is standalone */ 1495 public boolean isStandAlone(){ 1496 return fStandalone ; 1497 } 1498 /** 1499 * Scans an attribute name value pair. 1500 * <p> 1501 * <pre> 1502 * [41] Attribute ::= Name Eq AttValue 1503 * </pre> 1504 * <p> 1505 * <strong>Note:</strong> This method assumes that the next 1506 * character on the stream is the first character of the attribute 1507 * name. 1508 * <p> 1509 * <strong>Note:</strong> This method uses the fAttributeQName and 1510 * fQName variables. The contents of these variables will be 1511 * destroyed. 1512 * 1513 * @param attributes The attributes list for the scanned attribute. 1514 */ 1515 1516 protected void scanAttribute(XMLAttributes attributes) 1517 throws IOException, XNIException { 1518 if (DEBUG_START_END_ELEMENT) System.out.println(this.getClass().toString() +">>> scanAttribute()"); 1519 1520 // name 1521 if (fNamespaces) { 1522 fEntityScanner.scanQName(fAttributeQName, NameType.ATTRIBUTENAME); 1523 } else { 1524 String name = fEntityScanner.scanName(NameType.ATTRIBUTENAME); 1525 fAttributeQName.setValues(null, name, name, null); 1526 } 1527 1528 // equals 1529 fEntityScanner.skipSpaces(); 1530 if (!fEntityScanner.skipChar('=', NameType.ATTRIBUTE)) { 1531 reportFatalError("EqRequiredInAttribute", 1532 new Object[] {fCurrentElement.rawname, fAttributeQName.rawname}); 1533 } 1534 fEntityScanner.skipSpaces(); 1535 1536 int attIndex = 0 ; 1537 //REVISIT: one more case needs to be included: external PE and standalone is no 1538 boolean isVC = fHasExternalDTD && !fStandalone; 1539 //fTempString would store attribute value 1540 ///fTempString2 would store attribute non-normalized value 1541 1542 //this function doesn't use 'attIndex'. We are adding the attribute later 1543 //after we have figured out that current attribute is not namespace declaration 1544 //since scanAttributeValue doesn't use attIndex parameter therefore we 1545 //can safely add the attribute later.. 1546 XMLString tmpStr = getString(); 1547 1548 scanAttributeValue(tmpStr, fTempString2, fAttributeQName.rawname, attributes, 1549 attIndex, isVC, fCurrentElement.rawname, false); 1550 1551 // content 1552 int oldLen = attributes.getLength(); 1553 //if the attribute name already exists.. new value is replaced with old value 1554 attIndex = attributes.addAttribute(fAttributeQName, XMLSymbols.fCDATASymbol, null); 1555 1556 // WFC: Unique Att Spec 1557 //attributes count will be same if the current attribute name already exists for this element name. 1558 //this means there are two duplicate attributes. 1559 if (oldLen == attributes.getLength()) { 1560 reportFatalError("AttributeNotUnique", 1561 new Object[]{fCurrentElement.rawname, 1562 fAttributeQName.rawname}); 1563 } 1564 1565 //tmpString contains attribute value 1566 //we are passing null as the attribute value 1567 attributes.setValue(attIndex, null, tmpStr); 1568 1569 ///xxx: nonNormalizedValue is not being set as it is not required by SAX & DOM 1570 //attributes.setNonNormalizedValue(oldLen, fTempString2.toString()); 1571 attributes.setSpecified(attIndex, true); 1572 1573 if (DEBUG_START_END_ELEMENT) System.out.println(this.getClass().toString() +"<<< scanAttribute()"); 1574 1575 } // scanAttribute(XMLAttributes) 1576 1577 /** 1578 * Scans element content. 1579 * 1580 * @return Returns the next character on the stream. 1581 */ 1582 //CHANGED: 1583 //EARLIER: scanContent() 1584 //NOW: scanContent(XMLStringBuffer) 1585 //It makes things easy if this functions takes XMLStringBuffer as parameter.. 1586 //this function appends the data to the buffer. 1587 protected int scanContent(XMLStringBuffer content) throws IOException, XNIException { 1588 //set the fTempString length to 0 before passing it on to scanContent 1589 //scanContent sets the correct co-ordinates as per the content read 1590 fTempString.length = 0; 1591 int c = fEntityScanner.scanContent(fTempString); 1592 content.append(fTempString); 1593 fTempString.length = 0; 1594 if (c == '\r') { 1595 // happens when there is the character reference 1596 //xxx: We know the next chracter.. we should just skip it and add ']' directlry 1597 fEntityScanner.scanChar(null); 1598 content.append((char)c); 1599 c = -1; 1600 } else if (c == ']') { 1601 //fStringBuffer.clear(); 1602 //xxx: We know the next chracter.. we should just skip it and add ']' directlry 1603 content.append((char)fEntityScanner.scanChar(null)); 1604 // remember where we are in case we get an endEntity before we 1605 // could flush the buffer out - this happens when we're parsing an 1606 // entity which ends with a ] 1607 fInScanContent = true; 1608 // 1609 // We work on a single character basis to handle cases such as: 1610 // ']]]>' which we might otherwise miss. 1611 // 1612 if (fEntityScanner.skipChar(']', null)) { 1613 content.append(']'); 1614 while (fEntityScanner.skipChar(']', null)) { 1615 content.append(']'); 1616 } 1617 if (fEntityScanner.skipChar('>', null)) { 1618 reportFatalError("CDEndInContent", null); 1619 } 1620 } 1621 fInScanContent = false; 1622 c = -1; 1623 } 1624 if (fDocumentHandler != null && content.length > 0) { 1625 //fDocumentHandler.characters(content, null); 1626 } 1627 return c; 1628 1629 } // scanContent():int 1630 1631 1632 /** 1633 * Scans a CDATA section. 1634 * <p> 1635 * <strong>Note:</strong> This method uses the fTempString and 1636 * fStringBuffer variables. 1637 * 1638 * @param complete True if the CDATA section is to be scanned 1639 * completely. 1640 * 1641 * @return True if CDATA is completely scanned. 1642 */ 1643 //CHANGED: 1644 protected boolean scanCDATASection(XMLStringBuffer contentBuffer, boolean complete) 1645 throws IOException, XNIException { 1646 1647 // call handler 1648 if (fDocumentHandler != null) { 1649 //fDocumentHandler.startCDATA(null); 1650 } 1651 1652 while (true) { 1653 //scanData will fill the contentBuffer 1654 if (!fEntityScanner.scanData("]]>", contentBuffer)) { 1655 break ; 1656 /** We dont need all this code if we pass ']]>' as delimeter.. 1657 * int brackets = 2; 1658 * while (fEntityScanner.skipChar(']')) { 1659 * brackets++; 1660 * } 1661 * 1662 * //When we find more than 2 square brackets 1663 * if (fDocumentHandler != null && brackets > 2) { 1664 * //we dont need to clear the buffer.. 1665 * //contentBuffer.clear(); 1666 * for (int i = 2; i < brackets; i++) { 1667 * contentBuffer.append(']'); 1668 * } 1669 * fDocumentHandler.characters(contentBuffer, null); 1670 * } 1671 * 1672 * if (fEntityScanner.skipChar('>')) { 1673 * break; 1674 * } 1675 * if (fDocumentHandler != null) { 1676 * //we dont need to clear the buffer now.. 1677 * //contentBuffer.clear(); 1678 * contentBuffer.append("]]"); 1679 * fDocumentHandler.characters(contentBuffer, null); 1680 * } 1681 **/ 1682 } else { 1683 int c = fEntityScanner.peekChar(); 1684 if (c != -1 && isInvalidLiteral(c)) { 1685 if (XMLChar.isHighSurrogate(c)) { 1686 //contentBuffer.clear(); 1687 //scan surrogates if any.... 1688 scanSurrogates(contentBuffer); 1689 } else { 1690 reportFatalError("InvalidCharInCDSect", 1691 new Object[]{Integer.toString(c,16)}); 1692 fEntityScanner.scanChar(null); 1693 } 1694 } 1695 //by this time we have also read surrogate contents if any... 1696 if (fDocumentHandler != null) { 1697 //fDocumentHandler.characters(contentBuffer, null); 1698 } 1699 } 1700 } 1701 fMarkupDepth--; 1702 1703 if (fDocumentHandler != null && contentBuffer.length > 0) { 1704 //fDocumentHandler.characters(contentBuffer, null); 1705 } 1706 1707 // call handler 1708 if (fDocumentHandler != null) { 1709 //fDocumentHandler.endCDATA(null); 1710 } 1711 1712 return true; 1713 1714 } // scanCDATASection(XMLStringBuffer, boolean):boolean 1715 1716 /** 1717 * Scans an end element. 1718 * <p> 1719 * <pre> 1720 * [42] ETag ::= '</' Name S? '>' 1721 * </pre> 1722 * <p> 1723 * <strong>Note:</strong> This method uses the fElementQName variable. 1724 * The contents of this variable will be destroyed. The caller should 1725 * copy the needed information out of this variable before calling 1726 * this method. 1727 * 1728 * @return The element depth. 1729 */ 1730 protected int scanEndElement() throws IOException, XNIException { 1731 if (DEBUG_START_END_ELEMENT) System.out.println(this.getClass().toString() +">>> scanEndElement()"); 1732 1733 // pop context 1734 QName endElementName = fElementStack.popElement(); 1735 1736 String rawname = endElementName.rawname; 1737 if(DEBUG)System.out.println("endElementName = " + endElementName.toString()); 1738 // Take advantage of the fact that next string _should_ be "fElementQName.rawName", 1739 //In scanners most of the time is consumed on checks done for XML characters, we can 1740 // optimize on it and avoid the checks done for endElement, 1741 //we will also avoid symbol table lookup - neeraj.bajaj@sun.com 1742 1743 // this should work both for namespace processing true or false... 1744 1745 //REVISIT: if the string is not the same as expected.. we need to do better error handling.. 1746 //We can skip this for now... In any case if the string doesn't match -- document is not well formed. 1747 1748 if (!fEntityScanner.skipString(endElementName.rawname)) { 1749 reportFatalError("ETagRequired", new Object[]{rawname}); 1750 } 1751 1752 // end 1753 fEntityScanner.skipSpaces(); 1754 if (!fEntityScanner.skipChar('>', NameType.ELEMENTEND)) { 1755 reportFatalError("ETagUnterminated", 1756 new Object[]{rawname}); 1757 } 1758 fMarkupDepth--; 1759 1760 //we have increased the depth for two markup "<" characters 1761 fMarkupDepth--; 1762 1763 // check that this element was opened in the same entity 1764 if (fMarkupDepth < fEntityStack[fEntityDepth - 1]) { 1765 reportFatalError("ElementEntityMismatch", 1766 new Object[]{rawname}); 1767 } 1768 1769 //We should not be popping out the context here in endELement becaause the namespace context is still 1770 //valid when parser is at the endElement state. 1771 1772 //if (fNamespaces) { 1773 // fNamespaceContext.popContext(); 1774 //} 1775 1776 // call handler 1777 if (fDocumentHandler != null ) { 1778 //end element is scanned in this function so we can send a callback 1779 //here. 1780 //<strong>we shouldn't be sending callback in scanDocument()</strong> 1781 1782 fDocumentHandler.endElement(endElementName, null); 1783 } 1784 if(dtdGrammarUtil != null) 1785 dtdGrammarUtil.endElement(endElementName); 1786 1787 return fMarkupDepth; 1788 1789 } // scanEndElement():int 1790 1791 /** 1792 * Scans a character reference. 1793 * <p> 1794 * <pre> 1795 * [66] CharRef ::= '&#' [0-9]+ ';' | '&#x' [0-9a-fA-F]+ ';' 1796 * </pre> 1797 */ 1798 protected void scanCharReference() 1799 throws IOException, XNIException { 1800 1801 fStringBuffer2.clear(); 1802 int ch = scanCharReferenceValue(fStringBuffer2, null); 1803 fMarkupDepth--; 1804 if (ch != -1) { 1805 // call handler 1806 1807 if (fDocumentHandler != null) { 1808 if (fNotifyCharRefs) { 1809 fDocumentHandler.startGeneralEntity(fCharRefLiteral, null, null, null); 1810 } 1811 Augmentations augs = null; 1812 if (fValidation && ch <= 0x20) { 1813 if (fTempAugmentations != null) { 1814 fTempAugmentations.removeAllItems(); 1815 } 1816 else { 1817 fTempAugmentations = new AugmentationsImpl(); 1818 } 1819 augs = fTempAugmentations; 1820 augs.putItem(Constants.CHAR_REF_PROBABLE_WS, Boolean.TRUE); 1821 } 1822 //xxx: How do we deal with this - how to return charReferenceValues 1823 //now this is being commented because this is taken care in scanDocument() 1824 //fDocumentHandler.characters(fStringBuffer2, null); 1825 if (fNotifyCharRefs) { 1826 fDocumentHandler.endGeneralEntity(fCharRefLiteral, null); 1827 } 1828 } 1829 } 1830 1831 } // scanCharReference() 1832 1833 1834 /** 1835 * Scans an entity reference. 1836 * 1837 * @return returns true if the new entity is started. If it was built-in entity 1838 * 'false' is returned. 1839 * @throws IOException Thrown if i/o error occurs. 1840 * @throws XNIException Thrown if handler throws exception upon 1841 * notification. 1842 */ 1843 protected void scanEntityReference(XMLStringBuffer content) throws IOException, XNIException { 1844 String name = fEntityScanner.scanName(NameType.REFERENCE); 1845 if (name == null) { 1846 reportFatalError("NameRequiredInReference", null); 1847 return; 1848 } 1849 if (!fEntityScanner.skipChar(';', NameType.REFERENCE)) { 1850 reportFatalError("SemicolonRequiredInReference", new Object []{name}); 1851 } 1852 if (fEntityStore.isUnparsedEntity(name)) { 1853 reportFatalError("ReferenceToUnparsedEntity", new Object[]{name}); 1854 } 1855 fMarkupDepth--; 1856 fCurrentEntityName = name; 1857 1858 // handle built-in entities 1859 if (name == fAmpSymbol) { 1860 handleCharacter('&', fAmpSymbol, content); 1861 fScannerState = SCANNER_STATE_BUILT_IN_REFS; 1862 return ; 1863 } else if (name == fLtSymbol) { 1864 handleCharacter('<', fLtSymbol, content); 1865 fScannerState = SCANNER_STATE_BUILT_IN_REFS; 1866 return ; 1867 } else if (name == fGtSymbol) { 1868 handleCharacter('>', fGtSymbol, content); 1869 fScannerState = SCANNER_STATE_BUILT_IN_REFS; 1870 return ; 1871 } else if (name == fQuotSymbol) { 1872 handleCharacter('"', fQuotSymbol, content); 1873 fScannerState = SCANNER_STATE_BUILT_IN_REFS; 1874 return ; 1875 } else if (name == fAposSymbol) { 1876 handleCharacter('\'', fAposSymbol, content); 1877 fScannerState = SCANNER_STATE_BUILT_IN_REFS; 1878 return ; 1879 } 1880 1881 //1. if the entity is external and support to external entities is not required 1882 // 2. or entities should not be replaced 1883 //3. or if it is built in entity reference. 1884 boolean isEE = fEntityStore.isExternalEntity(name); 1885 if((isEE && !fSupportExternalEntities) || (!isEE && !fReplaceEntityReferences) || foundBuiltInRefs){ 1886 fScannerState = SCANNER_STATE_REFERENCE; 1887 return ; 1888 } 1889 // start general entity 1890 if (!fEntityStore.isDeclaredEntity(name)) { 1891 //SUPPORT_DTD=false && ReplaceEntityReferences should throw exception 1892 if (!fSupportDTD && fReplaceEntityReferences) { 1893 reportFatalError("EntityNotDeclared", new Object[]{name}); 1894 return; 1895 } 1896 //REVISIT: one more case needs to be included: external PE and standalone is no 1897 if ( fHasExternalDTD && !fStandalone) { 1898 if (fValidation) 1899 fErrorReporter.reportError(fEntityScanner, XMLMessageFormatter.XML_DOMAIN,"EntityNotDeclared", 1900 new Object[]{name}, XMLErrorReporter.SEVERITY_ERROR); 1901 } else 1902 reportFatalError("EntityNotDeclared", new Object[]{name}); 1903 } 1904 //we are starting the entity even if the entity was not declared 1905 //if that was the case it its taken care in XMLEntityManager.startEntity() 1906 //we immediately call the endEntity. Application gets to know if there was 1907 //any entity that was not declared. 1908 fEntityManager.startEntity(true, name, false); 1909 //set the scaner state to content.. parser will automatically revive itself at any point of time. 1910 //setScannerState(SCANNER_STATE_CONTENT); 1911 //return true ; 1912 } // scanEntityReference() 1913 1914 // utility methods 1915 1916 /** 1917 * Check if the depth exceeds the maxElementDepth limit 1918 * @param elementName name of the current element 1919 */ 1920 void checkDepth(String elementName) { 1921 fLimitAnalyzer.addValue(Limit.MAX_ELEMENT_DEPTH_LIMIT, elementName, fElementStack.fDepth); 1922 if (fSecurityManager.isOverLimit(Limit.MAX_ELEMENT_DEPTH_LIMIT,fLimitAnalyzer)) { 1923 fSecurityManager.debugPrint(fLimitAnalyzer); 1924 reportFatalError("MaxElementDepthLimit", new Object[]{elementName, 1925 fLimitAnalyzer.getTotalValue(Limit.MAX_ELEMENT_DEPTH_LIMIT), 1926 fSecurityManager.getLimit(Limit.MAX_ELEMENT_DEPTH_LIMIT), 1927 "maxElementDepth"}); 1928 } 1929 } 1930 1931 /** 1932 * Calls document handler with a single character resulting from 1933 * built-in entity resolution. 1934 * 1935 * @param c 1936 * @param entity built-in name 1937 * @param XMLStringBuffer append the character to buffer 1938 * 1939 * we really dont need to call this function -- this function is only required when 1940 * we integrate with rest of Xerces2. SO maintaining the current behavior and still 1941 * calling this function to hanlde built-in entity reference. 1942 * 1943 */ 1944 private void handleCharacter(char c, String entity, XMLStringBuffer content) throws XNIException { 1945 foundBuiltInRefs = true; 1946 checkEntityLimit(false, fEntityScanner.fCurrentEntity.name, 1); 1947 content.append(c); 1948 if (fDocumentHandler != null) { 1949 fSingleChar[0] = c; 1950 if (fNotifyBuiltInRefs) { 1951 fDocumentHandler.startGeneralEntity(entity, null, null, null); 1952 } 1953 fTempString.setValues(fSingleChar, 0, 1); 1954 if(!fIsCoalesce){ 1955 fDocumentHandler.characters(fTempString, null); 1956 builtInRefCharacterHandled = true; 1957 } 1958 1959 if (fNotifyBuiltInRefs) { 1960 fDocumentHandler.endGeneralEntity(entity, null); 1961 } 1962 } 1963 } // handleCharacter(char) 1964 1965 // helper methods 1966 1967 /** 1968 * Sets the scanner state. 1969 * 1970 * @param state The new scanner state. 1971 */ 1972 protected final void setScannerState(int state) { 1973 1974 fScannerState = state; 1975 if (DEBUG_SCANNER_STATE) { 1976 System.out.print("### setScannerState: "); 1977 //System.out.print(fScannerState); 1978 System.out.print(getScannerStateName(state)); 1979 System.out.println(); 1980 } 1981 1982 } // setScannerState(int) 1983 1984 1985 /** 1986 * Sets the Driver. 1987 * 1988 * @param Driver The new Driver. 1989 */ 1990 protected final void setDriver(Driver driver) { 1991 fDriver = driver; 1992 if (DEBUG_DISPATCHER) { 1993 System.out.print("%%% setDriver: "); 1994 System.out.print(getDriverName(driver)); 1995 System.out.println(); 1996 } 1997 } 1998 1999 // 2000 // Private methods 2001 // 2002 2003 /** Returns the scanner state name. */ 2004 protected String getScannerStateName(int state) { 2005 2006 switch (state) { 2007 case SCANNER_STATE_DOCTYPE: return "SCANNER_STATE_DOCTYPE"; 2008 case SCANNER_STATE_ROOT_ELEMENT: return "SCANNER_STATE_ROOT_ELEMENT"; 2009 case SCANNER_STATE_START_OF_MARKUP: return "SCANNER_STATE_START_OF_MARKUP"; 2010 case SCANNER_STATE_COMMENT: return "SCANNER_STATE_COMMENT"; 2011 case SCANNER_STATE_PI: return "SCANNER_STATE_PI"; 2012 case SCANNER_STATE_CONTENT: return "SCANNER_STATE_CONTENT"; 2013 case SCANNER_STATE_REFERENCE: return "SCANNER_STATE_REFERENCE"; 2014 case SCANNER_STATE_END_OF_INPUT: return "SCANNER_STATE_END_OF_INPUT"; 2015 case SCANNER_STATE_TERMINATED: return "SCANNER_STATE_TERMINATED"; 2016 case SCANNER_STATE_CDATA: return "SCANNER_STATE_CDATA"; 2017 case SCANNER_STATE_TEXT_DECL: return "SCANNER_STATE_TEXT_DECL"; 2018 case SCANNER_STATE_ATTRIBUTE: return "SCANNER_STATE_ATTRIBUTE"; 2019 case SCANNER_STATE_ATTRIBUTE_VALUE: return "SCANNER_STATE_ATTRIBUTE_VALUE"; 2020 case SCANNER_STATE_START_ELEMENT_TAG: return "SCANNER_STATE_START_ELEMENT_TAG"; 2021 case SCANNER_STATE_END_ELEMENT_TAG: return "SCANNER_STATE_END_ELEMENT_TAG"; 2022 case SCANNER_STATE_CHARACTER_DATA: return "SCANNER_STATE_CHARACTER_DATA" ; 2023 } 2024 2025 return "??? ("+state+')'; 2026 2027 } // getScannerStateName(int):String 2028 public String getEntityName(){ 2029 //return the cached name 2030 return fCurrentEntityName; 2031 } 2032 2033 /** Returns the driver name. */ 2034 public String getDriverName(Driver driver) { 2035 2036 if (DEBUG_DISPATCHER) { 2037 if (driver != null) { 2038 String name = driver.getClass().getName(); 2039 int index = name.lastIndexOf('.'); 2040 if (index != -1) { 2041 name = name.substring(index + 1); 2042 index = name.lastIndexOf('$'); 2043 if (index != -1) { 2044 name = name.substring(index + 1); 2045 } 2046 } 2047 return name; 2048 } 2049 } 2050 return "null"; 2051 2052 } // getDriverName():String 2053 2054 /** 2055 * Check the protocol used in the systemId against allowed protocols 2056 * 2057 * @param systemId the Id of the URI 2058 * @param allowedProtocols a list of allowed protocols separated by comma 2059 * @return the name of the protocol if rejected, null otherwise 2060 */ 2061 String checkAccess(String systemId, String allowedProtocols) throws IOException { 2062 String baseSystemId = fEntityScanner.getBaseSystemId(); 2063 String expandedSystemId = XMLEntityManager.expandSystemId(systemId, baseSystemId, fStrictURI); 2064 return SecuritySupport.checkAccess(expandedSystemId, allowedProtocols, Constants.ACCESS_EXTERNAL_ALL); 2065 } 2066 2067 // 2068 // Classes 2069 // 2070 2071 /** 2072 * @author Neeraj Bajaj, Sun Microsystems. 2073 */ 2074 protected static final class Element { 2075 2076 // 2077 // Data 2078 // 2079 2080 /** Symbol. */ 2081 public QName qname; 2082 2083 //raw name stored as characters 2084 public char[] fRawname; 2085 2086 /** The next Element entry. */ 2087 public Element next; 2088 2089 // 2090 // Constructors 2091 // 2092 2093 /** 2094 * Constructs a new Element from the given QName and next Element 2095 * reference. 2096 */ 2097 public Element(QName qname, Element next) { 2098 this.qname.setValues(qname); 2099 this.fRawname = qname.rawname.toCharArray(); 2100 this.next = next; 2101 } 2102 2103 } // class Element 2104 2105 /** 2106 * Element stack. 2107 * 2108 * @author Neeraj Bajaj, Sun Microsystems. 2109 */ 2110 protected class ElementStack2 { 2111 2112 // 2113 // Data 2114 // 2115 2116 /** The stack data. */ 2117 protected QName [] fQName = new QName[20]; 2118 2119 //Element depth 2120 protected int fDepth; 2121 //total number of elements 2122 protected int fCount; 2123 //current position 2124 protected int fPosition; 2125 //Mark refers to the position 2126 protected int fMark; 2127 2128 protected int fLastDepth ; 2129 2130 // 2131 // Constructors 2132 // 2133 2134 /** Default constructor. */ 2135 public ElementStack2() { 2136 for (int i = 0; i < fQName.length; i++) { 2137 fQName[i] = new QName(); 2138 } 2139 fMark = fPosition = 1; 2140 } // <init>() 2141 2142 public void resize(){ 2143 /** 2144 * int length = fElements.length; 2145 * Element [] temp = new Element[length * 2]; 2146 * System.arraycopy(fElements, 0, temp, 0, length); 2147 * fElements = temp; 2148 */ 2149 //resize QNames 2150 int oldLength = fQName.length; 2151 QName [] tmp = new QName[oldLength * 2]; 2152 System.arraycopy(fQName, 0, tmp, 0, oldLength); 2153 fQName = tmp; 2154 2155 for (int i = oldLength; i < fQName.length; i++) { 2156 fQName[i] = new QName(); 2157 } 2158 2159 } 2160 2161 2162 // 2163 // Public methods 2164 // 2165 2166 /** Check if the element scanned during the start element 2167 *matches the stored element. 2168 * 2169 *@return true if the match suceeds. 2170 */ 2171 public boolean matchElement(QName element) { 2172 //last depth is the depth when last elemnt was pushed 2173 //if last depth is greater than current depth 2174 if(DEBUG_SKIP_ALGORITHM){ 2175 System.out.println("fLastDepth = " + fLastDepth); 2176 System.out.println("fDepth = " + fDepth); 2177 } 2178 boolean match = false; 2179 if(fLastDepth > fDepth && fDepth <= 2){ 2180 if(DEBUG_SKIP_ALGORITHM){ 2181 System.out.println("Checking if the elements match " + element.rawname + " , " + fQName[fDepth].rawname); 2182 } 2183 if(element.rawname == fQName[fDepth].rawname){ 2184 fAdd = false; 2185 //mark this position 2186 //decrease the depth by 1 as arrays are 0 based 2187 fMark = fDepth - 1; 2188 //we found the match and from next element skipping will start, add 1 2189 fPosition = fMark + 1 ; 2190 match = true; 2191 //Once we get match decrease the count -- this was increased by nextElement() 2192 --fCount; 2193 if(DEBUG_SKIP_ALGORITHM){ 2194 System.out.println("fAdd FALSE -- NOW ELEMENT SHOULD NOT BE ADDED"); 2195 System.out.println("fMark = " + fMark); 2196 System.out.println("fPosition = " + fPosition); 2197 System.out.println("fDepth = " + fDepth); 2198 System.out.println("fCount = " + fCount); 2199 } 2200 }else{ 2201 fAdd = true; 2202 if(DEBUG_SKIP_ALGORITHM)System.out.println("fAdd is " + fAdd); 2203 } 2204 } 2205 //store the last depth 2206 fLastDepth = fDepth++; 2207 return match; 2208 } // pushElement(QName):QName 2209 2210 /** 2211 * This function doesn't increase depth. The function in this function is 2212 *broken down into two functions for efficiency. <@see>matchElement</see>. 2213 * This function just returns the pointer to the object and its values are set. 2214 * 2215 *@return QName reference to the next element in the list 2216 */ 2217 public QName nextElement() { 2218 2219 //if number of elements becomes equal to the length of array -- stop the skipping 2220 if (fCount == fQName.length) { 2221 fShouldSkip = false; 2222 fAdd = false; 2223 if(DEBUG_SKIP_ALGORITHM)System.out.println("SKIPPING STOPPED, fShouldSkip = " + fShouldSkip); 2224 //xxx: this is not correct, we are returning the last element 2225 //this wont make any difference since flag has been set to 'false' 2226 return fQName[--fCount]; 2227 } 2228 if(DEBUG_SKIP_ALGORITHM){ 2229 System.out.println("fCount = " + fCount); 2230 } 2231 return fQName[fCount++]; 2232 2233 } 2234 2235 /** Note that this function is considerably different than nextElement() 2236 * This function just returns the previously stored elements 2237 */ 2238 public QName getNext(){ 2239 //when position reaches number of elements in the list.. 2240 //set the position back to mark, making it a circular linked list. 2241 if(fPosition == fCount){ 2242 fPosition = fMark; 2243 } 2244 return fQName[fPosition++]; 2245 } 2246 2247 /** returns the current depth 2248 */ 2249 public int popElement(){ 2250 return fDepth--; 2251 } 2252 2253 2254 /** Clears the stack without throwing away existing QName objects. */ 2255 public void clear() { 2256 fLastDepth = 0; 2257 fDepth = 0; 2258 fCount = 0 ; 2259 fPosition = fMark = 1; 2260 } // clear() 2261 2262 } // class ElementStack 2263 2264 /** 2265 * Element stack. This stack operates without synchronization, error 2266 * checking, and it re-uses objects instead of throwing popped items 2267 * away. 2268 * 2269 * @author Andy Clark, IBM 2270 */ 2271 protected class ElementStack { 2272 2273 // 2274 // Data 2275 // 2276 2277 /** The stack data. */ 2278 protected QName[] fElements; 2279 protected int [] fInt = new int[20]; 2280 2281 2282 //Element depth 2283 protected int fDepth; 2284 //total number of elements 2285 protected int fCount; 2286 //current position 2287 protected int fPosition; 2288 //Mark refers to the position 2289 protected int fMark; 2290 2291 protected int fLastDepth ; 2292 2293 // 2294 // Constructors 2295 // 2296 2297 /** Default constructor. */ 2298 public ElementStack() { 2299 fElements = new QName[20]; 2300 for (int i = 0; i < fElements.length; i++) { 2301 fElements[i] = new QName(); 2302 } 2303 } // <init>() 2304 2305 // 2306 // Public methods 2307 // 2308 2309 /** 2310 * Pushes an element on the stack. 2311 * <p> 2312 * <strong>Note:</strong> The QName values are copied into the 2313 * stack. In other words, the caller does <em>not</em> orphan 2314 * the element to the stack. Also, the QName object returned 2315 * is <em>not</em> orphaned to the caller. It should be 2316 * considered read-only. 2317 * 2318 * @param element The element to push onto the stack. 2319 * 2320 * @return Returns the actual QName object that stores the 2321 */ 2322 //XXX: THIS FUNCTION IS NOT USED 2323 public QName pushElement(QName element) { 2324 if (fDepth == fElements.length) { 2325 QName[] array = new QName[fElements.length * 2]; 2326 System.arraycopy(fElements, 0, array, 0, fDepth); 2327 fElements = array; 2328 for (int i = fDepth; i < fElements.length; i++) { 2329 fElements[i] = new QName(); 2330 } 2331 } 2332 fElements[fDepth].setValues(element); 2333 return fElements[fDepth++]; 2334 } // pushElement(QName):QName 2335 2336 2337 /** Note that this function is considerably different than nextElement() 2338 * This function just returns the previously stored elements 2339 */ 2340 public QName getNext(){ 2341 //when position reaches number of elements in the list.. 2342 //set the position back to mark, making it a circular linked list. 2343 if(fPosition == fCount){ 2344 fPosition = fMark; 2345 } 2346 //store the position of last opened tag at particular depth 2347 //fInt[++fDepth] = fPosition; 2348 if(DEBUG_SKIP_ALGORITHM){ 2349 System.out.println("Element at fPosition = " + fPosition + " is " + fElements[fPosition].rawname); 2350 } 2351 //return fElements[fPosition++]; 2352 return fElements[fPosition]; 2353 } 2354 2355 /** This function should be called only when element was skipped sucessfully. 2356 * 1. Increase the depth - because element was sucessfully skipped. 2357 *2. Store the position of the element token in array "last opened tag" at depth. 2358 *3. increase the position counter so as to point to the next element in the array 2359 */ 2360 public void push(){ 2361 2362 fInt[++fDepth] = fPosition++; 2363 } 2364 2365 /** Check if the element scanned during the start element 2366 *matches the stored element. 2367 * 2368 *@return true if the match suceeds. 2369 */ 2370 public boolean matchElement(QName element) { 2371 //last depth is the depth when last elemnt was pushed 2372 //if last depth is greater than current depth 2373 //if(DEBUG_SKIP_ALGORITHM){ 2374 // System.out.println("Check if the element " + element.rawname + " matches"); 2375 // System.out.println("fLastDepth = " + fLastDepth); 2376 // System.out.println("fDepth = " + fDepth); 2377 //} 2378 boolean match = false; 2379 if(fLastDepth > fDepth && fDepth <= 3){ 2380 if(DEBUG_SKIP_ALGORITHM){ 2381 System.out.println("----------ENTERED THE LOOP WHERE WE CHECK FOR MATCHING OF ELMENT-----"); 2382 System.out.println("Depth = " + fDepth + " Checking if INCOMING element " + element.rawname + " match STORED ELEMENT " + fElements[fDepth - 1].rawname); 2383 } 2384 if(element.rawname == fElements[fDepth - 1].rawname){ 2385 fAdd = false; 2386 //mark this position 2387 //decrease the depth by 1 as arrays are 0 based 2388 fMark = fDepth - 1; 2389 //we found the match 2390 fPosition = fMark; 2391 match = true; 2392 //Once we get match decrease the count -- this was increased by nextElement() 2393 --fCount; 2394 if(DEBUG_SKIP_ALGORITHM){ 2395 System.out.println("NOW ELEMENT SHOULD NOT BE ADDED, fAdd is set to false"); 2396 System.out.println("fMark = " + fMark); 2397 System.out.println("fPosition = " + fPosition); 2398 System.out.println("fDepth = " + fDepth); 2399 System.out.println("fCount = " + fCount); 2400 System.out.println("---------MATCH SUCEEDED-----------------"); 2401 System.out.println(""); 2402 } 2403 }else{ 2404 fAdd = true; 2405 if(DEBUG_SKIP_ALGORITHM)System.out.println("fAdd is " + fAdd); 2406 } 2407 } 2408 //store the position for the current depth 2409 //when we are adding the elements, when skipping 2410 //starts even then this should be tracked ie. when 2411 //calling getNext() 2412 if(match){ 2413 //from next element skipping will start, add 1 2414 fInt[fDepth] = fPosition++; 2415 } else{ 2416 if(DEBUG_SKIP_ALGORITHM){ 2417 System.out.println("At depth = " + fDepth + "array position is = " + (fCount - 1)); 2418 } 2419 //sicne fInt[fDepth] contains pointer to the element array which are 0 based. 2420 fInt[fDepth] = fCount - 1; 2421 } 2422 2423 //if number of elements becomes equal to the length of array -- stop the skipping 2424 //xxx: should we do "fCount == fInt.length" 2425 if (fCount == fElements.length) { 2426 fSkip = false; 2427 fAdd = false; 2428 //reposition the stack -- it seems to be too complex document and there is no symmerty in structure 2429 reposition(); 2430 if(DEBUG_SKIP_ALGORITHM){ 2431 System.out.println("ALL THE ELMENTS IN ARRAY HAVE BEEN FILLED"); 2432 System.out.println("REPOSITIONING THE STACK"); 2433 System.out.println("-----------SKIPPING STOPPED----------"); 2434 System.out.println(""); 2435 } 2436 return false; 2437 } 2438 if(DEBUG_SKIP_ALGORITHM){ 2439 if(match){ 2440 System.out.println("Storing fPosition = " + fInt[fDepth] + " at fDepth = " + fDepth); 2441 }else{ 2442 System.out.println("Storing fCount = " + fInt[fDepth] + " at fDepth = " + fDepth); 2443 } 2444 } 2445 //store the last depth 2446 fLastDepth = fDepth; 2447 return match; 2448 } // matchElement(QName):QName 2449 2450 2451 /** 2452 * Returns the next element on the stack. 2453 * 2454 * @return Returns the actual QName object. Callee should 2455 * use this object to store the details of next element encountered. 2456 */ 2457 public QName nextElement() { 2458 if(fSkip){ 2459 fDepth++; 2460 //boundary checks are done in matchElement() 2461 return fElements[fCount++]; 2462 } else if (fDepth == fElements.length) { 2463 QName[] array = new QName[fElements.length * 2]; 2464 System.arraycopy(fElements, 0, array, 0, fDepth); 2465 fElements = array; 2466 for (int i = fDepth; i < fElements.length; i++) { 2467 fElements[i] = new QName(); 2468 } 2469 } 2470 2471 return fElements[fDepth++]; 2472 2473 } // pushElement(QName):QName 2474 2475 2476 /** 2477 * Pops an element off of the stack by setting the values of 2478 * the specified QName. 2479 * <p> 2480 * <strong>Note:</strong> The object returned is <em>not</em> 2481 * orphaned to the caller. Therefore, the caller should consider 2482 * the object to be read-only. 2483 */ 2484 public QName popElement() { 2485 //return the same object that was pushed -- this would avoid 2486 //setting the values for every end element. 2487 //STRONG: this object is read only -- this object reference shouldn't be stored. 2488 if(fSkip || fAdd ){ 2489 if(DEBUG_SKIP_ALGORITHM){ 2490 System.out.println("POPPING Element, at position " + fInt[fDepth] + " element at that count is = " + fElements[fInt[fDepth]].rawname); 2491 System.out.println(""); 2492 } 2493 return fElements[fInt[fDepth--]]; 2494 } else{ 2495 if(DEBUG_SKIP_ALGORITHM){ 2496 System.out.println("Retrieveing element at depth = " + fDepth + " is " + fElements[fDepth].rawname ); 2497 } 2498 return fElements[--fDepth] ; 2499 } 2500 //element.setValues(fElements[--fDepth]); 2501 } // popElement(QName) 2502 2503 /** Reposition the stack. fInt [] contains all the opened tags at particular depth. 2504 * Transfer all the opened tags starting from depth '2' to the current depth and reposition them 2505 *as per the depth. 2506 */ 2507 public void reposition(){ 2508 for( int i = 2 ; i <= fDepth ; i++){ 2509 fElements[i-1] = fElements[fInt[i]]; 2510 } 2511 if(DEBUG_SKIP_ALGORITHM){ 2512 for( int i = 0 ; i < fDepth ; i++){ 2513 System.out.println("fElements[" + i + "]" + " = " + fElements[i].rawname); 2514 } 2515 } 2516 } 2517 2518 /** Clears the stack without throwing away existing QName objects. */ 2519 public void clear() { 2520 fDepth = 0; 2521 fLastDepth = 0; 2522 fCount = 0 ; 2523 fPosition = fMark = 1; 2524 2525 } // clear() 2526 2527 /** 2528 * This function is as a result of optimization done for endElement -- 2529 * we dont need to set the value for every end element encouterd. 2530 * For Well formedness checks we can have the same QName object that was pushed. 2531 * the values will be set only if application need to know about the endElement 2532 * -- neeraj.bajaj@sun.com 2533 */ 2534 2535 public QName getLastPoppedElement(){ 2536 return fElements[fDepth]; 2537 } 2538 } // class ElementStack 2539 2540 /** 2541 * Drives the parser to the next state/event on the input. Parser is guaranteed 2542 * to stop at the next state/event. 2543 * 2544 * Internally XML document is divided into several states. Each state represents 2545 * a sections of XML document. When this functions returns normally, it has read 2546 * the section of XML document and returns the state corresponding to section of 2547 * document which has been read. For optimizations, a particular driver 2548 * can read ahead of the section of document (state returned) just read and 2549 * can maintain a different internal state. 2550 * 2551 * 2552 * @author Neeraj Bajaj, Sun Microsystems 2553 */ 2554 protected interface Driver { 2555 2556 2557 /** 2558 * Drives the parser to the next state/event on the input. Parser is guaranteed 2559 * to stop at the next state/event. 2560 * 2561 * Internally XML document is divided into several states. Each state represents 2562 * a sections of XML document. When this functions returns normally, it has read 2563 * the section of XML document and returns the state corresponding to section of 2564 * document which has been read. For optimizations, a particular driver 2565 * can read ahead of the section of document (state returned) just read and 2566 * can maintain a different internal state. 2567 * 2568 * @return state representing the section of document just read. 2569 * 2570 * @throws IOException Thrown on i/o error. 2571 * @throws XNIException Thrown on parse error. 2572 */ 2573 2574 public int next() throws IOException, XNIException; 2575 2576 } // interface Driver 2577 2578 /** 2579 * Driver to handle content scanning. This driver is capable of reading 2580 * the fragment of XML document. When it has finished reading fragment 2581 * of XML documents, it can pass the job of reading to another driver. 2582 * 2583 * This class has been modified as per the new design which is more suited to 2584 * efficiently build pull parser. Lot of performance improvements have been done and 2585 * the code has been added to support stax functionality/features. 2586 * 2587 * @author Neeraj Bajaj, Sun Microsystems 2588 * 2589 * 2590 * @author Andy Clark, IBM 2591 * @author Eric Ye, IBM 2592 */ 2593 protected class FragmentContentDriver 2594 implements Driver { 2595 2596 // 2597 // Driver methods 2598 // 2599 2600 /** 2601 * decides the appropriate state of the parser 2602 */ 2603 private void startOfMarkup() throws IOException { 2604 fMarkupDepth++; 2605 final int ch = fEntityScanner.peekChar(); 2606 if (isValidNameStartChar(ch) || isValidNameStartHighSurrogate(ch)) { 2607 setScannerState(SCANNER_STATE_START_ELEMENT_TAG); 2608 } else { 2609 switch(ch){ 2610 case '?' :{ 2611 setScannerState(SCANNER_STATE_PI); 2612 fEntityScanner.skipChar(ch, null); 2613 break; 2614 } 2615 case '!' :{ 2616 fEntityScanner.skipChar(ch, null); 2617 if (fEntityScanner.skipChar('-', null)) { 2618 if (!fEntityScanner.skipChar('-', NameType.COMMENT)) { 2619 reportFatalError("InvalidCommentStart", 2620 null); 2621 } 2622 setScannerState(SCANNER_STATE_COMMENT); 2623 } else if (fEntityScanner.skipString(cdata)) { 2624 setScannerState(SCANNER_STATE_CDATA ); 2625 } else if (!scanForDoctypeHook()) { 2626 reportFatalError("MarkupNotRecognizedInContent", 2627 null); 2628 } 2629 break; 2630 } 2631 case '/' :{ 2632 setScannerState(SCANNER_STATE_END_ELEMENT_TAG); 2633 fEntityScanner.skipChar(ch, NameType.ELEMENTEND); 2634 break; 2635 } 2636 default :{ 2637 reportFatalError("MarkupNotRecognizedInContent", null); 2638 } 2639 } 2640 } 2641 2642 }//startOfMarkup 2643 2644 private void startOfContent() throws IOException { 2645 if (fEntityScanner.skipChar('<', null)) { 2646 setScannerState(SCANNER_STATE_START_OF_MARKUP); 2647 } else if (fEntityScanner.skipChar('&', NameType.REFERENCE)) { 2648 setScannerState(SCANNER_STATE_REFERENCE) ; //XMLEvent.ENTITY_REFERENCE ); //SCANNER_STATE_REFERENCE 2649 } else { 2650 //element content is there.. 2651 setScannerState(SCANNER_STATE_CHARACTER_DATA); 2652 } 2653 }//startOfContent 2654 2655 2656 /** 2657 * 2658 * SCANNER_STATE_CONTENT and SCANNER_STATE_START_OF_MARKUP are two super states of the parser. 2659 * At any point of time when in doubt over the current state of the parser, the state should be 2660 * set to SCANNER_STATE_CONTENT. Parser will automatically revive itself and will set state of 2661 * the parser to one of its sub state. 2662 * sub states are defined in the parser on the basis of different XML component like 2663 * SCANNER_STATE_ENTITY_REFERENCE , SCANNER_STATE_START_ELEMENT, SCANNER_STATE_CDATA etc.. 2664 * These sub states help the parser to have fine control over the parsing. These are the 2665 * different milepost, parser stops at each sub state (milepost). Based on this state it is 2666 * decided if paresr needs to stop at next milepost ?? 2667 * 2668 */ 2669 public void decideSubState() throws IOException { 2670 while( fScannerState == SCANNER_STATE_CONTENT || fScannerState == SCANNER_STATE_START_OF_MARKUP){ 2671 2672 switch (fScannerState) { 2673 2674 case SCANNER_STATE_CONTENT: { 2675 startOfContent() ; 2676 break; 2677 } 2678 2679 case SCANNER_STATE_START_OF_MARKUP: { 2680 startOfMarkup() ; 2681 break; 2682 } 2683 } 2684 } 2685 }//decideSubState 2686 2687 /** 2688 * Drives the parser to the next state/event on the input. Parser is guaranteed 2689 * to stop at the next state/event. Internally XML document 2690 * is divided into several states. Each state represents a sections of XML 2691 * document. When this functions returns normally, it has read the section 2692 * of XML document and returns the state corresponding to section of 2693 * document which has been read. For optimizations, a particular driver 2694 * can read ahead of the section of document (state returned) just read and 2695 * can maintain a different internal state. 2696 * 2697 * State returned corresponds to Stax states. 2698 * 2699 * @return state representing the section of document just read. 2700 * 2701 * @throws IOException Thrown on i/o error. 2702 * @throws XNIException Thrown on parse error. 2703 */ 2704 2705 public int next() throws IOException, XNIException { 2706 while (true) { 2707 try { 2708 if(DEBUG_NEXT){ 2709 System.out.println("NOW IN FragmentContentDriver"); 2710 System.out.println("Entering the FragmentContentDriver with = " + getScannerStateName(fScannerState)); 2711 } 2712 2713 //decide the actual sub state of the scanner.For more information refer to the javadoc of 2714 //decideSubState. 2715 2716 switch (fScannerState) { 2717 case SCANNER_STATE_CONTENT: { 2718 final int ch = fEntityScanner.peekChar(); 2719 if (ch == '<') { 2720 fEntityScanner.scanChar(null); 2721 setScannerState(SCANNER_STATE_START_OF_MARKUP); 2722 } else if (ch == '&') { 2723 fEntityScanner.scanChar(NameType.REFERENCE); 2724 setScannerState(SCANNER_STATE_REFERENCE) ; //XMLEvent.ENTITY_REFERENCE ); //SCANNER_STATE_REFERENCE 2725 break; 2726 } else { 2727 //element content is there.. 2728 setScannerState(SCANNER_STATE_CHARACTER_DATA); 2729 break; 2730 } 2731 } 2732 2733 case SCANNER_STATE_START_OF_MARKUP: { 2734 startOfMarkup(); 2735 break; 2736 }//case: SCANNER_STATE_START_OF_MARKUP 2737 2738 }//end of switch 2739 //decideSubState() ; 2740 2741 //do some special handling if isCoalesce is set to true. 2742 if(fIsCoalesce){ 2743 fUsebuffer = true ; 2744 //if the last section was character data 2745 if(fLastSectionWasCharacterData){ 2746 2747 //if we dont encounter any CDATA or ENTITY REFERENCE and current state is also not SCANNER_STATE_CHARACTER_DATA 2748 //return the last scanned charactrer data. 2749 if((fScannerState != SCANNER_STATE_CDATA) && (fScannerState != SCANNER_STATE_REFERENCE) 2750 && (fScannerState != SCANNER_STATE_CHARACTER_DATA)){ 2751 fLastSectionWasCharacterData = false; 2752 return XMLEvent.CHARACTERS; 2753 } 2754 }//if last section was CDATA or ENTITY REFERENCE 2755 //xxx: there might be another entity reference or CDATA after this 2756 //<foo>blah blah &<<![CDATA[[aa]]>blah blah</foo> 2757 else if((fLastSectionWasCData || fLastSectionWasEntityReference)){ 2758 //and current state is not SCANNER_STATE_CHARACTER_DATA 2759 //or SCANNER_STATE_CDATA or SCANNER_STATE_REFERENCE 2760 //this means there is nothing more to be coalesced. 2761 //return the CHARACTERS event. 2762 if((fScannerState != SCANNER_STATE_CDATA) && (fScannerState != SCANNER_STATE_REFERENCE) 2763 && (fScannerState != SCANNER_STATE_CHARACTER_DATA)){ 2764 2765 fLastSectionWasCData = false; 2766 fLastSectionWasEntityReference = false; 2767 return XMLEvent.CHARACTERS; 2768 } 2769 } 2770 } 2771 2772 2773 if(DEBUG_NEXT){ 2774 System.out.println("Actual scanner state set by decideSubState is = " + getScannerStateName(fScannerState)); 2775 } 2776 2777 switch(fScannerState){ 2778 2779 case XMLEvent.START_DOCUMENT : 2780 return XMLEvent.START_DOCUMENT; 2781 2782 case SCANNER_STATE_START_ELEMENT_TAG :{ 2783 2784 //xxx this function returns true when element is empty.. can be linked to end element event. 2785 //returns true if the element is empty 2786 fEmptyElement = scanStartElement() ; 2787 //if the element is empty the next event is "end element" 2788 if(fEmptyElement){ 2789 setScannerState(SCANNER_STATE_END_ELEMENT_TAG); 2790 }else{ 2791 //set the next possible state 2792 setScannerState(SCANNER_STATE_CONTENT); 2793 } 2794 return XMLEvent.START_ELEMENT ; 2795 } 2796 2797 case SCANNER_STATE_CHARACTER_DATA: { 2798 if(DEBUG_COALESCE){ 2799 System.out.println("fLastSectionWasCData = " + fLastSectionWasCData); 2800 System.out.println("fIsCoalesce = " + fIsCoalesce); 2801 } 2802 //if last section was either entity reference or cdata or character data we should be using buffer 2803 fUsebuffer = fLastSectionWasEntityReference || fLastSectionWasCData || fLastSectionWasCharacterData ; 2804 2805 //When coalesce is set to true and last state was REFERENCE or CDATA or CHARACTER_DATA, buffer should not be cleared. 2806 if( fIsCoalesce && (fLastSectionWasEntityReference || fLastSectionWasCData || fLastSectionWasCharacterData) ){ 2807 fLastSectionWasEntityReference = false; 2808 fLastSectionWasCData = false; 2809 fLastSectionWasCharacterData = true ; 2810 fUsebuffer = true; 2811 }else{ 2812 //clear the buffer 2813 fContentBuffer.clear(); 2814 } 2815 2816 //set the fTempString length to 0 before passing it on to scanContent 2817 //scanContent sets the correct co-ordinates as per the content read 2818 fTempString.length = 0; 2819 int c = fEntityScanner.scanContent(fTempString); 2820 if(DEBUG){ 2821 System.out.println("fTempString = " + fTempString); 2822 } 2823 if(fEntityScanner.skipChar('<', null)){ 2824 //check if we have reached end of element 2825 if(fEntityScanner.skipChar('/', NameType.ELEMENTEND)){ 2826 //increase the mark up depth 2827 fMarkupDepth++; 2828 fLastSectionWasCharacterData = false; 2829 setScannerState(SCANNER_STATE_END_ELEMENT_TAG); 2830 //check if its start of new element 2831 }else if(XMLChar.isNameStart(fEntityScanner.peekChar())){ 2832 fMarkupDepth++; 2833 fLastSectionWasCharacterData = false; 2834 setScannerState(SCANNER_STATE_START_ELEMENT_TAG); 2835 }else{ 2836 setScannerState(SCANNER_STATE_START_OF_MARKUP); 2837 //there can be cdata ahead if coalesce is true we should call again 2838 if(fIsCoalesce){ 2839 fUsebuffer = true; 2840 fLastSectionWasCharacterData = true; 2841 fContentBuffer.append(fTempString); 2842 fTempString.length = 0; 2843 continue; 2844 } 2845 } 2846 //in case last section was either entity reference or cdata or character data -- we should be using buffer 2847 if(fUsebuffer){ 2848 fContentBuffer.append(fTempString); 2849 fTempString.length = 0; 2850 } 2851 if(DEBUG){ 2852 System.out.println("NOT USING THE BUFFER, STRING = " + fTempString.toString()); 2853 } 2854 if(dtdGrammarUtil!= null && dtdGrammarUtil.isIgnorableWhiteSpace(fContentBuffer)){ 2855 if(DEBUG)System.out.println("Return SPACE EVENT"); 2856 return XMLEvent.SPACE; 2857 }else 2858 return XMLEvent.CHARACTERS; 2859 2860 } else{ 2861 fUsebuffer = true ; 2862 if(DEBUG){ 2863 System.out.println("fContentBuffer = " + fContentBuffer); 2864 System.out.println("fTempString = " + fTempString); 2865 } 2866 fContentBuffer.append(fTempString); 2867 fTempString.length = 0; 2868 } 2869 if (c == '\r') { 2870 if(DEBUG){ 2871 System.out.println("'\r' character found"); 2872 } 2873 // happens when there is the character reference 2874 //xxx: We know the next chracter.. we should just skip it and add ']' directlry 2875 fEntityScanner.scanChar(null); 2876 fUsebuffer = true; 2877 fContentBuffer.append((char)c); 2878 c = -1 ; 2879 } else if (c == ']') { 2880 //fStringBuffer.clear(); 2881 //xxx: We know the next chracter.. we should just skip it and add ']' directlry 2882 fUsebuffer = true; 2883 fContentBuffer.append((char)fEntityScanner.scanChar(null)); 2884 // remember where we are in case we get an endEntity before we 2885 // could flush the buffer out - this happens when we're parsing an 2886 // entity which ends with a ] 2887 fInScanContent = true; 2888 2889 // We work on a single character basis to handle cases such as: 2890 // ']]]>' which we might otherwise miss. 2891 // 2892 if (fEntityScanner.skipChar(']', null)) { 2893 fContentBuffer.append(']'); 2894 while (fEntityScanner.skipChar(']', null)) { 2895 fContentBuffer.append(']'); 2896 } 2897 if (fEntityScanner.skipChar('>', null)) { 2898 reportFatalError("CDEndInContent", null); 2899 } 2900 } 2901 c = -1 ; 2902 fInScanContent = false; 2903 } 2904 2905 do{ 2906 //xxx: we should be using only one buffer.. 2907 // we need not to grow the buffer only when isCoalesce() is not true; 2908 2909 if (c == '<') { 2910 fEntityScanner.scanChar(null); 2911 setScannerState(SCANNER_STATE_START_OF_MARKUP); 2912 break; 2913 }//xxx what should be the behavior if entity reference is present in the content ? 2914 else if (c == '&') { 2915 fEntityScanner.scanChar(NameType.REFERENCE); 2916 setScannerState(SCANNER_STATE_REFERENCE); 2917 break; 2918 }///xxx since this part is also characters, it should be merged... 2919 else if (c != -1 && isInvalidLiteral(c)) { 2920 if (XMLChar.isHighSurrogate(c)) { 2921 // special case: surrogates 2922 scanSurrogates(fContentBuffer) ; 2923 setScannerState(SCANNER_STATE_CONTENT); 2924 } else { 2925 reportFatalError("InvalidCharInContent", 2926 new Object[] { 2927 Integer.toString(c, 16)}); 2928 fEntityScanner.scanChar(null); 2929 } 2930 break; 2931 } 2932 //xxx: scanContent also gives character callback. 2933 c = scanContent(fContentBuffer) ; 2934 //we should not be iterating again if fIsCoalesce is not set to true 2935 2936 if(!fIsCoalesce){ 2937 setScannerState(SCANNER_STATE_CONTENT); 2938 break; 2939 } 2940 2941 }while(true); 2942 2943 //if (fDocumentHandler != null) { 2944 // fDocumentHandler.characters(fContentBuffer, null); 2945 //} 2946 if(DEBUG)System.out.println("USING THE BUFFER, STRING START=" + fContentBuffer.toString() +"=END"); 2947 //if fIsCoalesce is true there might be more data so call fDriver.next() 2948 if(fIsCoalesce){ 2949 fLastSectionWasCharacterData = true ; 2950 continue; 2951 }else{ 2952 if(dtdGrammarUtil!= null && dtdGrammarUtil.isIgnorableWhiteSpace(fContentBuffer)){ 2953 if(DEBUG)System.out.println("Return SPACE EVENT"); 2954 return XMLEvent.SPACE; 2955 } else 2956 return XMLEvent.CHARACTERS ; 2957 } 2958 } 2959 2960 case SCANNER_STATE_END_ELEMENT_TAG :{ 2961 if(fEmptyElement){ 2962 //set it back to false. 2963 fEmptyElement = false; 2964 setScannerState(SCANNER_STATE_CONTENT); 2965 //check the case when there is comment after single element document 2966 //<foo/> and some comment after this 2967 return (fMarkupDepth == 0 && elementDepthIsZeroHook() ) ? XMLEvent.END_ELEMENT : XMLEvent.END_ELEMENT ; 2968 2969 } else if(scanEndElement() == 0) { 2970 //It is last element of the document 2971 if (elementDepthIsZeroHook()) { 2972 //if element depth is zero , it indicates the end of the document 2973 //the state shouldn't be set, because it is set by elementDepthIsZeroHook() function 2974 //xxx understand this point once again.. 2975 return XMLEvent.END_ELEMENT ; 2976 } 2977 2978 } 2979 setScannerState(SCANNER_STATE_CONTENT); 2980 return XMLEvent.END_ELEMENT ; 2981 } 2982 2983 case SCANNER_STATE_COMMENT: { //SCANNER_STATE_COMMENT: 2984 scanComment(); 2985 setScannerState(SCANNER_STATE_CONTENT); 2986 return XMLEvent.COMMENT; 2987 //break; 2988 } 2989 case SCANNER_STATE_PI:{ //SCANNER_STATE_PI: { 2990 //clear the buffer first 2991 fContentBuffer.clear() ; 2992 //xxx: which buffer should be passed. Ideally we shouldn't have 2993 //more than two buffers -- 2994 //xxx: where should we add the switch for buffering. 2995 scanPI(fContentBuffer); 2996 setScannerState(SCANNER_STATE_CONTENT); 2997 return XMLEvent.PROCESSING_INSTRUCTION; 2998 //break; 2999 } 3000 case SCANNER_STATE_CDATA :{ //SCANNER_STATE_CDATA: { 3001 //xxx: What if CDATA is the first event 3002 //<foo><![CDATA[hello<><>]]>append</foo> 3003 3004 //we should not clear the buffer only when the last state was either SCANNER_STATE_REFERENCE or 3005 //SCANNER_STATE_CHARACTER_DATA or SCANNER_STATE_REFERENCE 3006 if(fIsCoalesce && ( fLastSectionWasEntityReference || fLastSectionWasCData || fLastSectionWasCharacterData)){ 3007 fLastSectionWasCData = true ; 3008 fLastSectionWasEntityReference = false; 3009 fLastSectionWasCharacterData = false; 3010 }//if we dont need to coalesce clear the buffer 3011 else{ 3012 fContentBuffer.clear(); 3013 } 3014 fUsebuffer = true; 3015 //CDATA section is completely read in all the case. 3016 scanCDATASection(fContentBuffer , true); 3017 setScannerState(SCANNER_STATE_CONTENT); 3018 //1. if fIsCoalesce is set to true we set the variable fLastSectionWasCData to true 3019 //and just call fDispatche.next(). Since we have set the scanner state to 3020 //SCANNER_STATE_CONTENT (super state) parser will automatically recover and 3021 //behave appropriately. When isCoalesce is set to true we dont need to reportCDATA event 3022 //2. Check if application has set for reporting CDATA event 3023 //3. if the application has neither set the fIsCoalesce to true nor fReportCdataEvent 3024 //return the cdata event as characters. 3025 if(fIsCoalesce){ 3026 fLastSectionWasCData = true ; 3027 //there might be more data to coalesce. 3028 continue; 3029 }else if(fReportCdataEvent){ 3030 return XMLEvent.CDATA; 3031 } else{ 3032 return XMLEvent.CHARACTERS; 3033 } 3034 } 3035 3036 case SCANNER_STATE_REFERENCE :{ 3037 fMarkupDepth++; 3038 foundBuiltInRefs = false; 3039 3040 //we should not clear the buffer only when the last state was either CDATA or 3041 //SCANNER_STATE_CHARACTER_DATA or SCANNER_STATE_REFERENCE 3042 if(fIsCoalesce && ( fLastSectionWasEntityReference || fLastSectionWasCData || fLastSectionWasCharacterData)){ 3043 //fLastSectionWasEntityReference or fLastSectionWasCData are only 3044 //used when fIsCoalesce is set to true. 3045 fLastSectionWasEntityReference = true ; 3046 fLastSectionWasCData = false; 3047 fLastSectionWasCharacterData = false; 3048 }//if we dont need to coalesce clear the buffer 3049 else{ 3050 fContentBuffer.clear(); 3051 } 3052 fUsebuffer = true ; 3053 //take care of character reference 3054 if (fEntityScanner.skipChar('#', NameType.REFERENCE)) { 3055 scanCharReferenceValue(fContentBuffer, null); 3056 fMarkupDepth--; 3057 if(!fIsCoalesce){ 3058 setScannerState(SCANNER_STATE_CONTENT); 3059 return XMLEvent.CHARACTERS; 3060 } 3061 } else { 3062 // this function also starts new entity 3063 scanEntityReference(fContentBuffer); 3064 //if there was built-in entity reference & coalesce is not true 3065 //return CHARACTERS 3066 if(fScannerState == SCANNER_STATE_BUILT_IN_REFS && !fIsCoalesce){ 3067 setScannerState(SCANNER_STATE_CONTENT); 3068 if (builtInRefCharacterHandled) { 3069 builtInRefCharacterHandled = false; 3070 return XMLEvent.ENTITY_REFERENCE; 3071 } else { 3072 return XMLEvent.CHARACTERS; 3073 } 3074 } 3075 3076 //if there was a text declaration, call next() it will be taken care. 3077 if(fScannerState == SCANNER_STATE_TEXT_DECL){ 3078 fLastSectionWasEntityReference = true ; 3079 continue; 3080 } 3081 3082 if(fScannerState == SCANNER_STATE_REFERENCE){ 3083 setScannerState(SCANNER_STATE_CONTENT); 3084 if (fReplaceEntityReferences && fEntityStore.isDeclaredEntity(fCurrentEntityName)) { 3085 // Skip the entity reference, we don't care 3086 continue; 3087 } 3088 return XMLEvent.ENTITY_REFERENCE; 3089 } 3090 } 3091 //Wether it was character reference, entity reference or built-in entity 3092 //set the next possible state to SCANNER_STATE_CONTENT 3093 setScannerState(SCANNER_STATE_CONTENT); 3094 fLastSectionWasEntityReference = true ; 3095 continue; 3096 } 3097 3098 case SCANNER_STATE_TEXT_DECL: { 3099 // scan text decl 3100 if (fEntityScanner.skipString("<?xml")) { 3101 fMarkupDepth++; 3102 // NOTE: special case where entity starts with a PI 3103 // whose name starts with "xml" (e.g. "xmlfoo") 3104 if (isValidNameChar(fEntityScanner.peekChar())) { 3105 fStringBuffer.clear(); 3106 fStringBuffer.append("xml"); 3107 3108 if (fNamespaces) { 3109 while (isValidNCName(fEntityScanner.peekChar())) { 3110 fStringBuffer.append((char)fEntityScanner.scanChar(null)); 3111 } 3112 } else { 3113 while (isValidNameChar(fEntityScanner.peekChar())) { 3114 fStringBuffer.append((char)fEntityScanner.scanChar(null)); 3115 } 3116 } 3117 String target = fSymbolTable.addSymbol(fStringBuffer.ch, fStringBuffer.offset, fStringBuffer.length); 3118 fContentBuffer.clear(); 3119 scanPIData(target, fContentBuffer); 3120 } 3121 3122 // standard text declaration 3123 else { 3124 //xxx: this function gives callback 3125 scanXMLDeclOrTextDecl(true); 3126 } 3127 } 3128 // now that we've straightened out the readers, we can read in chunks: 3129 fEntityManager.fCurrentEntity.mayReadChunks = true; 3130 setScannerState(SCANNER_STATE_CONTENT); 3131 //xxx: we don't return any state, so how do we get to know about TEXT declarations. 3132 //it seems we have to careful when to allow function issue a callback 3133 //and when to allow adapter issue a callback. 3134 continue; 3135 } 3136 3137 3138 case SCANNER_STATE_ROOT_ELEMENT: { 3139 if (scanRootElementHook()) { 3140 fEmptyElement = true; 3141 //rest would be taken care by fTrailingMiscDriver set by scanRootElementHook 3142 return XMLEvent.START_ELEMENT; 3143 } 3144 setScannerState(SCANNER_STATE_CONTENT); 3145 return XMLEvent.START_ELEMENT ; 3146 } 3147 case SCANNER_STATE_CHAR_REFERENCE : { 3148 fContentBuffer.clear(); 3149 scanCharReferenceValue(fContentBuffer, null); 3150 fMarkupDepth--; 3151 setScannerState(SCANNER_STATE_CONTENT); 3152 return XMLEvent.CHARACTERS; 3153 } 3154 default: 3155 throw new XNIException("Scanner State " + fScannerState + " not Recognized "); 3156 3157 }//switch 3158 } 3159 // premature end of file 3160 catch (EOFException e) { 3161 endOfFileHook(e); 3162 return -1; 3163 } 3164 } //while loop 3165 }//next 3166 3167 // 3168 // Protected methods 3169 // 3170 3171 // hooks 3172 3173 // NOTE: These hook methods are added so that the full document 3174 // scanner can share the majority of code with this class. 3175 3176 /** 3177 * Scan for DOCTYPE hook. This method is a hook for subclasses 3178 * to add code to handle scanning for a the "DOCTYPE" string 3179 * after the string "<!" has been scanned. 3180 * 3181 * @return True if the "DOCTYPE" was scanned; false if "DOCTYPE" 3182 * was not scanned. 3183 */ 3184 protected boolean scanForDoctypeHook() 3185 throws IOException, XNIException { 3186 return false; 3187 } // scanForDoctypeHook():boolean 3188 3189 /** 3190 * Element depth iz zero. This methos is a hook for subclasses 3191 * to add code to handle when the element depth hits zero. When 3192 * scanning a document fragment, an element depth of zero is 3193 * normal. However, when scanning a full XML document, the 3194 * scanner must handle the trailing miscellanous section of 3195 * the document after the end of the document's root element. 3196 * 3197 * @return True if the caller should stop and return true which 3198 * allows the scanner to switch to a new scanning 3199 * driver. A return value of false indicates that 3200 * the content driver should continue as normal. 3201 */ 3202 protected boolean elementDepthIsZeroHook() 3203 throws IOException, XNIException { 3204 return false; 3205 } // elementDepthIsZeroHook():boolean 3206 3207 /** 3208 * Scan for root element hook. This method is a hook for 3209 * subclasses to add code that handles scanning for the root 3210 * element. When scanning a document fragment, there is no 3211 * "root" element. However, when scanning a full XML document, 3212 * the scanner must handle the root element specially. 3213 * 3214 * @return True if the caller should stop and return true which 3215 * allows the scanner to switch to a new scanning 3216 * driver. A return value of false indicates that 3217 * the content driver should continue as normal. 3218 */ 3219 protected boolean scanRootElementHook() 3220 throws IOException, XNIException { 3221 return false; 3222 } // scanRootElementHook():boolean 3223 3224 /** 3225 * End of file hook. This method is a hook for subclasses to 3226 * add code that handles the end of file. The end of file in 3227 * a document fragment is OK if the markup depth is zero. 3228 * However, when scanning a full XML document, an end of file 3229 * is always premature. 3230 */ 3231 protected void endOfFileHook(EOFException e) 3232 throws IOException, XNIException { 3233 3234 // NOTE: An end of file is only only an error if we were 3235 // in the middle of scanning some markup. -Ac 3236 if (fMarkupDepth != 0) { 3237 reportFatalError("PrematureEOF", null); 3238 } 3239 3240 } // endOfFileHook() 3241 3242 } // class FragmentContentDriver 3243 3244 static void pr(String str) { 3245 System.out.println(str) ; 3246 } 3247 3248 protected boolean fUsebuffer ; 3249 3250 /** this function gets an XMLString (which is used to store the attribute value) from the special pool 3251 * maintained for attributes. 3252 * fAttributeCacheUsedCount tracks the number of attributes that has been consumed from the pool. 3253 * if all the attributes has been consumed, it adds a new XMLString inthe pool and returns the same 3254 * XMLString. 3255 * 3256 * @return XMLString XMLString used to store an attribute value. 3257 */ 3258 3259 protected XMLString getString(){ 3260 if(fAttributeCacheUsedCount < initialCacheCount || fAttributeCacheUsedCount < attributeValueCache.size()){ 3261 return attributeValueCache.get(fAttributeCacheUsedCount++); 3262 } else{ 3263 XMLString str = new XMLString(); 3264 fAttributeCacheUsedCount++; 3265 attributeValueCache.add(str); 3266 return str; 3267 } 3268 } 3269 3270 /** 3271 * Implements XMLBufferListener interface. 3272 */ 3273 3274 public void refresh(){ 3275 refresh(0); 3276 } 3277 3278 /** 3279 * receives callbacks from {@link XMLEntityReader } when buffer 3280 * is being changed. 3281 * @param refreshPosition 3282 */ 3283 public void refresh(int refreshPosition){ 3284 //If you are reading attributes and you got a callback 3285 //cache available attributes. 3286 if(fReadingAttributes){ 3287 fAttributes.refresh(); 3288 } 3289 if(fScannerState == SCANNER_STATE_CHARACTER_DATA){ 3290 //since fTempString directly matches to the underlying main buffer 3291 //store the data into buffer 3292 fContentBuffer.append(fTempString); 3293 //clear the XMLString so that data can't be added again. 3294 fTempString.length = 0; 3295 fUsebuffer = true; 3296 } 3297 } 3298 3299 } // class XMLDocumentFragmentScannerImpl