View Javadoc

    1   package org.codehaus.xfire.util.stax;
    2
    3   import javax.xml.stream.XMLStreamException;
    4   import javax.xml.stream.XMLStreamReader;
    5
    6
    7   public class FragmentStreamReader
    8       extends DepthXMLStreamReader
    9   {
    10      private boolean startDoc = false;
    11      private boolean startElement = false;
    12      private boolean middle = true;
    13      private boolean endDoc = false;
    14
    15      private int depth;
    16      private int current = -1;
    17
    18      public FragmentStreamReader(XMLStreamReader reader)
    19      {
    20          super(reader);
    21      }
    22
    23      public int getEventType()
    24      {
    25          return current;
    26      }
    27
    28      public boolean hasNext()
    29          throws XMLStreamException
    30      {
    31          if (!startDoc)
    32          {
    33              return true;
    34          }
    35
    36          if (endDoc)
    37          {
    38              return false;
    39          }
    40
    41          return reader.hasNext();
    42      }
    43
    44      public int next()
    45          throws XMLStreamException
    46      {
    47          if (!startDoc)
    48          {
    49              startDoc = true;
    50              current = START_DOCUMENT;
    51          }
    52          else if (!startElement)
    53          {
    54              depth = getDepth();
    55              startElement = true;
    56              current = START_ELEMENT;
    57          }
    58          else if (middle)
    59          {
    60              current = super.next();
    61
    62              if (current == END_ELEMENT && depth < getDepth())
    63              {
    64                  middle = false;
    65              }
    66          }
    67          else if (!endDoc)
    68          {
    69              endDoc = true;
    70              current = END_DOCUMENT;
    71          }
    72          else
    73          {
    74              throw new XMLStreamException("Already at the end of the document.");
    75          }
    76
    77          return current;
    78      }
    79  }