KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > flow > javascript > fom > PageLocalScopeImpl


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.cocoon.components.flow.javascript.fom;
17
18 import org.mozilla.javascript.Context;
19 import org.mozilla.javascript.Scriptable;
20
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24
25 /**
26  * @version CVS $Id: PageLocalScopeImpl.java 54079 2004-10-08 13:30:28Z vgritsenko $
27  */

28 public class PageLocalScopeImpl implements PageLocalScope {
29
30     private Map JavaDoc locals;
31     private Scriptable scope;
32
33     public PageLocalScopeImpl(Scriptable scope) {
34         locals = new HashMap JavaDoc();
35         this.scope = scope;
36     }
37
38     private Scriptable newObject() {
39         try {
40             return Context.getCurrentContext().newObject(scope);
41         } catch (Exception JavaDoc ignored) {
42             // can't happen here
43
ignored.printStackTrace();
44             throw new Error JavaDoc("error: " + ignored);
45         }
46     }
47
48     private PageLocalScopeImpl(PageLocalScopeImpl toBeCloned) {
49         this.scope = toBeCloned.scope;
50         locals = new HashMap JavaDoc();
51         Iterator JavaDoc iter = toBeCloned.locals.entrySet().iterator();
52         while (iter.hasNext()) {
53             Map.Entry JavaDoc e = (Map.Entry JavaDoc)iter.next();
54             Object JavaDoc key = e.getKey();
55             Object JavaDoc value = e.getValue();
56             // clone it
57
Scriptable obj = (Scriptable)value;
58             Scriptable newObj = newObject();
59             Object JavaDoc[] ids = obj.getIds();
60             for (int i = 0; i < ids.length; i++) {
61                 String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc get(PageLocal local, String JavaDoc name) {
89         Scriptable obj = resolve(local);
90         return obj.get(name, obj);
91     }
92
93     public Object JavaDoc 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 JavaDoc name, Object JavaDoc value) {
99         Scriptable obj = resolve(local);
100         obj.put(name, obj, value);
101     }
102
103     public void put(PageLocal local, int index, Object JavaDoc value) {
104         Scriptable obj = resolve(local);
105         obj.put(index, obj, value);
106     }
107
108     public void delete(PageLocal local, String JavaDoc 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 JavaDoc[] getIds(PageLocal local) {
119         Scriptable obj = resolve(local);
120         return obj.getIds();
121     }
122
123     public Object JavaDoc getDefaultValue(PageLocal local, Class JavaDoc 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 }
137
Popular Tags