1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.springframework.extensions.webscripts;
20
21 import java.util.Collection;
22 import java.util.Iterator;
23 import java.util.Map;
24 import java.util.Set;
25
26 import org.mozilla.javascript.Scriptable;
27 import org.mozilla.javascript.Wrapper;
28
29
30
31
32
33
34
35
36
37
38
39
40 public class ScriptableWrappedMap implements ScriptableMap, Wrapper
41 {
42 private Map map;
43 private Scriptable parentScope;
44 private Scriptable prototype;
45
46
47
48
49
50
51
52
53
54
55 public static ScriptableWrappedMap wrap(Scriptable scope, Map<Object, Object> map)
56 {
57 return new ScriptableWrappedMap(scope, map);
58 }
59
60
61
62
63
64
65 public ScriptableWrappedMap(Map map)
66 {
67 this.map = map;
68 }
69
70
71
72
73
74
75
76 public ScriptableWrappedMap(Scriptable scope, Map map)
77 {
78 this.parentScope = scope;
79 this.map = map;
80 }
81
82
83
84
85 public Object unwrap()
86 {
87 return map;
88 }
89
90
91
92
93 public String getClassName()
94 {
95 return "ScriptableWrappedMap";
96 }
97
98
99
100
101 public Object get(String name, Scriptable start)
102 {
103
104 if ("length".equals(name))
105 {
106 return map.size();
107 }
108 else
109 {
110 return ScriptValueConverter.wrapValue(this.parentScope != null ? this.parentScope : start, map.get(name));
111 }
112 }
113
114
115
116
117 public Object get(int index, Scriptable start)
118 {
119 Object value = null;
120 int i=0;
121 Iterator itrValues = map.values().iterator();
122 while (i++ <= index && itrValues.hasNext())
123 {
124 value = itrValues.next();
125 }
126 return ScriptValueConverter.wrapValue(this.parentScope != null ? this.parentScope : start, value);
127 }
128
129
130
131
132 public boolean has(String name, Scriptable start)
133 {
134
135 return map.containsKey(name);
136 }
137
138
139
140
141 public boolean has(int index, Scriptable start)
142 {
143 return (index >= 0 && map.values().size() > index);
144 }
145
146
147
148
149 @SuppressWarnings("unchecked")
150 public void put(String name, Scriptable start, Object value)
151 {
152 map.put(name, ScriptValueConverter.unwrapValue(value));
153 }
154
155
156
157
158 public void put(int index, Scriptable start, Object value)
159 {
160
161 }
162
163
164
165
166 public void delete(String name)
167 {
168 map.remove(name);
169 }
170
171
172
173
174 public void delete(int index)
175 {
176 int i=0;
177 Iterator itrKeys = map.keySet().iterator();
178 while (i <= index && itrKeys.hasNext())
179 {
180 Object key = itrKeys.next();
181 if (i == index)
182 {
183 map.remove(key);
184 break;
185 }
186 }
187 }
188
189
190
191
192 public Scriptable getPrototype()
193 {
194 return this.prototype;
195 }
196
197
198
199
200 public void setPrototype(Scriptable prototype)
201 {
202 this.prototype = prototype;
203 }
204
205
206
207
208 public Scriptable getParentScope()
209 {
210 return this.parentScope;
211 }
212
213
214
215
216 public void setParentScope(Scriptable parent)
217 {
218 this.parentScope = parent;
219 }
220
221
222
223
224 public Object[] getIds()
225 {
226 return map.keySet().toArray();
227 }
228
229
230
231
232 public Object getDefaultValue(Class hint)
233 {
234 return null;
235 }
236
237
238
239
240 public boolean hasInstance(Scriptable value)
241 {
242 if (!(value instanceof Wrapper))
243 return false;
244 Object instance = ((Wrapper)value).unwrap();
245 return Map.class.isInstance(instance);
246 }
247
248
249
250
251 public void clear()
252 {
253 this.map.clear();
254 }
255
256
257
258
259 public boolean containsKey(Object key)
260 {
261 return this.map.containsKey(key);
262 }
263
264
265
266
267 public boolean containsValue(Object value)
268 {
269 return this.map.containsValue(value);
270 }
271
272
273
274
275 public Set entrySet()
276 {
277 return this.map.entrySet();
278 }
279
280
281
282
283 public Object get(Object key)
284 {
285 return this.map.get(key);
286 }
287
288
289
290
291 public boolean isEmpty()
292 {
293 return (this.map.size() == 0);
294 }
295
296
297
298
299 public Set keySet()
300 {
301 return this.map.keySet();
302 }
303
304
305
306
307 public Object put(Object key, Object value)
308 {
309 return this.map.put(key, value);
310 }
311
312
313
314
315 public void putAll(Map t)
316 {
317 this.putAll(t);
318 }
319
320
321
322
323 public Object remove(Object key)
324 {
325 return this.map.remove(key);
326 }
327
328
329
330
331 public int size()
332 {
333 return this.map.size();
334 }
335
336
337
338
339 public Collection values()
340 {
341 return this.map.values();
342 }
343 }