View Javadoc

    1   package org.codehaus.xfire.aegis.type.basic;
    2
    3   import javax.xml.parsers.DocumentBuilder;
    4   import javax.xml.parsers.DocumentBuilderFactory;
    5   import javax.xml.parsers.ParserConfigurationException;
    6   import javax.xml.stream.XMLStreamException;
    7   import javax.xml.stream.XMLStreamReader;
    8
    9   import org.codehaus.xfire.MessageContext;
    10  import org.codehaus.xfire.XFireRuntimeException;
    11  import org.codehaus.xfire.aegis.MessageReader;
    12  import org.codehaus.xfire.aegis.MessageWriter;
    13  import org.codehaus.xfire.aegis.stax.ElementReader;
    14  import org.codehaus.xfire.aegis.stax.ElementWriter;
    15  import org.codehaus.xfire.aegis.type.Type;
    16  import org.codehaus.xfire.fault.XFireFault;
    17  import org.codehaus.xfire.util.STAXUtils;
    18  import org.codehaus.xfire.util.stax.FragmentStreamReader;
    19  import org.w3c.dom.Document;
    20
    21  /***
    22   * Reads and writes <code>org.w3c.dom.Document</code> types.
    23   *
    24   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
    25   */
    26  public class DocumentType
    27      extends Type
    28  {
    29      private DocumentBuilder builder;
    30
    31      public DocumentType()
    32      {
    33          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    34          try
    35          {
    36              builder = factory.newDocumentBuilder();
    37          }
    38          catch (ParserConfigurationException e)
    39          {
    40             throw new XFireRuntimeException("Couldn't load document builder.", e);
    41          }
    42      }
    43
    44      public DocumentType(DocumentBuilder builder)
    45      {
    46          this.builder = builder;
    47      }
    48
    49      public Object readObject(MessageReader mreader, MessageContext context)
    50          throws XFireFault
    51      {
    52          try
    53          {
    54              XMLStreamReader reader = ((ElementReader) mreader).getXMLStreamReader();
    55              return STAXUtils.read(builder, new FragmentStreamReader(reader));
    56          }
    57          catch (XMLStreamException e)
    58          {
    59              throw new XFireFault("Could not parse xml.", e, XFireFault.SENDER);
    60          }
    61      }
    62
    63      public void writeObject(Object object, MessageWriter writer, MessageContext context)
    64          throws XFireFault
    65      {
    66          Document doc = (Document) object;
    67
    68          try
    69          {
    70              STAXUtils.writeElement(doc.getDocumentElement(),
    71                                     ((ElementWriter) writer).getXMLStreamWriter());
    72          }
    73          catch (XMLStreamException e)
    74          {
    75              throw new XFireFault("Could not write xml.", e, XFireFault.SENDER);
    76          }
    77      }
    78  }