gdaniels    02/01/31 20:12:28

  Modified:    java/src/org/apache/axis/encoding
                        SerializationContextImpl.java
                        SerializationContext.java
               java/src/org/apache/axis MessageContext.java
               java/src/org/apache/axis/encoding/ser ArraySerializer.java
  Log:
  Support serializing Arrays in "literal" fashion (a .NET-ism).
  
  We introduce an "isEncoded" flag on the MessageContext for now - this
  really wants to get set to the encoding state we're in at present, but I
  couldn't figure out how to coax that out of the serialization code yet.
  
  Let's say we have:
  
  class Bean {
    public String field1;
    public String [] field2;
  }
  
  if isEncoded is true (the default), we get XML that looks like:
  
  <bean>
    <field1>hi there</field1>
    <field2 SOAPENC:arrayType="xsd:string[2]">
      <item>1</item>
      <item>2</item>
    </field2>
  </bean>
  
  whereas if isEncoded is false, we now get:
  
  <bean>
    <field1>hi there</field1>
    <field2>1</field2>
    <field2>2</field2>
  </bean>
  
  TODOs:
  
  1) Figure out how to get encodingStyle for real
  2) Implement deserializing from arrays encoded like this
  
  Revision  Changes    Path
  1.4       +8 -1      
xml-axis/java/src/org/apache/axis/encoding/SerializationContextImpl.java
  
  Index: SerializationContextImpl.java
  ===================================================================
  RCS file: 
/home/cvs/xml-axis/java/src/org/apache/axis/encoding/SerializationContextImpl.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- SerializationContextImpl.java     29 Jan 2002 21:31:38 -0000      1.3
  +++ SerializationContextImpl.java     1 Feb 2002 04:12:28 -0000       1.4
  @@ -334,7 +334,14 @@
           return msgContext.getCurrentMessage();
       }
   
  -    /** 
  +    /**
  +     * Get the MessageContext we're operating with
  +     */
  +    public MessageContext getMessageContext() {
  +        return msgContext;
  +    }
  +
  +    /**
        * Convert QName to a string of the form <prefix>:<localpart>
        * @param QName
        * @return prefixed qname representation for serialization.
  
  
  
  1.73      +6 -1      
xml-axis/java/src/org/apache/axis/encoding/SerializationContext.java
  
  Index: SerializationContext.java
  ===================================================================
  RCS file: 
/home/cvs/xml-axis/java/src/org/apache/axis/encoding/SerializationContext.java,v
  retrieving revision 1.72
  retrieving revision 1.73
  diff -u -r1.72 -r1.73
  --- SerializationContext.java 26 Jan 2002 02:40:33 -0000      1.72
  +++ SerializationContext.java 1 Feb 2002 04:12:28 -0000       1.73
  @@ -186,7 +186,12 @@
        */
       public Message getCurrentMessage();
   
  -    /** 
  +    /**
  +     * Get the MessageContext we're operating with
  +     */
  +    public MessageContext getMessageContext();
  +
  +    /**
        * Convert QName to a string of the form <prefix>:<localpart>
        * @param QName
        * @return prefixed qname representation for serialization.
  
  
  
  1.69      +17 -0     xml-axis/java/src/org/apache/axis/MessageContext.java
  
  Index: MessageContext.java
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/MessageContext.java,v
  retrieving revision 1.68
  retrieving revision 1.69
  diff -u -r1.68 -r1.69
  --- MessageContext.java       6 Dec 2001 17:09:40 -0000       1.68
  +++ MessageContext.java       1 Feb 2002 04:12:28 -0000       1.69
  @@ -155,6 +155,12 @@
        * MessageContext.
        */
       private Hashtable bag ;
  +    
  +    /**
  +     * Are we using SOAP encoding?  Default is true for RPC services,
  +     * should be set to false for document/literal.
  +     */ 
  +    private boolean isEncoded = true;
   
       /**
        * Get the active message context. 
  @@ -220,6 +226,17 @@
           this.session = session;
       }
       
  +    /**
  +     * Encoding
  +     */
  +    public boolean isEncoded() {
  +        return isEncoded;
  +    }
  +
  +    public void setEncoded(boolean encoded) {
  +        isEncoded = encoded;
  +    }
  +
       /**
        * Set whether we are maintaining session state
        */
  
  
  
  1.5       +46 -31    
xml-axis/java/src/org/apache/axis/encoding/ser/ArraySerializer.java
  
  Index: ArraySerializer.java
  ===================================================================
  RCS file: 
/home/cvs/xml-axis/java/src/org/apache/axis/encoding/ser/ArraySerializer.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ArraySerializer.java      31 Jan 2002 21:52:19 -0000      1.4
  +++ ArraySerializer.java      1 Feb 2002 04:12:28 -0000       1.5
  @@ -209,38 +209,53 @@
               }
           }
           
  -        AttributesImpl attrs;
  -        if (attributes != null) {
  -            if (attributes instanceof AttributesImpl) {
  -                attrs = (AttributesImpl)attributes;
  +        // Are we encoded?
  +        boolean isEncoded = context.getMessageContext().isEncoded();
  +        
  +        if (isEncoded) {
  +            AttributesImpl attrs;
  +            if (attributes != null) {
  +                if (attributes instanceof AttributesImpl) {
  +                    attrs = (AttributesImpl)attributes;
  +                } else {
  +                    attrs = new AttributesImpl(attributes);
  +                }
               } else {
  -                attrs = new AttributesImpl(attributes);
  +                attrs = new AttributesImpl();
  +            }
  +            
  +            if (attrs.getIndex(Constants.URI_CURRENT_SOAP_ENC,
  +                               Constants.ATTR_ARRAY_TYPE) == -1) {
  +                String encprefix = 
  +                       context.getPrefixForURI(Constants.URI_CURRENT_SOAP_ENC);
  +                attrs.addAttribute(Constants.URI_CURRENT_SOAP_ENC, 
  +                                   Constants.ATTR_ARRAY_TYPE,
  +                                   encprefix + ":arrayType",
  +                                   "CDATA",
  +                                   arrayType);
  +            }
  +            
  +            // Force type to be SOAP_ARRAY for all array serialization.
  +            int typeI = attrs.getIndex(Constants.URI_CURRENT_SCHEMA_XSI,
  +                                       "type");
  +            if (typeI != -1) {
  +                attrs.removeAttribute(typeI);
  +                attributes = context.setTypeAttribute(attrs,
  +                                                      Constants.SOAP_ARRAY);
  +            } else {
  +                attributes = attrs;
               }
  -        } else {
  -            attrs = new AttributesImpl();
  -        }
  -
  -        if (attrs.getIndex(Constants.URI_CURRENT_SOAP_ENC,
  -                           Constants.ATTR_ARRAY_TYPE) == -1) {
  -            String encprefix = 
context.getPrefixForURI(Constants.URI_CURRENT_SOAP_ENC);
  -            attrs.addAttribute(Constants.URI_CURRENT_SOAP_ENC, 
  -                                  Constants.ATTR_ARRAY_TYPE,
  -                                  encprefix + ":arrayType",
  -                                  "CDATA",
  -                                  arrayType);
           }
           
  -        // Force type to be SOAP_ARRAY for all array serialization.
  -        int typeI = attrs.getIndex(Constants.URI_CURRENT_SCHEMA_XSI,
  -                                   "type");
  -        if (typeI != -1) {
  -            attrs.removeAttribute(typeI);
  -            attributes = context.setTypeAttribute(attrs, Constants.SOAP_ARRAY);
  -        } else {
  -            attributes = attrs;
  -        }
  +        // For non-encoded (literal) use, each item is named with the QName
  +        // we got in the arguments.  For encoded, we write an element with
  +        // that QName, and then each item is an <item> inside that.
  +        QName elementName = name;
           
  -        context.startElement(name, attributes);
  +        if (isEncoded) {
  +            context.startElement(name, attributes);
  +            elementName = new QName("","item");
  +        }
   
           if (dim2Len < 0) {
               // Normal case, serialize each array element
  @@ -248,7 +263,7 @@
                   Object aValue = (list == null) ? Array.get(value, index) : 
list.get(index);
                   Class aClass = (aValue == null) ? null : aValue.getClass();
                   
  -                context.serialize(new QName("","item"), null, aValue, aClass);
  +                context.serialize(elementName, null, aValue, aClass);
               }
           } else {
               // Serialize as a 2 dimensional array
  @@ -256,13 +271,13 @@
                   for (int index2 = 0; index2 < dim2Len; index2++) {
                       Object aValue = Array.get(Array.get(value, index), index2);
                       Class aClass = (aValue == null) ? null : aValue.getClass();
  -                    context.serialize(new QName("","item"), null, aValue, aClass);
  +                    context.serialize(elementName, null, aValue, aClass);
                   }
               }
           }
   
  -        
  -        context.endElement();
  +        if (isEncoded)
  +            context.endElement();
       }
   
       public String getMechanismType() { return Constants.AXIS_SAX; }
  
  
  


Reply via email to