1 /*
   2  * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 
   5 /*
   6  * Copyright 2005 The Apache Software Foundation.
   7  *
   8  * Licensed under the Apache License, Version 2.0 (the "License");
   9  * you may not use this file except in compliance with the License.
  10  * You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 package com.sun.org.apache.xerces.internal.impl;
  22 
  23 
  24 import com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDDescription;
  25 import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager;
  26 import com.sun.org.apache.xerces.internal.util.NamespaceSupport;
  27 import com.sun.org.apache.xerces.internal.util.XMLChar;
  28 import com.sun.org.apache.xerces.internal.util.XMLResourceIdentifierImpl;
  29 import com.sun.org.apache.xerces.internal.util.XMLStringBuffer;
  30 import com.sun.org.apache.xerces.internal.utils.SecuritySupport;
  31 import com.sun.org.apache.xerces.internal.xni.Augmentations;
  32 import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
  33 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier;
  34 import com.sun.org.apache.xerces.internal.xni.XNIException;
  35 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
  36 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
  37 import com.sun.org.apache.xerces.internal.xni.parser.XMLDTDScanner;
  38 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
  39 import com.sun.xml.internal.stream.Entity;
  40 import com.sun.xml.internal.stream.StaxXMLInputSource;
  41 import com.sun.xml.internal.stream.dtd.DTDGrammarUtil;
  42 import java.io.EOFException;
  43 import java.io.IOException;
  44 import javax.xml.stream.XMLInputFactory;
  45 import javax.xml.stream.events.XMLEvent;
  46 
  47 
  48 /**
  49  * This class is responsible for scanning XML document structure
  50  * and content.
  51  *
  52  * This class has been modified as per the new design which is more suited to
  53  * efficiently build pull parser. Lot of improvements have been done and
  54  * the code has been added to support stax functionality/features.
  55  *
  56  * @author Neeraj Bajaj, Sun Microsystems
  57  * @author K.Venugopal, Sun Microsystems
  58  * @author Glenn Marcy, IBM
  59  * @author Andy Clark, IBM
  60  * @author Arnaud  Le Hors, IBM
  61  * @author Eric Ye, IBM
  62  * @author Sunitha Reddy, Sun Microsystems
  63  *
  64  * Refer to the table in unit-test javax.xml.stream.XMLStreamReaderTest.SupportDTD for changes
  65  * related to property SupportDTD.
  66  * @author Joe Wang, Sun Microsystems
  67  */
  68 public class XMLDocumentScannerImpl
  69         extends XMLDocumentFragmentScannerImpl{
  70 
  71     //
  72     // Constants
  73     //
  74 
  75     // scanner states
  76 
  77     /** Scanner state: XML declaration. */
  78     protected static final int SCANNER_STATE_XML_DECL = 42;
  79 
  80     /** Scanner state: prolog. */
  81     protected static final int SCANNER_STATE_PROLOG = 43;
  82 
  83     /** Scanner state: trailing misc. */
  84     protected static final int SCANNER_STATE_TRAILING_MISC = 44;
  85 
  86     /** Scanner state: DTD internal declarations. */
  87     protected static final int SCANNER_STATE_DTD_INTERNAL_DECLS = 45;
  88 
  89     /** Scanner state: open DTD external subset. */
  90     protected static final int SCANNER_STATE_DTD_EXTERNAL = 46;
  91 
  92     /** Scanner state: DTD external declarations. */
  93     protected static final int SCANNER_STATE_DTD_EXTERNAL_DECLS = 47;
  94 
  95     /** Scanner state: NO MORE ELEMENTS. */
  96     protected static final int SCANNER_STATE_NO_SUCH_ELEMENT_EXCEPTION = 48;
  97 
  98     // feature identifiers
  99 
 100     /** Property identifier document scanner: */
 101     protected static final String DOCUMENT_SCANNER =
 102             Constants.XERCES_PROPERTY_PREFIX + Constants.DOCUMENT_SCANNER_PROPERTY;
 103 
 104     /** Feature identifier: load external DTD. */
 105     protected static final String LOAD_EXTERNAL_DTD =
 106             Constants.XERCES_FEATURE_PREFIX + Constants.LOAD_EXTERNAL_DTD_FEATURE;
 107 
 108     /** Feature identifier: load external DTD. */
 109     protected static final String DISALLOW_DOCTYPE_DECL_FEATURE =
 110             Constants.XERCES_FEATURE_PREFIX + Constants.DISALLOW_DOCTYPE_DECL_FEATURE;
 111 
 112     // property identifiers
 113 
 114     /** Property identifier: DTD scanner. */
 115     protected static final String DTD_SCANNER =
 116             Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_SCANNER_PROPERTY;
 117 
 118     // property identifier:  ValidationManager
 119     protected static final String VALIDATION_MANAGER =
 120             Constants.XERCES_PROPERTY_PREFIX + Constants.VALIDATION_MANAGER_PROPERTY;
 121 
 122     /** property identifier:  NamespaceContext */
 123     protected static final String NAMESPACE_CONTEXT =
 124         Constants.XERCES_PROPERTY_PREFIX + Constants.NAMESPACE_CONTEXT_PROPERTY;
 125 
 126     // recognized features and properties
 127 
 128     /** Recognized features. */
 129     private static final String[] RECOGNIZED_FEATURES = {
 130         LOAD_EXTERNAL_DTD,
 131                 DISALLOW_DOCTYPE_DECL_FEATURE,
 132     };
 133 
 134     /** Feature defaults. */
 135     private static final Boolean[] FEATURE_DEFAULTS = {
 136         Boolean.TRUE,
 137                 Boolean.FALSE,
 138     };
 139 
 140     /** Recognized properties. */
 141     private static final String[] RECOGNIZED_PROPERTIES = {
 142         DTD_SCANNER,
 143                 VALIDATION_MANAGER
 144     };
 145 
 146     /** Property defaults. */
 147     private static final Object[] PROPERTY_DEFAULTS = {
 148             null,
 149                 null
 150     };
 151 
 152     //
 153     // Data((Boolean)propertyManager.getProperty(XMLInputFactory.IS_NAMESPACE_AWARE)).booleanValue();
 154     //
 155 
 156     // properties
 157 
 158     /** DTD scanner. */
 159     protected XMLDTDScanner fDTDScanner = null;
 160 
 161     /** Validation manager . */
 162     //xxx: fValidationManager code needs to be added yet!
 163     protected ValidationManager fValidationManager;
 164 
 165     protected XMLStringBuffer fDTDDecl = null;
 166     protected boolean fReadingDTD = false;
 167     protected boolean fAddedListener = false;
 168 
 169     // protected data
 170 
 171     // other info
 172 
 173     /** Doctype name. */
 174     protected String fDoctypeName;
 175 
 176     /** Doctype declaration public identifier. */
 177     protected String fDoctypePublicId;
 178 
 179     /** Doctype declaration system identifier. */
 180     protected String fDoctypeSystemId;
 181 
 182     /** Namespace support. */
 183     protected NamespaceContext fNamespaceContext = new NamespaceSupport();
 184 
 185     // features
 186 
 187     /** Load external DTD. */
 188     protected boolean fLoadExternalDTD = true;
 189 
 190     // state
 191 
 192     /** Seen doctype declaration. */
 193     protected boolean fSeenDoctypeDecl;
 194 
 195     protected boolean fScanEndElement;
 196 
 197     //protected int fScannerLastState ;
 198 
 199     // drivers
 200 
 201     /** XML declaration driver. */
 202     protected Driver fXMLDeclDriver = new XMLDeclDriver();
 203 
 204     /** Prolog driver. */
 205     protected Driver fPrologDriver = new PrologDriver();
 206 
 207     /** DTD driver. */
 208     protected Driver fDTDDriver = null ;
 209 
 210     /** Trailing miscellaneous section driver. */
 211     protected Driver fTrailingMiscDriver = new TrailingMiscDriver();
 212     protected int fStartPos = 0;
 213     protected int fEndPos = 0;
 214     protected boolean fSeenInternalSubset= false;
 215     // temporary variables
 216 
 217     /** Array of 3 strings. */
 218     private String[] fStrings = new String[3];
 219 
 220     /** External subset source. */
 221     private XMLInputSource fExternalSubsetSource = null;
 222 
 223     /** A DTD Description. */
 224     private final XMLDTDDescription fDTDDescription = new XMLDTDDescription(null, null, null, null, null);
 225 
 226     private static final char [] DOCTYPE = {'D','O','C','T','Y','P','E'};
 227     private static final char [] COMMENTSTRING = {'-','-'};
 228 
 229     //
 230     // Constructors
 231     //
 232 
 233     /** Default constructor. */
 234     public XMLDocumentScannerImpl() {} // <init>()
 235 
 236 
 237     //
 238     // XMLDocumentScanner methods
 239     //
 240 
 241 
 242     /**
 243      * Sets the input source.
 244      *
 245      * @param inputSource The input source.
 246      *
 247      * @throws IOException Thrown on i/o error.
 248      */
 249     public void setInputSource(XMLInputSource inputSource) throws IOException {
 250         fEntityManager.setEntityHandler(this);
 251         //this starts a new entity and sets the current entity to the document entity.
 252         fEntityManager.startDocumentEntity(inputSource);
 253         // fDocumentSystemId = fEntityManager.expandSystemId(inputSource.getSystemId());
 254         setScannerState(XMLEvent.START_DOCUMENT);
 255     } // setInputSource(XMLInputSource)
 256 
 257 
 258 
 259     /**return the state of the scanner */
 260     public int getScannetState(){
 261         return fScannerState ;
 262     }
 263 
 264 
 265 
 266 
 267     public void reset(PropertyManager propertyManager) {
 268         super.reset(propertyManager);
 269         // other settings
 270         fDoctypeName = null;
 271         fDoctypePublicId = null;
 272         fDoctypeSystemId = null;
 273         fSeenDoctypeDecl = false;
 274         fNamespaceContext.reset();
 275         fSupportDTD = ((Boolean)propertyManager.getProperty(XMLInputFactory.SUPPORT_DTD)).booleanValue();
 276 
 277         // xerces features
 278         fLoadExternalDTD = !((Boolean)propertyManager.getProperty(Constants.ZEPHYR_PROPERTY_PREFIX + Constants.IGNORE_EXTERNAL_DTD)).booleanValue();
 279         setScannerState(XMLEvent.START_DOCUMENT);
 280         setDriver(fXMLDeclDriver);
 281         fSeenInternalSubset = false;
 282         if(fDTDScanner != null){
 283             ((XMLDTDScannerImpl)fDTDScanner).reset(propertyManager);
 284         }
 285         fEndPos = 0;
 286         fStartPos = 0;
 287         if(fDTDDecl != null){
 288             fDTDDecl.clear();
 289         }
 290 
 291     }
 292 
 293     /**
 294      * Resets the component. The component can query the component manager
 295      * about any features and properties that affect the operation of the
 296      * component.
 297      *
 298      * @param componentManager The component manager.
 299      *
 300      * @throws SAXException Thrown by component on initialization error.
 301      *                      For example, if a feature or property is
 302      *                      required for the operation of the component, the
 303      *                      component manager may throw a
 304      *                      SAXNotRecognizedException or a
 305      *                      SAXNotSupportedException.
 306      */
 307     public void reset(XMLComponentManager componentManager)
 308     throws XMLConfigurationException {
 309 
 310         super.reset(componentManager);
 311 
 312         // other settings
 313         fDoctypeName = null;
 314         fDoctypePublicId = null;
 315         fDoctypeSystemId = null;
 316         fSeenDoctypeDecl = false;
 317         fExternalSubsetSource = null;
 318 
 319         // xerces features
 320         fLoadExternalDTD = componentManager.getFeature(LOAD_EXTERNAL_DTD, true);
 321         fDisallowDoctype = componentManager.getFeature(DISALLOW_DOCTYPE_DECL_FEATURE, false);
 322 
 323         fNamespaces = componentManager.getFeature(NAMESPACES, true);
 324 
 325         fSeenInternalSubset = false;
 326         // xerces properties
 327         fDTDScanner = (XMLDTDScanner)componentManager.getProperty(DTD_SCANNER);
 328 
 329         fValidationManager = (ValidationManager)componentManager.getProperty(VALIDATION_MANAGER, null);
 330 
 331         try {
 332             fNamespaceContext = (NamespaceContext)componentManager.getProperty(NAMESPACE_CONTEXT);
 333         }
 334         catch (XMLConfigurationException e) { }
 335         if (fNamespaceContext == null) {
 336             fNamespaceContext = new NamespaceSupport();
 337         }
 338         fNamespaceContext.reset();
 339 
 340         fEndPos = 0;
 341         fStartPos = 0;
 342         if(fDTDDecl != null)
 343             fDTDDecl.clear();
 344 
 345 
 346         //fEntityScanner.registerListener((XMLBufferListener)componentManager.getProperty(DOCUMENT_SCANNER));
 347 
 348         // setup driver
 349         setScannerState(SCANNER_STATE_XML_DECL);
 350         setDriver(fXMLDeclDriver);
 351 
 352     } // reset(XMLComponentManager)
 353 
 354 
 355     /**
 356      * Returns a list of feature identifiers that are recognized by
 357      * this component. This method may return null if no features
 358      * are recognized by this component.
 359      */
 360     public String[] getRecognizedFeatures() {
 361         String[] featureIds = super.getRecognizedFeatures();
 362         int length = featureIds != null ? featureIds.length : 0;
 363         String[] combinedFeatureIds = new String[length + RECOGNIZED_FEATURES.length];
 364         if (featureIds != null) {
 365             System.arraycopy(featureIds, 0, combinedFeatureIds, 0, featureIds.length);
 366         }
 367         System.arraycopy(RECOGNIZED_FEATURES, 0, combinedFeatureIds, length, RECOGNIZED_FEATURES.length);
 368         return combinedFeatureIds;
 369     } // getRecognizedFeatures():String[]
 370 
 371     /**
 372      * Sets the state of a feature. This method is called by the component
 373      * manager any time after reset when a feature changes state.
 374      * <p>
 375      * <strong>Note:</strong> Components should silently ignore features
 376      * that do not affect the operation of the component.
 377      *
 378      * @param featureId The feature identifier.
 379      * @param state     The state of the feature.
 380      *
 381      * @throws SAXNotRecognizedException The component should not throw
 382      *                                   this exception.
 383      * @throws SAXNotSupportedException The component should not throw
 384      *                                  this exception.
 385      */
 386     public void setFeature(String featureId, boolean state)
 387     throws XMLConfigurationException {
 388 
 389         super.setFeature(featureId, state);
 390 
 391         // Xerces properties
 392         if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) {
 393             final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length();
 394 
 395             if (suffixLength == Constants.LOAD_EXTERNAL_DTD_FEATURE.length() &&
 396                 featureId.endsWith(Constants.LOAD_EXTERNAL_DTD_FEATURE)) {
 397                 fLoadExternalDTD = state;
 398                 return;
 399             }
 400             else if (suffixLength == Constants.DISALLOW_DOCTYPE_DECL_FEATURE.length() &&
 401                 featureId.endsWith(Constants.DISALLOW_DOCTYPE_DECL_FEATURE)) {
 402                 fDisallowDoctype = state;
 403                 return;
 404             }
 405         }
 406 
 407     } // setFeature(String,boolean)
 408 
 409     /**
 410      * Returns a list of property identifiers that are recognized by
 411      * this component. This method may return null if no properties
 412      * are recognized by this component.
 413      */
 414     public String[] getRecognizedProperties() {
 415         String[] propertyIds = super.getRecognizedProperties();
 416         int length = propertyIds != null ? propertyIds.length : 0;
 417         String[] combinedPropertyIds = new String[length + RECOGNIZED_PROPERTIES.length];
 418         if (propertyIds != null) {
 419             System.arraycopy(propertyIds, 0, combinedPropertyIds, 0, propertyIds.length);
 420         }
 421         System.arraycopy(RECOGNIZED_PROPERTIES, 0, combinedPropertyIds, length, RECOGNIZED_PROPERTIES.length);
 422         return combinedPropertyIds;
 423     } // getRecognizedProperties():String[]
 424 
 425     /**
 426      * Sets the value of a property. This method is called by the component
 427      * manager any time after reset when a property changes value.
 428      * <p>
 429      * <strong>Note:</strong> Components should silently ignore properties
 430      * that do not affect the operation of the component.
 431      *
 432      * @param propertyId The property identifier.
 433      * @param value      The value of the property.
 434      *
 435      * @throws SAXNotRecognizedException The component should not throw
 436      *                                   this exception.
 437      * @throws SAXNotSupportedException The component should not throw
 438      *                                  this exception.
 439      */
 440     public void setProperty(String propertyId, Object value)
 441     throws XMLConfigurationException {
 442 
 443         super.setProperty(propertyId, value);
 444 
 445         // Xerces properties
 446         if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) {
 447             final int suffixLength = propertyId.length() - Constants.XERCES_PROPERTY_PREFIX.length();
 448 
 449             if (suffixLength == Constants.DTD_SCANNER_PROPERTY.length() &&
 450                 propertyId.endsWith(Constants.DTD_SCANNER_PROPERTY)) {
 451                 fDTDScanner = (XMLDTDScanner)value;
 452             }
 453             if (suffixLength == Constants.NAMESPACE_CONTEXT_PROPERTY.length() &&
 454                 propertyId.endsWith(Constants.NAMESPACE_CONTEXT_PROPERTY)) {
 455                 if (value != null) {
 456                     fNamespaceContext = (NamespaceContext)value;
 457                 }
 458             }
 459 
 460             return;
 461         }
 462 
 463     } // setProperty(String,Object)
 464 
 465     /**
 466      * Returns the default state for a feature, or null if this
 467      * component does not want to report a default value for this
 468      * feature.
 469      *
 470      * @param featureId The feature identifier.
 471      *
 472      * @since Xerces 2.2.0
 473      */
 474     public Boolean getFeatureDefault(String featureId) {
 475 
 476         for (int i = 0; i < RECOGNIZED_FEATURES.length; i++) {
 477             if (RECOGNIZED_FEATURES[i].equals(featureId)) {
 478                 return FEATURE_DEFAULTS[i];
 479             }
 480         }
 481         return super.getFeatureDefault(featureId);
 482     } // getFeatureDefault(String):Boolean
 483 
 484     /**
 485      * Returns the default state for a property, or null if this
 486      * component does not want to report a default value for this
 487      * property.
 488      *
 489      * @param propertyId The property identifier.
 490      *
 491      * @since Xerces 2.2.0
 492      */
 493     public Object getPropertyDefault(String propertyId) {
 494         for (int i = 0; i < RECOGNIZED_PROPERTIES.length; i++) {
 495             if (RECOGNIZED_PROPERTIES[i].equals(propertyId)) {
 496                 return PROPERTY_DEFAULTS[i];
 497             }
 498         }
 499         return super.getPropertyDefault(propertyId);
 500     } // getPropertyDefault(String):Object
 501 
 502     //
 503     // XMLEntityHandler methods
 504     //
 505 
 506     /**
 507      * This method notifies of the start of an entity. The DTD has the
 508      * pseudo-name of "[dtd]" parameter entity names start with '%'; and
 509      * general entities are just specified by their name.
 510      *
 511      * @param name     The name of the entity.
 512      * @param identifier The resource identifier.
 513      * @param encoding The auto-detected IANA encoding name of the entity
 514      *                 stream. This value will be null in those situations
 515      *                 where the entity encoding is not auto-detected (e.g.
 516      *                 internal entities or a document entity that is
 517      *                 parsed from a java.io.Reader).
 518      *
 519      * @throws XNIException Thrown by handler to signal an error.
 520      */
 521     public void startEntity(String name,
 522             XMLResourceIdentifier identifier,
 523             String encoding, Augmentations augs) throws XNIException {
 524 
 525         super.startEntity(name, identifier, encoding,augs);
 526 
 527         //register current document scanner as a listener for XMLEntityScanner
 528         fEntityScanner.registerListener(this);
 529 
 530         // prepare to look for a TextDecl if external general entity
 531         if (!name.equals("[xml]") && fEntityScanner.isExternal()) {
 532             // Don't do this if we're skipping the entity!
 533             if (augs == null || !((Boolean) augs.getItem(Constants.ENTITY_SKIPPED)).booleanValue()) {
 534                 setScannerState(SCANNER_STATE_TEXT_DECL);
 535             }
 536         }
 537 
 538         // call handler
 539         /** comment this part.. LOCATOR problem.. */
 540         if (fDocumentHandler != null && name.equals("[xml]")) {
 541             fDocumentHandler.startDocument(fEntityScanner, encoding, fNamespaceContext, null);
 542         }
 543 
 544     } // startEntity(String,identifier,String)
 545 
 546 
 547     /**
 548      * This method notifies the end of an entity. The DTD has the pseudo-name
 549      * of "[dtd]" parameter entity names start with '%'; and general entities
 550      * are just specified by their name.
 551      *
 552      * @param name The name of the entity.
 553      *
 554      * @throws XNIException Thrown by handler to signal an error.
 555      */
 556     public void endEntity(String name, Augmentations augs) throws IOException, XNIException {
 557 
 558         super.endEntity(name, augs);
 559 
 560         if(name.equals("[xml]")){
 561             //if fMarkupDepth has reached 0.
 562             //and driver is fTrailingMiscDriver (which
 563             //handles end of document in normal case)
 564             //set the scanner state of SCANNER_STATE_TERMINATED
 565             if(fMarkupDepth == 0 && fDriver == fTrailingMiscDriver){
 566                 //set the scanner set to SCANNER_STATE_TERMINATED
 567                 setScannerState(SCANNER_STATE_TERMINATED) ;
 568             } else{
 569                 //else we have reached the end of document prematurely
 570                 //so throw EOFException.
 571                 throw new java.io.EOFException();
 572             }
 573 
 574             //this is taken care in wrapper which generates XNI callbacks, There are no next events
 575 
 576             //if (fDocumentHandler != null) {
 577                 //fDocumentHandler.endDocument(null);
 578             //}
 579         }
 580     } // endEntity(String)
 581 
 582 
 583     public XMLStringBuffer getDTDDecl(){
 584         Entity entity = fEntityScanner.getCurrentEntity();
 585         fDTDDecl.append(((Entity.ScannedEntity)entity).ch,fStartPos , fEndPos-fStartPos);
 586         if(fSeenInternalSubset)
 587             fDTDDecl.append("]>");
 588         return fDTDDecl;
 589     }
 590 
 591     public String getCharacterEncodingScheme(){
 592         return fDeclaredEncoding;
 593     }
 594 
 595     /** return the next state on the input
 596      *
 597      * @return int
 598      */
 599 
 600     public int next() throws IOException, XNIException {
 601         return fDriver.next();
 602     }
 603 
 604     //getNamespaceContext
 605     public NamespaceContext getNamespaceContext(){
 606         return fNamespaceContext ;
 607     }
 608 
 609 
 610 
 611     //
 612     // Protected methods
 613     //
 614 
 615     // driver factory methods
 616 
 617     /** Creates a content driver. */
 618     protected Driver createContentDriver() {
 619         return new ContentDriver();
 620     } // createContentDriver():Driver
 621 
 622     // scanning methods
 623 
 624     /** Scans a doctype declaration. */
 625     protected boolean scanDoctypeDecl(boolean supportDTD) throws IOException, XNIException {
 626 
 627         // spaces
 628         if (!fEntityScanner.skipSpaces()) {
 629             reportFatalError("MSG_SPACE_REQUIRED_BEFORE_ROOT_ELEMENT_TYPE_IN_DOCTYPEDECL",
 630                     null);
 631         }
 632 
 633         // root element name
 634         fDoctypeName = fEntityScanner.scanName();
 635         if (fDoctypeName == null) {
 636             reportFatalError("MSG_ROOT_ELEMENT_TYPE_REQUIRED", null);
 637         }
 638 
 639         // external id
 640         if (fEntityScanner.skipSpaces()) {
 641             scanExternalID(fStrings, false);
 642             fDoctypeSystemId = fStrings[0];
 643             fDoctypePublicId = fStrings[1];
 644             fEntityScanner.skipSpaces();
 645         }
 646 
 647         fHasExternalDTD = fDoctypeSystemId != null;
 648 
 649         // Attempt to locate an external subset with an external subset resolver.
 650         if (supportDTD && !fHasExternalDTD && fExternalSubsetResolver != null) {
 651             fDTDDescription.setValues(null, null, fEntityManager.getCurrentResourceIdentifier().getExpandedSystemId(), null);
 652             fDTDDescription.setRootName(fDoctypeName);
 653             fExternalSubsetSource = fExternalSubsetResolver.getExternalSubset(fDTDDescription);
 654             fHasExternalDTD = fExternalSubsetSource != null;
 655         }
 656 
 657         // call handler
 658         if (supportDTD && fDocumentHandler != null) {
 659             // NOTE: I don't like calling the doctypeDecl callback until
 660             //       end of the *full* doctype line (including internal
 661             //       subset) is parsed correctly but SAX2 requires that
 662             //       it knows the root element name and public and system
 663             //       identifier for the startDTD call. -Ac
 664             if (fExternalSubsetSource == null) {
 665                 fDocumentHandler.doctypeDecl(fDoctypeName, fDoctypePublicId, fDoctypeSystemId, null);
 666             }
 667             else {
 668                 fDocumentHandler.doctypeDecl(fDoctypeName, fExternalSubsetSource.getPublicId(), fExternalSubsetSource.getSystemId(), null);
 669             }
 670         }
 671 
 672         // is there an internal subset?
 673         boolean internalSubset = true;
 674         if (!fEntityScanner.skipChar('[')) {
 675             internalSubset = false;
 676             fEntityScanner.skipSpaces();
 677             if (!fEntityScanner.skipChar('>')) {
 678                 reportFatalError("DoctypedeclUnterminated", new Object[]{fDoctypeName});
 679             }
 680             fMarkupDepth--;
 681         }
 682         return internalSubset;
 683 
 684     } // scanDoctypeDecl():boolean
 685 
 686     //
 687     // Private methods
 688     //
 689     /** Set the scanner state after scanning DTD */
 690     protected void setEndDTDScanState() {
 691         setScannerState(SCANNER_STATE_PROLOG);
 692         setDriver(fPrologDriver);
 693         fEntityManager.setEntityHandler(XMLDocumentScannerImpl.this);
 694         fReadingDTD=false;
 695     }
 696 
 697     /** Returns the scanner state name. */
 698     protected String getScannerStateName(int state) {
 699 
 700         switch (state) {
 701             case SCANNER_STATE_XML_DECL: return "SCANNER_STATE_XML_DECL";
 702             case SCANNER_STATE_PROLOG: return "SCANNER_STATE_PROLOG";
 703             case SCANNER_STATE_TRAILING_MISC: return "SCANNER_STATE_TRAILING_MISC";
 704             case SCANNER_STATE_DTD_INTERNAL_DECLS: return "SCANNER_STATE_DTD_INTERNAL_DECLS";
 705             case SCANNER_STATE_DTD_EXTERNAL: return "SCANNER_STATE_DTD_EXTERNAL";
 706             case SCANNER_STATE_DTD_EXTERNAL_DECLS: return "SCANNER_STATE_DTD_EXTERNAL_DECLS";
 707         }
 708         return super.getScannerStateName(state);
 709 
 710     } // getScannerStateName(int):String
 711 
 712     //
 713     // Classes
 714     //
 715 
 716     /**
 717      * Driver to handle XMLDecl scanning.
 718      *
 719      * This class has been modified as per the new design which is more suited to
 720      * efficiently build pull parser. Lots of performance improvements have been done and
 721      * the code has been added to support stax functionality/features.
 722      *
 723      * @author Neeraj Bajaj, Sun Microsystems.
 724      *
 725      * @author Andy Clark, IBM
 726      */
 727     protected final class XMLDeclDriver
 728             implements Driver {
 729 
 730         //
 731         // Driver methods
 732         //
 733 
 734 
 735         public int next() throws IOException, XNIException {
 736             if(DEBUG_NEXT){
 737                 System.out.println("NOW IN XMLDeclDriver");
 738             }
 739 
 740             // next driver is prolog regardless of whether there
 741             // is an XMLDecl in this document
 742             setScannerState(SCANNER_STATE_PROLOG);
 743             setDriver(fPrologDriver);
 744 
 745             //System.out.println("fEntityScanner = " + fEntityScanner);
 746             // scan XMLDecl
 747             try {
 748                 if (fEntityScanner.skipString(xmlDecl)) {
 749                     fMarkupDepth++;
 750                     // NOTE: special case where document starts with a PI
 751                     //       whose name starts with "xml" (e.g. "xmlfoo")
 752                     if (XMLChar.isName(fEntityScanner.peekChar())) {
 753                         fStringBuffer.clear();
 754                         fStringBuffer.append("xml");
 755                         while (XMLChar.isName(fEntityScanner.peekChar())) {
 756                             fStringBuffer.append((char)fEntityScanner.scanChar());
 757                         }
 758                         String target = fSymbolTable.addSymbol(fStringBuffer.ch, fStringBuffer.offset, fStringBuffer.length);
 759                         //this function should fill the data.. and set the fEvent object to this event.
 760                         fContentBuffer.clear() ;
 761                         scanPIData(target, fContentBuffer);
 762                         //REVISIT:where else we can set this value to 'true'
 763                         fEntityManager.fCurrentEntity.mayReadChunks = true;
 764                         //return PI event since PI was encountered
 765                         return XMLEvent.PROCESSING_INSTRUCTION ;
 766                     }
 767                     // standard XML declaration
 768                     else {
 769                         scanXMLDeclOrTextDecl(false);
 770                         //REVISIT:where else we can set this value to 'true'
 771                         fEntityManager.fCurrentEntity.mayReadChunks = true;
 772                         return XMLEvent.START_DOCUMENT;
 773                     }
 774                 } else{
 775                     //REVISIT:where else we can set this value to 'true'
 776                     fEntityManager.fCurrentEntity.mayReadChunks = true;
 777                     //In both case return the START_DOCUMENT. ony difference is that first block will
 778                     //cosume the XML declaration if any.
 779                     return XMLEvent.START_DOCUMENT;
 780                 }
 781 
 782 
 783                 //START_OF_THE_DOCUMENT
 784 
 785 
 786             }
 787 
 788             // premature end of file
 789             catch (EOFException e) {
 790                 reportFatalError("PrematureEOF", null);
 791                 return -1;
 792                 //throw e;
 793             }
 794 
 795         }
 796     } // class XMLDeclDriver
 797 
 798     /**
 799      * Driver to handle prolog scanning.
 800      *
 801      * @author Andy Clark, IBM
 802      */
 803     protected final class PrologDriver
 804             implements Driver {
 805 
 806         /**
 807          * Drives the parser to the next state/event on the input. Parser is guaranteed
 808          * to stop at the next state/event.
 809          *
 810          * Internally XML document is divided into several states. Each state represents
 811          * a sections of XML document. When this functions returns normally, it has read
 812          * the section of XML document and returns the state corresponding to section of
 813          * document which has been read. For optimizations, a particular driver
 814          * can read ahead of the section of document (state returned) just read and
 815          * can maintain a different internal state.
 816          *
 817          * @return state representing the section of document just read.
 818          *
 819          * @throws IOException  Thrown on i/o error.
 820          * @throws XNIException Thrown on parse error.
 821          */
 822 
 823         public int next() throws IOException, XNIException {
 824             //System.out.println("here in next");
 825 
 826             if(DEBUG_NEXT){
 827                 System.out.println("NOW IN PrologDriver");
 828             }
 829             try {
 830                 do {
 831                     switch (fScannerState) {
 832                         case SCANNER_STATE_PROLOG: {
 833                             fEntityScanner.skipSpaces();
 834                             if (fEntityScanner.skipChar('<')) {
 835                                 setScannerState(SCANNER_STATE_START_OF_MARKUP);
 836                             } else if (fEntityScanner.skipChar('&')) {
 837                                 setScannerState(SCANNER_STATE_REFERENCE);
 838                             } else {
 839                                 setScannerState(SCANNER_STATE_CONTENT);
 840                             }
 841                             break;
 842                         }
 843 
 844                         case SCANNER_STATE_START_OF_MARKUP: {
 845                             fMarkupDepth++;
 846                             if (isValidNameStartChar(fEntityScanner.peekChar()) ||
 847                                     isValidNameStartHighSurrogate(fEntityScanner.peekChar())) {
 848                                 setScannerState(SCANNER_STATE_ROOT_ELEMENT);
 849                                 setDriver(fContentDriver);
 850                                 //from now onwards this would be handled by fContentDriver,in the same next() call
 851                                 return fContentDriver.next();
 852                             } else if (fEntityScanner.skipChar('!')) {
 853                                 if (fEntityScanner.skipChar('-')) {
 854                                     if (!fEntityScanner.skipChar('-')) {
 855                                         reportFatalError("InvalidCommentStart",
 856                                                 null);
 857                                     }
 858                                     setScannerState(SCANNER_STATE_COMMENT);
 859                                 } else if (fEntityScanner.skipString(DOCTYPE)) {
 860                                     setScannerState(SCANNER_STATE_DOCTYPE);
 861                                     Entity entity = fEntityScanner.getCurrentEntity();
 862                                     if(entity instanceof Entity.ScannedEntity){
 863                                         fStartPos=((Entity.ScannedEntity)entity).position;
 864                                     }
 865                                     fReadingDTD=true;
 866                                     if(fDTDDecl == null)
 867                                         fDTDDecl = new XMLStringBuffer();
 868                                     fDTDDecl.append("<!DOCTYPE");
 869 
 870                                 } else {
 871                                     reportFatalError("MarkupNotRecognizedInProlog",
 872                                             null);
 873                                 }
 874                             } else if (fEntityScanner.skipChar('?')) {
 875                                 setScannerState(SCANNER_STATE_PI);
 876                             } else {
 877                                 reportFatalError("MarkupNotRecognizedInProlog",
 878                                         null);
 879                             }
 880                             break;
 881                         }
 882                     }
 883                 } while (fScannerState == SCANNER_STATE_PROLOG || fScannerState == SCANNER_STATE_START_OF_MARKUP );
 884 
 885                 switch(fScannerState){
 886                     /**
 887                     //this part is handled by FragmentContentHandler
 888                     case SCANNER_STATE_ROOT_ELEMENT: {
 889                         //we have read '<' and beginning of reading the start element tag
 890                         setScannerState(SCANNER_STATE_START_ELEMENT_TAG);
 891                         setDriver(fContentDriver);
 892                         //from now onwards this would be handled by fContentDriver,in the same next() call
 893                         return fContentDriver.next();
 894                     }
 895                      */
 896                     case SCANNER_STATE_COMMENT: {
 897                         //this function fills the data..
 898                         scanComment();
 899                         setScannerState(SCANNER_STATE_PROLOG);
 900                         return XMLEvent.COMMENT;
 901                         //setScannerState(SCANNER_STATE_PROLOG);
 902                         //break;
 903                     }
 904                     case SCANNER_STATE_PI: {
 905                         fContentBuffer.clear() ;
 906                         scanPI(fContentBuffer);
 907                         setScannerState(SCANNER_STATE_PROLOG);
 908                         return XMLEvent.PROCESSING_INSTRUCTION;
 909                     }
 910 
 911                     case SCANNER_STATE_DOCTYPE: {
 912                         if (fDisallowDoctype) {
 913                             reportFatalError("DoctypeNotAllowed", null);
 914                         }
 915 
 916                         if (fSeenDoctypeDecl) {
 917                             reportFatalError("AlreadySeenDoctype", null);
 918                         }
 919                         fSeenDoctypeDecl = true;
 920 
 921                         // scanDoctypeDecl() sends XNI doctypeDecl event that
 922                         // in SAX is converted to startDTD() event.
 923                         if (scanDoctypeDecl(fSupportDTD)) {
 924                             //allow parsing of entity decls to continue in order to stay well-formed
 925                             setScannerState(SCANNER_STATE_DTD_INTERNAL_DECLS);
 926                             fSeenInternalSubset = true;
 927                             if(fDTDDriver == null){
 928                                 fDTDDriver = new DTDDriver();
 929                             }
 930                             setDriver(fContentDriver);
 931                             //always return DTD event, the event however, will not contain any entities
 932                             return fDTDDriver.next();
 933                         }
 934 
 935                         if(fSeenDoctypeDecl){
 936                             Entity entity = fEntityScanner.getCurrentEntity();
 937                             if(entity instanceof Entity.ScannedEntity){
 938                                 fEndPos = ((Entity.ScannedEntity)entity).position;
 939                             }
 940                             fReadingDTD = false;
 941                         }
 942 
 943                         // handle external subset
 944                         if (fDoctypeSystemId != null) {
 945                             if (((fValidation || fLoadExternalDTD)
 946                                 && (fValidationManager == null || !fValidationManager.isCachedDTD()))) {
 947                                 if (fSupportDTD) {
 948                                     setScannerState(SCANNER_STATE_DTD_EXTERNAL);
 949                                 } else {
 950                                     setScannerState(SCANNER_STATE_PROLOG);
 951                                 }
 952 
 953                                 setDriver(fContentDriver);
 954                                 if(fDTDDriver == null) {
 955                                     fDTDDriver = new DTDDriver();
 956                                 }
 957 
 958                                 return fDTDDriver.next();
 959                             }
 960                         }
 961                         else if (fExternalSubsetSource != null) {
 962                             if (((fValidation || fLoadExternalDTD)
 963                                 && (fValidationManager == null || !fValidationManager.isCachedDTD()))) {
 964                                 // This handles the case of a DOCTYPE that had neither an internal subset or an external subset.
 965                                 fDTDScanner.setInputSource(fExternalSubsetSource);
 966                                 fExternalSubsetSource = null;
 967                             if (fSupportDTD)
 968                                 setScannerState(SCANNER_STATE_DTD_EXTERNAL_DECLS);
 969                             else
 970                                 setScannerState(SCANNER_STATE_PROLOG);
 971                             setDriver(fContentDriver);
 972                             if(fDTDDriver == null)
 973                                 fDTDDriver = new DTDDriver();
 974                             return fDTDDriver.next();
 975                             }
 976                         }
 977 
 978                         // Send endDTD() call if:
 979                         // a) systemId is null or if an external subset resolver could not locate an external subset.
 980                         // b) "load-external-dtd" and validation are false
 981                         // c) DTD grammar is cached
 982 
 983                         // in XNI this results in 3 events:  doctypeDecl, startDTD, endDTD
 984                         // in SAX this results in 2 events: startDTD, endDTD
 985                         if (fDTDScanner != null) {
 986                             fDTDScanner.setInputSource(null);
 987                         }
 988                         setScannerState(SCANNER_STATE_PROLOG);
 989                         return XMLEvent.DTD;
 990                     }
 991 
 992                     case SCANNER_STATE_CONTENT: {
 993                         reportFatalError("ContentIllegalInProlog", null);
 994                         fEntityScanner.scanChar();
 995                     }
 996                     case SCANNER_STATE_REFERENCE: {
 997                         reportFatalError("ReferenceIllegalInProlog", null);
 998                     }
 999 
1000                     /**
1001                      * if (complete) {
1002                      * if (fEntityScanner.scanChar() != '<') {
1003                      * reportFatalError("RootElementRequired", null);
1004                      * }
1005                      * setScannerState(SCANNER_STATE_ROOT_ELEMENT);
1006                      * setDriver(fContentDriver);
1007                      * }
1008                      */
1009                 }
1010             }
1011             // premature end of file
1012             catch (EOFException e) {
1013                 reportFatalError("PrematureEOF", null);
1014                 //xxx  what should be returned here.... ???
1015                 return -1 ;
1016                 //throw e;
1017             }
1018             //xxx  what should be returned here.... ???
1019             return -1;
1020 
1021         }
1022 
1023 
1024     } // class PrologDriver
1025 
1026     /**
1027      * Driver to handle the internal and external DTD subsets.
1028      *
1029      * @author Andy Clark, IBM
1030      */
1031     protected final class DTDDriver
1032             implements Driver {
1033 
1034         //
1035         // Driver methods
1036         //
1037 
1038         public int next() throws IOException, XNIException{
1039             // throw new XNIException("DTD Parsing is currently not supported");
1040             if(DEBUG_NEXT){
1041                 System.out.println("Now in DTD Driver");
1042             }
1043 
1044             dispatch(true);
1045 
1046             if(DEBUG_NEXT){
1047                 System.out.println("After calling dispatch(true) -- At this point whole DTD is read.");
1048             }
1049 
1050             //xxx: remove this hack and align this with reusing DTD components
1051             //currently this routine will only be executed from Stax
1052             if(fPropertyManager != null){
1053                 dtdGrammarUtil =  new DTDGrammarUtil(((XMLDTDScannerImpl)fDTDScanner).getGrammar(),fSymbolTable, fNamespaceContext);
1054             }
1055 
1056             return XMLEvent.DTD ;
1057         }
1058 
1059         /**
1060          * Dispatch an XML "event".
1061          *
1062          * @param complete True if this driver is intended to scan
1063          *                 and dispatch as much as possible.
1064          *
1065          * @return True if there is more to dispatch either from this
1066          *          or a another driver.
1067          *
1068          * @throws IOException  Thrown on i/o error.
1069          * @throws XNIException Thrown on parse error.
1070          */
1071         public boolean dispatch(boolean complete)
1072         throws IOException, XNIException {
1073             fEntityManager.setEntityHandler(null);
1074             try {
1075                 boolean again;
1076                 XMLResourceIdentifierImpl resourceIdentifier = new XMLResourceIdentifierImpl();
1077                 if( fDTDScanner == null){
1078 
1079                     if (fEntityManager.getEntityScanner() instanceof XML11EntityScanner){
1080                         fDTDScanner = new XML11DTDScannerImpl();
1081                     } else
1082 
1083                     fDTDScanner = new XMLDTDScannerImpl();
1084 
1085                     ((XMLDTDScannerImpl)fDTDScanner).reset(fPropertyManager);
1086                 }
1087 
1088                 fDTDScanner.setLimitAnalyzer(fLimitAnalyzer);
1089                 do {
1090                     again = false;
1091                     switch (fScannerState) {
1092                         case SCANNER_STATE_DTD_INTERNAL_DECLS: {
1093                             boolean moreToScan = false;
1094                             if (!fDTDScanner.skipDTD(fSupportDTD)) {
1095                                 // REVISIT: Should there be a feature for
1096                                 //          the "complete" parameter?
1097                                 boolean completeDTD = true;
1098 
1099                                 moreToScan = fDTDScanner.scanDTDInternalSubset(completeDTD, fStandalone, fHasExternalDTD && fLoadExternalDTD);
1100                             }
1101                             Entity entity = fEntityScanner.getCurrentEntity();
1102                             if(entity instanceof Entity.ScannedEntity){
1103                                 fEndPos=((Entity.ScannedEntity)entity).position;
1104                             }
1105                             fReadingDTD=false;
1106                             if (!moreToScan) {
1107                                 // end doctype declaration
1108                                 if (!fEntityScanner.skipChar(']')) {
1109                                     reportFatalError("EXPECTED_SQUARE_BRACKET_TO_CLOSE_INTERNAL_SUBSET",
1110                                             null);
1111                                 }
1112                                 fEntityScanner.skipSpaces();
1113                                 if (!fEntityScanner.skipChar('>')) {
1114                                     reportFatalError("DoctypedeclUnterminated", new Object[]{fDoctypeName});
1115                                 }
1116                                 fMarkupDepth--;
1117 
1118                                 if (!fSupportDTD) {
1119                                     //simply reset the entity store without having to mess around
1120                                     //with the DTD Scanner code
1121                                     fEntityStore = fEntityManager.getEntityStore();
1122                                     fEntityStore.reset();
1123                                 } else {
1124                                     // scan external subset next unless we are ignoring DTDs
1125                                     if (fDoctypeSystemId != null && (fValidation || fLoadExternalDTD)) {
1126                                         setScannerState(SCANNER_STATE_DTD_EXTERNAL);
1127                                         break;
1128                                     }
1129                                 }
1130 
1131                                 setEndDTDScanState();
1132                                 return true;
1133 
1134                             }
1135                             break;
1136                         }
1137                         case SCANNER_STATE_DTD_EXTERNAL: {
1138                             /**
1139                             fDTDDescription.setValues(fDoctypePublicId, fDoctypeSystemId, null, null);
1140                             fDTDDescription.setRootName(fDoctypeName);
1141                             XMLInputSource xmlInputSource =
1142                                 fEntityManager.resolveEntity(fDTDDescription);
1143                             fDTDScanner.setInputSource(xmlInputSource);
1144                             setScannerState(SCANNER_STATE_DTD_EXTERNAL_DECLS);
1145                             again = true;
1146                             break;
1147                              */
1148 
1149                             resourceIdentifier.setValues(fDoctypePublicId, fDoctypeSystemId, null, null);
1150                             XMLInputSource xmlInputSource = null ;
1151                             StaxXMLInputSource staxInputSource =  fEntityManager.resolveEntityAsPerStax(resourceIdentifier);
1152 
1153                             // Check access permission. If the source is resolved by a resolver, the check is skipped.
1154                             if (!staxInputSource.isCreatedByResolver()) {
1155                                 String accessError = checkAccess(fDoctypeSystemId, fAccessExternalDTD);
1156                                 if (accessError != null) {
1157                                     reportFatalError("AccessExternalDTD", new Object[]{ SecuritySupport.sanitizePath(fDoctypeSystemId), accessError });
1158                                 }
1159                             }
1160                             xmlInputSource = staxInputSource.getXMLInputSource();
1161                             fDTDScanner.setInputSource(xmlInputSource);
1162                             if (fEntityScanner.fCurrentEntity != null) {
1163                                 setScannerState(SCANNER_STATE_DTD_EXTERNAL_DECLS);
1164                             } else {
1165                                 setScannerState(SCANNER_STATE_PROLOG);
1166                             }
1167                             again = true;
1168                             break;
1169                         }
1170                         case SCANNER_STATE_DTD_EXTERNAL_DECLS: {
1171                             // REVISIT: Should there be a feature for
1172                             //          the "complete" parameter?
1173                             boolean completeDTD = true;
1174                             boolean moreToScan = fDTDScanner.scanDTDExternalSubset(completeDTD);
1175                             if (!moreToScan) {
1176                                 setEndDTDScanState();
1177                                 return true;
1178                             }
1179                             break;
1180                         }
1181                         case SCANNER_STATE_PROLOG : {
1182                             // skip entity decls
1183                             setEndDTDScanState();
1184                             return true;
1185                         }
1186                         default: {
1187                             throw new XNIException("DTDDriver#dispatch: scanner state="+fScannerState+" ("+getScannerStateName(fScannerState)+')');
1188                         }
1189                     }
1190                 } while (complete || again);
1191             }
1192 
1193             // premature end of file
1194             catch (EOFException e) {
1195                 e.printStackTrace();
1196                 reportFatalError("PrematureEOF", null);
1197                 return false;
1198                 //throw e;
1199             }
1200 
1201             // cleanup
1202             finally {
1203                 fEntityManager.setEntityHandler(XMLDocumentScannerImpl.this);
1204             }
1205 
1206             return true;
1207 
1208         }
1209 
1210         // dispatch(boolean):boolean
1211 
1212     } // class DTDDriver
1213 
1214     /**
1215      * Driver to handle content scanning.
1216      *
1217      * @author Andy Clark, IBM
1218      * @author Eric Ye, IBM
1219      */
1220     protected class ContentDriver
1221             extends FragmentContentDriver {
1222 
1223         //
1224         // Protected methods
1225         //
1226 
1227         // hooks
1228 
1229         // NOTE: These hook methods are added so that the full document
1230         //       scanner can share the majority of code with this class.
1231 
1232         /**
1233          * Scan for DOCTYPE hook. This method is a hook for subclasses
1234          * to add code to handle scanning for a the "DOCTYPE" string
1235          * after the string "<!" has been scanned.
1236          *
1237          * @return True if the "DOCTYPE" was scanned; false if "DOCTYPE"
1238          *          was not scanned.
1239          */
1240         protected boolean scanForDoctypeHook()
1241         throws IOException, XNIException {
1242 
1243             if (fEntityScanner.skipString(DOCTYPE)) {
1244                 setScannerState(SCANNER_STATE_DOCTYPE);
1245                 //                fEntityScanner.markStartOfDTD();
1246                 return true;
1247             }
1248             return false;
1249 
1250         } // scanForDoctypeHook():boolean
1251 
1252         /**
1253          * Element depth iz zero. This methos is a hook for subclasses
1254          * to add code to handle when the element depth hits zero. When
1255          * scanning a document fragment, an element depth of zero is
1256          * normal. However, when scanning a full XML document, the
1257          * scanner must handle the trailing miscellanous section of
1258          * the document after the end of the document's root element.
1259          *
1260          * @return True if the caller should stop and return true which
1261          *          allows the scanner to switch to a new scanning
1262          *          driver. A return value of false indicates that
1263          *          the content driver should continue as normal.
1264          */
1265         protected boolean elementDepthIsZeroHook()
1266         throws IOException, XNIException {
1267 
1268             setScannerState(SCANNER_STATE_TRAILING_MISC);
1269             setDriver(fTrailingMiscDriver);
1270             return true;
1271 
1272         } // elementDepthIsZeroHook():boolean
1273 
1274         /**
1275          * Scan for root element hook. This method is a hook for
1276          * subclasses to add code that handles scanning for the root
1277          * element. When scanning a document fragment, there is no
1278          * "root" element. However, when scanning a full XML document,
1279          * the scanner must handle the root element specially.
1280          *
1281          * @return True if the caller should stop and return true which
1282          *          allows the scanner to switch to a new scanning
1283          *          driver. A return value of false indicates that
1284          *          the content driver should continue as normal.
1285          */
1286         protected boolean scanRootElementHook()
1287         throws IOException, XNIException {
1288 
1289             if (scanStartElement()) {
1290                 setScannerState(SCANNER_STATE_TRAILING_MISC);
1291                 setDriver(fTrailingMiscDriver);
1292                 return true;
1293             }
1294             return false;
1295 
1296         } // scanRootElementHook():boolean
1297 
1298         /**
1299          * End of file hook. This method is a hook for subclasses to
1300          * add code that handles the end of file. The end of file in
1301          * a document fragment is OK if the markup depth is zero.
1302          * However, when scanning a full XML document, an end of file
1303          * is always premature.
1304          */
1305         protected void endOfFileHook(EOFException e)
1306         throws IOException, XNIException {
1307 
1308             reportFatalError("PrematureEOF", null);
1309             // in case continue-after-fatal-error set, should not do this...
1310             //throw e;
1311 
1312         } // endOfFileHook()
1313 
1314         protected void resolveExternalSubsetAndRead()
1315         throws IOException, XNIException {
1316 
1317             fDTDDescription.setValues(null, null, fEntityManager.getCurrentResourceIdentifier().getExpandedSystemId(), null);
1318             fDTDDescription.setRootName(fElementQName.rawname);
1319             XMLInputSource src = fExternalSubsetResolver.getExternalSubset(fDTDDescription);
1320 
1321             if (src != null) {
1322                 fDoctypeName = fElementQName.rawname;
1323                 fDoctypePublicId = src.getPublicId();
1324                 fDoctypeSystemId = src.getSystemId();
1325                 // call document handler
1326                 if (fDocumentHandler != null) {
1327                     // This inserts a doctypeDecl event into the stream though no
1328                     // DOCTYPE existed in the instance document.
1329                     fDocumentHandler.doctypeDecl(fDoctypeName, fDoctypePublicId, fDoctypeSystemId, null);
1330                 }
1331                 try {
1332                     fDTDScanner.setInputSource(src);
1333                     while (fDTDScanner.scanDTDExternalSubset(true));
1334                 } finally {
1335                     fEntityManager.setEntityHandler(XMLDocumentScannerImpl.this);
1336                 }
1337             }
1338         } // resolveExternalSubsetAndRead()
1339 
1340 
1341 
1342     } // class ContentDriver
1343 
1344     /**
1345      * Driver to handle trailing miscellaneous section scanning.
1346      *
1347      * @author Andy Clark, IBM
1348      * @author Eric Ye, IBM
1349      */
1350     protected final class TrailingMiscDriver
1351             implements Driver {
1352 
1353         //
1354         // Driver methods
1355         //
1356         public int next() throws IOException, XNIException{
1357             //this could for cases like <foo/>
1358             //look at scanRootElementHook
1359             if(fEmptyElement){
1360                 fEmptyElement = false;
1361                 return XMLEvent.END_ELEMENT;
1362             }
1363 
1364             try {
1365                 if(fScannerState == SCANNER_STATE_TERMINATED){
1366                     return XMLEvent.END_DOCUMENT ;}
1367                 do {
1368                     switch (fScannerState) {
1369                         case SCANNER_STATE_TRAILING_MISC: {
1370 
1371                             fEntityScanner.skipSpaces();
1372                             //we should have reached the end of the document in
1373                             //most cases.
1374                             if(fScannerState == SCANNER_STATE_TERMINATED ){
1375                                 return XMLEvent.END_DOCUMENT ;
1376                             }
1377                             if (fEntityScanner.skipChar('<')) {
1378                                 setScannerState(SCANNER_STATE_START_OF_MARKUP);
1379                             } else {
1380                                 setScannerState(SCANNER_STATE_CONTENT);
1381                             }
1382                             break;
1383                         }
1384                         case SCANNER_STATE_START_OF_MARKUP: {
1385                             fMarkupDepth++;
1386                             if (fEntityScanner.skipChar('?')) {
1387                                 setScannerState(SCANNER_STATE_PI);
1388                             } else if (fEntityScanner.skipChar('!')) {
1389                                 setScannerState(SCANNER_STATE_COMMENT);
1390                             } else if (fEntityScanner.skipChar('/')) {
1391                                 reportFatalError("MarkupNotRecognizedInMisc",
1392                                         null);
1393                             } else if (isValidNameStartChar(fEntityScanner.peekChar()) ||
1394                                     isValidNameStartHighSurrogate(fEntityScanner.peekChar())) {
1395                                 reportFatalError("MarkupNotRecognizedInMisc",
1396                                         null);
1397                                 scanStartElement();
1398                                 setScannerState(SCANNER_STATE_CONTENT);
1399                             } else {
1400                                 reportFatalError("MarkupNotRecognizedInMisc",
1401                                         null);
1402                             }
1403                             break;
1404                         }
1405                     }
1406                 }while(fScannerState == SCANNER_STATE_START_OF_MARKUP || fScannerState == SCANNER_STATE_TRAILING_MISC);
1407                 if(DEBUG_NEXT){
1408                     System.out.println("State set by deciding while loop [TrailingMiscellaneous] is = " + getScannerStateName(fScannerState));
1409                 }
1410                 switch (fScannerState){
1411                     case SCANNER_STATE_PI: {
1412                         fContentBuffer.clear();
1413                         scanPI(fContentBuffer);
1414                         setScannerState(SCANNER_STATE_TRAILING_MISC);
1415                         return XMLEvent.PROCESSING_INSTRUCTION ;
1416                     }
1417                     case SCANNER_STATE_COMMENT: {
1418                         if (!fEntityScanner.skipString(COMMENTSTRING)) {
1419                             reportFatalError("InvalidCommentStart", null);
1420                         }
1421                         scanComment();
1422                         setScannerState(SCANNER_STATE_TRAILING_MISC);
1423                         return XMLEvent.COMMENT;
1424                     }
1425                     case SCANNER_STATE_CONTENT: {
1426                         int ch = fEntityScanner.peekChar();
1427                         if (ch == -1) {
1428                             setScannerState(SCANNER_STATE_TERMINATED);
1429                             return XMLEvent.END_DOCUMENT ;
1430                         } else{
1431                             reportFatalError("ContentIllegalInTrailingMisc",
1432                                     null);
1433                             fEntityScanner.scanChar();
1434                             setScannerState(SCANNER_STATE_TRAILING_MISC);
1435                             return XMLEvent.CHARACTERS;
1436                         }
1437 
1438                     }
1439                     case SCANNER_STATE_REFERENCE: {
1440                         reportFatalError("ReferenceIllegalInTrailingMisc",
1441                                 null);
1442                         setScannerState(SCANNER_STATE_TRAILING_MISC);
1443                         return XMLEvent.ENTITY_REFERENCE ;
1444                     }
1445                     case SCANNER_STATE_TERMINATED: {
1446                         //there can't be any element after SCANNER_STATE_TERMINATED or when the parser
1447                         //has reached the end of document
1448                         setScannerState(SCANNER_STATE_NO_SUCH_ELEMENT_EXCEPTION);
1449                         //xxx what to do when the scanner has reached the terminating state.
1450                         return XMLEvent.END_DOCUMENT ;
1451                     }
1452                     case SCANNER_STATE_NO_SUCH_ELEMENT_EXCEPTION:{
1453                         throw new java.util.NoSuchElementException("No more events to be parsed");
1454                     }
1455                     default: throw new XNIException("Scanner State " + fScannerState + " not Recognized ");
1456                 }//switch
1457 
1458             } catch (EOFException e) {
1459                 // NOTE: This is the only place we're allowed to reach
1460                 //       the real end of the document stream. Unless the
1461                 //       end of file was reached prematurely.
1462                 if (fMarkupDepth != 0) {
1463                     reportFatalError("PrematureEOF", null);
1464                     return -1;
1465                     //throw e;
1466                 }
1467                 //System.out.println("EOFException thrown") ;
1468                 setScannerState(SCANNER_STATE_TERMINATED);
1469             }
1470 
1471             return XMLEvent.END_DOCUMENT;
1472 
1473         }//next
1474 
1475     } // class TrailingMiscDriver
1476 
1477     /**
1478      * Implements XMLBufferListener interface.
1479      */
1480 
1481 
1482     /**
1483      * receives callbacks from {@link XMLEntityReader } when buffer
1484      * is being changed.
1485      * @param refreshPosition
1486      */
1487     public void refresh(int refreshPosition){
1488         super.refresh(refreshPosition);
1489         if(fReadingDTD){
1490             Entity entity = fEntityScanner.getCurrentEntity();
1491             if(entity instanceof Entity.ScannedEntity){
1492                 fEndPos=((Entity.ScannedEntity)entity).position;
1493             }
1494             fDTDDecl.append(((Entity.ScannedEntity)entity).ch,fStartPos , fEndPos-fStartPos);
1495             fStartPos = refreshPosition;
1496         }
1497     }
1498 
1499 } // class XMLDocumentScannerImpl