KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > impl > XMLStreamReaderImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://jaxp.dev.java.net/CDDLv1.0.html.
9  * See the License for the specific language governing
10  * permissions and limitations under the License.
11  *
12  * When distributing Covered Code, include this CDDL
13  * HEADER in each file and include the License file at
14  * https://jaxp.dev.java.net/CDDLv1.0.html
15  * If applicable add the following below this CDDL HEADER
16  * with the fields enclosed by brackets "[]" replaced with
17  * your own identifying information: Portions Copyright
18  * [year] [name of copyright owner]
19  */

20
21 /*
22  * $Id: XMLStreamReaderImpl.java,v 1.8 2006/06/06 06:28:41 sunithareddy Exp $
23  * @(#)XMLStreamReaderImpl.java 1.10 06/07/13
24  *
25  * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package com.sun.org.apache.xerces.internal.impl;
29
30 import com.sun.xml.internal.stream.Entity;
31 import com.sun.xml.internal.stream.StaxErrorReporter;
32 import com.sun.xml.internal.stream.XMLEntityStorage;
33 import com.sun.xml.internal.stream.events.EntityDeclarationImpl;
34 import com.sun.xml.internal.stream.events.NotationDeclarationImpl;
35 import javax.xml.namespace.NamespaceContext JavaDoc;
36 import com.sun.org.apache.xerces.internal.xni.XNIException;
37 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
38 import javax.xml.namespace.QName JavaDoc;
39 import javax.xml.stream.Location;
40 import javax.xml.stream.events.XMLEvent;
41 import com.sun.org.apache.xerces.internal.util.NamespaceContextWrapper;
42 import com.sun.org.apache.xerces.internal.util.SymbolTable;
43 import com.sun.xml.internal.stream.dtd.nonvalidating.XMLNotationDecl;
44 import com.sun.xml.internal.stream.dtd.nonvalidating.DTDGrammar;
45 import java.io.BufferedInputStream JavaDoc;
46 import java.io.BufferedReader JavaDoc;
47 import java.io.IOException JavaDoc;
48 import java.io.InputStream JavaDoc;
49 import java.io.Reader JavaDoc;
50 import java.util.ArrayList JavaDoc;
51 import java.util.Enumeration JavaDoc;
52 import java.util.Hashtable JavaDoc;
53 import java.util.Iterator JavaDoc;
54 import java.util.List JavaDoc;
55 import javax.xml.stream.XMLInputFactory;
56 import javax.xml.stream.XMLStreamConstants;
57 import javax.xml.stream.XMLStreamException;
58 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter;
59 import com.sun.org.apache.xerces.internal.util.XMLChar;
60 import com.sun.org.apache.xerces.internal.util.XMLStringBuffer;
61 import com.sun.org.apache.xerces.internal.util.NamespaceSupport;
62 import com.sun.org.apache.xerces.internal.impl.Constants;
63 import com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler;
64 import com.sun.xml.internal.stream.dtd.DTDGrammarUtil;
65
66 /** This class implements javax.xml.stream.XMLStreamReader. It makes use of XML*Scanner classes to
67  * derive most of its functionality. If desired, Application can reuse this instance by calling
68  * reset() and setInputSource().
69  *
70  * @author Neeraj Bajaj Sun Microsystems,Inc.
71  * @author K.Venugopal Sun Microsystems,Inc.
72  * @author Sunitha Reddy Sun Microsystems,Inc.
73  */

74 public class XMLStreamReaderImpl implements javax.xml.stream.XMLStreamReader {
75     
76     /** Property identifier: entity manager. */
77     protected static final String JavaDoc ENTITY_MANAGER =
78     Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY;
79     
80     /** Property identifier: Error Reporter. */
81     protected static final String JavaDoc ERROR_REPORTER =
82     Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
83     
84     /** Property identifier: Symbol table. */
85     protected static final String JavaDoc SYMBOL_TABLE =
86     Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY;
87     
88     protected static final String JavaDoc READER_IN_DEFINED_STATE =
89     Constants.READER_IN_DEFINED_STATE;
90     
91     private SymbolTable fSymbolTable = new SymbolTable();
92     
93     /** Document scanner. */
94     protected XMLDocumentScannerImpl fScanner = new XMLNSDocumentScannerImpl();
95         
96     //make Global NamespaceContextWrapper object, fScanner.getNamespaceContext() is dynamic object and ita value changes
97
//as per the state of the parser.
98
protected NamespaceContextWrapper fNamespaceContextWrapper = new NamespaceContextWrapper((NamespaceSupport)fScanner.getNamespaceContext()) ;
99     protected XMLEntityManager fEntityManager = new XMLEntityManager();
100     protected StaxErrorReporter fErrorReporter = new StaxErrorReporter();
101     
102     
103     /** Entity scanner, this alwasy works on last entity that was opened. */
104     protected XMLEntityScanner fEntityScanner = null;
105     
106     /** Input Source */
107     protected XMLInputSource fInputSource = null;
108     /** Store properties*/
109     protected PropertyManager fPropertyManager = null ;
110     
111     /** current event type */
112     private int fEventType ;
113     /** debug flag*/
114     static final boolean DEBUG = false ;
115     /** more to scan */
116     private boolean fReuse = true;
117     private boolean fReaderInDefinedState = true ;
118     private boolean fBindNamespaces = true;
119     private String JavaDoc fDTDDecl = null;
120     private String JavaDoc versionStr = null;
121     
122     /**
123      * @param inputStream
124      * @param props
125      * @throws XMLStreamException
126      */

127     public XMLStreamReaderImpl(InputStream JavaDoc inputStream, PropertyManager props) throws XMLStreamException {
128         init(props);
129         //publicId, systemid, baseSystemId, inputStream, enocding
130
XMLInputSource inputSource = new XMLInputSource(null,null,null,inputStream,null);
131         //pass the input source to document scanner impl.
132
setInputSource(inputSource);
133     }
134     
135     public XMLDocumentScannerImpl getScanner(){
136         System.out.println("returning scanner");
137         return fScanner;
138     }
139     /**
140      * @param systemid
141      * @param props
142      * @throws XMLStreamException
143      */

144     public XMLStreamReaderImpl(String JavaDoc systemid, PropertyManager props) throws XMLStreamException {
145         init(props);
146         //publicId, systemid, baseSystemId, inputStream, enocding
147
XMLInputSource inputSource = new XMLInputSource(null,systemid,null);
148         //pass the input source to document scanner impl.
149
setInputSource(inputSource);
150     }
151     
152     
153     /**
154      * @param inputStream
155      * @param encoding
156      * @param props
157      * @throws XMLStreamException
158      */

159     public XMLStreamReaderImpl(InputStream JavaDoc inputStream, String JavaDoc encoding, PropertyManager props ) throws XMLStreamException {
160         init(props);
161         //publicId, systemid, baseSystemId, inputStream, enocding
162
XMLInputSource inputSource = new XMLInputSource(null,null,null, new BufferedInputStream JavaDoc(inputStream),encoding );
163         //pass the input source to document scanner impl.
164
setInputSource(inputSource);
165     }
166     
167     /**
168      * @param reader
169      * @param props
170      * @throws XMLStreamException
171      */

172     public XMLStreamReaderImpl(Reader JavaDoc reader, PropertyManager props) throws XMLStreamException {
173         init(props);
174         //publicId, systemid, baseSystemId, inputStream, enocding
175
//xxx: Using buffered reader
176
XMLInputSource inputSource = new XMLInputSource(null,null,null,new BufferedReader JavaDoc(reader),null);
177         //pass the input source to document scanner impl.
178
setInputSource(inputSource);
179     }
180     
181     /**
182      * @param inputSource
183      * @param props
184      * @throws XMLStreamException
185      */

186     public XMLStreamReaderImpl(XMLInputSource inputSource, PropertyManager props) throws XMLStreamException {
187         init(props);
188         //pass the input source to document scanner impl.
189
setInputSource(inputSource);
190     }
191     
192     /**
193      * @param inputSource
194      * @throws XMLStreamException
195      */

196     public void setInputSource(XMLInputSource inputSource ) throws XMLStreamException {
197         //once setInputSource() is called this instance is busy parsing the inputsource supplied
198
//this instances is free for reuse if parser has reached END_DOCUMENT state or application has
199
//called close()
200
fReuse = false;
201         
202         try{
203             
204             fScanner.setInputSource(inputSource) ;
205             //XMLStreamReader should be in defined state
206
if(fReaderInDefinedState){
207                 fEventType = fScanner.next();
208                 if (versionStr == null)
209                     versionStr = getVersion();
210                 
211                 if (fEventType == XMLStreamConstants.START_DOCUMENT && versionStr != null && versionStr.equals("1.1")){
212                     switchToXML11Scanner();
213                 }
214                 
215             }
216         }catch(java.io.IOException JavaDoc ex){
217             throw new XMLStreamException(ex);
218         }
219     }//setInputSource
220

221     void init(PropertyManager propertyManager) throws XMLStreamException {
222         fPropertyManager = propertyManager;
223         //set Stax internal properties -- Note that these instances are being created in XMLReaderImpl.
224
//1.SymbolTable
225
//2.XMLMessageFormatter
226
//3.XMLEntityManager
227
//4. call reset()
228
//1.
229
propertyManager.setProperty(SYMBOL_TABLE, fSymbolTable ) ;
230         //2.
231
propertyManager.setProperty(ERROR_REPORTER, fErrorReporter ) ;
232         //3.
233
propertyManager.setProperty(ENTITY_MANAGER, fEntityManager);
234         //4.
235
reset();
236     }
237     
238     /** This function tells if this instances is available for reuse.
239      * One must call reset() and setInputSource() to be able to reuse
240      * this instance.
241      */

242     public boolean canReuse(){
243         if(DEBUG){
244             System.out.println("fReuse = " + fReuse);
245             System.out.println("fEventType = " + getEventTypeString(fEventType) );
246         }
247         //when parsing begins, fReuse is set to false
248
//fReuse is set to 'true' when application calls close()
249
return fReuse;
250     }
251     
252     /**
253      * Resets this instance so that this instance is ready for reuse.
254      */

255     public void reset(){
256         fReuse = true;
257         fEventType = 0 ;
258         //reset entity manager
259
fEntityManager.reset(fPropertyManager);
260         //reset the scanner
261
fScanner.reset(fPropertyManager);
262         //REVISIT:this is too ugly -- we are getting XMLEntityManager and XMLEntityReader from
263
//property manager, it should be only XMLEntityManager
264
fDTDDecl = null;
265         fEntityScanner = (XMLEntityScanner)fEntityManager.getEntityScanner() ;
266         //default value for this property is true. However, this should be false when using XMLEventReader... Ugh..
267
//because XMLEventReader should not have defined state.
268
fReaderInDefinedState = ((Boolean JavaDoc)fPropertyManager.getProperty(READER_IN_DEFINED_STATE)).booleanValue();
269         fBindNamespaces = ((Boolean JavaDoc)fPropertyManager.getProperty(XMLInputFactory.IS_NAMESPACE_AWARE)).booleanValue();
270         versionStr = null;
271     }
272     
273     
274     /** Frees any resources associated with this Reader. This method does not close the underlying input source.
275      * @throws XMLStreamException if there are errors freeing associated resources
276      */

277     public void close() throws XMLStreamException {
278         //xxx: Check what this function is intended to do.
279
//reset();
280
fReuse = true ;
281     }
282     
283     
284     /** Returns the character encoding declared on the xml declaration Returns null if none was declared
285      * @return the encoding declared in the document or null
286      */

287     public String JavaDoc getCharacterEncodingScheme() {
288         return fScanner.getCharacterEncodingScheme();
289         
290     }
291     
292     
293     /**
294      * @return
295      */

296     public int getColumnNumber() {
297         return fEntityScanner.getColumnNumber();
298     }//getColumnNumber
299

300     /** Return input encoding if known or null if unknown.
301      * @return the encoding of this instance or null
302      */

303     public String JavaDoc getEncoding() {
304         return fEntityScanner.getEncoding();
305     }//getEncoding
306

307     /** Returns the current value of the parse event as a string, this returns the string value of a CHARACTERS event, returns the value of a COMMENT, the replacement value for an ENTITY_REFERENCE, the string value of a CDATA section, the string value for a SPACE event, or the String value of the internal subset of the DTD. If an ENTITY_REFERENCE has been resolved, any character data will be reported as CHARACTERS events.
308      * @return the current text or null
309      */

310     public int getEventType() {
311         return fEventType ;
312     }//getEventType
313

314     /**
315      * @return
316      */

317     public int getLineNumber() {
318         return fEntityScanner.getLineNumber() ;
319     }//getLineNumber
320

321     /** For START_ELEMENT or END_ELEMENT returns the (local) name of the current element. For ENTITY_REF it returns entity name. For
322      * PROCESSING_INSTRUCTION it returns the target. The current event must be START_ELEMENT or END_ELEMENT, PROCESSING_INSTRUCTION, or
323      * ENTITY_REF, otherwise null is returned.
324      * @return
325      */

326     public String JavaDoc getLocalName() {
327         if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT){
328             //xxx check whats the value of fCurrentElement
329
return fScanner.getElementQName().localpart ;
330         }
331         else if(fEventType == XMLEvent.PROCESSING_INSTRUCTION){
332             return fScanner.getPITarget();
333         }
334         else if(fEventType == XMLEvent.ENTITY_REFERENCE){
335             return fScanner.getEntityName();
336         }
337         return null;
338     }//getLocalName()
339

340     /**
341      * @return
342      */

343     public String JavaDoc getNamespaceURI() {
344         //doesn't take care of Attribute as separte event
345
if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT){
346             return fScanner.getElementQName().uri ;
347         }
348         return null ;
349     }//getNamespaceURI
350

351     /** Get the data section of a processing instruction
352      * @return the data or null
353      */

354     
355     public String JavaDoc getPIData() {
356         if( fEventType == XMLEvent.PROCESSING_INSTRUCTION){
357             return fScanner.getPIData().toString();
358         }
359         else throw new java.lang.IllegalStateException JavaDoc("Current state of the parser is " + getEventTypeString(fEventType) +
360         " But Expected state is " + XMLEvent.PROCESSING_INSTRUCTION ) ;
361     }//getPIData
362

363     
364     /** Get the target of a processing instruction
365      * @return the target or null
366      */

367     public String JavaDoc getPITarget() {
368         if( fEventType == XMLEvent.PROCESSING_INSTRUCTION){
369             return fScanner.getPITarget();
370         }
371         else throw new java.lang.IllegalStateException JavaDoc("Current state of the parser is " + getEventTypeString(fEventType) +
372         " But Expected state is " + XMLEvent.PROCESSING_INSTRUCTION ) ;
373         
374     }//getPITarget
375

376     
377     /**
378      * @return
379      */

380     public String JavaDoc getPrefix() {
381         //doesn't take care of Attribute as separte event
382
if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT){
383             return fScanner.getElementQName().prefix ;
384         }
385         return null ;
386     }//getPrefix()
387

388     
389     
390     /**
391      * @return
392      */

393     public char[] getTextCharacters() {
394         if( fEventType == XMLEvent.CHARACTERS || fEventType == XMLEvent.COMMENT
395                  || fEventType == XMLEvent.CDATA || fEventType == XMLEvent.SPACE){
396              return fScanner.getCharacterData().ch;
397          } else{
398              throw new IllegalStateException JavaDoc("Current state = " + getEventTypeString(fEventType)
399              + " is not among the states " + getEventTypeString(XMLEvent.CHARACTERS) + " , "
400                      + getEventTypeString(XMLEvent.COMMENT) + " , " + getEventTypeString(XMLEvent.CDATA)
401                      + " , " + getEventTypeString(XMLEvent.SPACE) +" valid for getTextCharacters() " ) ;
402          }
403     }
404     
405     /**
406      * @return
407      */

408     public int getTextLength() {
409         if( fEventType == XMLEvent.CHARACTERS || fEventType == XMLEvent.COMMENT
410                  || fEventType == XMLEvent.CDATA || fEventType == XMLEvent.SPACE){
411              return fScanner.getCharacterData().length;
412          } else{
413              throw new IllegalStateException JavaDoc("Current state = " + getEventTypeString(fEventType)
414              + " is not among the states " + getEventTypeString(XMLEvent.CHARACTERS) + " , "
415                      + getEventTypeString(XMLEvent.COMMENT) + " , " + getEventTypeString(XMLEvent.CDATA)
416                      + " , " + getEventTypeString(XMLEvent.SPACE) +" valid for getTextLength() " ) ;
417          }
418         
419    }
420     
421     /**
422      * @return
423      */

424     public int getTextStart() {
425         if( fEventType == XMLEvent.CHARACTERS || fEventType == XMLEvent.COMMENT || fEventType == XMLEvent.CDATA || fEventType == XMLEvent.SPACE){
426              return fScanner.getCharacterData().offset;
427          } else{
428              throw new IllegalStateException JavaDoc("Current state = " + getEventTypeString(fEventType)
429              + " is not among the states " + getEventTypeString(XMLEvent.CHARACTERS) + " , "
430                      + getEventTypeString(XMLEvent.COMMENT) + " , " + getEventTypeString(XMLEvent.CDATA)
431                      + " , " + getEventTypeString(XMLEvent.SPACE) +" valid for getTextStart() " ) ;
432          }
433     }
434     
435     /**
436      * @return
437      */

438     public String JavaDoc getValue() {
439         if(fEventType == XMLEvent.PROCESSING_INSTRUCTION){
440             return fScanner.getPIData().toString();
441         } else if(fEventType == XMLEvent.COMMENT){
442             return fScanner.getComment();
443         } else if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT){
444             return fScanner.getElementQName().localpart ;
445         } else if(fEventType == XMLEvent.CHARACTERS){
446             return fScanner.getCharacterData().toString();
447         }
448         return null;
449     }//getValue()
450

451     /** Get the XML language version of the current document being parsed */
452     public String JavaDoc getVersion() {
453         return fEntityScanner.getXMLVersion();
454     }
455     
456     /**
457      * @return
458      */

459     public boolean hasAttributes() {
460         return fScanner.getAttributeIterator().getLength() > 0 ? true : false ;
461     }
462     
463     /** this Funtion returns true if the current event has name */
464     public boolean hasName() {
465         if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT
466         || fEventType == XMLEvent.ENTITY_REFERENCE || fEventType == XMLEvent.PROCESSING_INSTRUCTION) {
467             return true;
468         } else {
469             return false;
470         }
471     }//hasName()
472

473     /**
474      * @throws XMLStreamException
475      * @return
476      */

477     public boolean hasNext() throws XMLStreamException {
478         //we can check in scanners if the scanner state is not set to
479
//terminating, we still have more events.
480
return fEventType != XMLEvent.END_DOCUMENT;
481     }
482     
483     /**
484      * @return
485      */

486     public boolean hasValue() {
487         if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT
488         || fEventType == XMLEvent.ENTITY_REFERENCE || fEventType == XMLEvent.PROCESSING_INSTRUCTION
489         || fEventType == XMLEvent.COMMENT || fEventType == XMLEvent.CHARACTERS) {
490             return true;
491         } else {
492             return false;
493         }
494         
495     }
496     
497     /**
498      * @return
499      */

500     public boolean isEndElement() {
501         return fEventType == XMLEvent.END_ELEMENT;
502     }
503     
504     /**
505      * @return
506      */

507     public boolean isStandalone() {
508         return fScanner.isStandAlone();
509     }
510     
511     /**
512      * @return
513      */

514     public boolean isStartElement() {
515         return fEventType == XMLEvent.START_ELEMENT;
516     }
517     
518     /**
519      * Returns true if the cursor points to a character data event that consists of all whitespace
520      * Application calling this method needs to cache the value and avoid calling this method again
521      * for the same event.
522      * @return
523      */

524     public boolean isWhiteSpace() {
525         if(isCharacters() || (fEventType == XMLStreamConstants.CDATA)){
526             char [] ch = this.getTextCharacters();
527             final int start = this.getTextStart();
528             final int end = start + this.getTextLength();
529             for (int i = start; i < end; i++){
530                 if(!XMLChar.isSpace(ch[i])){
531                     return false;
532                 }
533             }
534             return true;
535         }
536         return false;
537     }
538     
539     
540     
541     /**
542      * @throws XMLStreamException
543      * @return
544      */

545     public int next() throws XMLStreamException {
546
547         try {
548             fEventType = fScanner.next();
549
550             if (versionStr == null) {
551                 versionStr = getVersion();
552             }
553
554             if (fEventType == XMLStreamConstants.START_DOCUMENT
555                     && versionStr != null
556                     && versionStr.equals("1.1")) {
557                 switchToXML11Scanner();
558             }
559
560             return fEventType;
561         } catch (IOException JavaDoc ex) {
562             // if this error occured trying to resolve the external DTD subset
563
// and IS_VALIDATING == false, then this is not an XML error
564
if (fScanner.fScannerState == fScanner.SCANNER_STATE_DTD_EXTERNAL) {
565                 Boolean JavaDoc isValidating = (Boolean JavaDoc) fPropertyManager.getProperty(
566                         XMLInputFactory.IS_VALIDATING);
567                 if (isValidating != null
568                         && !isValidating.booleanValue()) {
569                     // ignore the error, set scanner to known state
570
fEventType = XMLEvent.DTD;
571                     fScanner.setScannerState(fScanner.SCANNER_STATE_PROLOG);
572                     fScanner.setDriver(fScanner.fPrologDriver);
573                     if (fDTDDecl == null
574                             || fDTDDecl.length() == 0) {
575                         fDTDDecl = "<!-- "
576                                 + "Exception scanning External DTD Subset. "
577                                 + "True contents of DTD cannot be determined. "
578                                 + "Processing will continue as XMLInputFactory.IS_VALIDATING == false."
579                                 + " -->";
580                     }
581                     return XMLEvent.DTD;
582                 }
583             }
584
585             // else real error
586
throw new XMLStreamException(ex.getMessage(), getLocation(), ex);
587         } catch (XNIException ex) {
588             throw new XMLStreamException(
589                     ex.getMessage(),
590                     getLocation(),
591                     ex.getException());
592         }
593     } //next()
594

595     private void switchToXML11Scanner() throws IOException JavaDoc{
596         
597         int oldEntityDepth = fScanner.fEntityDepth;
598         com.sun.org.apache.xerces.internal.xni.NamespaceContext oldNamespaceContext = fScanner.fNamespaceContext;
599         
600         fScanner = new XML11NSDocumentScannerImpl();
601         
602         //get the new scanner state to old scanner's previous state
603
fScanner.reset(fPropertyManager);
604         fScanner.setPropertyManager(fPropertyManager);
605         fEntityScanner = (XMLEntityScanner)fEntityManager.getEntityScanner() ;
606         fEntityManager.fCurrentEntity.mayReadChunks = true;
607         fScanner.setScannerState(XMLEvent.START_DOCUMENT);
608         
609         fScanner.fEntityDepth = oldEntityDepth;
610         fScanner.fNamespaceContext = oldNamespaceContext;
611         fEventType = fScanner.next();
612     }
613     
614     
615     
616     final static String JavaDoc getEventTypeString(int eventType) {
617         switch (eventType){
618             case XMLEvent.START_ELEMENT:
619                 return "START_ELEMENT";
620             case XMLEvent.END_ELEMENT:
621                 return "END_ELEMENT";
622             case XMLEvent.PROCESSING_INSTRUCTION:
623                 return "PROCESSING_INSTRUCTION";
624             case XMLEvent.CHARACTERS:
625                 return "CHARACTERS";
626             case XMLEvent.COMMENT:
627                 return "COMMENT";
628             case XMLEvent.START_DOCUMENT:
629                 return "START_DOCUMENT";
630             case XMLEvent.END_DOCUMENT:
631                 return "END_DOCUMENT";
632             case XMLEvent.ENTITY_REFERENCE:
633                 return "ENTITY_REFERENCE";
634             case XMLEvent.ATTRIBUTE:
635                 return "ATTRIBUTE";
636             case XMLEvent.DTD:
637                 return "DTD";
638             case XMLEvent.CDATA:
639                 return "CDATA";
640             case XMLEvent.SPACE:
641                 return "SPACE";
642         }
643         return "UNKNOWN_EVENT_TYPE, " + String.valueOf(eventType);
644     }
645     
646     /** Returns the count of attributes on this START_ELEMENT,
647      * this method is only valid on a START_ELEMENT or ATTRIBUTE. This
648      * count excludes namespace definitions. Attribute indices are
649      * zero-based.
650      * @return returns the number of attributes
651      * @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE
652      */

653     public int getAttributeCount() {
654         //xxx: recognize SAX properties namespace, namespace-prefix to get XML Namespace declarations
655
//does length includes namespace declarations ?
656

657         //State should be either START_ELEMENT or ATTRIBUTE
658
if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) {
659             return fScanner.getAttributeIterator().getLength() ;
660         } else{
661             throw new java.lang.IllegalStateException JavaDoc( "Current state is not among the states "
662                      + getEventTypeString(XMLEvent.START_ELEMENT) + " , "
663                      + getEventTypeString(XMLEvent.ATTRIBUTE)
664                      + "valid for getAttributeCount()") ;
665         }
666     }//getAttributeCount
667

668     /** Returns the localName of the attribute at the provided
669      * index
670      * @param index the position of the attribute
671      * @return the localName of the attribute
672      * @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE
673      */

674     public QName JavaDoc getAttributeName(int index) {
675         //State should be either START_ELEMENT or ATTRIBUTE
676
if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) {
677             return convertXNIQNametoJavaxQName(fScanner.getAttributeIterator().getQualifiedName(index)) ;
678         } else{
679             throw new java.lang.IllegalStateException JavaDoc("Current state is not among the states "
680                      + getEventTypeString(XMLEvent.START_ELEMENT) + " , "
681                      + getEventTypeString(XMLEvent.ATTRIBUTE)
682                      + "valid for getAttributeName()") ;
683         }
684     }//getAttributeName
685

686     /**
687      * @param index
688      * @return
689      */

690     public String JavaDoc getAttributeLocalName(int index) {
691         //State should be either START_ELEMENT or ATTRIBUTE
692
if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) {
693             return fScanner.getAttributeIterator().getLocalName(index) ;
694         } else{
695             throw new java.lang.IllegalStateException JavaDoc() ;
696         }
697     }//getAttributeName
698

699     /** Returns the namespace of the attribute at the provided
700      * index
701      * @param index the position of the attribute
702      * @return the namespace URI (can be null)
703      * @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE
704      */

705     public String JavaDoc getAttributeNamespace(int index) {
706         //State should be either START_ELEMENT or ATTRIBUTE
707
if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) {
708             return fScanner.getAttributeIterator().getURI(index);
709         } else{
710             throw new java.lang.IllegalStateException JavaDoc("Current state is not among the states "
711                      + getEventTypeString(XMLEvent.START_ELEMENT) + " , "
712                      + getEventTypeString(XMLEvent.ATTRIBUTE)
713                      + "valid for getAttributeNamespace()") ;
714         }
715         
716     }//getAttributeNamespace
717

718     /** Returns the prefix of this attribute at the
719      * provided index
720      * @param index the position of the attribute
721      * @return the prefix of the attribute
722      * @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE
723      */

724     public String JavaDoc getAttributePrefix(int index) {
725         //State should be either START_ELEMENT or ATTRIBUTE
726
if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) {
727             return fScanner.getAttributeIterator().getPrefix(index);
728         } else{
729             throw new java.lang.IllegalStateException JavaDoc("Current state is not among the states "
730                      + getEventTypeString(XMLEvent.START_ELEMENT) + " , "
731                      + getEventTypeString(XMLEvent.ATTRIBUTE)
732                      + "valid for getAttributePrefix()") ;
733         }
734     }//getAttributePrefix
735

736     /** Returns the qname of the attribute at the provided index
737      *
738      * @param index the position of the attribute
739      * @return the QName of the attribute
740      * @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE
741      */

742     public javax.xml.namespace.QName JavaDoc getAttributeQName(int index) {
743         //State should be either START_ELEMENT or ATTRIBUTE
744
if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) {
745             // create new object at runtime..
746
String JavaDoc localName = fScanner.getAttributeIterator().getLocalName(index) ;
747             String JavaDoc uri = fScanner.getAttributeIterator().getURI(index) ;
748             return new javax.xml.namespace.QName JavaDoc(uri, localName) ;
749         } else{
750             throw new java.lang.IllegalStateException JavaDoc("Current state is not among the states "
751                      + getEventTypeString(XMLEvent.START_ELEMENT) + " , "
752                      + getEventTypeString(XMLEvent.ATTRIBUTE)
753                      + "valid for getAttributeQName()") ;
754         }
755     }//getAttributeQName
756

757     /** Returns the XML type of the attribute at the provided
758      * index
759      * @param index the position of the attribute
760      * @return the XML type of the attribute
761      * @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE
762      */

763     public String JavaDoc getAttributeType(int index) {
764         //State should be either START_ELEMENT or ATTRIBUTE
765
if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) {
766             return fScanner.getAttributeIterator().getType(index) ;
767         } else{
768             throw new java.lang.IllegalStateException JavaDoc("Current state is not among the states "
769                      + getEventTypeString(XMLEvent.START_ELEMENT) + " , "
770                      + getEventTypeString(XMLEvent.ATTRIBUTE)
771                      + "valid for getAttributeType()") ;
772         }
773         
774     }//getAttributeType
775

776     /** Returns the value of the attribute at the
777      * index
778      * @param index the position of the attribute
779      * @return the attribute value
780      * @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE
781      */

782     public String JavaDoc getAttributeValue(int index) {
783         //State should be either START_ELEMENT or ATTRIBUTE
784
if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) {
785             return fScanner.getAttributeIterator().getValue(index) ;
786         } else{
787             throw new java.lang.IllegalStateException JavaDoc("Current state is not among the states "
788                      + getEventTypeString(XMLEvent.START_ELEMENT) + " , "
789                      + getEventTypeString(XMLEvent.ATTRIBUTE)
790                      + "valid for getAttributeValue()") ;
791         }
792         
793     }//getAttributeValue
794

795     /**
796      * @param namespaceURI
797      * @param localName
798      * @return
799      */

800     public String JavaDoc getAttributeValue(String JavaDoc namespaceURI, String JavaDoc localName) {
801         //State should be either START_ELEMENT or ATTRIBUTE
802
if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) {
803             return fScanner.getAttributeIterator().getValue(namespaceURI, localName) ;
804         } else{
805             throw new java.lang.IllegalStateException JavaDoc("Current state is not among the states "
806                      + getEventTypeString(XMLEvent.START_ELEMENT) + " , "
807                      + getEventTypeString(XMLEvent.ATTRIBUTE)
808                      + "valid for getAttributeValue()") ;
809         }
810         
811     }
812     
813     /** Reads the content of a text-only element. Precondition:
814      * the current event is START_ELEMENT. Postcondition:
815      * The current event is the corresponding END_ELEMENT.
816      * @throws XMLStreamException if the current event is not a START_ELEMENT or if
817      * a non text element is encountered
818      */

819     public String JavaDoc getElementText() throws XMLStreamException {
820         
821         if(getEventType() != XMLStreamConstants.START_ELEMENT) {
822             throw new XMLStreamException(
823             "parser must be on START_ELEMENT to read next text", getLocation());
824         }
825         int eventType = next();
826         StringBuffer JavaDoc content = new StringBuffer JavaDoc();
827         while(eventType != XMLStreamConstants.END_ELEMENT ) {
828             if(eventType == XMLStreamConstants.CHARACTERS
829             || eventType == XMLStreamConstants.CDATA
830             || eventType == XMLStreamConstants.SPACE
831             || eventType == XMLStreamConstants.ENTITY_REFERENCE) {
832                 content.append(getText());
833             } else if(eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
834             || eventType == XMLStreamConstants.COMMENT) {
835                 // skipping
836
} else if(eventType == XMLStreamConstants.END_DOCUMENT) {
837                 throw new XMLStreamException("unexpected end of document when reading element text content");
838             } else if(eventType == XMLStreamConstants.START_ELEMENT) {
839                 throw new XMLStreamException(
840                 "elementGetText() function expects text only elment but START_ELEMENT was encountered.", getLocation());
841             } else {
842                 throw new XMLStreamException(
843                 "Unexpected event type "+ eventType, getLocation());
844             }
845             eventType = next();
846         }
847         return content.toString();
848     }
849     
850     /** Return the current location of the processor.
851      * If the Location is unknown the processor should return
852      * an implementation of Location that returns -1 for the
853      * location and null for the publicId and systemId.
854      * The location information is only valid until next() is
855      * called.
856      */

857     public Location getLocation() {
858         return new Location() {
859             
860             public String JavaDoc getLocationURI(){
861                 return fEntityScanner.getExpandedSystemId();
862             }
863             
864             public int getCharacterOffset(){
865                 return fEntityScanner.getCharacterOffset();
866             }
867             
868             public int getColumnNumber() {
869                 return fEntityScanner.getColumnNumber();
870             }
871             
872             public int getLineNumber(){
873                 return fEntityScanner.getLineNumber();
874             }
875             
876             public String JavaDoc getPublicId(){
877                 return fEntityScanner.getPublicId();
878             }
879             
880             public String JavaDoc getSystemId(){
881                 return fEntityScanner.getExpandedSystemId();
882             }
883             
884             public String JavaDoc toString(){
885                 StringBuffer JavaDoc sbuffer = new StringBuffer JavaDoc() ;
886                 sbuffer.append("Line number = " + getLineNumber());
887                 sbuffer.append("\n") ;
888                 sbuffer.append("Column number = " + getColumnNumber());
889                 sbuffer.append("\n") ;
890                 sbuffer.append("System Id = " + getSystemId());
891                 sbuffer.append("\n") ;
892                 sbuffer.append("Public Id = " + getPublicId());
893                 sbuffer.append("\n") ;
894                 sbuffer.append("Location Uri= " + getLocationURI());
895                 sbuffer.append("\n") ;
896                 sbuffer.append("CharacterOffset = " + getCharacterOffset());
897                 sbuffer.append("\n") ;
898                 return sbuffer.toString();
899             }
900         } ;
901         
902     }
903     
904     /** Returns a QName for the current START_ELEMENT or END_ELEMENT event
905      * @return the QName for the current START_ELEMENT or END_ELEMENT event
906      */

907     public javax.xml.namespace.QName JavaDoc getName() {
908         if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT)
909             return convertXNIQNametoJavaxQName(fScanner.getElementQName());
910         else
911             throw new java.lang.IllegalArgumentException JavaDoc("Illegal to call getName() "+
912             "when event type is "+ getEventTypeString(fEventType) + "."
913                      + " Valid states are " + getEventTypeString(XMLEvent.START_ELEMENT) + ", "
914                      + getEventTypeString(XMLEvent.END_ELEMENT));
915     }
916     
917     /** Returns a read only namespace context for the current
918      * position. The context is transient and only valid until
919      * a call to next() changes the state of the reader.
920      * @return return a namespace context
921      */

922     public NamespaceContext JavaDoc getNamespaceContext() {
923         return fNamespaceContextWrapper ;
924     }
925     
926     /** Returns the count of namespaces declared on this START_ELEMENT or END_ELEMENT,
927      * this method is only valid on a START_ELEMENT, END_ELEMENT or NAMESPACE. On
928      * an END_ELEMENT the count is of the namespaces that are about to go
929      * out of scope. This is the equivalent of the information reported
930      * by SAX callback for an end element event.
931      * @return returns the number of namespace declarations on this specific element
932      * @throws IllegalStateException if this is not a START_ELEMENT, END_ELEMENT or NAMESPACE
933      */

934     public int getNamespaceCount() {
935         //namespaceContext is dynamic object.
936
//REVISIT: check if it specifies all conditions mentioned in the javadoc
937
if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT || fEventType == XMLEvent.NAMESPACE){
938             return fScanner.getNamespaceContext().getDeclaredPrefixCount() ;
939         } else{
940             throw new IllegalStateException JavaDoc("Current event state is " + getEventTypeString(fEventType)
941              + " is not among the states " + getEventTypeString(XMLEvent.START_ELEMENT)
942              + ", " + getEventTypeString(XMLEvent.END_ELEMENT) + ", "
943                      + getEventTypeString(XMLEvent.NAMESPACE)
944              + " valid for getNamespaceCount()." );
945         }
946     }
947     
948     /** Returns the prefix for the namespace declared at the
949      * index. Returns null if this is the default namespace
950      * declaration
951      *
952      * @param index the position of the namespace declaration
953      * @return returns the namespace prefix
954      * @throws IllegalStateException if this is not a START_ELEMENT, END_ELEMENT or NAMESPACE
955      */

956     public String JavaDoc getNamespacePrefix(int index) {
957         if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT || fEventType == XMLEvent.NAMESPACE){
958             //namespaceContext is dynamic object.
959
String JavaDoc prefix = fScanner.getNamespaceContext().getDeclaredPrefixAt(index) ;
960             return prefix.equals("") ? null : prefix ;
961         }
962         else{
963             throw new IllegalStateException JavaDoc("Current state " + getEventTypeString(fEventType)
964              + " is not among the states " + getEventTypeString(XMLEvent.START_ELEMENT)
965              + ", " + getEventTypeString(XMLEvent.END_ELEMENT) + ", "
966                      + getEventTypeString(XMLEvent.NAMESPACE)
967              + " valid for getNamespacePrefix()." );
968         }
969     }
970     
971     /** Returns the uri for the namespace declared at the
972      * index.
973      *
974      * @param index the position of the namespace declaration
975      * @return returns the namespace uri
976      * @throws IllegalStateException if this is not a START_ELEMENT, END_ELEMENT or NAMESPACE
977      */

978     public String JavaDoc getNamespaceURI(int index) {
979         if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT || fEventType == XMLEvent.NAMESPACE){
980             //namespaceContext is dynamic object.
981
return fScanner.getNamespaceContext().getURI(fScanner.getNamespaceContext().getDeclaredPrefixAt(index));
982         }
983         else{
984             throw new IllegalStateException JavaDoc("Current state " + getEventTypeString(fEventType)
985              + " is not among the states " + getEventTypeString(XMLEvent.START_ELEMENT)
986              + ", " + getEventTypeString(XMLEvent.END_ELEMENT) + ", "
987                      + getEventTypeString(XMLEvent.NAMESPACE)
988              + " valid for getNamespaceURI()." );
989         }
990         
991     }
992     
993     /** Get the value of a feature/property from the underlying implementation
994      * @param name The name of the property, may not be null
995      * @return The value of the property
996      * @throws IllegalArgumentException if name is null
997      */

998     public Object JavaDoc getProperty(java.lang.String JavaDoc name) throws java.lang.IllegalArgumentException JavaDoc {
999         if(name == null) throw new java.lang.IllegalArgumentException JavaDoc() ;
1000        if (fPropertyManager != null ){
1001            if(name.equals(fPropertyManager.STAX_NOTATIONS)){
1002                return getNotationDecls();
1003            }else if(name.equals(fPropertyManager.STAX_ENTITIES)){
1004                return getEntityDecls();
1005            }else
1006                return fPropertyManager.getProperty(name);
1007        }
1008        return null;
1009    }
1010    
1011    /** Returns the current value of the parse event as a string,
1012     * this returns the string value of a CHARACTERS event,
1013     * returns the value of a COMMENT, the replacement value
1014     * for an ENTITY_REFERENCE,
1015     * or the String value of the DTD
1016     * @return the current text or null
1017     * @throws java.lang.IllegalStateException if this state is not
1018     * a valid text state.
1019     */

1020    public String JavaDoc getText() {
1021        if( fEventType == XMLEvent.CHARACTERS || fEventType == XMLEvent.COMMENT
1022                || fEventType == XMLEvent.CDATA || fEventType == XMLEvent.SPACE){
1023            //this requires creation of new string
1024
//fEventType == XMLEvent.ENTITY_REFERENCE
1025
return fScanner.getCharacterData().toString() ;
1026        } else if(fEventType == XMLEvent.ENTITY_REFERENCE){
1027            String JavaDoc name = fScanner.getEntityName();
1028            if(name != null){
1029                if(fScanner.foundBuiltInRefs)
1030                    return fScanner.getCharacterData().toString();
1031                
1032                XMLEntityStorage entityStore = fEntityManager.getEntityStore();
1033                Hashtable JavaDoc ht = entityStore.getDeclaredEntities();
1034                 Entity en = (Entity)ht.get(name);
1035                  if(en == null)
1036                      return null;
1037                  if(en.isExternal())
1038                    return ((Entity.ExternalEntity)en).entityLocation.getExpandedSystemId();
1039                else
1040                    return ((Entity.InternalEntity)en).text;
1041            }else
1042                return null;
1043        }
1044        else if(fEventType == XMLEvent.DTD){
1045                if(fDTDDecl != null){
1046                    return fDTDDecl;
1047                }
1048                XMLStringBuffer tmpBuffer = fScanner.getDTDDecl();
1049                fDTDDecl = tmpBuffer.toString();
1050                return fDTDDecl;
1051        } else{
1052                throw new IllegalStateException JavaDoc("Current state " + getEventTypeString(fEventType)
1053                     + " is not among the states" + getEventTypeString(XMLEvent.CHARACTERS) + ", "
1054                     + getEventTypeString(XMLEvent.COMMENT) + ", "
1055                     + getEventTypeString(XMLEvent.CDATA) + ", "
1056                     + getEventTypeString(XMLEvent.SPACE) + ", "
1057                     + getEventTypeString(XMLEvent.ENTITY_REFERENCE) + ", "
1058                     + getEventTypeString(XMLEvent.DTD) + " valid for getText() " ) ;
1059        }
1060    }//getText
1061

1062    
1063    /** Test if the current event is of the given type and if the namespace and name match the current namespace and name of the current event.
1064     * If the namespaceURI is null it is not checked for equality, if the localName is null it is not checked for equality.
1065     * @param type the event type
1066     * @param namespaceURI the uri of the event, may be null
1067     * @param localName the localName of the event, may be null
1068     * @throws XMLStreamException if the required values are not matched.
1069     */

1070    public void require(int type, String JavaDoc namespaceURI, String JavaDoc localName) throws XMLStreamException {
1071        if( type != fEventType)
1072             throw new XMLStreamException("Event type " + getEventTypeString(type) + " specified did " +
1073                     "not match with current parser event " + getEventTypeString(fEventType));
1074          if( namespaceURI != null && !namespaceURI.equals(getNamespaceURI()) )
1075             throw new XMLStreamException("Namespace URI " + namespaceURI +" specified did not match " +
1076                     "with current namespace URI");
1077          if(localName != null && !localName.equals(getLocalName()))
1078             throw new XMLStreamException("LocalName " + localName +" specified did not match with " +
1079                     "current local name");
1080        return;
1081    }
1082    
1083    /** Gets the the text associated with a CHARACTERS, SPACE or CDATA event.
1084     * Text starting a "sourceStart" is copied into "destination" starting at "targetStart".
1085     * Up to "length" characters are copied. The number of characters actually copied is returned.
1086     *
1087     * The "sourceStart" argument must be greater or equal to 0 and less than or equal to
1088     * the number of characters associated with the event. Usually, one requests text starting at a "sourceStart" of 0.
1089     * If the number of characters actually copied is less than the "length", then there is no more text.
1090     * Otherwise, subsequent calls need to be made until all text has been retrieved. For example:
1091     *
1092     * <code>
1093     * int length = 1024;
1094     * char[] myBuffer = new char[ length ];
1095     *
1096     * for ( int sourceStart = 0 ; ; sourceStart += length )
1097     * {
1098     * int nCopied = stream.getTextCharacters( sourceStart, myBuffer, 0, length );
1099     *
1100     * if (nCopied < length)
1101     * break;
1102     * }
1103     * </code>
1104     * XMLStreamException may be thrown if there are any XML errors in the underlying source.
1105     * The "targetStart" argument must be greater than or equal to 0 and less than the length of "target",
1106     * Length must be greater than 0 and "targetStart + length" must be less than or equal to length of "target".
1107     *
1108     * @param sourceStart the index of the first character in the source array to copy
1109     * @param target the destination array
1110     * @param targetStart the start offset in the target array
1111     * @param length the number of characters to copy
1112     * @return the number of characters actually copied
1113     * @throws XMLStreamException if the underlying XML source is not well-formed
1114     * @throws IndexOutOfBoundsException if targetStart < 0 or > than the length of target
1115     * @throws IndexOutOfBoundwhile(isCharacters()) ;sException if length < 0 or targetStart + length > length of target
1116     * @throws UnsupportedOperationException if this method is not supported
1117     * @throws NullPointerException is if target is null
1118     */

1119    public int getTextCharacters(int sourceStart, char[] target, int targetStart, int length) throws XMLStreamException {
1120        
1121        if(target == null){
1122            throw new NullPointerException JavaDoc("target char array can't be null") ;
1123        }
1124        
1125        if(targetStart < 0 || length < 0 || sourceStart < 0 || targetStart >= target.length ||
1126            (targetStart + length ) > target.length) {
1127            throw new IndexOutOfBoundsException JavaDoc();
1128        }
1129        
1130        //getTextStart() + sourceStart should not be greater than the lenght of number of characters
1131
//present
1132
int copiedLength = 0;
1133        //int presentDataLen = getTextLength() - (getTextStart()+sourceStart);
1134
int available = getTextLength() - sourceStart;
1135        if(available < 0){
1136            throw new IndexOutOfBoundsException JavaDoc("sourceStart is greater than" +
1137                "number of characters associated with this event");
1138        }
1139        if(available < length){
1140            copiedLength = available;
1141        } else{
1142            copiedLength = length;
1143        }
1144        
1145        System.arraycopy(getTextCharacters(), getTextStart() + sourceStart , target, targetStart, copiedLength);
1146        return copiedLength;
1147    }
1148    
1149    /** Return true if the current event has text, false otherwise
1150     * The following events have text:
1151     * CHARACTERS,DTD ,ENTITY_REFERENCE, COMMENT
1152     */

1153    public boolean hasText() {
1154        if(DEBUG) pr("XMLReaderImpl#EVENT TYPE = " + fEventType ) ;
1155        if( fEventType == XMLEvent.CHARACTERS || fEventType == XMLEvent.COMMENT || fEventType == XMLEvent.CDATA) {
1156            return fScanner.getCharacterData().length > 0 ? true : false;
1157        } else if(fEventType == XMLEvent.ENTITY_REFERENCE) {
1158            String JavaDoc name = fScanner.getEntityName();
1159            if(name != null){
1160                if(fScanner.foundBuiltInRefs)
1161                    return true;
1162                
1163                XMLEntityStorage entityStore = fEntityManager.getEntityStore();
1164                Hashtable JavaDoc ht = entityStore.getDeclaredEntities();
1165                Entity en =(Entity)ht.get(name);
1166                if(en == null)
1167                    return false;
1168                if(en.isExternal()){
1169                    return ((Entity.ExternalEntity)en).entityLocation.getExpandedSystemId() != null ? true : false;
1170                } else{
1171                    return ((Entity.InternalEntity)en).text != null ? true : false ;
1172                }
1173            }else
1174                return false;
1175        } else {
1176            if(fEventType == XMLEvent.DTD)
1177                return fScanner.fSeenDoctypeDecl;
1178        }
1179        return false;
1180    }
1181    
1182    /** Returns a boolean which indicates if this
1183     * attribute was created by default
1184     * @param index the position of the attribute
1185     * @return true if this is a default attribute
1186     * @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE
1187     */

1188    public boolean isAttributeSpecified(int index) {
1189        //check that current state should be either START_ELEMENT or ATTRIBUTE
1190
if( (fEventType == XMLEvent.START_ELEMENT) || (fEventType == XMLEvent.ATTRIBUTE)){
1191            return fScanner.getAttributeIterator().isSpecified(index) ;
1192        } else{
1193            throw new IllegalStateException JavaDoc("Current state is not among the states "
1194                     + getEventTypeString(XMLEvent.START_ELEMENT) + " , "
1195                     + getEventTypeString(XMLEvent.ATTRIBUTE)
1196                     + "valid for isAttributeSpecified()") ;
1197        }
1198    }
1199    
1200    /** Returns true if the cursor points to a character data event
1201     * @return true if the cursor points to character data, false otherwise
1202     */

1203    public boolean isCharacters() {
1204        return fEventType == XMLEvent.CHARACTERS ;
1205    }
1206    
1207    /** Skips any insignificant events (COMMENT and PROCESSING_INSTRUCTION)
1208     * until a START_ELEMENT or
1209     * END_ELEMENT is reached. If other than space characters are
1210     * encountered, an exception is thrown. This method should
1211     * be used when processing element-only content because
1212     * the parser is not able to recognize ignorable whitespace if
1213     * then DTD is missing or not interpreted.
1214     * @return the event type of the element read
1215     * @throws XMLStreamException if the current event is not white space
1216     */

1217    public int nextTag() throws XMLStreamException {
1218        
1219        int eventType = next();
1220        while((eventType == XMLStreamConstants.CHARACTERS && isWhiteSpace()) // skip whitespace
1221
|| (eventType == XMLStreamConstants.CDATA && isWhiteSpace())
1222        // skip whitespace
1223
|| eventType == XMLStreamConstants.SPACE
1224        || eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
1225        || eventType == XMLStreamConstants.COMMENT
1226        ) {
1227            eventType = next();
1228        }
1229
1230        if (eventType != XMLStreamConstants.START_ELEMENT && eventType != XMLStreamConstants.END_ELEMENT) {
1231            throw new XMLStreamException(
1232                    "found: " + getEventTypeString(eventType)
1233                    + ", expected " + getEventTypeString(XMLStreamConstants.START_ELEMENT)
1234                    + " or " + getEventTypeString(XMLStreamConstants.END_ELEMENT),
1235                    getLocation());
1236        }
1237
1238        return eventType;
1239    }
1240    
1241    /** Checks if standalone was set in the document
1242     * @return true if standalone was set in the document, or false otherwise
1243     */

1244    public boolean standaloneSet() {
1245        //xxx: it requires if the standalone was set in the document ? This is different that if the document
1246
// is standalone
1247
return fScanner.isStandAlone() ;
1248    }
1249    
1250    /**
1251     * @param qname
1252     * @return
1253     */

1254    public javax.xml.namespace.QName JavaDoc convertXNIQNametoJavaxQName(com.sun.org.apache.xerces.internal.xni.QName qname){
1255        //xxx: prefix definition ?
1256
if(qname.prefix == null){
1257            return new javax.xml.namespace.QName JavaDoc(qname.uri, qname.localpart) ;
1258        } else{
1259            return new javax.xml.namespace.QName JavaDoc(qname.uri, qname.localpart, qname.prefix) ;
1260        }
1261    }
1262    
1263    /** Return the uri for the given prefix.
1264     * The uri returned depends on the current state of the processor.
1265     *
1266     * <p><strong>NOTE:</strong>The 'xml' prefix is bound as defined in
1267     * <a HREF="http://www.w3.org/TR/REC-xml-names/#ns-using">Namespaces in XML</a>
1268     * specification to "http://www.w3.org/XML/1998/namespace".
1269     *
1270     * <p><strong>NOTE:</strong> The 'xmlns' prefix must be resolved to following namespace
1271     * <a HREF="http://www.w3.org/2000/xmlns/">http://www.w3.org/2000/xmlns/</a>
1272     * @return the uri bound to the given prefix or null if it is not bound
1273     * @param prefix The prefix to lookup, may not be null
1274     */

1275    public String JavaDoc getNamespaceURI(String JavaDoc prefix) {
1276        //first add the string to symbol table.. since internally identity comparisons are done.
1277
return fScanner.getNamespaceContext().getURI(fSymbolTable.addSymbol(prefix)) ;
1278    }
1279    
1280    //xxx: this function is not being used.
1281
protected void setPropertyManager(PropertyManager propertyManager){
1282        fPropertyManager = propertyManager ;
1283        //REVISIT: we were supplying hashmap ealier
1284
fScanner.setProperty("stax-properties",propertyManager);
1285        fScanner.setPropertyManager(propertyManager) ;
1286    }
1287    
1288    /**
1289     * @return returns the reference to property manager.
1290     */

1291    protected PropertyManager getPropertyManager(){
1292        return fPropertyManager ;
1293    }
1294    
1295    static void pr(String JavaDoc str) {
1296        System.out.println(str) ;
1297    }
1298    
1299    protected List JavaDoc getEntityDecls(){
1300        if(fEventType == XMLStreamConstants.DTD){
1301            XMLEntityStorage entityStore = fEntityManager.getEntityStore();
1302            Hashtable JavaDoc ht = entityStore.getDeclaredEntities();
1303            ArrayList JavaDoc list = null;
1304            if(ht != null){
1305                EntityDeclarationImpl decl = null;
1306                list = new ArrayList JavaDoc(ht.size());
1307                Enumeration JavaDoc enu = ht.keys();
1308                while(enu.hasMoreElements()){
1309                    String JavaDoc key = (String JavaDoc)enu.nextElement();
1310                    Entity en = (Entity)ht.get(key);
1311                    decl = new EntityDeclarationImpl();
1312                    decl.setEntityName(key);
1313                    if(en.isExternal()){
1314                        decl.setXMLResourceIdentifier(((Entity.ExternalEntity)en).entityLocation);
1315                        decl.setNotationName(((Entity.ExternalEntity)en).notation);
1316                    }
1317                    else
1318                        decl.setEntityReplacementText(((Entity.InternalEntity)en).text);
1319                    list.add(decl);
1320                }
1321            }
1322            return list;
1323        }
1324        return null;
1325    }
1326    
1327    protected List JavaDoc getNotationDecls(){
1328        if(fEventType == XMLStreamConstants.DTD){
1329            if(fScanner.fDTDScanner == null) return null;
1330            DTDGrammar grammar = ((XMLDTDScannerImpl)(fScanner.fDTDScanner)).getGrammar();
1331            if(grammar == null) return null;
1332            List JavaDoc notations = grammar.getNotationDecls();
1333            
1334            Iterator JavaDoc it = notations.iterator();
1335            ArrayList JavaDoc list = new ArrayList JavaDoc();
1336            while(it.hasNext()){
1337                XMLNotationDecl ni = (XMLNotationDecl)it.next();
1338                if(ni!= null){
1339                    list.add(new NotationDeclarationImpl(ni));
1340                }
1341            }
1342            return list;
1343        }
1344        return null;
1345    }
1346    
1347    
1348    
1349}//XMLReaderImpl
1350
Popular Tags