View Javadoc

    1   package org.codehaus.xfire.aegis.type.collection;
    2
    3   import java.util.ArrayList;
    4   import java.util.Collection;
    5   import java.util.HashSet;
    6   import java.util.Iterator;
    7   import java.util.List;
    8   import java.util.Set;
    9
    10  import org.codehaus.xfire.MessageContext;
    11  import org.codehaus.xfire.XFireRuntimeException;
    12  import org.codehaus.xfire.aegis.MessageReader;
    13  import org.codehaus.xfire.aegis.MessageWriter;
    14  import org.codehaus.xfire.aegis.type.Type;
    15  import org.codehaus.xfire.aegis.type.basic.ArrayType;
    16  import org.codehaus.xfire.fault.XFireFault;
    17
    18  public class CollectionType
    19      extends ArrayType
    20  {
    21      private Class componentType;
    22
    23      public CollectionType(Class componentType)
    24      {
    25          super();
    26
    27          this.componentType = componentType;
    28      }
    29
    30      public Object readObject(MessageReader reader, MessageContext context)
    31          throws XFireFault
    32      {
    33          try
    34          {
    35              Type compType = getComponentType();
    36
    37              Collection values = createCollection();
    38
    39              while (reader.hasMoreElementReaders())
    40              {
    41                  MessageReader childReader = reader.getNextElementReader();
    42
    43                  values.add(compType.readObject(childReader, context));
    44              }
    45
    46              return values;
    47          }
    48          catch (IllegalArgumentException e)
    49          {
    50              throw new XFireRuntimeException("Illegal argument.", e);
    51          }
    52      }
    53
    54      protected Collection createCollection()
    55      {
    56          Collection values = null;
    57
    58          if (getTypeClass().isAssignableFrom(List.class))
    59          {
    60              values = new ArrayList();
    61          }
    62          else if (getTypeClass().isAssignableFrom(Set.class))
    63          {
    64              values = new HashSet();
    65          }
    66          else
    67          {
    68              values = new ArrayList();
    69          }
    70
    71          return values;
    72      }
    73
    74      public void writeObject(Object object, MessageWriter writer, MessageContext context)
    75          throws XFireFault
    76      {
    77          if (object == null)
    78              return;
    79
    80          try
    81          {
    82              Collection list = (Collection) object;
    83
    84              Type type = getComponentType();
    85
    86              if (type == null)
    87                  throw new XFireRuntimeException("Couldn't find type for " + type.getTypeClass() + ".");
    88
    89              for (Iterator itr = list.iterator(); itr.hasNext();)
    90              {
    91                  String ns = null;
    92                  if (type.isAbstract())
    93                      ns = getSchemaType().getNamespaceURI();
    94                  else
    95                      ns = type.getSchemaType().getNamespaceURI();
    96
    97                  MessageWriter cwriter = writer
    98                          .getElementWriter(type.getSchemaType().getLocalPart(), ns);
    99
    100                 type.writeObject(itr.next(), writer, context);
    101                 cwriter.close();
    102             }
    103         }
    104         catch (IllegalArgumentException e)
    105         {
    106             throw new XFireRuntimeException("Illegal argument.", e);
    107         }
    108     }
    109
    110     public Type getComponentType()
    111     {
    112         Type type = getTypeMapping().getType(componentType);
    113
    114         if (type == null)
    115         {
    116             type = getTypeMapping().getTypeCreator().createType(componentType);
    117             getTypeMapping().register(type);
    118         }
    119
    120         return type;
    121     }
    122 }