1 38 39 package com.sun.xml.fastinfoset.stax; 40 41 import com.sun.xml.fastinfoset.stax.events.StAXEventAllocatorBase; 42 import com.sun.xml.fastinfoset.stax.events.StAXEventAllocator; 43 import java.util.NoSuchElementException ; 44 import javax.xml.stream.XMLInputFactory; 45 import javax.xml.stream.XMLStreamConstants; 46 import javax.xml.stream.XMLStreamException; 47 import javax.xml.stream.XMLStreamReader; 48 import javax.xml.stream.events.EntityReference; 49 import javax.xml.stream.events.XMLEvent; 50 import javax.xml.stream.util.XMLEventAllocator; 51 import com.sun.xml.fastinfoset.CommonResourceBundle; 52 53 54 55 public class StAXEventReader implements javax.xml.stream.XMLEventReader{ 56 protected XMLStreamReader _streamReader ; 57 protected XMLEventAllocator _eventAllocator; 58 private XMLEvent _currentEvent; private XMLEvent[] events = new XMLEvent[3]; 60 private int size = 3; 61 private int currentIndex = 0; 62 private boolean hasEvent = false; 64 public StAXEventReader(XMLStreamReader reader) throws XMLStreamException { 66 _streamReader = reader ; 67 _eventAllocator = (XMLEventAllocator)reader.getProperty(XMLInputFactory.ALLOCATOR); 68 if(_eventAllocator == null){ 69 _eventAllocator = new StAXEventAllocatorBase(); 70 } 71 if (_streamReader.hasNext()) 73 { 74 _streamReader.next(); 75 _currentEvent =_eventAllocator.allocate(_streamReader); 76 events[0] = _currentEvent; 77 System.out.println("current event:" + _currentEvent); 78 hasEvent = true; 79 } else { 80 throw new XMLStreamException(CommonResourceBundle.getInstance().getString("message.noElement")); 81 } 82 } 83 84 public boolean hasNext() { 85 return hasEvent; 86 } 87 88 public XMLEvent nextEvent() throws XMLStreamException { 89 XMLEvent event = null; 90 XMLEvent nextEvent = null; 91 if (hasEvent) 92 { 93 event = events[currentIndex]; 94 events[currentIndex] = null; 95 if (_streamReader.hasNext()) 96 { 97 _streamReader.next(); 99 nextEvent = _eventAllocator.allocate(_streamReader); 100 if (++currentIndex==size) 101 currentIndex = 0; 102 events[currentIndex] = nextEvent; 103 hasEvent = true; 104 } else { 105 _currentEvent = null; 106 hasEvent = false; 107 } 108 return event; 109 } 110 else{ 111 throw new NoSuchElementException (); 112 } 113 } 114 115 public void remove(){ 116 throw new java.lang.UnsupportedOperationException (); 118 } 119 120 121 public void close() throws XMLStreamException { 122 _streamReader.close(); 123 } 124 125 131 public String getElementText() throws XMLStreamException { 132 if(!hasEvent) { 133 throw new NoSuchElementException (); 134 } 135 136 if(!_currentEvent.isStartElement()) { 137 StAXDocumentParser parser = (StAXDocumentParser)_streamReader; 138 return parser.getElementText(true); 139 } else { 140 return _streamReader.getElementText(); 141 } 142 } 143 144 149 public Object getProperty(java.lang.String name) throws java.lang.IllegalArgumentException { 150 return _streamReader.getProperty(name) ; 151 } 152 153 161 public XMLEvent nextTag() throws XMLStreamException { 162 if(!hasEvent) { 163 throw new NoSuchElementException (); 164 } 165 StAXDocumentParser parser = (StAXDocumentParser)_streamReader; 166 parser.nextTag(true); 167 return _eventAllocator.allocate(_streamReader); 168 } 169 170 public Object next() { 172 try{ 173 return nextEvent(); 174 }catch(XMLStreamException streamException){ 175 return null; 176 } 177 } 178 179 public XMLEvent peek() throws XMLStreamException{ 180 if (!hasEvent) 181 throw new XMLStreamException(CommonResourceBundle.getInstance().getString("message.noElement")); 182 _currentEvent = events[currentIndex]; 183 return _currentEvent; 184 } 185 186 public void setAllocator(XMLEventAllocator allocator) { 187 if (allocator == null) 188 throw new IllegalArgumentException (CommonResourceBundle.getInstance().getString("message.nullXMLEventAllocator")); 189 190 _eventAllocator = allocator; 191 } 192 193 194 } 195 | Popular Tags |