You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by gd...@apache.org on 2001/08/23 00:55:10 UTC

cvs commit: xml-axis/java/test/encoding TestXsiType.java PackageTests.java

gdaniels    01/08/22 15:55:10

  Modified:    java/src/org/apache/axis/encoding SOAPEncoding.java
                        SerializationContext.java TypeMappingRegistry.java
               java/test/encoding PackageTests.java
  Added:       java/test/encoding TestXsiType.java
  Log:
  Simplify SOAPEncoding (since type attrs are added by the
  TypeMappingRegistry already), enable switching the sending
  of xsi:type attributes on and off via the ServiceDescription,
  and add a unit test for same.
  
  Revision  Changes    Path
  1.8       +2 -47     xml-axis/java/src/org/apache/axis/encoding/SOAPEncoding.java
  
  Index: SOAPEncoding.java
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/encoding/SOAPEncoding.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- SOAPEncoding.java	2001/07/08 14:18:37	1.7
  +++ SOAPEncoding.java	2001/08/22 22:55:10	1.8
  @@ -39,54 +39,9 @@
                             Object value, SerializationContext context)
           throws IOException
       {
  -        QName xsitype = null;
  -        Class type = null;
  -        
  +        context.startElement(qname, attributes);
           if (value != null) {
  -            type = value.getClass();
  -        } else {
  -            // !!! add xsi:null attribute
  -        }
  -        
  -        if (type != null) {
  -            if (qname == null) {
  -                qname = (QName)namemap.get(type);
  -            }
  -            xsitype = (QName)typemap.get(type);
  -        }
  -        
  -        Attributes attrs = attributes;
  -        String str = context.qName2String(xsitype);
  -        
  -        if (xsitype != null) {
  -            // !!! should check if we're writing types or not?
  -            AttributesImpl impl = new AttributesImpl();
  -            boolean gotType = false;
  -            
  -            if (attributes != null) {
  -                for (int i = 0; i < attributes.getLength(); i++) {
  -                    impl.addAttribute(attributes.getURI(i),
  -                                      attributes.getLocalName(i),
  -                                      attributes.getQName(i), "CDATA",
  -                                      attributes.getValue(i));
  -                    if (attributes.getURI(i).equals(Constants.URI_CURRENT_SCHEMA_XSI) &&
  -                        attributes.getLocalName(i).equals("type"))
  -                        gotType = true;
  -                }
  -            }
  -            
  -            if (!gotType) {
  -                impl.addAttribute(Constants.URI_CURRENT_SCHEMA_XSI,
  -                               "type", "xsi:type",
  -                               "CDATA", str);
  -            }
  -            
  -            attrs = impl;
  -        }
  -
  -        context.startElement(qname, attrs);
  -        if (value != null) {
  -            if (type.equals(String.class))
  +            if (value instanceof String)
                   context.writeString(
                                   XMLUtils.xmlEncodeString(value.toString()));
               else
  
  
  
  1.35      +15 -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.34
  retrieving revision 1.35
  diff -u -r1.34 -r1.35
  --- SerializationContext.java	2001/08/14 20:41:36	1.34
  +++ SerializationContext.java	2001/08/22 22:55:10	1.35
  @@ -108,7 +108,12 @@
        * Should I send an XML declaration?
        */
       private boolean sendXMLDecl = true;
  -    
  +
  +    /**
  +     * Should I send xsi:type attributes?
  +     */
  +    private boolean sendXSIType = true;
  +
       /**
        * A place to hold objects we cache for multi-ref serialization, and
        * remember the IDs we assigned them.
  @@ -139,6 +144,11 @@
                                                     AxisEngine.PROP_DOMULTIREFS);
           if (shouldSendMultiRefs != null)
               doMultiRefs = shouldSendMultiRefs.booleanValue();
  +
  +        ServiceDescription sd = msgContext.getServiceDescription();
  +        if (sd != null) {
  +            sendXSIType = sd.getSendTypeAttr();
  +        }
       }
       
       public void setSendDecl(boolean sendDecl)
  @@ -146,6 +156,10 @@
           sendXMLDecl = sendDecl;
       }
       
  +    public boolean shouldSendXSIType() {
  +        return sendXSIType;
  +    }
  +
       public ServiceDescription getServiceDescription()
       {
           return msgContext.getServiceDescription();
  
  
  
  1.23      +4 -3      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.22
  retrieving revision 1.23
  diff -u -r1.22 -r1.23
  --- TypeMappingRegistry.java	2001/08/14 03:49:18	1.22
  +++ TypeMappingRegistry.java	2001/08/22 22:55:10	1.23
  @@ -209,9 +209,10 @@
       public Attributes setTypeAttribute(Attributes attributes, QName type,
                                          SerializationContext context)
       {
  -        if ((attributes != null) &&
  -            (attributes.getIndex(Constants.URI_CURRENT_SCHEMA_XSI,
  -                                "type") != -1))
  +        if (!context.shouldSendXSIType() ||
  +            ((attributes != null) &&
  +             (attributes.getIndex(Constants.URI_CURRENT_SCHEMA_XSI,
  +                                "type") != -1)))
               return attributes;
           
           AttributesImpl attrs = new AttributesImpl();
  
  
  
  1.10      +1 -0      xml-axis/java/test/encoding/PackageTests.java
  
  Index: PackageTests.java
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/test/encoding/PackageTests.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- PackageTests.java	2001/08/09 22:59:14	1.9
  +++ PackageTests.java	2001/08/22 22:55:10	1.10
  @@ -29,6 +29,7 @@
           suite.addTestSuite(TestBody.class);
           suite.addTestSuite(TestDOM.class);
           suite.addTestSuite(TestArrayListConversions.class);
  +        suite.addTestSuite(TestXsiType.class);
   
           return suite;
       }
  
  
  
  1.1                  xml-axis/java/test/encoding/TestXsiType.java
  
  Index: TestXsiType.java
  ===================================================================
  package test.encoding;
  
  import org.apache.axis.MessageContext;
  import org.apache.axis.message.SOAPEnvelope;
  import org.apache.axis.message.RPCParam;
  import org.apache.axis.message.RPCElement;
  import org.apache.axis.encoding.*;
  import org.apache.axis.server.AxisServer;
  import org.apache.axis.utils.QName;
  
  import java.io.Writer;
  import java.io.StringWriter;
  
  import junit.framework.TestCase;
  
  /**
   * Verify that shutting off xsi:types in the ServiceDescription works
   * as expected.
   */
  public class TestXsiType extends TestCase {
  
      private String header;
      private String footer;
      private AxisServer server = new AxisServer();
  
      public TestXsiType(String name) {
          super(name);
      }
  
      public void testNoXsiTypes()
         throws Exception
      {
          MessageContext msgContext = new MessageContext(new AxisServer());
          ServiceDescription sd = new ServiceDescription("testXsiType", true);
  
          // Don't serialize xsi:type attributes
          sd.setSendTypeAttr(false);
  
          msgContext.setServiceDescription(sd);
  
          SOAPEnvelope msg = new SOAPEnvelope();
          RPCParam arg1 = new RPCParam("urn:myNamespace",
                                       "testParam",
                                       "this is a string");
          RPCElement body = new RPCElement("urn:myNamespace",
                                           "method1",
                                           new Object[]{ arg1 });
          msg.addBodyElement(body);
  
          Writer stringWriter = new StringWriter();
          SerializationContext context = new SerializationContext(stringWriter,
                                                                  msgContext);
  
          msg.output(context);
  
          String msgString = stringWriter.toString();
          assertTrue("Found unexpected xsi:type!",
                     msgString.indexOf("xsi:type") == -1);
      }
  
  
      public static void main(String [] args) throws Exception
      {
          TestXsiType tester = new TestXsiType("test");
          tester.testNoXsiTypes();
      }
  }