View Javadoc

    1   package org.codehaus.xfire.aegis.stax;
    2
    3   import java.io.OutputStream;
    4
    5   import javax.xml.namespace.QName;
    6   import javax.xml.stream.XMLOutputFactory;
    7   import javax.xml.stream.XMLStreamException;
    8   import javax.xml.stream.XMLStreamWriter;
    9
    10  import org.codehaus.xfire.XFireRuntimeException;
    11  import org.codehaus.xfire.aegis.AbstractMessageWriter;
    12  import org.codehaus.xfire.aegis.MessageWriter;
    13  import org.codehaus.xfire.util.NamespaceHelper;
    14
    15  /***
    16   * LiteralWriter
    17   *
    18   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
    19   */
    20  public class ElementWriter
    21      extends AbstractMessageWriter
    22      implements MessageWriter
    23  {
    24      private XMLStreamWriter writer;
    25
    26      private String namespace;
    27
    28      private String name;
    29
    30      private String prefix;
    31
    32      /***
    33       * Create a LiteralWriter but without writing an element name.
    34       *
    35       * @param writer
    36       */
    37      public ElementWriter(XMLStreamWriter writer)
    38      {
    39          this.writer = writer;
    40      }
    41
    42      public ElementWriter(XMLStreamWriter writer, String name, String namespace)
    43      {
    44          this(writer, name, namespace, null);
    45      }
    46
    47      public ElementWriter(XMLStreamWriter streamWriter, QName name)
    48      {
    49          this(streamWriter, name.getLocalPart(), name.getNamespaceURI());
    50      }
    51
    52      public ElementWriter(XMLStreamWriter writer, String name, String namespace, String prefix)
    53      {
    54          this.writer = writer;
    55          this.namespace = namespace;
    56          this.name = name;
    57          this.prefix = prefix;
    58
    59          try
    60          {
    61              writeStartElement();
    62          }
    63          catch (XMLStreamException e)
    64          {
    65              throw new XFireRuntimeException("Error writing document.", e);
    66          }
    67      }
    68
    69      /***
    70       * @param os
    71       * @throws XMLStreamException
    72       */
    73      public ElementWriter(OutputStream os, String name, String namespace)
    74          throws XMLStreamException
    75      {
    76          XMLOutputFactory ofactory = XMLOutputFactory.newInstance();
    77          this.writer = ofactory.createXMLStreamWriter(os);
    78
    79          this.namespace = namespace;
    80          this.name = name;
    81
    82          try
    83          {
    84              writeStartElement();
    85          }
    86          catch ( XMLStreamException e )
    87          {
    88              throw new XFireRuntimeException("Error writing document.", e);
    89          }
    90      }
    91
    92      private void writeStartElement()
    93          throws XMLStreamException
    94      {
    95          if (namespace != null)
    96          {
    97              boolean declare = false;
    98
    99              String decPrefix = writer.getPrefix(namespace);
    100
    101             // If the user didn't specify a prefix, create one
    102             if (prefix == null && decPrefix == null)
    103             {
    104                 declare = true;
    105                 prefix = NamespaceHelper.getUniquePrefix(writer);
    106             }
    107             else if (prefix == null)
    108             {
    109                 prefix = decPrefix;
    110             }
    111             else if (!prefix.equals(decPrefix))
    112             {
    113                 declare = true;
    114             }
    115
    116             writer.writeStartElement(prefix, name, namespace);
    117
    118             if (declare)
    119             {
    120                 writer.setPrefix(prefix, namespace);
    121                 writer.writeNamespace(prefix, namespace);
    122             }
    123         }
    124         else
    125         {
    126             writer.writeStartElement(name);
    127         }
    128     }
    129
    130     /***
    131      * @see org.codehaus.xfire.aegis.MessageWriter#writeValue(java.lang.Object)
    132      */
    133     public void writeValue(Object value)
    134     {
    135         try
    136         {
    137             if ( value != null )
    138                 writer.writeCharacters( value.toString() );
    139         }
    140         catch ( XMLStreamException e )
    141         {
    142             throw new XFireRuntimeException("Error writing document.", e);
    143         }
    144     }
    145
    146     /***
    147      * @see org.codehaus.xfire.aegis.MessageWriter#getWriter(java.lang.String)
    148      */
    149     public MessageWriter getElementWriter(String name)
    150     {
    151         return new ElementWriter(writer, name, namespace);
    152     }
    153
    154     public MessageWriter getElementWriter(String name, String ns)
    155     {
    156         return new ElementWriter(writer, name, ns);
    157     }
    158
    159     public MessageWriter getElementWriter(QName qname)
    160     {
    161         return new ElementWriter(writer,
    162                                  qname.getLocalPart(),
    163                                  qname.getNamespaceURI(),
    164                                  qname.getPrefix());
    165     }
    166
    167     public String getNamespace()
    168     {
    169         return namespace;
    170     }
    171
    172     public void close()
    173     {
    174         try
    175         {
    176             writer.writeEndElement();
    177         }
    178         catch ( XMLStreamException e )
    179         {
    180             throw new XFireRuntimeException("Error writing document.", e);
    181         }
    182     }
    183
    184     public void flush() throws XMLStreamException
    185     {
    186         writer.flush();
    187     }
    188
    189     public XMLStreamWriter getXMLStreamWriter()
    190     {
    191         return writer;
    192     }
    193
    194     public MessageWriter getAttributeWriter(String name)
    195     {
    196         return new AttributeWriter(writer, name, namespace);
    197     }
    198
    199     public MessageWriter getAttributeWriter(String name, String namespace)
    200     {
    201         return new AttributeWriter(writer, name, namespace);
    202     }
    203
    204     public MessageWriter getAttributeWriter(QName qname)
    205     {
    206         return new AttributeWriter(writer, qname.getLocalPart(), qname.getLocalPart());
    207     }
    208 }