View Javadoc

1   /*
2    * Copyright 1999-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.struts.flow.sugar;
17  
18  import org.mozilla.javascript.NativeJavaObject;
19  import org.mozilla.javascript.Scriptable;
20  import org.mozilla.javascript.Wrapper;
21  
22  import java.util.Map;
23  import java.io.Serializable;
24  
25  /***
26   * Wrap a java.util.Map for JavaScript.
27   */
28  public class ScriptableMap extends JavaObjectWrapper implements Scriptable, Wrapper, Serializable {
29  
30      private Map map;
31  
32      public ScriptableMap() {
33          super();
34      }
35  
36      public ScriptableMap(Map map) {
37          this.map = map;
38      }
39      
40      public ScriptableMap(Scriptable scope, Object javaObject, Class staticType, Map functions) {
41          super(scope, javaObject, staticType, functions);
42          if (javaObject instanceof Map) {
43              this.map = (Map)javaObject;
44          } else {
45              throw new IllegalArgumentException("Passed object "+javaObject+" is not an instance of Map");
46          }
47      }
48  
49      public String getClassName() {
50          return "Map";
51      }
52  
53      public boolean has(String name, Scriptable start) {
54          return (super.has(name, start) || this.map.containsKey(name));
55      }
56  
57      /***
58       * no numeric properties
59       */
60      public boolean has(int index, Scriptable start) {
61          return false;
62      }
63  
64      public Object get(String name, Scriptable start) {
65          if (super.has(name, start)) {
66              return super.get(name, start);
67          } else if (this.map.containsKey(name)) {
68              return this.map.get(name);
69          } else {
70              return Scriptable.NOT_FOUND;
71          }
72      }
73  
74      public Object get(int index, Scriptable start) {
75          return NOT_FOUND;
76      }
77  
78      public void put(String name, Scriptable start, Object value) {
79          if (value instanceof NativeJavaObject) {
80              value = ((NativeJavaObject)value).unwrap();
81          }
82          map.put(name, value);
83      }
84  
85      public void put(int index, Scriptable start, Object value) {
86      }
87  
88      public void delete(String id) {
89          map.remove(id);
90      }
91  
92      public void delete(int index) {
93      }
94  
95      public Scriptable getPrototype() {
96          return prototype;
97      }
98  
99      public void setPrototype(Scriptable prototype) {
100         this.prototype = prototype;
101     }
102 
103     public Scriptable getParentScope() {
104         return parent;
105     }
106 
107     public void setParentScope(Scriptable parent) {
108         this.parent = parent;
109     }
110 
111     public Object[] getIds() {
112         return this.map.keySet().toArray();
113     }
114 
115     public Object getDefaultValue(Class typeHint) {
116         return this.map.toString();
117     }
118 
119     public boolean hasInstance(Scriptable value) {
120         Scriptable proto = value.getPrototype();
121         while (proto != null) {
122             if (proto.equals(this)) 
123                 return true;
124             proto = proto.getPrototype();
125         }
126 
127         return false;
128     }
129 
130     /***
131      * Return the java.util.Map that is wrapped by this class.
132      */
133     public Object unwrap() {
134         return this.map;
135     }
136 
137 }