axis-java-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bu...@apache.org
Subject cvs commit: xml-axis/java/src/org/apache/axis/encoding EnumSerializer.java BeanSerializer.java
Date Thu, 01 Nov 2001 16:49:59 GMT
butek       01/11/01 08:49:59

  Modified:    java/src/org/apache/axis/encoding BeanSerializer.java
  Added:       java/src/org/apache/axis/encoding EnumSerializer.java
  Log:
  Make enumerated types work in the runtime.
  
  Submitted by Richard Scheuerle.
  
  Revision  Changes    Path
  1.21      +39 -3     xml-axis/java/src/org/apache/axis/encoding/BeanSerializer.java
  
  Index: BeanSerializer.java
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/encoding/BeanSerializer.java,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- BeanSerializer.java	2001/10/30 16:46:37	1.20
  +++ BeanSerializer.java	2001/11/01 16:49:58	1.21
  @@ -94,6 +94,7 @@
        * Property Descriptors.  Retrieved once and cached in the serializer.
        */
       private PropertyDescriptor[] pd = null;
  +    private EnumSerializer enumSerializer = null;
   
       protected PropertyDescriptor[] getPd() {
           if (pd==null) {
  @@ -103,6 +104,10 @@
                  // this should never happen
                  throw new NullPointerException(e.toString());
               }
  +            // If this is an enumeration class, delegate all serialization to the enum
serializer
  +            if (isEnumClass(cls)) {
  +                enumSerializer = new EnumSerializer(cls);
  +            }
           }
   
           return pd;
  @@ -119,6 +124,28 @@
       }
   
       /**
  +     * Determine if the class is a JAX-RPC enum class.
  +     * An enumeration class is recognized by
  +     * a getValue() method, a fromValue(type) method and the lack
  +     * of a setValue(type) method
  +     */
  +    protected static boolean isEnumClass(Class cls) {
  +        try {
  +            java.lang.reflect.Method m = cls.getMethod("getValue", null);
  +            if (m != null &&
  +                cls.getMethod("fromValue", new Class[] {m.getReturnType()}) != null) {
  +                try {
  +                    if (cls.getMethod("setValue",  new Class[] {m.getReturnType()}) ==
null)
  +                        return true;
  +                    return false;
  +                } catch (java.lang.NoSuchMethodException e) {
  +                    return true;  // getValue & fromValue exist.  setValue does not
exist.  Thus return true. 
  +                }
  +            }
  +        } catch (java.lang.NoSuchMethodException e) {}
  +        return false;
  +    }  
  +    /**
        * Default constructor.
        */
       public BeanSerializer() {
  @@ -150,11 +177,10 @@
        */
       public static class BeanSerFactory implements DeserializerFactory {
           private Hashtable propertyDescriptors = new Hashtable();
  -
  +      
           public Deserializer getDeserializer(Class cls) {
               PropertyDescriptor [] pd =
                     (PropertyDescriptor [])propertyDescriptors.get(cls);
  -
               if (pd == null) {
                 try {
                   pd = Introspector.getBeanInfo(cls).getPropertyDescriptors();
  @@ -164,6 +190,11 @@
                   propertyDescriptors.put(cls, pd);
               }
   
  +            // If an enum class.  Return the Deserializer for Enumeration
  +            if (isEnumClass(cls)) {
  +                return EnumSerializer.getFactory().getDeserializer(cls);
  +            }                
  +
               BeanSerializer bs = new BeanSerializer();
               bs.setCls(cls);
               bs.setPd(pd);
  @@ -275,8 +306,13 @@
                             Object value, SerializationContext context)
           throws IOException
       {
  -        context.startElement(name, attributes);
           PropertyDescriptor[] pd = getPd(value);
  +        if (enumSerializer != null) {
  +          enumSerializer.serialize(name, attributes, value, context);
  +          return;
  +        }
  +
  +        context.startElement(name, attributes);
   
           try {
               for (int i=0; i<pd.length; i++) {
  
  
  
  1.1                  xml-axis/java/src/org/apache/axis/encoding/EnumSerializer.java
  
  Index: EnumSerializer.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Axis" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.axis.encoding;
  
  import org.xml.sax.Attributes;
  
  import javax.xml.rpc.namespace.QName;
  import java.io.IOException;
  import java.io.Serializable;
  /**
   * Serializer for a JAX-RPC enum.
   *
   * @author Rich Scheuerle <scheu@us.ibm.com>
   */
  public class EnumSerializer implements Serializer, Serializable {
  
      private Class cls;
      private java.lang.reflect.Method getValueMethod = null;
      public EnumSerializer(Class cls) {
          super();
          this.cls = cls;
      }
  
     /** 
       * Serialize an enumeration
       */
      public void serialize(QName name, Attributes attributes,
                            Object value, SerializationContext context)
          throws IOException
      {
          context.startElement(name, attributes);
  
          // Invoke the getValue method on the enumeration class and
          // write out the result as a string.
          try {
              if (getValueMethod == null) {
                  getValueMethod = cls.getMethod("getValue", null);
              }
              Object propValue = getValueMethod.invoke(value, null);
              context.writeString(propValue.toString());
          } catch (Exception e) {
              e.printStackTrace();
              throw new IOException(e.toString());
          }
  
          context.endElement();
      }
  
      public static DeserializerFactory getFactory()
      {
        return new EnumDeserializerFactory();
      }
  
      public static class EnumDeserializer extends SOAPTypeMappingRegistry.BasicDeser {
          java.lang.reflect.Method fromValueMethod;
          public EnumDeserializer(Class cls)
          {
              try {
                  java.lang.reflect.Method m = cls.getMethod("getValue", null);
                  fromValueMethod = cls.getMethod("fromValue", new Class[] {m.getReturnType()});
              } catch (Exception e) {
                  e.printStackTrace();
                  throw new NullPointerException(e.toString());
              }
          }
          
          public Object makeValue(String source) throws Exception
          {
              // Invoke the fromValue static method to get the Enumeration value
              return fromValueMethod.invoke(null,new Object [] { source });
          }
      }
  
      static public class EnumDeserializerFactory implements DeserializerFactory {
          public Deserializer getDeserializer(Class cls) {return new EnumDeserializer(cls);}
      }
  }
  
  
  
  

Mime
View raw message