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