KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
11
12
13 /**
14  * Class documentation
15  *
16  * @author <a HREF="mailto:jc@sapia-oss.org">Jean-Cedric Desrochers</a>
17  * <dl>
18  * <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>
19  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
20  * <a HREF="http://www.sapia-oss.org/license.html" target="sapia-license">license page</a> at the Sapia OSS web site</dd></dt>
21  * </dl>
22  */

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

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

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

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