View Javadoc

    1   package org.codehaus.xfire.aegis.yom;
    2
    3   import javax.xml.namespace.QName;
    4   import javax.xml.stream.XMLStreamWriter;
    5
    6   import org.codehaus.xfire.aegis.AbstractMessageWriter;
    7   import org.codehaus.xfire.aegis.MessageWriter;
    8   import org.codehaus.xfire.util.NamespaceHelper;
    9   import org.codehaus.yom.Attribute;
    10  import org.codehaus.yom.Element;
    11
    12  public class YOMWriter
    13      extends AbstractMessageWriter
    14  {
    15      private Element element;
    16
    17      public YOMWriter(Element element)
    18      {
    19          this.element = element;
    20      }
    21
    22      public void writeValue(Object value)
    23      {
    24          element.appendChild(value.toString());
    25      }
    26
    27      public void writeValue(Object value, String ns, String attr)
    28      {
    29          element.addAttribute(new Attribute(attr, ns, value.toString()));
    30      }
    31
    32      public MessageWriter getElementWriter(String name)
    33      {
    34          return getElementWriter(name, element.getNamespaceURI());
    35      }
    36
    37      public MessageWriter getElementWriter(String name, String namespace)
    38      {
    39          String prefix = NamespaceHelper.getUniquePrefix(element, namespace);
    40
    41          Element child = new Element(prefix + ":" + name, namespace);
    42          element.appendChild(child);
    43
    44          return new YOMWriter(child);
    45      }
    46
    47      public MessageWriter getElementWriter(QName qname)
    48      {
    49          String name = qname.getLocalPart();
    50          if (qname.getPrefix().length() > 0)
    51          {
    52              name = qname.getPrefix() + ":" + name;
    53          }
    54          else
    55          {
    56              String prefix = NamespaceHelper.getUniquePrefix(element, qname.getNamespaceURI());
    57              name = prefix + ":" + name;
    58          }
    59
    60          Element child = new Element(name, qname.getNamespaceURI());
    61          element.appendChild(child);
    62
    63          return new YOMWriter(child);
    64      }
    65
    66      public XMLStreamWriter getXMLStreamWriter()
    67      {
    68          throw new UnsupportedOperationException("Stream writing not supported from a YOMWriter.");
    69      }
    70
    71      public MessageWriter getAttributeWriter(String name)
    72      {
    73          Attribute att = new Attribute(element.getNamespacePrefix() + ":" + name,
    74                                        element.getNamespaceURI(),
    75                                        "");
    76          element.addAttribute(att);
    77          return new AttributeWriter(att);
    78      }
    79
    80      public MessageWriter getAttributeWriter(String name, String namespace)
    81      {
    82          Attribute att = null;
    83          if (namespace != null && namespace.length() > 0)
    84          {
    85              String prefix = NamespaceHelper.getUniquePrefix(element, namespace);
    86              att = new Attribute(prefix + ":" + name, namespace, "");
    87          }
    88          else
    89          {
    90              att = new Attribute(name, "");
    91          }
    92
    93          element.addAttribute(att);
    94          return new AttributeWriter(att);
    95      }
    96
    97      public MessageWriter getAttributeWriter(QName qname)
    98      {
    99          return getAttributeWriter(qname.getLocalPart(), qname.getNamespaceURI());
    100     }
    101
    102     public void close()
    103     {
    104     }
    105 }