// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class CustomSerialize {
    public static void main(String[] args) throws IOException {
        Big o = new Big("Fine", new Problem("OK", "Ooops"));
        // standard
        XStream xs1 = new XStream(new DomDriver());
        xs1.alias("big", Big.class);
        OutputStream s1 = new FileOutputStream("C:\\standard.xml");
        xs1.toXML(o, s1);
        s1.close();
        XStream xs2 = new XStream(new DomDriver());
        xs2.alias("big", Big.class);
        InputStream s2 = new FileInputStream("C:\\standard.xml");
        Big o2 = (Big)xs2.fromXML(s2);
        s2.close();
        System.out.println(o2);
        // special
        XStream xs3 = new XStream(new DomDriver());
        xs3.alias("big", Big.class);
        xs3.registerConverter(new ProblemConverter());
        OutputStream s3 = new FileOutputStream("C:\\special.xml");
        xs3.toXML(o, s3);
        s3.close();
        XStream xs4 = new XStream(new DomDriver());
        xs4.alias("big", Big.class);
        xs4.registerConverter(new ProblemConverter());
        InputStream s4 = new FileInputStream("C:\\special.xml");
        Big o4 = (Big)xs4.fromXML(s4);
        s4.close();
        System.out.println(o4);
    }
}

class Big {
    private String good;
    private Problem mixed;
    public Big() {
        this("", new Problem());
    }
    public Big(String good, Problem mixed) {
        super();
        this.good = good;
        this.mixed = mixed;
    }
    public String getGood() {
        return good;
    }
    public void setGood(String good) {
        this.good = good;
    }
    public Problem getMixed() {
        return mixed;
    }
    public void setMixed(Problem mixed) {
        this.mixed = mixed;
    }
    public String toString() {
        return "[" + good + "," + mixed + "]";
    }
}

class Problem {
    private String good;
    private String bad;
    public Problem() {
        this("", "");
    }
    public Problem(String good, String bad) {
        super();
        this.good = good;
        this.bad = bad;
    }
    public String getGood() {
        return good;
    }
    public void setGood(String good) {
        this.good = good;
    }
    public String getBad() {
        return bad;
    }
    public void setBad(String bad) {
        this.bad = bad;
    }
    public String toString() {
        return "[" + good + "," + bad + "]";
    }
}

class ProblemConverter implements Converter {
    public void marshal(Object o, HierarchicalStreamWriter hsr, MarshallingContext mc) {
        Problem o2 = (Problem)o; 
        hsr.startNode("good");
        hsr.setValue(o2.getGood());
        hsr.endNode();
    }
    public Object unmarshal(HierarchicalStreamReader hsr, UnmarshallingContext mc) {
        Problem o = new Problem();
        while (hsr.hasMoreChildren()) {
            hsr.moveDown();
            if(hsr.getNodeName().equals("good")) {
                o.setGood(hsr.getValue());
            }
            hsr.moveUp();
        }
        return o;
    }
    public boolean canConvert(Class c) {
        return c.equals(Problem.class);
    }
}
