package com.arondor.common.reflection.xstream;

import java.util.Map;
import java.util.logging.Logger;

import com.arondor.common.reflection.model.config.ElementConfiguration;
import com.arondor.common.reflection.model.config.ObjectConfiguration;
import com.arondor.common.reflection.model.config.PrimitiveConfiguration;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.XMLParser;

public class GWTObjectConfigurationSerializer
{
    private static final Logger LOG = Logger.getLogger(GWTObjectConfigurationSerializer.class.getName());

    private String classAttributeName = "class";

    public String serialize(ElementConfiguration oc)
    {
        Document document = XMLParser.createDocument();

        Element firstChild = doSerialize(document, oc, null);
        document.appendChild(firstChild);
        return document.toString();
    }

    public Element doSerialize(Document document, ElementConfiguration ec, String name)
    {
        String tagName = name, className = null;
        if (tagName == null)
        {
            if (ec instanceof ObjectConfiguration)
            {
                ObjectConfiguration oc = (ObjectConfiguration) ec;
                if (oc.getObjectName() != null)
                {
                    tagName = oc.getObjectName();
                }
                if (oc.getClassName() != null)
                {
                    if (tagName == null)
                    {
                        tagName = oc.getClassName();
                    }
                    else
                    {
                        className = oc.getClassName();
                    }
                }
            }
        }
        if (tagName == null)
        {
            tagName = "{unknown}";
        }
        Element child = document.createElement(tagName);
        if (className != null)
        {
            child.setAttribute(classAttributeName, className);
        }
        if (ec instanceof ObjectConfiguration)
        {
            ObjectConfiguration oc = (ObjectConfiguration) ec;
            for (Map.Entry<String, ElementConfiguration> entry : oc.getFields().entrySet())
            {
                Element grandChild = doSerialize(document, entry.getValue(), entry.getKey());
                child.appendChild(grandChild);
            }
        }
        else if (ec instanceof PrimitiveConfiguration)
        {
            PrimitiveConfiguration pc = (PrimitiveConfiguration) ec;
            child.appendChild(document.createTextNode(pc.getValue()));
        }
        return child;
    }
}

@@ -1089,53 +1089,8 @@
 l;%0D%0A
-        if (tagName == null)%0D%0A        %7B%0D%0A    
     
@@ -1137,35 +1137,27 @@
 n)%0D%0A        
-   
- 
 %7B%0D%0A
-    
             
@@ -1212,36 +1212,32 @@
 c;%0D%0A            
-    
 if (oc.getObject
@@ -1261,34 +1261,27 @@
             
-    %7B%0D%0A   
+%7B%0D%0A
             
@@ -1276,33 +1276,32 @@
                 
- 
 tagName = oc.get
@@ -1323,34 +1323,27 @@
             
-    %7D%0D%0A   
+%7D%0D%0A
             
@@ -1334,33 +1334,32 @@
  %7D%0D%0A            
- 
 if (oc.getClassN
@@ -1382,34 +1382,27 @@
             
-    %7B%0D%0A   
+%7B%0D%0A
             
@@ -1401,25 +1401,24 @@
             
- 
 if (tagName 
@@ -1439,35 +1439,27 @@
             
-    %7B%0D%0A    
+%7B%0D%0A
             
@@ -1508,34 +1508,27 @@
             
-    %7D%0D%0A   
+%7D%0D%0A
             
@@ -1527,25 +1527,24 @@
             
- 
 else%0D%0A      
@@ -1549,35 +1549,27 @@
             
-    %7B%0D%0A    
+%7B%0D%0A
             
@@ -1612,31 +1612,8 @@
 );%0D%0A
-                    %7D%0D%0A
     

Fix classname setting