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.LinkedHashMap;
23  import java.util.Map;
24  
25  import org.mozilla.javascript.Scriptable;
26  
27  /**
28   * Implementation of a Scriptable Map. This is the best choice for maps that want to represent
29   * JavaScript associative arrays - allowing access via key and integer index. It maintains and
30   * respects insertion order of the elements and allows either string or integer keys.
31   * 
32   * @author Kevin Roast
33   */
34  public class ScriptableLinkedHashMap<K,V> extends LinkedHashMap<K, V> implements ScriptableMap<K,V>
35  {
36      private static final long serialVersionUID = 3774167893214964123L;
37      
38      private Scriptable parentScope;
39      private Scriptable prototype;
40      
41      
42      public ScriptableLinkedHashMap()
43      {
44      }
45      
46      public ScriptableLinkedHashMap(int initialCapacity)
47      {
48          super(initialCapacity);
49      }
50      
51      public ScriptableLinkedHashMap(Map<K, V> source)
52      {
53          super(source);
54      }
55      
56      /**
57       * @see org.mozilla.javascript.Scriptable#getClassName()
58       */
59      public String getClassName()
60      {
61          return "ScriptableMap";
62      }
63  
64      /**
65       * @see org.mozilla.javascript.Scriptable#get(java.lang.String, org.mozilla.javascript.Scriptable)
66       */
67      public Object get(String name, Scriptable start)
68      {
69          // get the property from the underlying QName map
70          if ("length".equals(name))
71          {
72              return this.size();
73          }
74          else
75          {
76              return get(name);
77          }
78      }
79  
80      /**
81       * @see org.mozilla.javascript.Scriptable#get(int, org.mozilla.javascript.Scriptable)
82       */
83      public Object get(int index, Scriptable start)
84      {
85          Object value =  null;
86          int i=0;
87          Iterator itrValues = this.values().iterator();
88          while (i++ <= index && itrValues.hasNext())
89          {
90              value = itrValues.next();
91          }
92          return value;
93      }
94  
95      /**
96       * @see org.mozilla.javascript.Scriptable#has(java.lang.String, org.mozilla.javascript.Scriptable)
97       */
98      public boolean has(String name, Scriptable start)
99      {
100         // locate the property in the underlying map
101         return containsKey(name);
102     }
103 
104     /**
105      * @see org.mozilla.javascript.Scriptable#has(int, org.mozilla.javascript.Scriptable)
106      */
107     public boolean has(int index, Scriptable start)
108     {
109         return (index >= 0 && this.values().size() > index);
110     }
111 
112     /**
113      * @see org.mozilla.javascript.Scriptable#put(java.lang.String, org.mozilla.javascript.Scriptable, java.lang.Object)
114      */
115     @SuppressWarnings("unchecked")
116     public void put(String name, Scriptable start, Object value)
117     {
118         // add the property to the underlying QName map
119         put((K)name, (V)value);
120     }
121 
122     /**
123      * @see org.mozilla.javascript.Scriptable#put(int, org.mozilla.javascript.Scriptable, java.lang.Object)
124      */
125     public void put(int index, Scriptable start, Object value)
126     {
127         // TODO: implement?
128     }
129 
130     /**
131      * @see org.mozilla.javascript.Scriptable#delete(java.lang.String)
132      */
133     public void delete(String name)
134     {
135         // remove the property from the underlying QName map
136         remove(name);
137     }
138 
139     /**
140      * @see org.mozilla.javascript.Scriptable#delete(int)
141      */
142     public void delete(int index)
143     {
144         int i=0;
145         Iterator itrKeys = this.keySet().iterator();
146         while (i <= index && itrKeys.hasNext())
147         {
148             Object key = itrKeys.next();
149             if (i == index)
150             {
151                 remove(key);
152                 break;
153             }
154         }
155     }
156 
157     /**
158      * @see org.mozilla.javascript.Scriptable#getPrototype()
159      */
160     public Scriptable getPrototype()
161     {
162         return this.prototype;
163     }
164 
165     /**
166      * @see org.mozilla.javascript.Scriptable#setPrototype(org.mozilla.javascript.Scriptable)
167      */
168     public void setPrototype(Scriptable prototype)
169     {
170         this.prototype = prototype;
171     }
172 
173     /**
174      * @see org.mozilla.javascript.Scriptable#getParentScope()
175      */
176     public Scriptable getParentScope()
177     {
178         return this.parentScope;
179     }
180 
181     /**
182      * @see org.mozilla.javascript.Scriptable#setParentScope(org.mozilla.javascript.Scriptable)
183      */
184     public void setParentScope(Scriptable parent)
185     {
186         this.parentScope = parent;
187     }
188 
189     /**
190      * @see org.mozilla.javascript.Scriptable#getIds()
191      */
192     public Object[] getIds()
193     {
194         return keySet().toArray();
195     }
196 
197     /**
198      * @see org.mozilla.javascript.Scriptable#getDefaultValue(java.lang.Class)
199      */
200     public Object getDefaultValue(Class hint)
201     {
202         return null;
203     }
204 
205     /**
206      * @see org.mozilla.javascript.Scriptable#hasInstance(org.mozilla.javascript.Scriptable)
207      */
208     public boolean hasInstance(Scriptable instance)
209     {
210         return instance instanceof ScriptableLinkedHashMap;
211     }
212 }
213