package org.codehaus.xfire.aegis.type.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.codehaus.xfire.MessageContext;
import org.codehaus.xfire.XFireRuntimeException;
import org.codehaus.xfire.aegis.MessageReader;
import org.codehaus.xfire.aegis.MessageWriter;
import org.codehaus.xfire.aegis.type.Type;
import org.codehaus.xfire.aegis.type.basic.ArrayType;
import org.codehaus.xfire.fault.XFireFault;

public class CollectionType
    extends ArrayType
{
    private Class componentType;
    
    public CollectionType(Class componentType)
    {
        super();
        
        this.componentType = componentType;
    }

    public Object readObject(MessageReader reader, MessageContext context)
        throws XFireFault
    {
        try
        {
            Type compType = getComponentType();
            
            List values = new ArrayList();
            
            while (reader.hasMoreElementReaders())
            {
                MessageReader childReader = reader.getNextElementReader();
                
                values.add(compType.readObject(childReader, context));
            }
            
            return values;
        }
        catch (IllegalArgumentException e)
        {
            throw new XFireRuntimeException("Illegal argument.", e);
        }
    }
    public void writeObject(Object object, MessageWriter writer, MessageContext context)
        throws XFireFault
    {
        if (object == null)
            return;
    
        try
        {
            Collection list = (Collection) object;

            Type type = getComponentType();

            if (type == null)
                throw new XFireRuntimeException("Couldn't find type for " + type.getTypeClass() + ".");

            for (Iterator itr = list.iterator(); itr.hasNext();)
            {
                String ns = null;
                if (type.isAbstract())
                    ns = getSchemaType().getNamespaceURI();
                else
                    ns = type.getSchemaType().getNamespaceURI();

                MessageWriter cwriter = writer
                        .getElementWriter(type.getSchemaType().getLocalPart(), ns);

                type.writeObject(itr.next(), writer, context);
                cwriter.close();
            }
        }
        catch (IllegalArgumentException e)
        {
            throw new XFireRuntimeException("Illegal argument.", e);
        }
    }    
    
    protected Type getComponentType()
    {
        return getTypeMapping().getType(componentType);
    }
}
@@ -97,24 +97,50 @@
 Collection;%0A
+import java.util.HashSet;%0A
 import java.
@@ -154,16 +154,16 @@
 erator;%0A
-
 import j
@@ -176,16 +176,38 @@
 il.List;
+%0Aimport java.util.Set;
 %0A%0Aimport
@@ -953,35 +953,44 @@
     
-List values = new ArrayList
+Collection values = createCollection
 ();%0A
@@ -1426,22 +1426,458 @@
       %7D%0A
-
     %7D%0A
+%0A    protected Collection createCollection()%0A    %7B%0A        Collection values = null;%0A        %0A        if (getTypeClass().isAssignableFrom(List.class))%0A        %7B%0A            values = new ArrayList();%0A        %7D%0A        else if (getTypeClass().isAssignableFrom(Set.class))%0A        %7B%0A            values = new HashSet();%0A        %7D%0A        else%0A        %7B%0A            values = new ArrayList();%0A        %7D%0A        %0A        return values;%0A    %7D%0A%0A
     publ

Add the ability to handle Set/List/Collection types.