KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > util > xml > idefix > serializer > CollectionSerializer


1 package org.sapia.util.xml.idefix.serializer;
2
3 import org.sapia.util.xml.Namespace;
4 import org.sapia.util.xml.idefix.NamespaceFactoryIF;
5 import org.sapia.util.xml.idefix.SerializationContext;
6 import org.sapia.util.xml.idefix.SerializationException;
7 import org.sapia.util.xml.idefix.SerializerIF;
8 import org.sapia.util.xml.idefix.SerializerNotFoundException;
9
10 import java.util.Collection JavaDoc;
11 import java.util.Iterator JavaDoc;
12
13
14 /**
15  * Class documentation
16  *
17  * @author <a HREF="mailto:jc@sapia-oss.org">Jean-Cedric Desrochers</a>
18  * <dl>
19  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
20  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
21  * <a HREF="http://www.sapia-oss.org/license.html" target="sapia-license">license page</a> at the Sapia OSS web site</dd></dt>
22  * </dl>
23  */

24 public class CollectionSerializer implements SerializerIF {
25   /**
26    * Creates a new PrimitiveSerializer instance.
27    */

28   public CollectionSerializer() {
29     super();
30   }
31
32   /**
33    * Transforms the object passed in into an XML representation. This method is called when the
34    * object to transform is represents the root element of the XML document to create.
35    *
36    * @param anObject The object to serialize.
37    * @param aContext The serialization context to use.
38    * @exception SerializationException If an error occurs serializing the object.
39    */

40   public void serialize(Object JavaDoc anObject, SerializationContext aContext)
41     throws SerializationException {
42     Namespace aNamespace = NamespaceFactoryIF.DEFAULT_NAMESPACE;
43     String JavaDoc anObjectName = SerializerHelper.getLocalClassName(anObject);
44     serialize(anObject, aNamespace, anObjectName, aContext);
45   }
46
47   /**
48    * Transforms the object passed in into an XML representation. This method is called when the
49    * object to transform is nested inside another object.
50    *
51    * @param aCollection The collection to serialize.
52    * @param aNamespace The namespace of the object to serialize.
53    * @param anObjectName The name of the object to serialize.
54    * @param aContext The serialization context to use.
55    * @exception SerializationException If an error occurs serializing the object.
56    */

57   public void serialize(Object JavaDoc aCollection, Namespace aNamespace,
58     String JavaDoc anObjectName, SerializationContext aContext)
59     throws SerializationException {
60     Object JavaDoc anObject = null;
61
62     try {
63       // Extract all the objects of the collection
64
for (Iterator JavaDoc it = ((Collection JavaDoc) aCollection).iterator(); it.hasNext();) {
65         anObject = it.next();
66
67         // Get a serializer for the object
68
SerializerIF aSerializer = aContext.getSerializerFactory()
69                                            .getSerializer(anObject.getClass());
70
71         // Serialize the object
72
aSerializer.serialize(anObject, aContext);
73       }
74     } catch (SerializerNotFoundException snfe) {
75       String JavaDoc aMessage = "Unable to find a serializer for the object: " +
76         anObject;
77       throw new SerializationException(aMessage, snfe);
78     }
79   }
80 }
81
Popular Tags