I've written a simple class to handle serialization of Java 5 enums, I
wonder if this is planned for a future release.  In any case, here's the
code I'm using in case it's helpful to someone else:

import java.io.IOException;
import java.lang.reflect.Method;

import javax.xml.namespace.QName;

import org.apache.axis.components.logger.LogFactory;
import org.apache.axis.encoding.SerializationContext;
import org.apache.axis.encoding.ser.SimpleSerializer;
import org.apache.axis.utils.Messages;
import org.apache.axis.wsdl.fromJava.Types;
import org.apache.commons.logging.Log;
import org.w3c.dom.Element;
import org.xml.sax.Attributes;

public class EnumSerializer extends SimpleSerializer {

   protected static Log log =
       LogFactory.getLog(EnumSerializer.class.getName());

   private Method toStringMethod = null;

   public EnumSerializer(Class javaType, QName xmlType) {
       super(javaType, xmlType);
   }

   /**
    * Serialize an enumeration
    */
   public void serialize(QName name, Attributes attributes,
                         Object value, SerializationContext context)
       throws IOException
   {
       context.startElement(name, attributes);
       context.writeString(getValueAsString(value, context));
       context.endElement();
   }

   public String getValueAsString(Object value, SerializationContext
context) {
       // Invoke the toString method on the enumeration class and
       // write out the result as a string.
       try {
           if (toStringMethod == null) {
               toStringMethod = javaType.getMethod("toString",
(Class[])null);
           }
           return (String) toStringMethod.invoke(value, (Object[])null);
       } catch (Exception e) {
           log.error(Messages.getMessage("exception00"), e);
       }
       return null;
   }

   /**
    * Return XML schema for the specified type, suitable for insertion into
    * the <types> element of a WSDL document, or underneath an
    * <element> or <attribute> declaration.
    *
    * @param javaType the Java Class we're writing out schema for
    * @param types the Java2WSDL Types object which holds the context
    *              for the WSDL being generated.
    * @return a type element containing a schema simpleType/complexType
    * @see org.apache.axis.wsdl.fromJava.Types
    */
   public Element writeSchema(Class javaType, Types types) throws
Exception {
       Element simpleType = types.createElement("simpleType");
       Element restriction = types.createElement("restriction");
       restriction.setAttribute("base", types.writeType(String.class));
       simpleType.appendChild(restriction);
       Object[] enums = javaType.getEnumConstants();
       for (Object o : enums) {
           if (javaType.isInstance(o)) {
               Element enumeration = types.createElement("enumeration");
               enumeration.setAttribute("value", o.toString());
               restriction.appendChild(enumeration);
           }
       }
       return simpleType;
   }
}

===============================================================================

import java.beans.IntrospectionException;
import java.lang.reflect.Method;

import javax.xml.namespace.QName;

import org.apache.axis.encoding.ser.SimpleDeserializer;

/**
* Deserializer for a JAVA 5 enum.
*
*/
public class EnumDeserializer extends SimpleDeserializer {

   private static final long serialVersionUID = 7550697103681899189L;
   private Method valueOfMethod = null;

   public EnumDeserializer(Class javaType, QName xmlType) {
       super(javaType, xmlType);
   }

   public Object makeValue(String source) throws Exception
   {
       // Invoke the fromString static method to get the Enumeration value
       if (isNil)
           return null;
       if (valueOfMethod == null) {
           try {
               valueOfMethod = javaType.getMethod("valueOf", new
Class[] {source.getClass()});
           } catch (Exception e) {
               throw new IntrospectionException(e.toString());
           }
       }
       return valueOfMethod.invoke(null,new Object [] { source });
   }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to