package org.codehaus.xstream.modeller.config;

import java.util.Collection;

import org.codehaus.xstream.modeller.Graph;
import org.codehaus.xstream.modeller.XNode;
import org.codehaus.xstream.modeller.dom.Attribute;
import org.codehaus.xstream.modeller.dom.Element;
import org.codehaus.xstream.modeller.dom.NodeType;

public class XStreamConfigGenerator implements ConfigGenerator {

	private final String basePackage;

	public XStreamConfigGenerator(String basePackage) {
		this.basePackage = basePackage;
	}

	public void printConfiguration(Graph graph) {

		String code = "";
		if (!basePackage.equals("")) {
			code += "package " + basePackage + ";\n\n";
		}
		code += "public class CustomXStream extends XStream {\n";
		code += "\tpublic CustomXStream() {\n";

		Collection<XNode> nodes = graph.getElements();
		for (XNode node : nodes) {
			Element type = (Element) node.getType();
			String name = type.getName();
			if (type.isImplicitCollection()) {
				continue;
			}
			code += "\t\talias(\"" + name + "\", " + name + ".class);\n";
			for (XNode child : type.getChildren()) {
				NodeType childType = child.getType();
				// TODO nasty... should use some type of enum? factory? anything else
				if (childType.getClass().equals(Element.class)) {
					Element el = (Element) childType;
					if (el.isImplicitCollection()) {
						code += "\t\taddImplicitCollection(\"" + el.getName()
								+ "\", \"" + el.getName() + "\", " + name
								+ ".class);\n";
					}
				} else if (childType.getClass().equals(Attribute.class)) {
					code += "\t\tuseAttributeFor(\"" + childType.getName()
							+ "\", \"" + childType.getName() + "\", " + name
							+ ".class);\n";
				}
			}
		}

		code += "\t}\n";
		code += "}\n";
		System.out.println(code);
	}

}

@@ -1012,28 +1012,36 @@
  + %22%5C%22, %22 + 
+tipify(
 name
+)
  + %22.class);
@@ -1433,36 +1433,44 @@
 me() + %22%5C%22, %22 + 
+tipify(
 name
+)
 %0A%09%09%09%09%09%09%09%09+ %22.cla
@@ -1659,20 +1659,28 @@
 %5C%22, %22 + 
+tipify(
 name
+)
 %0A%09%09%09%09%09%09%09
@@ -1747,16 +1747,16 @@
  %22%7D%5Cn%22;%0A
-
 %09%09System
@@ -1779,10 +1779,116 @@
 e);%0A%09%7D%0A%0A
+%09private String tipify(String name) %7B%0A%09%09return name.substring(0,1).toUpperCase() + name.substring(1);%0A%09%7D%0A%0A
 %7D%0A

tipify class names