8787import org .xml .sax .ext .Attributes2 ;
8888import org .xml .sax .ext .DeclHandler ;
8989import org .xml .sax .ext .LexicalHandler ;
90+ import org .xml .sax .ext .Locator2 ;
9091import org .xml .sax .helpers .NamespaceSupport ;
9192
9293/**
@@ -156,14 +157,16 @@ protected XMLStreamReaderImpl(InputStream in,
156157 // Configure the SAX parser and perform the parse
157158 try
158159 {
159- CallbackHandler ch = this .new CallbackHandler ();
160160 SAXParserFactory f = SAXParserFactory .newInstance ();
161161 f .setNamespaceAware (namespaceAware );
162162 f .setValidating (validating );
163163 SAXParser p = f .newSAXParser ();
164164 XMLReader r = p .getXMLReader ();
165+ CallbackHandler ch = this .new CallbackHandler (r );
165166 r .setFeature ("http://xml.org/sax/features/external-general-entities" ,
166167 externalEntities );
168+ r .setFeature ("http://xml.org/sax/features/namespaces" ,
169+ namespaceAware );
167170 r .setContentHandler (ch );
168171 r .setDTDHandler (ch );
169172 r .setEntityResolver (ch );
@@ -220,14 +223,16 @@ protected XMLStreamReaderImpl(Reader reader,
220223 // Configure the SAX parser and perform the parse
221224 try
222225 {
223- CallbackHandler ch = this .new CallbackHandler ();
224226 SAXParserFactory f = SAXParserFactory .newInstance ();
225227 f .setNamespaceAware (namespaceAware );
226228 f .setValidating (validating );
227229 SAXParser p = f .newSAXParser ();
228230 XMLReader r = p .getXMLReader ();
231+ CallbackHandler ch = this .new CallbackHandler (r );
229232 r .setFeature ("http://xml.org/sax/features/external-general-entities" ,
230233 externalEntities );
234+ r .setFeature ("http://xml.org/sax/features/namespaces" ,
235+ namespaceAware );
231236 r .setContentHandler (ch );
232237 r .setDTDHandler (ch );
233238 r .setEntityResolver (ch );
@@ -265,7 +270,7 @@ public int next()
265270 {
266271 if (events .isEmpty ())
267272 throw new XMLStreamException ("EOF" );
268- Object event = events .removeLast ();
273+ Object event = events .removeFirst ();
269274 if (event instanceof Exception )
270275 {
271276 Exception e = (Exception ) event ;
@@ -378,6 +383,7 @@ public int getAttributeCount()
378383 int count = 0 ;
379384 for (Iterator i = se .getAttributes (); i .hasNext (); )
380385 {
386+ i .next ();
381387 count ++;
382388 }
383389 return count ;
@@ -462,20 +468,43 @@ public boolean isAttributeSpecified(int index)
462468
463469 public int getNamespaceCount ()
464470 {
465- StartElement se = (StartElement ) currentEvent ;
471+ Iterator i = null ;
472+ switch (eventType )
473+ {
474+ case XMLStreamConstants .START_ELEMENT :
475+ i = ((StartElement ) currentEvent ).getNamespaces ();
476+ break ;
477+ case XMLStreamConstants .END_ELEMENT :
478+ i = ((EndElement ) currentEvent ).getNamespaces ();
479+ break ;
480+ default :
481+ throw new IllegalStateException ();
482+ }
466483 int count = 0 ;
467- for ( Iterator i = se . getNamespaces (); i . hasNext (); )
484+ while ( i . hasNext ())
468485 {
486+ i .next ();
469487 count ++;
470488 }
471489 return count ;
472490 }
473491
474492 public String getNamespacePrefix (int index )
475493 {
476- StartElement se = (StartElement ) currentEvent ;
494+ Iterator i = null ;
495+ switch (eventType )
496+ {
497+ case XMLStreamConstants .START_ELEMENT :
498+ i = ((StartElement ) currentEvent ).getNamespaces ();
499+ break ;
500+ case XMLStreamConstants .END_ELEMENT :
501+ i = ((EndElement ) currentEvent ).getNamespaces ();
502+ break ;
503+ default :
504+ throw new IllegalStateException ();
505+ }
477506 int count = 0 ;
478- for ( Iterator i = se . getNamespaces (); i . hasNext (); )
507+ while ( i . hasNext ())
479508 {
480509 Namespace ns = (Namespace ) i .next ();
481510 if (index == count )
@@ -487,9 +516,20 @@ public String getNamespacePrefix(int index)
487516
488517 public String getNamespaceURI (int index )
489518 {
490- StartElement se = (StartElement ) currentEvent ;
519+ Iterator i = null ;
520+ switch (eventType )
521+ {
522+ case XMLStreamConstants .START_ELEMENT :
523+ i = ((StartElement ) currentEvent ).getNamespaces ();
524+ break ;
525+ case XMLStreamConstants .END_ELEMENT :
526+ i = ((EndElement ) currentEvent ).getNamespaces ();
527+ break ;
528+ default :
529+ throw new IllegalStateException ();
530+ }
491531 int count = 0 ;
492- for ( Iterator i = se . getNamespaces (); i . hasNext (); )
532+ while ( i . hasNext ())
493533 {
494534 Namespace ns = (Namespace ) i .next ();
495535 if (index == count )
@@ -653,14 +693,22 @@ class CallbackHandler
653693 DeclHandler , EntityResolver , ErrorHandler
654694 {
655695
696+ XMLReader reader ;
697+ Locator locator ;
656698 Location location ;
657699 private boolean inCDATA ;
658700 private LinkedList namespaces = new LinkedList ();
659701 private LinkedList notations ;
660702 private LinkedList entities ;
661703
704+ CallbackHandler (XMLReader reader )
705+ {
706+ this .reader = reader ;
707+ }
708+
662709 public void setDocumentLocator (Locator locator )
663710 {
711+ this .locator = locator ;
664712 location = new LocationImpl (-1 ,
665713 locator .getColumnNumber (),
666714 locator .getLineNumber (),
@@ -670,12 +718,14 @@ public void setDocumentLocator(Locator locator)
670718 public void startDocument ()
671719 throws SAXException
672720 {
673- String version = null ;
674- String encoding = null ;
675- boolean standalone = false ;
676- boolean standaloneDeclared = false ;
677- boolean encodingDeclared = false ;
678- // XXX can't get these values from SAX
721+ String version = (locator instanceof Locator2 ) ?
722+ ((Locator2 ) locator ).getXMLVersion () : null ;
723+ String encoding = (locator instanceof Locator2 ) ?
724+ ((Locator2 ) locator ).getEncoding () : null ;
725+ boolean standalone =
726+ reader .getFeature ("http://xml.org/sax/features/is-standalone" );
727+ boolean standaloneDeclared = standalone ;
728+ boolean encodingDeclared = (encoding != null );
679729 events .add (new StartDocumentImpl (location ,
680730 location .getLocationURI (),
681731 encoding ,
@@ -722,10 +772,8 @@ public void startElement(String namespaceURI, String localName,
722772 attrs , ns , null );
723773 events .add (se );
724774 // Add namespaces
725- for (Iterator i = ns .iterator (); i .hasNext (); )
726- {
727- events .add (i .next ());
728- }
775+ //for (Iterator i = ns.iterator(); i.hasNext(); )
776+ // events.add(i.next());
729777 // Add attributes
730778 int len = atts .getLength ();
731779 for (int i = 0 ; i < len ; i ++)
@@ -750,7 +798,7 @@ public void startElement(String namespaceURI, String localName,
750798 AttributeImpl attr = new AttributeImpl (location , attrName ,
751799 value , type , specified );
752800 attrs .add (attr );
753- events .add (attr );
801+ // events.add(attr);
754802 }
755803 }
756804
@@ -859,7 +907,7 @@ public void notationDecl(String name, String publicId, String systemId)
859907 Object n = new NotationDeclarationImpl (location , name , publicId ,
860908 systemId );
861909 notations .add (n );
862- events .add (n );
910+ // events.add(n);
863911 }
864912
865913 public void unparsedEntityDecl (String name , String publicId ,
@@ -870,7 +918,7 @@ public void unparsedEntityDecl(String name, String publicId,
870918 name , notationName ,
871919 null , null );
872920 entities .add (e );
873- events .add (e );
921+ // events.add(e);
874922 }
875923
876924 public void elementDecl (String name , String model )
@@ -890,7 +938,7 @@ public void internalEntityDecl(String name, String value)
890938 Object e = new EntityDeclarationImpl (location , null , null ,
891939 name , null , value , null );
892940 entities .add (e );
893- events .add (e );
941+ // events.add(e);
894942 }
895943
896944 public void externalEntityDecl (String name , String publicId ,
@@ -900,7 +948,7 @@ public void externalEntityDecl(String name, String publicId,
900948 Object e = new EntityDeclarationImpl (location , publicId , systemId ,
901949 name , null , null , null );
902950 entities .add (e );
903- events .add (e );
951+ // events.add(e);
904952 }
905953
906954 public void warning (SAXParseException e )
0 commit comments