View Javadoc

1   /*
2    * CastorXmlSerializer.java
3    * 
4    * Created on Feb 17, 2004 at 2:03:04 AM
5    */
6   package yawn.io.serialization.xml;
7   
8   import java.beans.IntrospectionException;
9   import java.io.IOException;
10  import java.io.InputStreamReader;
11  import java.io.OutputStreamWriter;
12  
13  import org.apache.commons.betwixt.io.BeanReader;
14  import org.apache.commons.betwixt.io.BeanWriter;
15  import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper;
16  import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
17  import org.xml.sax.SAXException;
18  
19  import yawn.io.serialization.ISerializer;
20  import yawn.io.serialization.SerializationException;
21  
22  /***
23   * XML configuration serializer.
24   * 
25   * <p>$Id: BetwixtXmlSerializer.java,v 1.4 2005/04/20 18:55:19 supermarti Exp $</p>
26   * 
27   * @author <a href="mailto:alexei.guevara@uhn.on.ca">Alexei Guevara </a>
28   * @version $Revision: 1.4 $
29   */
30  public class BetwixtXmlSerializer implements ISerializer {
31  
32      /***
33       * 
34       * @see yawn.io.serialization.ISerializer#serialize(Object,
35       *      OutputStreamWriter)
36       */
37      public void serialize(Object network, OutputStreamWriter theDestination)
38              throws SerializationException {
39          // create write and set basic properties
40          BeanWriter writer = new BeanWriter(theDestination);
41  
42          writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
43          writer.getBindingConfiguration().setMapIDs(false);
44          writer.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(true);
45          // writer.enablePrettyPrint();
46  
47          // set a custom name mapper for attributes
48          writer.getXMLIntrospector().getConfiguration().setAttributeNameMapper(
49                  new HyphenatedNameMapper());
50          // set a custom name mapper for elements
51          writer.getXMLIntrospector().getConfiguration().setElementNameMapper(
52                  new DecapitalizeNameMapper());
53  
54          try {
55              writer.writeXmlDeclaration("<?xml version='1.0' ?>");
56              writer.write(network);
57          } catch (IOException e) {
58              throw new SerializationException(e);
59          } catch (SAXException e) {
60              throw new SerializationException(e);
61          } catch (IntrospectionException e) {
62              throw new SerializationException(e);
63          }
64  
65      }
66  
67      /***
68       * 
69       * @see yawn.io.serialization.ISerializer#deSerialize(Class, InputStreamReader)
70       */
71      public Object deSerialize(Class root, InputStreamReader reader) throws SerializationException {
72          // Now convert this to a bean using betwixt
73          // Create BeanReader
74          BeanReader beanReader = new BeanReader();
75  
76          // Configure the reader
77          // If you're round-tripping, make sure that the configurations are
78          // compatible!
79          beanReader.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
80          beanReader.getBindingConfiguration().setMapIDs(true);
81          beanReader.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(true);
82  
83          beanReader.getXMLIntrospector().getConfiguration().setAttributeNameMapper(
84                  new HyphenatedNameMapper());
85          // set a custom name mapper for elements
86          beanReader.getXMLIntrospector().getConfiguration().setElementNameMapper(
87                  new DecapitalizeNameMapper());
88          try {
89              // Register beans so that betwixt knows what the xml is to be
90              // converted to
91              // Since the element mapped to a PersonBean isn't called the same,
92              // need to register the path as well
93              beanReader.registerBeanClass(root);
94              return beanReader.parse(reader);
95              // } catch (IntrospectionException e) {
96              // throw new SerializationException(e);
97          } catch (SAXException e) {
98              throw new SerializationException(e);
99          } catch (IntrospectionException e) {
100             throw new SerializationException(e);
101         } catch (IOException e) {
102             throw new SerializationException(e);
103         }
104     }
105 
106 }