Attached is an updated patch for elements with maxOccurs > 1 which 
switches the public {type} [] get method to public ArrayList get.

Regards,

Glenn

Glenn Nielsen wrote:
> 
> Attached is a patch which switchs the code generated for a get to
> return an ArrayList instead of converting the ArrayList to an object []
> array.
> 
> Regards,
> 
> Glenn
> 
> Glenn Nielsen wrote:
> 
> > I use Castor to generate java source for elements which have maxOccurs > 1.
> > I pass the arg -types j2 on the command line so that Castor will use Java 2
> > Collection types.  Internally the code generated uses an ArrayList to store
> > the element values.  But the get method for that element converts the
> > ArrayList
> > to an array, then returns the array.  Shouldn't the get method just
> > return the
> > ArrayList if you specify -types j2 to the source generator?
> >
> > Regards,
> >
> > Glenn
> >
> > ----------------------------------------------------------------------
> > Glenn Nielsen             [EMAIL PROTECTED] | /* Spelin donut madder    |
> > MOREnet System Programming               |  * if iz ina coment.      |
> > Missouri Research and Education Network  |  */                       |
> > ----------------------------------------------------------------------
> >
> > ----------------------------------------------------------- If you wish
> > to unsubscribe from this mailing, send mail to
> > [EMAIL PROTECTED] with a subject of:
> >     unsubscribe castor-dev
> 
> --
> ----------------------------------------------------------------------
> Glenn Nielsen             [EMAIL PROTECTED] | /* Spelin donut madder    |
> MOREnet System Programming               |  * if iz ina coment.      |
> Missouri Research and Education Network  |  */                       |
> ----------------------------------------------------------------------
> 
>   
>------------------------------------------------------------------------------------------
>                                 Name: castor-collectionj2.patch
>    castor-collectionj2.patch    Type: Plain Text (text/plain)
>                             Encoding: 7bit

-- 
----------------------------------------------------------------------
Glenn Nielsen             [EMAIL PROTECTED] | /* Spelin donut madder    |
MOREnet System Programming               |  * if iz ina coment.      |
Missouri Research and Education Network  |  */                       |
----------------------------------------------------------------------
diff -ur castor/src/main/org/exolab/castor/builder/CollectionInfoJ2.java 
castor-patched/src/main/org/exolab/castor/builder/CollectionInfoJ2.java
--- castor/src/main/org/exolab/castor/builder/CollectionInfoJ2.java     Thu Aug 30 
14:49:24 2001
+++ castor-patched/src/main/org/exolab/castor/builder/CollectionInfoJ2.java     Wed 
+Nov 14 06:59:40 2001
@@ -129,8 +129,8 @@
         method.addParameter(new JParameter(JType.Int, "index"));
         createGetByIndexMethod(method);
 
-        //-- {type}[] get{Name}
-        method = new JMethod(jType.createArray(), "get"+cName);
+        //-- ArrayList get{Name}
+        method = new JMethod(getSchemaType().getJType(), "get"+cName);
         jClass.addMethod(method);
         createGetMethod(method);
 
@@ -276,7 +276,7 @@
     } //-- createEnumerateMethod
 
     /**
-     * Creates implementation of object[] get() method.
+     * Creates implementation of ArrayList get() method.
      *
      * @param method the JMethod in which to create the source
      * code.
@@ -286,40 +286,9 @@
         JSourceCode jsc = method.getSourceCode();
         JType jType = method.getReturnType();
 
-        jsc.add("int size = ");
+        jsc.add("return ");
         jsc.append(getName());
-        jsc.append(".size();");
-
-        String variableName = getName()+".get(index)";
-
-        JType compType = jType.getComponentType();
-
-        jsc.add(compType.toString());
-        jsc.append("[] mArray = new ");
-        jsc.append(compType.toString());
-        jsc.append("[size]");
-        //-- if component is an array, we must add [] after setting
-        //-- size
-        if (compType.isArray()) jsc.append("[]");
-        jsc.append(";");
-
-        jsc.add("for (int index = 0; index < size; index++) {");
-        jsc.indent();
-        jsc.add("mArray[index] = ");
-        if (getContentType().getType() == XSType.CLASS) {
-            jsc.append("(");
-            jsc.append(jType.getLocalName());
-            jsc.append(") ");
-            jsc.append(variableName);
-        }
-        else {
-            jsc.append(getContentType()
-                       .createFromJavaObjectCode(variableName));
-        }
         jsc.append(";");
-        jsc.unindent();
-        jsc.add("}");
-        jsc.add("return mArray;");
     } //-- createGetMethod
 
     /**
diff -ur castor/src/main/org/exolab/castor/xml/Marshaller.java 
castor-patched/src/main/org/exolab/castor/xml/Marshaller.java
--- castor/src/main/org/exolab/castor/xml/Marshaller.java       Mon Oct 29 15:45:39 
2001
+++ castor-patched/src/main/org/exolab/castor/xml/Marshaller.java       Wed Nov 14 
+09:00:38 2001
@@ -72,8 +72,10 @@
 import java.io.Serializable;
 import java.io.Writer;
 import java.lang.reflect.*;
+import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.Hashtable;
+import java.util.Iterator;
 import java.util.Vector;
 
 /**
@@ -526,6 +528,7 @@
         if (_debug) {
             System.out.println("Marshalling " + object.getClass().getName());
         }
+
         if (object instanceof AnyNode) {
            try{
               AnyNode2SAX.fireEvents((AnyNode)object, _handler);
@@ -694,6 +697,9 @@
         if (saveType && (descriptor.getHandler() instanceof DateFieldHandler))
             saveType = false;
         //-- XXXX end Date fix
+        //-- Suppress 'xsi:type' attributes when object is an ArrayList
+        if (saveType && object instanceof ArrayList)
+            saveType = false;
 
         if (saveType) {
             // When the type of the instance of the field is not the
@@ -968,6 +974,15 @@
                     if (item != null)
                        marshal(item, elemDescriptor, handler);
                 }
+            }
+            //-- handle ArrayList
+            else if (obj instanceof java.util.ArrayList) {
+                Iterator it = ((ArrayList)obj).iterator();
+                while (it.hasNext()) {            
+                    Object item = it.next();
+                    if (item != null)                
+                       marshal(item, elemDescriptor, handler);
+                }                                             
             }
 
             else marshal(obj, elemDescriptor, handler);

Reply via email to