KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

26   public PrimitiveSerializer() {
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     // Extract the value of the object
41
String JavaDoc aValue = anObject.toString();
42
43     // Add the value as the content of the current element
44
aContext.getXmlBuffer().addContent(aValue);
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 anObject The object 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 anObject, Namespace aNamespace,
58     String JavaDoc anObjectName, SerializationContext aContext)
59     throws SerializationException {
60     // Extract the value of the object
61
String JavaDoc aValue = anObject.toString();
62
63     // Modify the case of the object name
64
String JavaDoc aName = SerializerHelper.firstToLowerFromIndex(anObjectName, 0);
65
66     // Generated the XML attribute
67
XmlBuffer aBuffer = aContext.getXmlBuffer();
68     aBuffer.addNamespace(aNamespace.getURI(), aNamespace.getPrefix());
69     aBuffer.addAttribute(aNamespace.getURI(), aName, aValue);
70     aBuffer.removeNamespace(aNamespace.getURI());
71   }
72 }
73
Popular Tags