|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package com.sun.xml.internal.stream; |
|
|
|
import com.sun.xml.internal.stream.events.XMLEventAllocatorImpl; |
|
import java.util.NoSuchElementException; |
|
import javax.xml.stream.XMLInputFactory; |
|
import javax.xml.stream.XMLStreamConstants; |
|
import javax.xml.stream.XMLStreamException; |
|
import javax.xml.stream.XMLStreamReader; |
|
import javax.xml.stream.events.EntityReference; |
|
import javax.xml.stream.events.XMLEvent; |
|
import javax.xml.stream.util.XMLEventAllocator; |
|
|
|
/** |
|
* @author @author Neeraj Bajaj Sun Microsystems |
|
* |
|
*/ |
|
|
|
public class XMLEventReaderImpl implements javax.xml.stream.XMLEventReader{ |
|
|
|
protected XMLStreamReader fXMLReader ; |
|
protected XMLEventAllocator fXMLEventAllocator; |
|
|
|
|
|
public XMLEventReaderImpl(XMLStreamReader reader) throws XMLStreamException { |
|
fXMLReader = reader ; |
|
fXMLEventAllocator = (XMLEventAllocator)reader.getProperty(XMLInputFactory.ALLOCATOR); |
|
if(fXMLEventAllocator == null){ |
|
fXMLEventAllocator = new XMLEventAllocatorImpl(); |
|
} |
|
fPeekedEvent = fXMLEventAllocator.allocate(fXMLReader); |
|
} |
|
|
|
|
|
public boolean hasNext() { |
|
|
|
if(fPeekedEvent != null)return true; |
|
//this is strange XMLStreamReader throws XMLStreamException |
|
|
|
boolean next = false ; |
|
try{ |
|
next = fXMLReader.hasNext(); |
|
}catch(XMLStreamException ex){ |
|
return false; |
|
} |
|
return next ; |
|
} |
|
|
|
|
|
public XMLEvent nextEvent() throws XMLStreamException { |
|
|
|
if(fPeekedEvent != null){ |
|
fLastEvent = fPeekedEvent ; |
|
fPeekedEvent = null; |
|
return fLastEvent ; |
|
} |
|
else if(fXMLReader.hasNext()){ |
|
|
|
fXMLReader.next(); |
|
return fLastEvent = fXMLEventAllocator.allocate(fXMLReader); |
|
} |
|
else{ |
|
fLastEvent = null; |
|
throw new NoSuchElementException(); |
|
} |
|
} |
|
|
|
public void remove(){ |
|
|
|
throw new java.lang.UnsupportedOperationException(); |
|
} |
|
|
|
|
|
public void close() throws XMLStreamException { |
|
fXMLReader.close(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public String getElementText() throws XMLStreamException { |
|
//we have to keep reference to the 'last event' of the stream to be able |
|
|
|
if(fLastEvent.getEventType() != XMLEvent.START_ELEMENT){ |
|
throw new XMLStreamException( |
|
"parser must be on START_ELEMENT to read next text", fLastEvent.getLocation()); |
|
} |
|
|
|
// STag content ETag |
|
//[43] content ::= CharData? ((element | Reference | CDSect | PI | Comment) CharData?)* |
|
|
|
//<foo>....some long text say in KB and underlying parser reports multiple character |
|
// but getElementText() events....</foo> |
|
|
|
String data = null; |
|
|
|
if(fPeekedEvent != null){ |
|
XMLEvent event = fPeekedEvent ; |
|
fPeekedEvent = null; |
|
int type = event.getEventType(); |
|
|
|
if( type == XMLEvent.CHARACTERS || type == XMLEvent.SPACE || |
|
type == XMLEvent.CDATA){ |
|
data = event.asCharacters().getData(); |
|
} |
|
else if(type == XMLEvent.ENTITY_REFERENCE){ |
|
data = ((EntityReference)event).getDeclaration().getReplacementText(); |
|
} |
|
else if(type == XMLEvent.COMMENT || type == XMLEvent.PROCESSING_INSTRUCTION){ |
|
//ignore |
|
} else if(type == XMLEvent.START_ELEMENT) { |
|
throw new XMLStreamException( |
|
"elementGetText() function expects text only elment but START_ELEMENT was encountered.", event.getLocation()); |
|
}else if(type == XMLEvent.END_ELEMENT){ |
|
return ""; |
|
} |
|
|
|
|
|
StringBuffer buffer = new StringBuffer(); |
|
if(data != null && data.length() > 0 ) { |
|
buffer.append(data); |
|
} |
|
//get the next event -- we should stop at END_ELEMENT but it can be any thing |
|
//things are worse when implementing this function in XMLEventReader because |
|
//there isn't any function called getText() which can get values for |
|
//space, cdata, characters and entity reference |
|
|
|
event = nextEvent(); |
|
while ((type = event.getEventType()) != XMLEvent.END_ELEMENT) { |
|
if (type == XMLEvent.CHARACTERS || type == XMLEvent.SPACE || |
|
type == XMLEvent.CDATA){ |
|
data = event.asCharacters().getData(); |
|
} |
|
else if(type == XMLEvent.ENTITY_REFERENCE){ |
|
data = ((EntityReference)event).getDeclaration().getReplacementText(); |
|
} |
|
else if(type == XMLEvent.COMMENT || type == XMLEvent.PROCESSING_INSTRUCTION){ |
|
|
|
data = null; |
|
} else if(type == XMLEvent.END_DOCUMENT) { |
|
throw new XMLStreamException("unexpected end of document when reading element text content"); |
|
} else if(type == XMLEvent.START_ELEMENT) { |
|
throw new XMLStreamException( |
|
"elementGetText() function expects text only elment but START_ELEMENT was encountered.", event.getLocation()); |
|
} else { |
|
throw new XMLStreamException( |
|
"Unexpected event type "+ type, event.getLocation()); |
|
} |
|
|
|
if(data != null && data.length() > 0 ) { |
|
buffer.append(data); |
|
} |
|
event = nextEvent(); |
|
} |
|
return buffer.toString(); |
|
}//if (fPeekedEvent != null) |
|
|
|
//if there was no peeked, delegate everything to fXMLReader |
|
|
|
data = fXMLReader.getElementText(); |
|
fLastEvent = fXMLEventAllocator.allocate(fXMLReader); |
|
return data; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public Object getProperty(java.lang.String name) throws java.lang.IllegalArgumentException { |
|
return fXMLReader.getProperty(name) ; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public XMLEvent nextTag() throws XMLStreamException { |
|
|
|
if(fPeekedEvent != null){ |
|
|
|
XMLEvent event = fPeekedEvent; |
|
fPeekedEvent = null ; |
|
int eventType = event.getEventType(); |
|
//if peeked event is whitespace move to the next event |
|
|
|
if( (event.isCharacters() && event.asCharacters().isWhiteSpace()) |
|
|| eventType == XMLStreamConstants.PROCESSING_INSTRUCTION |
|
|| eventType == XMLStreamConstants.COMMENT |
|
|| eventType == XMLStreamConstants.START_DOCUMENT){ |
|
event = nextEvent(); |
|
eventType = event.getEventType(); |
|
} |
|
|
|
|
|
while((event.isCharacters() && event.asCharacters().isWhiteSpace()) |
|
|| eventType == XMLStreamConstants.PROCESSING_INSTRUCTION |
|
|| eventType == XMLStreamConstants.COMMENT){ |
|
|
|
event = nextEvent(); |
|
eventType = event.getEventType(); |
|
} |
|
|
|
if (eventType != XMLStreamConstants.START_ELEMENT && eventType != XMLStreamConstants.END_ELEMENT) { |
|
throw new XMLStreamException("expected start or end tag", event.getLocation()); |
|
} |
|
return event; |
|
} |
|
|
|
|
|
fXMLReader.nextTag(); |
|
return (fLastEvent = fXMLEventAllocator.allocate(fXMLReader)); |
|
} |
|
|
|
public Object next() { |
|
Object object = null; |
|
try{ |
|
object = nextEvent(); |
|
}catch(XMLStreamException streamException){ |
|
fLastEvent = null ; |
|
|
|
NoSuchElementException e = new NoSuchElementException(streamException.getMessage()); |
|
e.initCause(streamException.getCause()); |
|
throw e; |
|
|
|
} |
|
return object; |
|
} |
|
|
|
public XMLEvent peek() throws XMLStreamException{ |
|
//if someone call peek() two times we should just return the peeked event |
|
|
|
if(fPeekedEvent != null) return fPeekedEvent; |
|
|
|
if(hasNext()){ |
|
//revisit: we can implement peek() by calling underlying reader to advance |
|
// the stream and returning the event without the knowledge of the user |
|
// that the stream was advanced but the point is we are advancing the stream |
|
//here. -- nb. |
|
|
|
// Is there any application that relies on this behavior ? |
|
//Can it be an application knows that there is particularly very large 'comment' section |
|
//or character data which it doesn't want to read or to be returned as event |
|
//But as of now we are creating every event but it can be optimized not to create |
|
|
|
fXMLReader.next(); |
|
fPeekedEvent = fXMLEventAllocator.allocate(fXMLReader); |
|
return fPeekedEvent; |
|
}else{ |
|
return null; |
|
} |
|
}//peek() |
|
|
|
private XMLEvent fPeekedEvent; |
|
private XMLEvent fLastEvent; |
|
|
|
}//XMLEventReaderImpl |