1 package net.sourceforge.phpeclipse.wiki.xml;
3 import java.io.InputStream;
4 import java.io.InputStreamReader;
7 import com.thoughtworks.xstream.XStream;
8 import com.thoughtworks.xstream.io.xml.DomDriver;
10 public class XStreamManager {
12 public static String toXML(Page page) {
13 XStream xstream = new XStream(new DomDriver());
14 xstream.alias("page", Page.class);
15 xstream.changeDefaultConverter(new PageConverter(xstream.getClassMapper(), "revision"));
16 xstream.alias("revision", Revision.class);
17 return xstream.toXML(page);
20 public static Page fromXML(String xmlString) {
21 XStream xstream = new XStream(new DomDriver());
22 xstream.alias("page", Page.class);
23 xstream.changeDefaultConverter(new PageConverter(xstream.getClassMapper(), "revision"));
24 xstream.alias("revision", Revision.class);
25 return (Page) xstream.fromXML(xmlString);
28 public static Page fromXML(Reader xmlReader) {
29 XStream xstream = new XStream(new DomDriver());
30 xstream.alias("page", Page.class);
31 xstream.changeDefaultConverter(new PageConverter(xstream.getClassMapper(), "revision"));
32 xstream.alias("revision", Revision.class);
33 return (Page) xstream.fromXML(xmlReader);
36 public static Page fromXML(InputStream iStream) {
37 InputStreamReader sReader = new InputStreamReader(iStream);
38 return fromXML(sReader);
41 public static void main(String[] args) {
42 Page page = new Page("20050129", "title", "body1");
43 page.add(new Revision("20050130", "body2", "Joe"));
44 page.add(new Revision("20050131", "body3"));
45 String xml = toXML(page);
46 System.out.println(xml);
48 Page newXML = (Page) fromXML(xml);
49 System.out.println(newXML);