You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xmlbeans-cvs@xml.apache.org by zi...@apache.org on 2004/02/12 22:18:15 UTC

cvs commit: xml-xmlbeans/v2/test/src/drt/drtcases MarshalTests.java

zieg        2004/02/12 13:18:15

  Modified:    v2/src/binding/org/apache/xmlbeans/impl/binding/bts
                        BindingProperty.java MethodName.java
               v2/src/configschema/schema binding-config.xsd
               v2/src/marshal/org/apache/xmlbeans/impl/marshal
                        ByNameRuntimeBindingType.java
                        ByNameTypeVisitor.java
               v2/test/cases/marshal example_config.xml
               v2/test/cases/marshal/com/mytest MySubClass.java
               v2/test/src/drt/drtcases MarshalTests.java
  Log:
  update binding-config.xsd for isSet methods
  update marshalling path to check isSet methods if present
  
  Revision  Changes    Path
  1.10      +14 -0     xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/bts/BindingProperty.java
  
  Index: BindingProperty.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/bts/BindingProperty.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- BindingProperty.java	12 Feb 2004 20:06:06 -0000	1.9
  +++ BindingProperty.java	12 Feb 2004 21:18:15 -0000	1.10
  @@ -33,6 +33,7 @@
     private BindingTypeName btName;
     private MethodName getter;
     private MethodName setter;
  +  private MethodName issetter;
     private String field;
     private JavaTypeName collection;
   
  @@ -58,6 +59,7 @@
               XmlTypeName.forString(node.getXmlcomponent()));
       this.getter = MethodName.create(node.getGetter());
       this.setter = MethodName.create(node.getSetter());
  +    this.issetter = MethodName.create(node.getIssetter());
       this.field = node.getField();
       String collection = node.getCollection();
       if (collection != null)
  @@ -123,6 +125,18 @@
   
     public void setSetterName(MethodName mn) {
       setter = mn;
  +  }
  +
  +  public boolean hasIssetter() {
  +    return !isField() && issetter != null;
  +  }
  +
  +  public MethodName getIssetterName() {
  +    return isField() ? null : issetter;
  +  }
  +
  +  public void setIssetterName(MethodName mn) {
  +    issetter = mn;
     }
   
     public String getFieldName() {
  
  
  
  1.4       +2 -1      xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/bts/MethodName.java
  
  Index: MethodName.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/bts/MethodName.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- MethodName.java	12 Feb 2004 20:06:06 -0000	1.3
  +++ MethodName.java	12 Feb 2004 21:18:15 -0000	1.4
  @@ -96,7 +96,8 @@
      */
     /*package*/
     static MethodName create(JavaMethodName jmn) {
  -
  +    if (jmn == null) return null;
  +    
       return create(jmn.getMethodName(),
                     namesForStrings(jmn.getParamTypeArray()));
     }
  
  
  
  1.13      +1 -0      xml-xmlbeans/v2/src/configschema/schema/binding-config.xsd
  
  Index: binding-config.xsd
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/configschema/schema/binding-config.xsd,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- binding-config.xsd	12 Jan 2004 07:26:01 -0000	1.12
  +++ binding-config.xsd	12 Feb 2004 21:18:15 -0000	1.13
  @@ -105,6 +105,7 @@
           <xs:sequence>
             <xs:element name="getter" type="tns:java-method-name"/>
             <xs:element name="setter" type="tns:java-method-name" minOccurs="0"/>
  +          <xs:element name="issetter" type="tns:java-method-name" minOccurs="0"/>
           </xs:sequence>
           <xs:element name="field" type="tns:java-field-name"/>
           <xs:element name="static" type="tns:java-field-name"/>
  
  
  
  1.28      +74 -55    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.27
  retrieving revision 1.28
  diff -u -r1.27 -r1.28
  --- ByNameRuntimeBindingType.java	12 Feb 2004 20:06:16 -0000	1.27
  +++ ByNameRuntimeBindingType.java	12 Feb 2004 21:18:15 -0000	1.28
  @@ -271,6 +271,7 @@
           private final TypeMarshaller marshaller; // used only for simple types
           private final Method getMethod;
           private final Method setMethod;
  +        private final Method issetMethod;
           private final boolean javaPrimitive;
           private final Object defaultValue;
   
  @@ -301,6 +302,7 @@
               collectionElementClass = getCollectionElementClass(prop, bindingType);
               getMethod = getGetterMethod(prop, beanClass);
               setMethod = getSetterMethod(prop, beanClass);
  +            issetMethod = getIssetterMethod(prop, beanClass);
               javaPrimitive = propertyClass.isPrimitive();
   
               String def = bindingProperty.getDefault();
  @@ -472,7 +474,8 @@
               return unmarshaller;
           }
   
  -        public void fill(final Object inter, final Object prop_obj) throws XmlException
  +        public void fill(final Object inter, final Object prop_obj)
  +            throws XmlException
           {
               //means xsi:nil was true but we're a primtive.
               //schema should have nillable="false" so this
  @@ -480,31 +483,20 @@
               if (prop_obj == null && javaPrimitive)
                   return;
   
  -            try {
  -                if (beanHasMulti) {
  -                    final UResultHolder rh = (UResultHolder)inter;
  +            if (beanHasMulti) {
  +                final UResultHolder rh = (UResultHolder)inter;
   
  -                    if (isMultiple()) {
  -                        rh.addItem(propertyIndex, prop_obj);
  -                    } else {
  -                        setMethod.invoke(rh.getValue(), new Object[]{prop_obj});
  -                    }
  +                if (isMultiple()) {
  +                    rh.addItem(propertyIndex, prop_obj);
                   } else {
  -                    setMethod.invoke(inter, new Object[]{prop_obj});
  +                    invokeMethod(rh.getValue(), setMethod,
  +                                 new Object[]{prop_obj});
                   }
  -            }
  -            catch (SecurityException e) {
  -                throw new XmlException(e);
  -            }
  -            catch (IllegalAccessException e) {
  -                throw new XmlException(e);
  -            }
  -            catch (InvocationTargetException e) {
  -                throw new XmlException(e);
  +            } else {
  +                invokeMethod(inter, setMethod, new Object[]{prop_obj});
               }
           }
   
  -
           public void fillDefaultValue(Object inter)
               throws XmlException
           {
  @@ -517,18 +509,7 @@
               throws XmlException
           {
               assert isMultiple();
  -            try {
  -                setMethod.invoke(inter, new Object[]{prop_obj});
  -            }
  -            catch (SecurityException e) {
  -                throw new XmlException(e);
  -            }
  -            catch (IllegalAccessException e) {
  -                throw new XmlException(e);
  -            }
  -            catch (InvocationTargetException e) {
  -                throw new XmlException(e);
  -            }
  +            invokeMethod(inter, setMethod, new Object[]{prop_obj});
           }
   
           public CharSequence getLexical(Object value, MarshalResult result)
  @@ -553,13 +534,35 @@
               assert parentObject != null;
               assert beanClass.isAssignableFrom(parentObject.getClass()) :
                   parentObject.getClass() + " is not a " + beanClass;
  +
  +            return invokeMethod(parentObject, getMethod, EMPTY_OBJECT_ARRAY);
  +        }
  +
  +        //TODO: check isSet methods
  +        public boolean isSet(Object parentObject, MarshalResult result)
  +            throws XmlException
  +        {
  +            if (issetMethod == null)
  +                return isSetFallback(parentObject, result);
  +
  +            final Boolean isset =
  +                (Boolean)invokeMethod(parentObject, issetMethod,
  +                                      EMPTY_OBJECT_ARRAY);
  +            return isset.booleanValue();
  +        }
  +
  +        private static Object invokeMethod(Object target,
  +                                           Method method,
  +                                           Object[] params)
  +            throws XmlException
  +        {
               try {
  -                return getMethod.invoke(parentObject, EMPTY_OBJECT_ARRAY);
  +                return method.invoke(target, params);
               }
  -            catch (SecurityException e) {
  +            catch (IllegalAccessException e) {
                   throw new XmlException(e);
               }
  -            catch (IllegalAccessException e) {
  +            catch (IllegalArgumentException e) {
                   throw new XmlException(e);
               }
               catch (InvocationTargetException e) {
  @@ -567,10 +570,10 @@
               }
           }
   
  -        //TODO: check isSet methods
  -        public boolean isSet(Object parentObject, MarshalResult result)
  +        private boolean isSetFallback(Object parentObject, MarshalResult result)
               throws XmlException
           {
  +            //REVIEW: nillable is winning over minOccurs="0".  Is this correct?
               if (bindingProperty.isNillable())
                   return true;
   
  @@ -592,20 +595,30 @@
                                                 Class beanClass)
               throws XmlException
           {
  +            if (!binding_prop.hasSetter()) return null;
  +
               MethodName setterName = binding_prop.getSetterName();
  -            try {
  -                final Method set_method = setterName.getMethodOn(beanClass);
  -                return set_method;
  -            }
  -            catch (NoSuchMethodException e) {
  -                throw new XmlException(e);
  -            }
  -            catch (SecurityException e) {
  -                throw new XmlException(e);
  -            }
  -            catch (ClassNotFoundException cnfe) {
  -                throw new XmlException(cnfe);
  +            return getMethodOnClass(setterName, beanClass);
  +        }
  +
  +        private static Method getIssetterMethod(QNameProperty binding_prop,
  +                                                Class clazz)
  +            throws XmlException
  +        {
  +            if (!binding_prop.hasIssetter())
  +                return null;
  +
  +            Method isset_method =
  +                getMethodOnClass(binding_prop.getIssetterName(), clazz);
  +
  +            if (!isset_method.getReturnType().equals(Boolean.TYPE)) {
  +                String msg = "invalid isset method: " + isset_method +
  +                    " -- return type must be boolean not " +
  +                    isset_method.getReturnType().getName();
  +                throw new XmlException(msg);
               }
  +
  +            return isset_method;
           }
   
   
  @@ -614,19 +627,25 @@
               throws XmlException
           {
               MethodName getterName = binding_prop.getGetterName();
  +            return getMethodOnClass(getterName, beanClass);
  +        }
  +
  +
  +        private static Method getMethodOnClass(MethodName method_name,
  +                                               Class clazz)
  +            throws XmlException
  +        {
               try {
  -                final Method get_method =
  -                    getterName.getMethodOn(beanClass);
  -                return get_method;
  +                return method_name.getMethodOn(clazz);
               }
               catch (NoSuchMethodException e) {
                   throw new XmlException(e);
               }
  -            catch (SecurityException e) {
  -                throw new XmlException(e);
  +            catch (SecurityException se) {
  +                throw new XmlException(se);
               }
               catch (ClassNotFoundException cnfe) {
  -                throw new XmlException(cnfe);//should never happen
  +                throw new XmlException(cnfe);
               }
           }
   
  
  
  
  1.16      +4 -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.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- ByNameTypeVisitor.java	12 Feb 2004 20:06:16 -0000	1.15
  +++ ByNameTypeVisitor.java	12 Feb 2004 21:18:15 -0000	1.16
  @@ -109,7 +109,9 @@
       {
           final RuntimeBindingProperty property = getCurrentElementProperty();
           if (property.isMultiple()) {
  -            Object prop_obj = property.getValue(getParentObject(), marshalResult);
  +            final Object parent = getParentObject();
  +            final Object prop_obj = property.isSet(parent, marshalResult) ?
  +                property.getValue(parent, marshalResult) : null;
               final Iterator itr = MarshalResult.getCollectionIterator(prop_obj);
               currMultipleIterator = itr;
               if (itr.hasNext()) {
  @@ -135,8 +137,7 @@
   
           final RuntimeBindingProperty property = getCurrentElementProperty();
   
  -        final boolean set = property.isSet(getParentObject(), marshalResult);
  -        return set;
  +        return property.isSet(getParentObject(), marshalResult);
       }
   
       public XmlTypeVisitor getCurrentChild() throws XmlException
  
  
  
  1.19      +7 -3      xml-xmlbeans/v2/test/cases/marshal/example_config.xml
  
  Index: example_config.xml
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/test/cases/marshal/example_config.xml,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- example_config.xml	26 Jan 2004 03:34:37 -0000	1.18
  +++ example_config.xml	12 Feb 2004 21:18:15 -0000	1.19
  @@ -240,13 +240,17 @@
                   <bin:xmlcomponent>t=integer@http://www.w3.org/2001/XMLSchema</bin:xmlcomponent>
                   <bin:javatype>java.math.BigInteger</bin:javatype>
                   <bin:getter>
  -                   <bin:method-name>getBigInt</bin:method-name>
  +                    <bin:method-name>getBigInt</bin:method-name>
                   </bin:getter>
                   <bin:setter>
  -                   <bin:method-name>setBigInt</bin:method-name>
  -                   <bin:param-type>java.math.BigInteger</bin:param-type>
  +                    <bin:method-name>setBigInt</bin:method-name>
  +                    <bin:param-type>java.math.BigInteger</bin:param-type>
                   </bin:setter>
  +                <bin:issetter>
  +                    <bin:method-name>isSetBigInt</bin:method-name>
  +                </bin:issetter>
                   <bin:qname>java:BigInt</bin:qname>
  +                <bin:nillable>true</bin:nillable>
               </bin:qname-property>
           </bin:binding-type>
       </bin:bindings>
  
  
  
  1.4       +10 -0     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.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- MySubClass.java	12 Feb 2004 20:06:30 -0000	1.3
  +++ MySubClass.java	12 Feb 2004 21:18:15 -0000	1.4
  @@ -22,6 +22,7 @@
   {
   
       private BigInteger bigInt;
  +    private boolean issetBigInt;
   
       public BigInteger getBigInt()
       {
  @@ -31,6 +32,15 @@
       public void setBigInt(BigInteger bigInt)
       {
           this.bigInt = bigInt;
  +        setIsSetBigInt(bigInt != null);
  +    }
  +
  +    public boolean isSetBigInt() {
  +        return issetBigInt;
  +    }
  +
  +    public void setIsSetBigInt(boolean val) {
  +        issetBigInt = val;
       }
   
       public String toString()
  
  
  
  1.32      +8 -1      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.31
  retrieving revision 1.32
  diff -u -r1.31 -r1.32
  --- MarshalTests.java	12 Feb 2004 20:06:30 -0000	1.31
  +++ MarshalTests.java	12 Feb 2004 21:18:15 -0000	1.32
  @@ -254,8 +254,15 @@
           myelt.setAttrib(99999.777f);
           myelt.setMyFloat(5555.4444f);
   //        myelt.setMyClass(new com.mytest.MyClass());
  +
  +
           MySubClass sub = new MySubClass();
  -        sub.setBigInt(new BigInteger("123431234321234321234321234212341234"));
  +        sub.setBigInt(new BigInteger("23522352235223522352"));
  +//        sub.setIsSetBigInt(false); //TESTING;
  +//        sub.setBigInt(null);
  +//        sub.setIsSetBigInt(true); //TESTING;
  +
  +
           myelt.setMyClass(sub);
           mc.setMyelt(myelt);
   
  
  
  

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