KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > flow > javascript > ScriptableMap


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;
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 JavaDoc;
23
24 /**
25  * Wrap a java.util.Map for JavaScript.
26  *
27  * @version CVS $Id: ScriptableMap.java 30932 2004-07-29 17:35:38Z vgritsenko $
28  */

29 public class ScriptableMap implements Scriptable, Wrapper {
30
31     private Map JavaDoc map;
32     private Scriptable prototype, parent;
33
34     public ScriptableMap() {
35     }
36
37     public ScriptableMap(Map JavaDoc map) {
38         this.map = map;
39     }
40
41     public String JavaDoc getClassName() {
42         return "Map";
43     }
44
45     public boolean has(String JavaDoc name, Scriptable start) {
46         return this.map.containsKey(name);
47     }
48
49     /**
50      * no numeric properties
51      */

52     public boolean has(int index, Scriptable start) {
53         return false;
54     }
55
56     public Object JavaDoc get(String JavaDoc name, Scriptable start) {
57         if (this.map.containsKey(name))
58             return this.map.get(name);
59
60         return NOT_FOUND;
61     }
62
63     public Object JavaDoc get(int index, Scriptable start) {
64         return NOT_FOUND;
65     }
66
67     public void put(String JavaDoc name, Scriptable start, Object JavaDoc value) {
68         if (value instanceof NativeJavaObject) {
69             value = ((NativeJavaObject)value).unwrap();
70         }
71         map.put(name, value);
72     }
73
74     public void put(int index, Scriptable start, Object JavaDoc value) {
75     }
76
77     public void delete(String JavaDoc id) {
78         map.remove(id);
79     }
80
81     public void delete(int index) {
82     }
83
84     public Scriptable getPrototype() {
85         return prototype;
86     }
87
88     public void setPrototype(Scriptable prototype) {
89         this.prototype = prototype;
90     }
91
92     public Scriptable getParentScope() {
93         return parent;
94     }
95
96     public void setParentScope(Scriptable parent) {
97         this.parent = parent;
98     }
99
100     public Object JavaDoc[] getIds() {
101         return this.map.keySet().toArray();
102     }
103
104     public Object JavaDoc getDefaultValue(Class JavaDoc typeHint) {
105         return this.map.toString();
106     }
107
108     public boolean hasInstance(Scriptable value) {
109         Scriptable proto = value.getPrototype();
110         while (proto != null) {
111             if (proto.equals(this))
112                 return true;
113             proto = proto.getPrototype();
114         }
115
116         return false;
117     }
118
119     /**
120      * Return the java.util.Map that is wrapped by this class.
121      */

122     public Object JavaDoc unwrap() {
123         return this.map;
124     }
125
126 }
127
Popular Tags