View Javadoc

1   package net.sf.crispy.impl.rest;
2   
3   import java.util.Hashtable;
4   import java.util.Iterator;
5   import java.util.Map;
6   import java.util.Vector;
7   import java.util.Map.Entry;
8   
9   import net.sf.crispy.util.Converter;
10  
11  /**
12   * Serialize complex object graph into a XML reprensation.
13   * 
14   * @author Linke
15   *
16   */
17  public class Serializer {
18  	
19  	public static String serialize(Object pvObject) throws Exception {
20  		if (pvObject == null) { return ""; }
21  		 
22  		Object lvSimpleObject = new Converter().makeSimple(pvObject);
23  		
24  		StringBuffer lvXmlStr = new StringBuffer("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>");
25  		if (lvSimpleObject instanceof Map) {
26  			serializeHashtable((Hashtable) lvSimpleObject, 0, -1, lvXmlStr);
27  		}
28  		else if (lvSimpleObject instanceof Vector) {
29  			serializeVector ((Vector) lvSimpleObject, null, 0, -1, lvXmlStr);
30  		}			
31  		else {
32  			serializeSimple(lvSimpleObject, null, -1, lvXmlStr);
33  		}
34  		
35  		return lvXmlStr.toString();
36  		
37  	}
38  	
39  	
40  	public static void serializeHashtable(Hashtable pvHashtable, int id, int parentRef, StringBuffer pvXmlStr) {
41  		int lvParentRef = id;
42  		Iterator it = pvHashtable.entrySet().iterator();
43  		pvXmlStr.append("<CLASS id=\"").append(id).append("\" parentRef=\"").append(parentRef).append("\"").append(" type=\"java.util.Hashtable\">");
44  		while (it.hasNext()) {
45  			Map.Entry lvMapEntry = (Entry) it.next();
46  			Object lvKey = lvMapEntry.getKey();
47  			Object lvValue = lvMapEntry.getValue();
48  			
49  			if (lvValue instanceof Map) {
50  				id++;
51  //				serializeHashtable(pvHashtable, id, lvParentRef, pvXmlStr);
52  				serializeHashtable((Hashtable) lvValue, id, lvParentRef, pvXmlStr);
53  			}
54  			else if (lvValue instanceof Vector) {
55  				id++;
56  				serializeVector ((Vector) lvValue, lvKey, id, lvParentRef, pvXmlStr);
57  			}			
58  			else {
59  				serializeSimple(lvValue, lvKey, lvParentRef, pvXmlStr);
60  			}
61  		}
62  		pvXmlStr.append("</CLASS>");
63  	}
64  	
65  	public static void serializeVector (Vector pvVector, Object pvKey, int id, int parentRef, StringBuffer pvXmlStr) {
66  		if (pvVector.size() > 0) {
67  			int lvParentRef = id;
68  			if (pvKey != null) {
69  				pvXmlStr.append("<CLASS id=\"").append(id).append("\" name=\"").append(pvKey).append("\" parentRef=\"").append(parentRef)
70  											.append("\"").append(" type=\"java.util.Vector\">");
71  				} else {
72  					pvXmlStr.append("<CLASS id=\"").append(id).append("\" parentRef=\"").append(parentRef).append("\"").append(" type=\"java.util.Vector\">");
73  				}
74  			for (int i = 0; i < pvVector.size(); i++) {
75  				Object lvValue = pvVector.get(i);
76  				if (lvValue instanceof Map) {
77  					id++;
78  					serializeHashtable((Hashtable) lvValue, id, lvParentRef, pvXmlStr);
79  				}
80  				else if (lvValue instanceof Vector) {
81  					id++;
82  					serializeVector ((Vector) lvValue, null, id, lvParentRef, pvXmlStr);
83  				}			
84  				else {
85  					id++;
86  					String lvKey = "param" + id;
87  					serializeSimple(lvValue, lvKey, lvParentRef, pvXmlStr);
88  				}
89  			}
90  			pvXmlStr.append("</CLASS>");
91  			parentRef++;
92  		}
93  	}
94  
95  	public static void serializeSimple (Object pvValue, Object pvKey, int parentRef, StringBuffer pvXmlStr) {
96  		Object lvKey = (pvKey != null ? pvKey : pvValue.getClass().getName());
97  		String lvType = pvValue.getClass().getName();
98  		if (lvKey.equals(lvType)) {
99  			pvXmlStr.append("<property parentRef=\"").append(parentRef).append("\" type=\"").append(lvType)
100 			.append("\"><![CDATA[").append(pvValue).append("]]></property>");	
101 		} else {
102 			pvXmlStr.append("<property parentRef=\"").append(parentRef).append("\" name=\"").append(lvKey)
103 					.append("\" type=\"").append(lvType)
104 					.append("\"><![CDATA[").append(pvValue).append("]]></property>");
105 		}
106 	}
107 }