AnAlternativeWriter.java
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import java.io.PrintWriter;
/**
* This XStream writer allows objects to be serialized to an alternative
* indentation based syntax, rather than XML.
*
* DISCLAIMER: This class is provided as an example for how to create your
* own HierarchicalStreamWriter in XStream...
* IT IS BY NO MEANS A COMPLETE IMPLEMENTATION!
*
* See http://joe.truemesh.com/blog/000479.html
*
* @author Joe Walnes
*/
public class AnAlternativeWriter implements HierarchicalStreamWriter {
private int indent;
private final PrintWriter out;
public AnAlternativeWriter(PrintWriter out) {
this.out = out;
}
public void startNode(String name) {
if (indent > 0) {
out.println();
}
for (int i = 0; i < indent; i++) {
out.print(" ");
}
out.print(name);
indent++;
}
public void addAttribute(String name, String value) {
out.print(" [" + name + "=" + value + "]");
}
public void setValue(String text) {
out.print(" = " + text);
// note, this does not correctly escape dodgy chars
}
public void endNode() {
indent--;
if (indent == 0) {
out.flush();
}
}
}