View Javadoc

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