axis-java-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gdani...@apache.org
Subject cvs commit: xml-axis/java/src/org/apache/axis/encoding MapSerializer.java Deserializer.java SOAPTypeMappingRegistry.java TypeMappingRegistry.java
Date Sun, 29 Jul 2001 13:43:25 GMT
gdaniels    01/07/29 06:43:25

  Modified:    java/src/org/apache/axis/encoding Deserializer.java
                        SOAPTypeMappingRegistry.java
                        TypeMappingRegistry.java
  Added:       java/src/org/apache/axis/encoding MapSerializer.java
  Log:
  Serialization / Deserialization for Maps.
  
  Right now serializes both Hashtables and HashMaps, but only Deserializes
  to HashMap.
  
  It would be kind of cool in the future to be able to serialize an Interface
  rather than inidvidual classes, but the lookup might be a bit expensive
  if we have to use reflection and walk the interface list for the object.
  
  Revision  Changes    Path
  1.9       +2 -1      xml-axis/java/src/org/apache/axis/encoding/Deserializer.java
  
  Index: Deserializer.java
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/encoding/Deserializer.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- Deserializer.java	2001/07/21 15:36:01	1.8
  +++ Deserializer.java	2001/07/29 13:43:24	1.9
  @@ -377,6 +377,7 @@
               // values for the later multiRefs just drop into the existing
               // object in place.
           }
  -        valueComplete();
  +        if (value != null)
  +            valueComplete();
       }
   }
  
  
  
  1.32      +9 -0      xml-axis/java/src/org/apache/axis/encoding/SOAPTypeMappingRegistry.java
  
  Index: SOAPTypeMappingRegistry.java
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/encoding/SOAPTypeMappingRegistry.java,v
  retrieving revision 1.31
  retrieving revision 1.32
  diff -u -r1.31 -r1.32
  --- SOAPTypeMappingRegistry.java	2001/07/18 22:17:52	1.31
  +++ SOAPTypeMappingRegistry.java	2001/07/29 13:43:25	1.32
  @@ -93,6 +93,8 @@
       public static final QName SOAP_BYTE = new QName(Constants.URI_SOAP_ENC, "byte");
       public static final QName SOAP_ARRAY = new QName(Constants.URI_SOAP_ENC, "Array");
   
  +    public static final QName TYPE_MAP = new QName("http://xml.apache.org/xml-soap", "Map");
  +
       public static       QName XSD_DATE;
       
       static {
  @@ -286,6 +288,13 @@
   
           // !!! Seems a little weird to pass a null class here...?
           addDeserializerFactory(SOAP_ARRAY, null, ArraySerializer.factory);
  +        
  +        addSerializer(java.util.Hashtable.class, TYPE_MAP,
  +                      new MapSerializer());
  +        addSerializer(java.util.HashMap.class, TYPE_MAP,
  +                      new MapSerializer());
  +        addDeserializerFactory(TYPE_MAP, java.util.HashMap.class,
  +                               MapSerializer.factory);
       }
       
       private static SOAPTypeMappingRegistry singleton = null;
  
  
  
  1.21      +3 -0      xml-axis/java/src/org/apache/axis/encoding/TypeMappingRegistry.java
  
  Index: TypeMappingRegistry.java
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/encoding/TypeMappingRegistry.java,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- TypeMappingRegistry.java	2001/07/15 16:40:05	1.20
  +++ TypeMappingRegistry.java	2001/07/29 13:43:25	1.21
  @@ -174,6 +174,9 @@
       }
       
       public Deserializer getDeserializer(QName qname) {
  +        if (qname == null)
  +            return null;
  +        
           if (d != null) {
               DeserializerDescriptor desc = (DeserializerDescriptor)d.get(qname);
               if ((desc != null) && (desc.factory != null))
  
  
  
  1.1                  xml-axis/java/src/org/apache/axis/encoding/MapSerializer.java
  
  Index: MapSerializer.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 java.util.*;
  import java.io.IOException;
  import org.xml.sax.*;
  
  import org.apache.axis.message.SOAPHandler;
  import org.apache.axis.utils.*;
  
  /**
   * A <code>MapSerializer</code> is be used to serialize and
   * deserialize Maps using the <code>SOAP-ENC</code>
   * encoding style.<p>
   * 
   * @author Glen Daniels (gdaniels@macromedia.com)
   */
  public class MapSerializer extends Deserializer implements Serializer
  {
      private static final boolean DEBUG_LOG = false;
      
      // QNames we deal with
      private static final QName QNAME_KEY = new QName("","key");
      private static final QName QNAME_ITEM = new QName("", "item");
      private static final QName QNAME_VALUE = new QName("", "value");
  
      // Fixed objects to act as hints to the valueReady() callback
      public static final Object KEYHINT = new Object();
      public static final Object VALHINT = new Object();
      
      // Our static deserializer factory
      public static class Factory implements DeserializerFactory {
          public Deserializer getDeserializer(Class cls) {
              return new MapSerializer();
          }
      }
      public static DeserializerFactory factory = new Factory();
  
      /** Serialize a Map
       * 
       * Walk the collection of keys, serializing each key/value pair
       * inside an <item> element.
       * 
       * @param name the desired QName for the element
       * @param attributes the desired attributes for the element
       * @param value the Object to serialize
       * @param context the SerializationContext in which to do all this
       * @exception IOException
       */
      public void serialize(QName name, Attributes attributes,
                            Object value, SerializationContext context)
          throws IOException
      {
          if (!(value instanceof Map))
              throw new IOException("MapSerializer: " +
                   value.getClass().getName() + " is not a Map");
          
          Map map = (Map)value;
          
          context.startElement(name, attributes);
  
          for (Iterator i = map.keySet().iterator(); i.hasNext(); )
          {
              Object key = i.next();
              Object val = map.get(key);
  
              context.startElement(QNAME_ITEM, null);
  
              context.serialize(QNAME_KEY, null, key);
              context.serialize(QNAME_VALUE, null, val);
  
              context.endElement();
          }
  
          context.endElement();
      }
      
      /** A deserializer for an <item>.  Handles getting the key and
       * value objects from their own deserializers, and then putting
       * the values into the HashMap we're building.
       * 
       */
      class ItemHandler extends Deserializer implements ValueReceiver {
          Object key;
          Object myValue;
          int numSet = 0;
          
          /** Callback from our deserializers.  The hint indicates
           * whether the passed "val" argument is the key or the value
           * for this mapping.
           */
          public void valueReady(Object val, Object hint)
          {
              if (hint == KEYHINT) {
                  key = val;
              } else if (hint == VALHINT) {
                  myValue = val;
              } else {
                  return;
              }
              numSet++;
              if (numSet == 2)
                  doPut(key, myValue);
          }
          
          public SOAPHandler onStartChild(String namespace,
                                      String localName,
                                      String prefix,
                                      Attributes attributes,
                                      DeserializationContext context)
          throws SAXException
          {
              QName typeQName = context.getTypeFromAttributes(namespace,
                                                              localName,
                                                              attributes);
              Deserializer dser = context.getTypeMappingRegistry().
                                   getDeserializer(typeQName);
              if (dser == null)
                  dser = new Deserializer();
              
              if (localName.equals("key")) {
                  dser.registerCallback(this, KEYHINT);
              } else if (localName.equals("value")) {
                  dser.registerCallback(this, VALHINT);
              } else {
                  // Do nothing
              }
              
              return dser;
          }
      }
      
      public void onStartElement(String namespace, String localName,
                               String qName, Attributes attributes,
                               DeserializationContext context)
          throws SAXException
      {
          if (DEBUG_LOG) {
              System.err.println("In MapSerializer.startElement()");
          }
          
          value = new HashMap();
          
          if (DEBUG_LOG) {
              System.err.println("Out MapSerializer.startElement()");
          }
      }
      
      public SOAPHandler onStartChild(String namespace,
                                      String localName,
                                      String prefix,
                                      Attributes attributes,
                                      DeserializationContext context)
          throws SAXException
      {
          if (DEBUG_LOG) {
              System.err.println("In MapSerializer.onStartChild()");
          }
  
          if (!localName.equals("item"))
              throw new SAXException("Only 'item' elements are allowed in a Map!");
          
          return new ItemHandler();
      }
          
      protected void doPut(Object key, Object value)
      {
          ((Map)this.value).put(key, value);
      }
  }
  
  
  

Mime
View raw message