XFire
  1. XFire
  2. XFIRE-961

Include basic formatting option when copying an XMLStreamReader object to a XMLStreamWriter object via the STAXUtils copy method.

    Details

    • Type: Improvement Improvement
    • Status: Open Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 1.2.4
    • Fix Version/s: None
    • Component/s: Core
    • Labels:
      None
    • Environment:
      JDK1.5.0_09
    • Number of attachments :
      0

      Description

      I have created a Web Service whose i/o is an XMLStreamReader object and this service is invoked via XFire's MessageBinding implementation. In a custom handler on the client side, I invoke the STAXUtils copy method to create an equivalent XMLStreamWriter object from the XMLStreamReader InMessage. When I flush this XMLStreamWriter object to the defined output stream, the output is not formatted and is delivered as one long continuous string.

      I have updated the STAXUtils copy method to implement some basic formatting (line breaks and hierarchical indentation) when the format flag is set to true. I have overloaded the existing method, but a new method to be used by client programs that implement the MessageBinding solution would also be sufficient.

      public static void copy( XMLStreamReader reader, XMLStreamWriter writer )
            throws XMLStreamException
      {
            copy( reader, writer, false );
      }
      
      public static void copy( XMLStreamReader reader, XMLStreamWriter writer, boolean format )
            throws XMLStreamException
      {
              int read = 0; // number of elements read in
              int event = reader.getEventType();
              String offset = "";
              boolean isNext = false;
              boolean isParentElement = true;
              
              while ( reader.hasNext() )
              {
                  switch( event )
                  {
                      case XMLStreamConstants.START_ELEMENT:
                          read++;
                          if( format )
                            writer.writeCharacters( offset );
                          writeStartElement( reader, writer );
                          if( format )
                          {
                            isNext = true;
                            event = reader.next();
                            if( event == XMLStreamConstants.START_ELEMENT )
                            {
                              writer.writeCharacters( "\n" );
                              offset += "  ";
                            }
                            else
                            {
                              isParentElement = false;
                            }
                          }
                          break;
                      case XMLStreamConstants.END_ELEMENT:
                          if( format )
                          {
                            if( isParentElement )
                            {
                              if( offset.length() > 1 )
                                offset = offset.substring( 0, offset.length() - 2 );
                              writer.writeCharacters( offset );
                            }
                            isParentElement = true;
                          }
                          writer.writeEndElement();
                          if( format )
                            writer.writeCharacters( "\n");
                          read--;
                          if ( read <= 0 )
                              return;
                          break;
                      case XMLStreamConstants.CHARACTERS:
                          writer.writeCharacters( reader.getText() );
                          break;
                      case XMLStreamConstants.START_DOCUMENT:
                      case XMLStreamConstants.END_DOCUMENT:
                      case XMLStreamConstants.ATTRIBUTE:
                      case XMLStreamConstants.NAMESPACE:
                          break;
                      case XMLStreamConstants.CDATA:
                      	writer.writeCData(reader.getText());
                      	break;
                      default:
                          break;
                  }
                  if( !isNext )
                    event = reader.next();
                  else
                    isNext = false;
              }
      }
      

        Activity

        There are no comments yet on this issue.

          People

          • Assignee:
            Dan Diephouse
            Reporter:
            Scott Seixas
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated: