Example #1
0
 /** Note: calling this method will move stream to the next non-textual event. */
 protected String collectAllText(XMLStreamReader sr) throws XMLStreamException {
   StringBuilder sb = new StringBuilder(100);
   while (true) {
     int type = sr.getEventType();
     if (type == CHARACTERS || type == SPACE || type == CDATA) {
       sb.append(sr.getText());
       sr.next();
     } else {
       break;
     }
   }
   return sb.toString();
 }
 /** @see IBailsStream#getCharSequence() */
 @Override
 public CharSequence getCharSequence() {
   // This should contain the tags char sequence along with any surrounding whitespace.
   return currentEventString.toString();
 }
  /** @see IBailsStream#next() */
  @Override
  public void next() {
    try {
      currentEventString.setLength(0); // Clear the xml events char sequence.
      currentEvent = this.parser.nextEvent(); // get the next event.

      ELEMENT_TYPE previousType = type; // Record the previous type to check for openClose tags.

      // Search for the next start/end document/element or a character event.
      while (currentEvent.getEventType() != START_DOCUMENT
          && currentEvent.getEventType() != START_ELEMENT
          && currentEvent.getEventType() != CHARACTERS
          && currentEvent.getEventType() != END_ELEMENT
          && currentEvent.getEventType() != END_DOCUMENT) {
        currentEvent = this.parser.nextEvent();
      }

      switch (currentEvent.getEventType()) {
        case START_DOCUMENT:
          type = ELEMENT_TYPE.DOCUMENT_START;
          break;
        case START_ELEMENT:
          if (this.parser.peek().getEventType() == END_ELEMENT) type = ELEMENT_TYPE.OPENCLOSE;
          else type = ELEMENT_TYPE.OPEN;
          break;
        case END_ELEMENT:
          type = ELEMENT_TYPE.CLOSE;
          break;
        case CHARACTERS:
          type = ELEMENT_TYPE.CHARACTERS;
          break;
        case END_DOCUMENT:
          type = ELEMENT_TYPE.DOCUMENT_END;
          break;
      }

      // If we are at the start or end of the document don't do any more processing.
      if (type != ELEMENT_TYPE.DOCUMENT_START && type != ELEMENT_TYPE.DOCUMENT_END) {

        // Add the white space that should be at the start of the char sequence.
        currentEventString.append(preWhiteSpace);
        // Add char sequence of the current event.
        currentEventString.append(currentEvent);

        // Get the white space that should be at the end of this char sequence as well as the white
        // space that
        // should be before the next.
        checkForNewLine();

        // Add the white space that should be at the end of the char sequence.
        currentEventString.append(postWhiteSpace);

        // If this is an opening tag any attributes will need be recorded.
        if (currentEvent.getEventType() == START_ELEMENT) {

          attributes =
              parsAttributes((StartElement) currentEvent); // Pars the start events attributes.

          if (bailsTag)
            type = ELEMENT_TYPE.BAILS; // If this is a bails tag set it to the correct type.

        } else { // Otherwise there should be no attributes.
          attributes = null;
        }
      }
    } catch (XMLStreamException e) {
      System.out.println("SORT THIS OUT!: " + e.getMessage());
    }
  }