|
1 |
| package net.sf.crispy.impl.rest; |
|
2 |
| |
|
3 |
| import java.util.Iterator; |
|
4 |
| import java.util.List; |
|
5 |
| import java.util.Map; |
|
6 |
| import java.util.TreeMap; |
|
7 |
| import java.util.Vector; |
|
8 |
| import java.util.Map.Entry; |
|
9 |
| |
|
10 |
| import net.sf.crispy.util.Converter; |
|
11 |
| import net.sf.crispy.util.Util; |
|
12 |
| |
|
13 |
| public class ParameterSerializer { |
|
14 |
| |
|
15 |
| public static final String DELIMITTER = "_"; |
|
16 |
| public static final String MAP = "MAP"; |
|
17 |
| public static final String MAPSIMPLE = "MAPSIMPLE"; |
|
18 |
| public static final String LIST = "LIST"; |
|
19 |
| public static final String ARRAY = "ARRAY"; |
|
20 |
| public static final String PARAM = "param"; |
|
21 |
| |
|
22 |
| |
|
23 |
| private StringBuffer strBuff = null; |
|
24 |
| private int id = -1; |
|
25 |
| private int idListMap = -1; |
|
26 |
| |
|
27 |
83
| public String serialize(Object pvObject) throws Exception {
|
|
28 |
83
| strBuff = new StringBuffer();
|
|
29 |
83
| id = -1;
|
|
30 |
83
| idListMap = -1;
|
|
31 |
| |
|
32 |
3
| if (pvObject == null) { return ""; }
|
|
33 |
| |
|
34 |
80
| if (pvObject.getClass().isArray()) {
|
|
35 |
44
| Object o[] = (Object[]) pvObject;
|
|
36 |
44
| for (int i = 0; i < o.length; i++) {
|
|
37 |
61
| if (o[i].getClass().isArray()) {
|
|
38 |
| |
|
39 |
| |
|
40 |
0
| throw new IllegalArgumentException("The Crispy REST implementation not supported Arrays: "
|
|
41 |
| + Util.array2String((Object[]) o[i]) + " (" + o[i] + ")"); |
|
42 |
| } |
|
43 |
| } |
|
44 |
| } |
|
45 |
| |
|
46 |
| |
|
47 |
80
| Object lvSimpleObject = new Converter().makeSimple(pvObject);
|
|
48 |
| |
|
49 |
80
| if (lvSimpleObject instanceof Map) {
|
|
50 |
20
| serializeMap((Map) lvSimpleObject, null, getNextId());
|
|
51 |
| } |
|
52 |
60
| else if (lvSimpleObject instanceof Vector) {
|
|
53 |
56
| List lvList = (List) lvSimpleObject;
|
|
54 |
| |
|
55 |
44
| if (isSimpleList(lvList)) { makeSimpleList(lvList); }
|
|
56 |
12
| else { serializeList(lvList, null, getNextId()); }
|
|
57 |
| } |
|
58 |
| else { |
|
59 |
4
| serializeSimple(null, lvSimpleObject, getNextId());
|
|
60 |
| } |
|
61 |
| |
|
62 |
80
| String ret = strBuff.toString();
|
|
63 |
80
| if (ret.endsWith("&")) { ret = ret.substring(0, (ret.length() - 1)); }
|
|
64 |
80
| return ret;
|
|
65 |
| } |
|
66 |
| |
|
67 |
92
| public void serializeMap(final Map pvMap, String pvParentProperty, int pvId) {
|
|
68 |
92
| Iterator it = pvMap.entrySet().iterator();
|
|
69 |
| |
|
70 |
92
| String lvClassString = (String) pvMap.get("class");
|
|
71 |
92
| while (it.hasNext()) {
|
|
72 |
558
| Map.Entry lvMapEntry = (Entry) it.next();
|
|
73 |
558
| String lvKey = (String) lvMapEntry.getKey();
|
|
74 |
558
| Object lvValue = lvMapEntry.getValue();
|
|
75 |
558
| if (lvValue instanceof Map) {
|
|
76 |
92
| Map lvMapValue = (Map) lvValue;
|
|
77 |
92
| if (lvMapValue.size() > 0) {
|
|
78 |
17
| int lvId = getNextId();
|
|
79 |
| |
|
80 |
17
| if ((lvClassString == null) && (pvParentProperty == null)) {
|
|
81 |
2
| serializeSimple(new StringBuffer(MAPSIMPLE).append(DELIMITTER).append(lvKey).toString(), PARAM + getNextId(), pvId);
|
|
82 |
2
| serializeMap(lvMapValue, lvKey, getId());
|
|
83 |
| } else { |
|
84 |
| |
|
85 |
15
| if (pvParentProperty == null) {
|
|
86 |
8
| serializeSimple(new StringBuffer(MAP).append(DELIMITTER).append(lvKey).toString(), PARAM + lvId, pvId);
|
|
87 |
8
| serializeMap(lvMapValue, lvKey, getId());
|
|
88 |
| } else { |
|
89 |
7
| serializeSimple(new StringBuffer(MAP).append(DELIMITTER).append(pvParentProperty)
|
|
90 |
| .append(DELIMITTER).append(lvKey).toString(), PARAM + lvId, pvId); |
|
91 |
7
| serializeMap(lvMapValue, null, getId());
|
|
92 |
| } |
|
93 |
| } |
|
94 |
| } |
|
95 |
466
| } else if (lvValue instanceof List) {
|
|
96 |
177
| List lvListValue = (List) lvValue;
|
|
97 |
177
| if (lvListValue.size() > 0) {
|
|
98 |
32
| serializeList(lvListValue, lvKey, pvId);
|
|
99 |
| } |
|
100 |
| } |
|
101 |
| else { |
|
102 |
289
| if (lvClassString == null) {
|
|
103 |
| |
|
104 |
13
| if (lvValue.toString().startsWith(Converter.HASH_CODE_KEY)) {
|
|
105 |
5
| serializeSimple(new StringBuffer(MAP).append(DELIMITTER).append(pvParentProperty)
|
|
106 |
| .append(DELIMITTER).append(lvKey).toString(), lvValue, pvId); |
|
107 |
| |
|
108 |
| } else { |
|
109 |
8
| serializeSimple(new StringBuffer(MAPSIMPLE).append(DELIMITTER).append(lvKey).toString(), lvValue, pvId);
|
|
110 |
| } |
|
111 |
| } else { |
|
112 |
276
| serializeSimple(lvKey, lvValue, pvId);
|
|
113 |
| } |
|
114 |
| } |
|
115 |
| } |
|
116 |
| } |
|
117 |
| |
|
118 |
| |
|
119 |
44
| public void serializeList(final List pvList, String pvParentProperty, int pvId) {
|
|
120 |
44
| getNextIdListMap ();
|
|
121 |
44
| for (Iterator it = pvList.iterator(); it.hasNext();) {
|
|
122 |
73
| Object lvValue = it.next();
|
|
123 |
73
| if (lvValue instanceof Map) {
|
|
124 |
55
| Map lvMapValue = (Map) lvValue;
|
|
125 |
55
| if (lvMapValue.size() > 0) {
|
|
126 |
55
| int lvId = getNextId();
|
|
127 |
55
| if (pvParentProperty != null) {
|
|
128 |
40
| serializeSimple(new StringBuffer(LIST).append(DELIMITTER).append(pvParentProperty).toString(), PARAM + getNextId(), pvId);
|
|
129 |
| } else { |
|
130 |
15
| serializeSimple(new StringBuffer(LIST).append(DELIMITTER).append(getNextIdListMap()).toString(), PARAM + lvId, pvId);
|
|
131 |
| } |
|
132 |
55
| serializeMap(lvMapValue, pvParentProperty, getId());
|
|
133 |
| } |
|
134 |
18
| } else if (lvValue instanceof List) {
|
|
135 |
0
| List lvListValue = (List) lvValue;
|
|
136 |
0
| if (lvListValue.size() > 0) {
|
|
137 |
| |
|
138 |
0
| serializeList(lvListValue, pvParentProperty, pvId);
|
|
139 |
| } |
|
140 |
| } |
|
141 |
| else { |
|
142 |
| |
|
143 |
18
| if (pvParentProperty == null) {
|
|
144 |
8
| serializeSimple(new StringBuffer(LIST).append(DELIMITTER).append(getIdListMap()).toString(), lvValue, pvId);
|
|
145 |
| } else { |
|
146 |
| |
|
147 |
10
| serializeSimple(new StringBuffer(LIST).append(DELIMITTER).append(pvParentProperty).toString(), lvValue, pvId);
|
|
148 |
| } |
|
149 |
| |
|
150 |
| } |
|
151 |
| } |
|
152 |
| } |
|
153 |
| |
|
154 |
| |
|
155 |
448
| public void serializeSimple (String pvKey, Object pvValue, int pvId) {
|
|
156 |
448
| strBuff.append("param").append(pvId);
|
|
157 |
448
| if ((pvKey != null) && (pvKey.length() > 0)) {
|
|
158 |
379
| strBuff.append(DELIMITTER).append(pvKey);
|
|
159 |
| } |
|
160 |
448
| strBuff.append("=").append(pvValue).append("&");
|
|
161 |
| } |
|
162 |
| |
|
163 |
215
| private int getNextId () { return ++id; }
|
|
164 |
137
| private int getId () { return id; }
|
|
165 |
| |
|
166 |
59
| private int getNextIdListMap () { return ++idListMap; }
|
|
167 |
8
| private int getIdListMap () { return idListMap; }
|
|
168 |
| |
|
169 |
| |
|
170 |
56
| private boolean isSimpleList (final List pvList) {
|
|
171 |
56
| for (Iterator it = pvList.iterator(); it.hasNext();) {
|
|
172 |
77
| Object lvValue = it.next();
|
|
173 |
77
| if ((lvValue instanceof Map) || (lvValue instanceof List)) {
|
|
174 |
12
| return false;
|
|
175 |
| } |
|
176 |
| } |
|
177 |
44
| return true;
|
|
178 |
| } |
|
179 |
| |
|
180 |
44
| private void makeSimpleList (final List pvList) {
|
|
181 |
| |
|
182 |
44
| for (Iterator it = pvList.iterator(); it.hasNext();) {
|
|
183 |
65
| Object lvValue = it.next();
|
|
184 |
65
| getNextId();
|
|
185 |
65
| serializeSimple(null, lvValue, getId());
|
|
186 |
| } |
|
187 |
| } |
|
188 |
| |
|
189 |
6
| public Map toSortedMap () {
|
|
190 |
6
| String s[] = strBuff.toString().split("&");
|
|
191 |
6
| Map sortMap = new TreeMap();
|
|
192 |
6
| for (int i = 0; i < s.length; i++) {
|
|
193 |
61
| sortMap.put(s[i], "");
|
|
194 |
| } |
|
195 |
6
| return sortMap;
|
|
196 |
| } |
|
197 |
| |
|
198 |
0
| public void printSortedMap (Map pvMap) {
|
|
199 |
0
| Iterator it = pvMap.keySet().iterator();
|
|
200 |
0
| while (it.hasNext()) {
|
|
201 |
0
| System.out.println(it.next());
|
|
202 |
| } |
|
203 |
| } |
|
204 |
| } |