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.Iterator;
22 import java.util.Map;
23
24 import org.mozilla.javascript.Scriptable;
25 import org.mozilla.javascript.Wrapper;
26
27
28
29
30
31
32
33
34
35 public class NativeMap implements Scriptable, Wrapper
36 {
37 private static final long serialVersionUID = 3664761893203964569L;
38
39 private Map<Object, Object> map;
40 private Scriptable parentScope;
41 private Scriptable prototype;
42
43
44
45
46
47
48
49
50
51 public static NativeMap wrap(Scriptable scope, Map<Object, Object> map)
52 {
53 return new NativeMap(scope, map);
54 }
55
56
57
58
59
60
61
62 public NativeMap(Scriptable scope, Map<Object, Object> map)
63 {
64 this.parentScope = scope;
65 this.map = map;
66 }
67
68
69
70
71 public Object unwrap()
72 {
73 return map;
74 }
75
76
77
78
79 public String getClassName()
80 {
81 return "NativeMap";
82 }
83
84
85
86
87 public Object get(String name, Scriptable start)
88 {
89
90 if ("length".equals(name))
91 {
92 return map.size();
93 }
94 else
95 {
96 return map.get(name);
97 }
98 }
99
100
101
102
103 public Object get(int index, Scriptable start)
104 {
105 Object value = null;
106 int i=0;
107 Iterator itrValues = map.values().iterator();
108 while (i++ <= index && itrValues.hasNext())
109 {
110 value = itrValues.next();
111 }
112 return value;
113 }
114
115
116
117
118 public boolean has(String name, Scriptable start)
119 {
120
121 return map.containsKey(name);
122 }
123
124
125
126
127 public boolean has(int index, Scriptable start)
128 {
129 return (index >= 0 && map.values().size() > index);
130 }
131
132
133
134
135 @SuppressWarnings("unchecked")
136 public void put(String name, Scriptable start, Object value)
137 {
138 map.put(name, value);
139 }
140
141
142
143
144 public void put(int index, Scriptable start, Object value)
145 {
146
147 }
148
149
150
151
152 public void delete(String name)
153 {
154 map.remove(name);
155 }
156
157
158
159
160 public void delete(int index)
161 {
162 int i=0;
163 Iterator itrKeys = map.keySet().iterator();
164 while (i <= index && itrKeys.hasNext())
165 {
166 Object key = itrKeys.next();
167 if (i == index)
168 {
169 map.remove(key);
170 break;
171 }
172 }
173 }
174
175
176
177
178 public Scriptable getPrototype()
179 {
180 return this.prototype;
181 }
182
183
184
185
186 public void setPrototype(Scriptable prototype)
187 {
188 this.prototype = prototype;
189 }
190
191
192
193
194 public Scriptable getParentScope()
195 {
196 return this.parentScope;
197 }
198
199
200
201
202 public void setParentScope(Scriptable parent)
203 {
204 this.parentScope = parent;
205 }
206
207
208
209
210 public Object[] getIds()
211 {
212 return map.keySet().toArray();
213 }
214
215
216
217
218 public Object getDefaultValue(Class hint)
219 {
220 return null;
221 }
222
223
224
225
226 public boolean hasInstance(Scriptable value)
227 {
228 if (!(value instanceof Wrapper))
229 return false;
230 Object instance = ((Wrapper)value).unwrap();
231 return Map.class.isInstance(instance);
232 }
233
234 }