Hi,

When using Soap 2.2 I had trouble because there was no Serializer 
for a java.util.Enumeration class. I quickly created one based on 
the HashtableSerializer in the org.apache.soap.encoding.soapenc 
package. The class is 
org.apache.soap.encoding.soapenc.EnumerationSerializer

I have attached it. Please feel free to use it if you find it of 
any use.

Thanks,
Charles

p.s. Great work on SOAP!
 
           

package org.apache.soap.encoding.soapenc;

import java.beans.*;
import java.io.*;
import java.util.*;
import java.lang.reflect.*;
import org.w3c.dom.*;
import org.apache.soap.util.*;
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.rpc.*;
import org.apache.soap.encoding.soapenc.*;

/**
 * A <code>EnumerationSerializer</code> can be used to serialize and
 * deserialize Enumerations using the <code>SOAP-ENC</code>
 * encoding style.<p>
 *
 *
 * @author Charles Mendis ([EMAIL PROTECTED])
 */

public class EnumerationSerializer implements Serializer, Deserializer
{
  private static final String STR_ITEM = "item";
  private static final String STR_VALUE = "value";

  public void marshall(String inScopeEncStyle, Class javaType, Object src,
                       Object context, Writer sink, NSStack nsStack,
                       XMLJavaMappingRegistry xjmr, SOAPContext ctx)
    throws IllegalArgumentException, IOException
  {
    if (src == null)
    {
      SoapEncUtils.generateNullStructure(inScopeEncStyle,
                                         javaType,
                                         context,
                                         sink,
                                         nsStack,
                                         xjmr);
    }
    else if (src instanceof Enumeration)
    {
      SoapEncUtils.generateStructureHeader(inScopeEncStyle,
                                           javaType,
                                           context,
                                           sink,
                                           nsStack,
                                           xjmr);

      sink.write(StringUtils.lineSeparator);

      for (Enumeration e = (Enumeration)src; e.hasMoreElements(); )
      {
        Object value = e.nextElement();

        sink.write("<" + STR_ITEM + ">");
        sink.write(StringUtils.lineSeparator);

        if (value == null)
        {
          SoapEncUtils.generateNullStructure(inScopeEncStyle, Object.class,
                                             STR_VALUE, sink, nsStack, xjmr);
        }
        else
        {
          Class actualComponentType = value.getClass();

          xjmr.marshall(inScopeEncStyle, actualComponentType, value, STR_VALUE,
                        sink, nsStack, ctx);
        }

        sink.write(StringUtils.lineSeparator);
        sink.write("</" + STR_ITEM + ">");
        sink.write(StringUtils.lineSeparator);
      }

      sink.write("</" + context + '>');
    }
    else
    {
      throw new IllegalArgumentException("Tried to pass a '" +
                                         src.getClass().toString() +
                                         "' to HashtableSerializer");
    }
  }

  public Bean unmarshall(String inScopeEncStyle, QName elementType, Node src,
                         XMLJavaMappingRegistry xjmr, SOAPContext ctx)
    throws IllegalArgumentException
  {
    Element root = (Element)src;
    String name = root.getTagName();

    if (SoapEncUtils.isNull(root))
    {
      return new Bean(Enumeration.class, null);
    }

    Hashtable hash = new Hashtable();
    Element tempEl = DOMUtils.getFirstChildElement(root);

    while (tempEl != null) {
      // got an item
      Element valEl = DOMUtils.getFirstChildElement(tempEl);
      String tagName = valEl.getTagName();

      if (!tagName.equalsIgnoreCase("value"))
      {
        throw new IllegalArgumentException("Got <" + tagName +
                                           "> tag when expecting <" +
                                           STR_VALUE + ">");
      }

      Bean valBean = unmarshallEl(inScopeEncStyle, xjmr, valEl, ctx);

      hash.put(valBean.value, valBean.value);

      tempEl = DOMUtils.getNextSiblingElement(tempEl);
    }

    return new Bean(Enumeration.class, hash.elements());
  }

  private Bean unmarshallEl(String inScopeEncStyle,
                            XMLJavaMappingRegistry xjmr,
                            Element targetEl, SOAPContext ctx)
  {
    String declEncStyle = DOMUtils.getAttributeNS(targetEl,
        Constants.NS_URI_SOAP_ENV, Constants.ATTR_ENCODING_STYLE);
    String actualEncStyle = (declEncStyle != null)
                            ? declEncStyle
                            : inScopeEncStyle;
    QName declItemType = SoapEncUtils.getTypeQName(targetEl);

    return xjmr.unmarshall(actualEncStyle, declItemType, targetEl, ctx);
  }
}

Reply via email to