View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * Wrapper for exposing maps in Rhino scripts. 
30   * 
31   * Note: A duplicate of this map implementation exists in the Repository project
32   * 
33   * @author davidc
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       * Construct
46       * 
47       * @param scope
48       * @param map
49       * @return  native map
50       */
51      public static NativeMap wrap(Scriptable scope, Map<Object, Object> map)
52      {
53          return new NativeMap(scope, map);
54      }
55  
56      /**
57       * Construct
58       * 
59       * @param scope
60       * @param map
61       */
62      public NativeMap(Scriptable scope, Map<Object, Object> map)
63      {
64          this.parentScope = scope;
65          this.map = map;
66      }
67  
68      /* (non-Javadoc)
69       * @see org.mozilla.javascript.Wrapper#unwrap()
70       */
71      public Object unwrap()
72      {
73          return map;
74      }
75  
76      /* (non-Javadoc)
77       * @see org.mozilla.javascript.Scriptable#getClassName()
78       */
79      public String getClassName()
80      {
81          return "NativeMap";
82      }
83  
84      /* (non-Javadoc)
85       * @see org.mozilla.javascript.Scriptable#get(java.lang.String, org.mozilla.javascript.Scriptable)
86       */
87      public Object get(String name, Scriptable start)
88      {
89          // get the property from the underlying QName map
90          if ("length".equals(name))
91          {
92              return map.size();
93          }
94          else
95          {
96              return map.get(name);
97          }
98      }
99  
100     /* (non-Javadoc)
101      * @see org.mozilla.javascript.Scriptable#get(int, org.mozilla.javascript.Scriptable)
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     /* (non-Javadoc)
116      * @see org.mozilla.javascript.Scriptable#has(java.lang.String, org.mozilla.javascript.Scriptable)
117      */
118     public boolean has(String name, Scriptable start)
119     {
120         // locate the property in the underlying map
121         return map.containsKey(name);
122     }
123 
124     /* (non-Javadoc)
125      * @see org.mozilla.javascript.Scriptable#has(int, org.mozilla.javascript.Scriptable)
126      */
127     public boolean has(int index, Scriptable start)
128     {
129         return (index >= 0 && map.values().size() > index);
130     }
131 
132     /* (non-Javadoc)
133      * @see org.mozilla.javascript.Scriptable#put(java.lang.String, org.mozilla.javascript.Scriptable, java.lang.Object)
134      */
135     @SuppressWarnings("unchecked")
136     public void put(String name, Scriptable start, Object value)
137     {
138         map.put(name, value);
139     }
140 
141     /* (non-Javadoc)
142      * @see org.mozilla.javascript.Scriptable#put(int, org.mozilla.javascript.Scriptable, java.lang.Object)
143      */
144     public void put(int index, Scriptable start, Object value)
145     {
146         // TODO: implement?
147     }
148 
149     /* (non-Javadoc)
150      * @see org.mozilla.javascript.Scriptable#delete(java.lang.String)
151      */
152     public void delete(String name)
153     {
154         map.remove(name);
155     }
156 
157     /* (non-Javadoc)
158      * @see org.mozilla.javascript.Scriptable#delete(int)
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     /* (non-Javadoc)
176      * @see org.mozilla.javascript.Scriptable#getPrototype()
177      */
178     public Scriptable getPrototype()
179     {
180         return this.prototype;
181     }
182 
183     /* (non-Javadoc)
184      * @see org.mozilla.javascript.Scriptable#setPrototype(org.mozilla.javascript.Scriptable)
185      */
186     public void setPrototype(Scriptable prototype)
187     {
188         this.prototype = prototype;
189     }
190 
191     /* (non-Javadoc)
192      * @see org.mozilla.javascript.Scriptable#getParentScope()
193      */
194     public Scriptable getParentScope()
195     {
196         return this.parentScope;
197     }
198 
199     /* (non-Javadoc)
200      * @see org.mozilla.javascript.Scriptable#setParentScope(org.mozilla.javascript.Scriptable)
201      */
202     public void setParentScope(Scriptable parent)
203     {
204         this.parentScope = parent;
205     }
206 
207     /* (non-Javadoc)
208      * @see org.mozilla.javascript.Scriptable#getIds()
209      */
210     public Object[] getIds()
211     {
212         return map.keySet().toArray();
213     }
214 
215     /* (non-Javadoc)
216      * @see org.mozilla.javascript.Scriptable#getDefaultValue(java.lang.Class)
217      */
218     public Object getDefaultValue(Class hint)
219     {
220         return null;
221     }
222 
223     /* (non-Javadoc)
224      * @see org.mozilla.javascript.Scriptable#hasInstance(org.mozilla.javascript.Scriptable)
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 }