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.core.javascript.fom;
17  
18  import org.mozilla.javascript.Context;
19  import org.mozilla.javascript.Scriptable;
20  
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.Map;
24  
25  /***
26   * @version CVS $Id: PageLocalScopeImpl.java 36239 2004-08-11 18:28:06Z vgritsenko $
27   */
28  public class PageLocalScopeImpl implements PageLocalScope {
29  
30      private Map locals;
31      private Scriptable scope;
32  
33      public PageLocalScopeImpl(Scriptable scope) {
34          locals = new HashMap();
35          this.scope = scope;
36      }
37  
38      private Scriptable newObject() {
39          try {
40              return Context.getCurrentContext().newObject(scope);
41          } catch (Exception ignored) {
42              // can't happen here
43              ignored.printStackTrace();
44              throw new Error("error: " + ignored);
45          }
46      }
47  
48      private PageLocalScopeImpl(PageLocalScopeImpl toBeCloned) {
49          this.scope = toBeCloned.scope;
50          locals = new HashMap();
51          Iterator iter = toBeCloned.locals.entrySet().iterator();
52          while (iter.hasNext()) {
53              Map.Entry e = (Map.Entry)iter.next();
54              Object key = e.getKey();
55              Object value = e.getValue();
56              // clone it
57              Scriptable obj = (Scriptable)value;
58              Scriptable newObj = newObject();
59              Object[] ids = obj.getIds();
60              for (int i = 0; i < ids.length; i++) {
61                  String name = ids[i].toString();
62                  newObj.put(name, newObj, obj.get(name, obj));
63              }
64              value = newObj;
65              locals.put(key, value);
66          }
67     }
68  
69      private Scriptable resolve(PageLocal local) {
70          final Object id = local.getId();
71          Scriptable result = (Scriptable)locals.get(id);
72          if (result == null) {
73              locals.put(id, result = newObject());
74          }
75          return result;
76      }
77  
78      public boolean has(PageLocal local, String name) {
79          Scriptable obj = resolve(local);
80          return obj.has(name, obj);
81      }
82  
83      public boolean has(PageLocal local, int index) {
84          Scriptable obj = resolve(local);
85          return obj.has(index, obj);
86      }
87  
88      public Object get(PageLocal local, String name) {
89          Scriptable obj = resolve(local);
90          return obj.get(name, obj);
91      }
92  
93      public Object get(PageLocal local, int index) {
94          Scriptable obj = resolve(local);
95          return obj.get(index, obj);
96      }
97  
98      public void put(PageLocal local, String name, Object value) {
99          Scriptable obj = resolve(local);
100         obj.put(name, obj, value);
101     }
102 
103     public void put(PageLocal local, int index, Object value) {
104         Scriptable obj = resolve(local);
105         obj.put(index, obj, value);
106     }
107 
108     public void delete(PageLocal local, String name) {
109         Scriptable obj = resolve(local);
110         obj.delete(name);
111     }
112 
113     public void delete(PageLocal local, int index) {
114         Scriptable obj = resolve(local);
115         obj.delete(index);
116     }
117 
118     public Object[] getIds(PageLocal local) {
119         Scriptable obj = resolve(local);
120         return obj.getIds();
121     }
122 
123     public Object getDefaultValue(PageLocal local, Class hint) {
124         Scriptable obj = resolve(local);
125         return obj.getDefaultValue(hint);
126     }
127 
128     public PageLocalScopeImpl duplicate() {
129         return new PageLocalScopeImpl(this);
130     }
131 
132     public PageLocal createPageLocal() {
133         // not used
134         return null;
135     }
136 }