You are viewing a plain text version of this content. The canonical link for it is here.
Posted to soap-dev@xml.apache.org by du...@apache.org on 2001/05/18 05:14:44 UTC

cvs commit: xml-soap/java/src/org/apache/soap/encoding/soapenc HashtableSerializer.java MapSerializer.java

duftler     01/05/17 20:14:44

  Modified:    java/src/org/apache/soap/encoding SOAPMappingRegistry.java
               java/src/org/apache/soap/encoding/soapenc MapSerializer.java
  Added:       java/src/org/apache/soap/encoding/soapenc
                        HashtableSerializer.java
  Log:
  Renamed MapSerializer to HashtableSerializer.
  Added a MapSerializer to serialize java.util.Maps. (Works only
    with JDK1.2 and higher.)
  
  Revision  Changes    Path
  1.18      +20 -12    xml-soap/java/src/org/apache/soap/encoding/SOAPMappingRegistry.java
  
  Index: SOAPMappingRegistry.java
  ===================================================================
  RCS file: /home/cvs/xml-soap/java/src/org/apache/soap/encoding/SOAPMappingRegistry.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- SOAPMappingRegistry.java	2001/04/04 22:30:15	1.17
  +++ SOAPMappingRegistry.java	2001/05/18 03:14:42	1.18
  @@ -98,7 +98,7 @@
     private final ParameterSerializer paramSer = new ParameterSerializer();
     private final ArraySerializer arraySer = new ArraySerializer();
     private final VectorSerializer vectorSer = new VectorSerializer();
  -  private final MapSerializer mapSer = new MapSerializer();
  +  private final HashtableSerializer hashtableSer = new HashtableSerializer();
     public static final MimePartSerializer partSer = new MimePartSerializer();
     private final XMLParameterSerializer xmlParamSer =
                                                       new XMLParameterSerializer();
  @@ -334,9 +334,9 @@
   
       try {
         Class XMISerializer = 
  -                           Class.forName("org.apache.soap.util.xml.XMISerializer");
  +        Class.forName("org.apache.soap.util.xml.XMISerializer");
         Class XMIParameterSerializer =
  -                                    Class.forName("org.apache.soap.encoding.xmi.XMIParameterSerializer");
  +        Class.forName("org.apache.soap.encoding.xmi.XMIParameterSerializer");
   
         // Register default serializers for XMI encoding style.
         mapTypes(Constants.NS_URI_XMI_ENC, null, null,
  @@ -346,27 +346,35 @@
         // Register serializer for Parameter class - not deserializer!
         mapTypes(Constants.NS_URI_XMI_ENC, null, Parameter.class,
                  (Serializer)XMIParameterSerializer.newInstance(), null);
  -
       } catch (IllegalAccessException iae) {
       } catch (InstantiationException ie) {
       } catch (ClassNotFoundException cnfe) {
       } catch (NoClassDefFoundError ncdfe) {
  -
  -        // If the class can't be loaded, continue without it...
  -
  +      // If the class can't be loaded, continue without it...
       }
   
  -    /**
  -    * Basic collection types - these should map fine to Perl, Python, C++...
  -    * (but an encoding like this needs to be agreed upon)
  +    /*
  +      Basic collection types - these should map fine to Perl, Python, C++...
  +      (but an encoding like this needs to be agreed upon)
       */
       mapTypes(soapEncURI, new QName(Constants.NS_URI_XML_SOAP, "Vector"),
                Vector.class, vectorSer, vectorSer);
       mapTypes(soapEncURI, new QName(Constants.NS_URI_XML_SOAP, "Map"),
  -             Hashtable.class, mapSer, mapSer);
  +             Hashtable.class, hashtableSer, hashtableSer);
  +
  +    try {
  +      Class mapClass = Class.forName("java.util.Map");
  +      MapSerializer mapSer = new MapSerializer();
  +
  +      mapTypes(soapEncURI, new QName(Constants.NS_URI_XML_SOAP, "Map"),
  +               mapClass, mapSer, mapSer);
  +    } catch (ClassNotFoundException cnfe) {
  +    } catch (NoClassDefFoundError ncdfe) {
  +      // If the class can't be loaded, continue without it...
  +    }
   
       /*
  -    Map a Java byte array to the SOAP-ENC:base64 subtype.
  +      Map a Java byte array to the SOAP-ENC:base64 subtype.
       */
       Base64Serializer base64Ser = new Base64Serializer();
       QName base64QName = new QName(soapEncURI, "base64");
  
  
  
  1.8       +48 -125   xml-soap/java/src/org/apache/soap/encoding/soapenc/MapSerializer.java
  
  Index: MapSerializer.java
  ===================================================================
  RCS file: /home/cvs/xml-soap/java/src/org/apache/soap/encoding/soapenc/MapSerializer.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- MapSerializer.java	2001/05/17 21:08:33	1.7
  +++ MapSerializer.java	2001/05/18 03:14:43	1.8
  @@ -69,147 +69,70 @@
   
   /**
    * A <code>MapSerializer</code> can be used to serialize and
  - * deserialize Hashtables using the <code>SOAP-ENC</code>
  - * encoding style.<p>
  - * 
  - * TODO: This should eventually deal with Maps, but doesn't yet.
  - * 
  - * @author Glen Daniels (gdaniels@allaire.com)
  + * deserialize Maps using the <code>SOAP-ENC</code>
  + * encoding style. This is a quick implementation that simply
  + * delegates to the HashtableSerializer (which used to be
  + * named the MapSerializer).
  + *
  + * @author Matthew J. Duftler (duftler@us.ibm.com)
    */
   public class MapSerializer implements Serializer, Deserializer
   {
  -  private static final String STR_KEY = "key";
  -  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)
  +  private final HashtableSerializer hashtableSer = new HashtableSerializer();
  +
  +  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) && !(src instanceof Hashtable))
  -    {
  -      throw new IllegalArgumentException("Tried to pass a '" +
  -                        src.getClass().toString() + "' to MapSerializer");
  -    }
  +    System.err.println("Delegating to ht.serialize()...");
  +
  +    Hashtable hashtable = null;
   
  -    if (src == null)
  +    if (src instanceof Hashtable)
       {
  -      SoapEncUtils.generateNullStructure(inScopeEncStyle,
  -                                     javaType,
  -                                     context,
  -                                     sink,
  -                                     nsStack,
  -                                     xjmr);
  +      hashtable = (Hashtable)src;
       }
  -    else
  +    else if (src instanceof Map)
       {
  -      SoapEncUtils.generateStructureHeader(inScopeEncStyle,
  -                                       javaType,
  -                                       context,
  -                                       sink,
  -                                       nsStack,
  -                                       xjmr);
  -
  -      sink.write(StringUtils.lineSeparator);
  -
  -      Hashtable hash = (Hashtable)src;
  -
  -      for (Enumeration e = hash.keys(); e.hasMoreElements(); )
  -      {
  -        Object key = e.nextElement();
  -        Object value = hash.get(key);
  -
  -        sink.write("<" + STR_ITEM + ">");
  -        sink.write(StringUtils.lineSeparator);
  -
  -        // ??? Deal with null keys?
  -        xjmr.marshall(inScopeEncStyle, key.getClass(), key, STR_KEY,
  -                      sink, nsStack, ctx);
  -        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 + '>');
  +      hashtable = new Hashtable();
  +      hashtable.putAll((Map)src);
       }
  -  }
  -
  -  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))
  +    else
       {
  -      return new Bean(Hashtable.class, null);
  -    }
  -
  -    Hashtable hash = new Hashtable();
  -    Element tempEl = DOMUtils.getFirstChildElement(root);
  -
  -    while (tempEl != null) {
  -      // got an item
  -      Element keyEl = DOMUtils.getFirstChildElement(tempEl);
  -      String tagName = keyEl.getTagName();
  -
  -      if (!tagName.equalsIgnoreCase(STR_KEY))
  -      {
  -        throw new IllegalArgumentException("Got <" + tagName +
  -                                           "> tag when expecting <" +
  -                                           STR_KEY + ">");
  -      }
  -
  -      Element valEl = DOMUtils.getNextSiblingElement(keyEl);
  -
  -      tagName = valEl.getTagName();
  -
  -      if (!tagName.equalsIgnoreCase("value"))
  -      {
  -        throw new IllegalArgumentException("Got <" + tagName + 
  -                                           "> tag when expecting <" +
  -                                           STR_VALUE + ">");
  -      }
  -
  -      Bean keyBean = unmarshallEl(inScopeEncStyle, xjmr, keyEl, ctx);
  -      Bean valBean = unmarshallEl(inScopeEncStyle, xjmr, valEl, ctx);
  -
  -      hash.put(keyBean.value, valBean.value);
  -
  -      tempEl = DOMUtils.getNextSiblingElement(tempEl);
  +      throw new IllegalArgumentException("Tried to pass a '" +
  +                                         src.getClass().toString() +
  +                                         "' to MapSerializer");
       }
   
  -    return new Bean(Hashtable.class, hash);
  +    hashtableSer.marshall(inScopeEncStyle,
  +                          Hashtable.class,
  +                          hashtable,
  +                          context,
  +                          sink,
  +                          nsStack,
  +                          xjmr,
  +                          ctx);
     }
   
  -  private Bean unmarshallEl(String inScopeEncStyle,
  -                            XMLJavaMappingRegistry xjmr,
  -                            Element targetEl, SOAPContext ctx)
  +  public Bean unmarshall(String inScopeEncStyle,
  +                         QName elementType,
  +                         Node src,
  +                         XMLJavaMappingRegistry xjmr,
  +                         SOAPContext ctx)
  +    throws IllegalArgumentException
     {
  -    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);
  +    System.err.println("Delegating to ht.deserialize()...");
   
  -    return xjmr.unmarshall(actualEncStyle, declItemType, targetEl, ctx);
  +    return hashtableSer.unmarshall(inScopeEncStyle,
  +                                   elementType,
  +                                   src,
  +                                   xjmr,
  +                                   ctx);
     }
   }
  
  
  
  1.1                  xml-soap/java/src/org/apache/soap/encoding/soapenc/HashtableSerializer.java
  
  Index: HashtableSerializer.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 2000 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 "SOAP" 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 and was
   * originally based on software copyright (c) 2000, International
   * Business Machines, Inc., http://www.apache.org.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  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.*;
  
  /**
   * A <code>HashtableSerializer</code> can be used to serialize and
   * deserialize Hashtables using the <code>SOAP-ENC</code>
   * encoding style.<p>
   * 
   * TODO: This should eventually deal with Maps, but doesn't yet.
   * 
   * @author Glen Daniels (gdaniels@allaire.com)
   */
  public class HashtableSerializer implements Serializer, Deserializer
  {
    private static final String STR_KEY = "key";
    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 Hashtable)
      {
        SoapEncUtils.generateStructureHeader(inScopeEncStyle,
                                             javaType,
                                             context,
                                             sink,
                                             nsStack,
                                             xjmr);
  
        sink.write(StringUtils.lineSeparator);
  
        Hashtable hash = (Hashtable)src;
  
        for (Enumeration e = hash.keys(); e.hasMoreElements(); )
        {
          Object key = e.nextElement();
          Object value = hash.get(key);
  
          sink.write("<" + STR_ITEM + ">");
          sink.write(StringUtils.lineSeparator);
  
          // ??? Deal with null keys?
          xjmr.marshall(inScopeEncStyle, key.getClass(), key, STR_KEY,
                        sink, nsStack, ctx);
          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(Hashtable.class, null);
      }
  
      Hashtable hash = new Hashtable();
      Element tempEl = DOMUtils.getFirstChildElement(root);
  
      while (tempEl != null) {
        // got an item
        Element keyEl = DOMUtils.getFirstChildElement(tempEl);
        String tagName = keyEl.getTagName();
  
        if (!tagName.equalsIgnoreCase(STR_KEY))
        {
          throw new IllegalArgumentException("Got <" + tagName +
                                             "> tag when expecting <" +
                                             STR_KEY + ">");
        }
  
        Element valEl = DOMUtils.getNextSiblingElement(keyEl);
  
        tagName = valEl.getTagName();
  
        if (!tagName.equalsIgnoreCase("value"))
        {
          throw new IllegalArgumentException("Got <" + tagName + 
                                             "> tag when expecting <" +
                                             STR_VALUE + ">");
        }
  
        Bean keyBean = unmarshallEl(inScopeEncStyle, xjmr, keyEl, ctx);
        Bean valBean = unmarshallEl(inScopeEncStyle, xjmr, valEl, ctx);
  
        hash.put(keyBean.value, valBean.value);
  
        tempEl = DOMUtils.getNextSiblingElement(tempEl);
      }
  
      return new Bean(Hashtable.class, hash);
    }
  
    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);
    }
  }