Added xstream handiling for Wikipedia upload/download
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / xml / PageConverter.java
1 package net.sourceforge.phpeclipse.wiki.xml;
2
3 import java.util.ArrayList;
4
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;
12
13 /**
14  *  Converts a Page object from and to xml
15  */
16 public class PageConverter implements Converter {
17   protected ClassMapper classMapper;
18
19   protected String classAttributeIdentifier;
20
21   public PageConverter(ClassMapper classMapper, String classAttributeIdentifier) {
22     this.classMapper = classMapper;
23     this.classAttributeIdentifier = classAttributeIdentifier;
24   }
25
26   public boolean canConvert(Class type) {
27     return type.equals(Page.class);
28   }
29
30   public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
31     Page page = (Page) source;
32     writer.startNode("title");
33     writer.setValue(page.title);
34     writer.endNode();
35
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);
40     }
41   }
42
43   public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
44     Page page = (Page) createPage(context.getRequiredType());
45     reader.moveDown();
46     page.title = (String) reader.getValue();
47     reader.moveUp();
48     populatePage(reader, context, page);
49     return page;
50   }
51
52   protected void populatePage(HierarchicalStreamReader reader, UnmarshallingContext context, Page page) {
53     ArrayList list = page.listOfRevisions;
54     if (list == null) {
55       page.listOfRevisions = new ArrayList();
56       list = page.listOfRevisions;
57     }
58     while (reader.hasMoreChildren()) {
59       reader.moveDown();
60       Object item = readItem(reader, context, page);
61       list.add(item);
62       reader.moveUp();
63     }
64   }
65
66   protected void writeItem(Object item, MarshallingContext context, HierarchicalStreamWriter writer) {
67     // PUBLISHED API METHOD! If changing signature, ensure backwards compatability.
68     if (item == null) {
69       // todo: this is duplicated in TreeMarshaller.start()
70       writer.startNode(classMapper.lookupName(ClassMapper.Null.class));
71       writer.endNode();
72     } else {
73       writer.startNode(classMapper.lookupName(item.getClass()));
74       context.convertAnother(item);
75       writer.endNode();
76     }
77   }
78
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);
82     Class type;
83     if (classAttribute == null) {
84       type = classMapper.lookupType(reader.getNodeName());
85     } else {
86       type = classMapper.lookupType(classAttribute);
87     }
88     return context.convertAnother(current, type);
89   }
90
91   protected Object createPage(Class type) {
92     Class defaultType = classMapper.lookupDefaultType(type);
93     try {
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);
99     }
100   }
101 }