org.dellroad.stuff.xml
Class AnnotatedXMLEventReader

java.lang.Object
  extended by javax.xml.stream.util.EventReaderDelegate
      extended by org.dellroad.stuff.xml.AnnotatedXMLEventReader
All Implemented Interfaces:
Iterator, XMLEventReader
Direct Known Subclasses:
UpdatesXMLEventReader

public abstract class AnnotatedXMLEventReader
extends EventReaderDelegate

XMLEventReader that reads and removes an initial annotation element from an XML document. The annotation element, if present, must be the first element inside the top-level document element. When the annotation element is encountered, readAnnotationElement() will be invoked.

This class can be used in combination with AnnotatedXMLEventWriter to transparently annotate XML documents.

See Also:
AnnotatedXMLEventWriter

Constructor Summary
AnnotatedXMLEventReader(XMLEventReader inner)
          Constructor.
 
Method Summary
protected  XMLEvent advance()
          Advance one event in the event stream.
protected abstract  boolean isAnnotationElement(StartElement event)
          Determine if the given event represents the start of the annotation element.
 Object next()
           
 XMLEvent nextEvent()
           
 XMLEvent peek()
           
protected abstract  void readAnnotationElement(StartElement event)
          Read the annotation element.
 void setParent(XMLEventReader reader)
           
protected  XMLEvent skipWhiteSpace(XMLEvent event, boolean comments)
          Skip whitespace.
 
Methods inherited from class javax.xml.stream.util.EventReaderDelegate
close, getElementText, getParent, getProperty, hasNext, nextTag, remove
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AnnotatedXMLEventReader

public AnnotatedXMLEventReader(XMLEventReader inner)
Constructor.

The given parameter should expect to read the XML document without the annotation element.

Parameters:
inner - nested reader
Method Detail

setParent

public void setParent(XMLEventReader reader)
Overrides:
setParent in class EventReaderDelegate

next

public Object next()
Specified by:
next in interface Iterator
Overrides:
next in class EventReaderDelegate

peek

public XMLEvent peek()
              throws XMLStreamException
Specified by:
peek in interface XMLEventReader
Overrides:
peek in class EventReaderDelegate
Throws:
XMLStreamException

nextEvent

public XMLEvent nextEvent()
                   throws XMLStreamException
Specified by:
nextEvent in interface XMLEventReader
Overrides:
nextEvent in class EventReaderDelegate
Throws:
XMLStreamException

isAnnotationElement

protected abstract boolean isAnnotationElement(StartElement event)
Determine if the given event represents the start of the annotation element.


readAnnotationElement

protected abstract void readAnnotationElement(StartElement event)
                                       throws XMLStreamException
Read the annotation element.

When this method is invoked, event represents the StartElement event for the annotation element (i.e., isAnnotationElement(event) has returned true); it is also the next event in the pipeline, i.e., the next event returned by nextEvent().

The last event consumed by this method should be the EndElement event for the annotation element, and the event returned by peek() should be the next event after the EndElement.

Parameters:
event - the StartElement event for the annotation element
Throws:
XMLStreamException

skipWhiteSpace

protected XMLEvent skipWhiteSpace(XMLEvent event,
                                  boolean comments)
                           throws XMLStreamException
Skip whitespace.

Parameters:
event - the next event, as returned by peek()
comments - whether to also skip comments
Returns:
the next event, as returned by peek(), after skipping any leading whitespace (and, optionally, comments)
Throws:
XMLStreamException

advance

protected XMLEvent advance()
                    throws XMLStreamException
Advance one event in the event stream.

Returns:
the next event, as returned by peek(), after the advance
Throws:
XMLStreamException