1 package net.sourceforge.phpeclipse.wiki.xml;
3 import java.util.ArrayList;
5 import com.thoughtworks.xstream.alias.ClassMapper;
6 import com.thoughtworks.xstream.converters.ConversionException;
7 import com.thoughtworks.xstream.converters.Converter;
8 import com.thoughtworks.xstream.converters.MarshallingContext;
9 import com.thoughtworks.xstream.converters.UnmarshallingContext;
10 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
11 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
14 * Converts a Page object from and to xml
16 public class PageConverter implements Converter {
17 protected ClassMapper classMapper;
19 protected String classAttributeIdentifier;
21 public PageConverter(ClassMapper classMapper, String classAttributeIdentifier) {
22 this.classMapper = classMapper;
23 this.classAttributeIdentifier = classAttributeIdentifier;
26 public boolean canConvert(Class type) {
27 return type.equals(Page.class);
30 public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
31 Page page = (Page) source;
32 writer.startNode("title");
33 writer.setValue(page.title);
36 ArrayList list = page.listOfRevisions;
37 for (int i = 0; i < list.size(); i++) {
38 Object item = list.get(i);
39 writeItem(item, context, writer);
43 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
44 Page page = (Page) createPage(context.getRequiredType());
46 page.title = (String) reader.getValue();
48 populatePage(reader, context, page);
52 protected void populatePage(HierarchicalStreamReader reader, UnmarshallingContext context, Page page) {
53 ArrayList list = page.listOfRevisions;
55 page.listOfRevisions = new ArrayList();
56 list = page.listOfRevisions;
58 while (reader.hasMoreChildren()) {
60 Object item = readItem(reader, context, page);
66 protected void writeItem(Object item, MarshallingContext context, HierarchicalStreamWriter writer) {
67 // PUBLISHED API METHOD! If changing signature, ensure backwards compatability.
69 // todo: this is duplicated in TreeMarshaller.start()
70 writer.startNode(classMapper.lookupName(ClassMapper.Null.class));
73 writer.startNode(classMapper.lookupName(item.getClass()));
74 context.convertAnother(item);
79 protected Object readItem(HierarchicalStreamReader reader, UnmarshallingContext context, Object current) {
80 // PUBLISHED API METHOD! If changing signature, ensure backwards compatability.
81 String classAttribute = reader.getAttribute(classAttributeIdentifier);
83 if (classAttribute == null) {
84 type = classMapper.lookupType(reader.getNodeName());
86 type = classMapper.lookupType(classAttribute);
88 return context.convertAnother(current, type);
91 protected Object createPage(Class type) {
92 Class defaultType = classMapper.lookupDefaultType(type);
94 return defaultType.newInstance();
95 } catch (InstantiationException e) {
96 throw new ConversionException("Cannot instantiate " + defaultType.getName(), e);
97 } catch (IllegalAccessException e) {
98 throw new ConversionException("Cannot instantiate " + defaultType.getName(), e);