1
2
3
4
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
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
46
47
48 writer.getXMLIntrospector().getConfiguration().setAttributeNameMapper(
49 new HyphenatedNameMapper());
50
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
73
74 BeanReader beanReader = new BeanReader();
75
76
77
78
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
86 beanReader.getXMLIntrospector().getConfiguration().setElementNameMapper(
87 new DecapitalizeNameMapper());
88 try {
89
90
91
92
93 beanReader.registerBeanClass(root);
94 return beanReader.parse(reader);
95
96
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 }