View Javadoc

    1   package org.codehaus.xfire.util;
    2
    3   import javax.xml.stream.XMLStreamException;
    4   import javax.xml.stream.XMLStreamWriter;
    5
    6   import org.codehaus.xfire.MessageContext;
    7   import org.codehaus.xfire.exchange.InMessage;
    8   import org.codehaus.xfire.exchange.MessageSerializer;
    9   import org.codehaus.xfire.exchange.OutMessage;
    10  import org.codehaus.xfire.fault.XFireFault;
    11  import org.codehaus.yom.Document;
    12  import org.codehaus.yom.stax.StaxBuilder;
    13  import org.codehaus.yom.stax.StaxSerializer;
    14
    15  /***
    16   * Reads/writes YOM documents to the body of a message.
    17   *
    18   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
    19   */
    20  public class YOMSerializer
    21      implements MessageSerializer
    22  {
    23      public void readMessage(InMessage message, MessageContext context)
    24          throws XFireFault
    25      {
    26          StaxBuilder builder = new StaxBuilder();
    27          try
    28          {
    29              Document doc = builder.build(message.getXMLStreamReader());
    30              message.setBody(doc);
    31          }
    32          catch (XMLStreamException e)
    33          {
    34              throw new XFireFault("Couldn't parse message.", e, XFireFault.SENDER);
    35          }
    36      }
    37
    38      public void writeMessage(OutMessage message, XMLStreamWriter writer, MessageContext context)
    39          throws XFireFault
    40      {
    41          Document doc = (Document) message.getBody();
    42          StaxSerializer serializer = new StaxSerializer();
    43
    44          try
    45          {
    46              serializer.writeElement(doc.getRootElement(), writer);
    47          }
    48          catch (XMLStreamException e)
    49          {
    50              throw new XFireFault("Couldn't write message.", e, XFireFault.RECEIVER);
    51          }
    52      }
    53
    54  }