xml-xmlbeans-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From z...@apache.org
Subject cvs commit: xml-xmlbeans/v2/test/src/drt/drtcases MarshalTests.java
Date Tue, 09 Dec 2003 02:17:30 GMT
zieg        2003/12/08 18:17:30

  Modified:    v2/src/marshal/org/apache/xmlbeans/impl/marshal
                        ByNameRuntimeBindingType.java
                        ByNameTypeVisitor.java MarshalContextImpl.java
                        MarshalStreamUtils.java
               v2/test/cases/marshal doc.xml
               v2/test/cases/marshal/com/mytest MySubClass.java
               v2/test/src/drt/drtcases MarshalTests.java
  Log:
  support inheritance during marshalling
  add some rough tests for inheritance
  
  Revision  Changes    Path
  1.11      +28 -1     xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/ByNameRuntimeBindingType.java
  
  Index: ByNameRuntimeBindingType.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/ByNameRuntimeBindingType.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- ByNameRuntimeBindingType.java	8 Dec 2003 07:16:35 -0000	1.10
  +++ ByNameRuntimeBindingType.java	9 Dec 2003 02:17:30 -0000	1.11
  @@ -81,6 +81,12 @@
       private final Class javaClass;
       private final boolean hasMulti;  //has any multi properties
   
  +    //is this a subtype of something besides the ultimate parent type?
  +    //(XmlObject or java.lang.Object, though only the latter
  +    //is currently considered)
  +    private final boolean isSubType;
  +
  +
       ByNameRuntimeBindingType(ByNameBean btype)
       {
           byNameBean = btype;
  @@ -89,11 +95,23 @@
           }
           catch (ClassNotFoundException e) {
               final String msg = "failed to load " + btype.getName().getJavaName();
  -            throw (RuntimeException)(new RuntimeException(msg).initCause(e));
  +            throw new XmlRuntimeException(msg, e);
           }
   
           properties = new Property[btype.getProperties().size()];
           hasMulti = hasMulti(btype);
  +
  +        isSubType = determineIsSubType(javaClass);
  +    }
  +
  +    private static boolean determineIsSubType(Class javaClass)
  +    {
  +        int cnt = 0;
  +        for (Class p = javaClass.getSuperclass(); p != null; p = p.getSuperclass()) {
  +            if (cnt > 0) return true;
  +            cnt++;
  +        }
  +        return false;
       }
   
       private static boolean hasMulti(ByNameBean btype)
  @@ -191,7 +209,16 @@
           return properties.length;
       }
   
  +    public boolean isSubType()
  +    {
  +        return isSubType;
  +    }
  +
  +    public QName getSchemaTypeName() {
  +        return byNameBean.getName().getXmlName().getQName();
  +    }
   
  +    
       private static final class Property implements RuntimeBindingProperty
       {
           private final int propertyIndex;
  
  
  
  1.8       +16 -3     xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/ByNameTypeVisitor.java
  
  Index: ByNameTypeVisitor.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/ByNameTypeVisitor.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ByNameTypeVisitor.java	18 Nov 2003 23:52:30 -0000	1.7
  +++ ByNameTypeVisitor.java	9 Dec 2003 02:17:30 -0000	1.8
  @@ -56,6 +56,9 @@
   
   package org.apache.xmlbeans.impl.marshal;
   
  +import org.apache.xmlbeans.impl.binding.bts.BindingType;
  +import org.apache.xmlbeans.impl.common.XsTypeConverter;
  +
   import javax.xml.namespace.QName;
   import java.util.ArrayList;
   import java.util.Collection;
  @@ -78,7 +81,8 @@
                         MarshalContextImpl context)
       {
           super(obj, property, context);
  -        type = (ByNameRuntimeBindingType)context.createRuntimeBindingType(property.getType());
  +        final BindingType pt = property.getType();
  +        type = (ByNameRuntimeBindingType)context.createRuntimeBindingType(pt, obj);
           maxPropCount = obj == null ? 0 : type.getPropertyCount();
       }
   
  @@ -211,9 +215,18 @@
           if (getParentObject() == null) {
               QName nil_qn = fillPrefix(MarshalStreamUtils.XSI_NIL_QNAME);
               names.add(nil_qn);
  -            vals.add("true");
  +            vals.add(XsTypeConverter.printBoolean(true));
           } else {
  -            //TODO: xsi:type for polymorphism
  +
  +            if (type.isSubType()) {
  +                QName aname = fillPrefix(MarshalStreamUtils.XSI_TYPE_QNAME);
  +                names.add(aname);
  +                QName tn = fillPrefix(type.getSchemaTypeName());
  +                String aval = XsTypeConverter.getQNameString(tn.getNamespaceURI(),
  +                                                             tn.getLocalPart(),
  +                                                             tn.getPrefix());
  +                vals.add(aval);
  +            }
   
               for (int i = 0, len = maxPropCount; i < len; i++) {
                   final RuntimeBindingProperty prop = type.getProperty(i);
  
  
  
  1.2       +23 -1     xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/MarshalContextImpl.java
  
  Index: MarshalContextImpl.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/MarshalContextImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MarshalContextImpl.java	14 Nov 2003 00:40:29 -0000	1.1
  +++ MarshalContextImpl.java	9 Dec 2003 02:17:30 -0000	1.2
  @@ -59,6 +59,8 @@
   import org.apache.xmlbeans.MarshalContext;
   import org.apache.xmlbeans.impl.binding.bts.BindingLoader;
   import org.apache.xmlbeans.impl.binding.bts.BindingType;
  +import org.apache.xmlbeans.impl.binding.bts.JavaTypeName;
  +import org.apache.xmlbeans.impl.binding.bts.BindingTypeName;
   
   import javax.xml.namespace.NamespaceContext;
   import java.util.Collection;
  @@ -110,8 +112,28 @@
           return namespaceContext;
       }
   
  -    RuntimeBindingType createRuntimeBindingType(BindingType type)
  +    RuntimeBindingType createRuntimeBindingType(BindingType type, Object instance)
       {
  +        String expectedJavaClass = type.getName().getJavaName().toString();
  +        String actualJavaClass = instance.getClass().getName();
  +        if (!actualJavaClass.equals(expectedJavaClass)) {
  +            //redefine type
  +            JavaTypeName tn = JavaTypeName.forString(actualJavaClass);
  +            BindingTypeName bindingTypeName = loader.lookupTypeFor(tn);
  +            if (bindingTypeName != null) {
  +                final BindingType bindingType =
  +                    loader.getBindingType(bindingTypeName);
  +                if (bindingType != null) {
  +                    type = bindingType;
  +                } else {
  +                    //fallthrough to use the expected type...
  +                    //TODO: consider adding a warning to the errors?
  +                }
  +            } else {
  +                //fallthrough to use the expected type...
  +                //TODO: consider adding a warning to the errors?
  +            }
  +        }
           return runtimeTypeFactory.createRuntimeType(type, typeTable, loader);
       }
   
  
  
  
  1.8       +1 -0      xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/MarshalStreamUtils.java
  
  Index: MarshalStreamUtils.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/MarshalStreamUtils.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- MarshalStreamUtils.java	8 Dec 2003 07:16:35 -0000	1.7
  +++ MarshalStreamUtils.java	9 Dec 2003 02:17:30 -0000	1.8
  @@ -78,6 +78,7 @@
           "noNamespaceSchemaLocation";
   
       static final QName XSI_NIL_QNAME = new QName(XSI_NS, XSI_NIL_ATTR);
  +    static final QName XSI_TYPE_QNAME = new QName(XSI_NS, XSI_TYPE_ATTR);
   
   
       static void getXsiAttributes(XsiAttributeHolder holder,
  
  
  
  1.8       +17 -1     xml-xmlbeans/v2/test/cases/marshal/doc.xml
  
  Index: doc.xml
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/test/cases/marshal/doc.xml,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- doc.xml	8 Dec 2003 07:16:35 -0000	1.7
  +++ doc.xml	9 Dec 2003 02:17:30 -0000	1.8
  @@ -1,7 +1,8 @@
   <?xml version="1.0" encoding="utf-8"?>
   <My:load xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  -    xmlns:My="java:com.mytest" >
  +    xmlns:My="java:com.mytest"
  +    xmlns:n1="java:com.mytest" >
   
       <My:Myelt ignore="someval" My:Attrib="4444.123456789f">
           <My:MyFloat>111111165.34</My:MyFloat>
  @@ -11,6 +12,21 @@
               <My:BigInt>  7474747474747474747474747474747474  </My:BigInt>
   
           </My:MyClass>
  +
  +
  +        <n1:MyClassArray n3:type="n1:MySubClass" xmlns:n3="http://www.w3.org/2001/XMLSchema-instance">
  +            <n1:Myatt>DEFAULT:1071412448</n1:Myatt>
  +            <n1:BigInt>1111111111</n1:BigInt>
  +        </n1:MyClassArray>
  +        <n1:MyClassArray>
  +            <n1:Myatt>DEFAULT:380079710</n1:Myatt>
  +        </n1:MyClassArray>
  +        <n1:MyClassArray n4:type="n1:MySubClass" xmlns:n4="http://www.w3.org/2001/XMLSchema-instance">
  +            <n1:Myatt>DEFAULT:1071412448</n1:Myatt>
  +            <n1:BigInt>2222222222</n1:BigInt>
  +        </n1:MyClassArray>
  +
  +
       </My:Myelt>
   
       <!--    test as-if for simple types-->
  
  
  
  1.2       +5 -3      xml-xmlbeans/v2/test/cases/marshal/com/mytest/MySubClass.java
  
  Index: MySubClass.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/test/cases/marshal/com/mytest/MySubClass.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MySubClass.java	8 Dec 2003 07:16:35 -0000	1.1
  +++ MySubClass.java	9 Dec 2003 02:17:30 -0000	1.2
  @@ -74,11 +74,13 @@
           this.bigInt = bigInt;
       }
   
  -    //TODO: clean this up
       public String toString()
       {
  -        String parent = super.toString();
  -        return parent + " AND bigInt=" + bigInt;
  +        return "com.mytest.MySubClass{" +
  +            "super=" + super.toString() +
  +            " bigInt=" + bigInt +
  +            "}";
       }
  +
   
   }
  
  
  
  1.18      +7 -2      xml-xmlbeans/v2/test/src/drt/drtcases/MarshalTests.java
  
  Index: MarshalTests.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/test/src/drt/drtcases/MarshalTests.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- MarshalTests.java	8 Dec 2003 07:16:35 -0000	1.17
  +++ MarshalTests.java	9 Dec 2003 02:17:30 -0000	1.18
  @@ -39,6 +39,9 @@
   import java.math.BigInteger;
   import java.math.BigDecimal;
   
  +import com.mytest.MySubClass;
  +import com.mytest.MyClass;
  +
   
   public class MarshalTests extends TestCase
   {
  @@ -155,11 +158,13 @@
           myelt.setAttrib(99999.777f);
           myelt.setMyFloat(5555.4444f);
   //        myelt.setMyClass(new com.mytest.MyClass());
  -        myelt.setMyClass(null);
  +        MySubClass sub = new MySubClass();
  +        sub.setBigInt(new BigInteger("123431234321234321234321234212341234"));
  +        myelt.setMyClass(sub);
           mc.setMyelt(myelt);
   
           myelt.setStringArray(new String[]{"one", "two", "three"});
  -
  +        myelt.setMyClassArray(new MyClass[]{sub, new MyClass(), sub});
   
           final File bcdoc = getBindingConfigDocument();
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xmlbeans-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-cvs-help@xml.apache.org


Mime
View raw message