KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.util.xml.idefix.serializer;
2
3 import org.sapia.util.xml.Namespace;
4 import org.sapia.util.xml.idefix.SerializationContext;
5 import org.sapia.util.xml.idefix.SerializationException;
6 import org.sapia.util.xml.idefix.SerializerIF;
7 import org.sapia.util.xml.idefix.SerializerNotFoundException;
8
9 import java.lang.reflect.Array JavaDoc;
10
11
12 /**
13  * Class documentation
14  *
15  * @author <a HREF="mailto:jc@sapia-oss.org">Jean-Cedric Desrochers</a>
16  * <dl>
17  * <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>
18  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
19  * <a HREF="http://www.sapia-oss.org/license.html" target="sapia-license">license page</a> at the Sapia OSS web site</dd></dt>
20  * </dl>
21  */

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

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

38   public void serialize(Object JavaDoc anObject, SerializationContext aContext)
39     throws SerializationException {
40     throw new UnsupportedOperationException JavaDoc(
41       "The array serializer can serialize a root element");
42   }
43
44   /**
45    * Transforms the object passed in into an XML representation. This method is called when the
46    * object to transform is nested inside another object.
47    *
48    * @param anArray The array to serialize.
49    * @param aNamespace The namespace of the object to serialize.
50    * @param anObjectName The name of the object to serialize.
51    * @param aContext The serialization context to use.
52    * @exception SerializationException If an error occurs serializing the object.
53    */

54   public void serialize(Object JavaDoc anArray, Namespace aNamespace,
55     String JavaDoc anObjectName, SerializationContext aContext)
56     throws SerializationException {
57     try {
58       // Start the XML element
59
aContext.getXmlBuffer().addNamespace(aNamespace.getURI(),
60         aNamespace.getPrefix());
61       aContext.getXmlBuffer().startElement(aNamespace.getURI(), anObjectName);
62
63       // Get a serializer for the array
64
SerializerIF aSerializer = aContext.getSerializerFactory().getSerializer(anArray.getClass()
65                                                                                       .getComponentType());
66
67       // Extract all the objects of the array
68
int anArrayLength = Array.getLength(anArray);
69
70       for (int i = 0; i < anArrayLength; i++) {
71         Object JavaDoc anObject = Array.get(anArray, i);
72         aSerializer.serialize(anObject, aContext);
73       }
74
75       // End the XML element
76
aContext.getXmlBuffer().endElement(aNamespace.getURI(), anObjectName);
77       aContext.getXmlBuffer().removeNamespace(aNamespace.getURI());
78     } catch (SerializerNotFoundException snfe) {
79       String JavaDoc aMessage = "Unable to found a serializer for the array: " +
80         anArray;
81       throw new SerializationException(aMessage, snfe);
82     }
83   }
84 }
85
Popular Tags