001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.flow.javascript;
018:
019: import org.mozilla.javascript.NativeJavaObject;
020: import org.mozilla.javascript.Scriptable;
021: import org.mozilla.javascript.Wrapper;
022:
023: import java.util.Map;
024:
025: /**
026: * Wrap a java.util.Map for JavaScript.
027: *
028: * @version CVS $Id: ScriptableMap.java 433543 2006-08-22 06:22:54Z crossley $
029: */
030: public class ScriptableMap implements Scriptable, Wrapper {
031:
032: private Map map;
033: private Scriptable prototype, parent;
034:
035: public ScriptableMap() {
036: }
037:
038: public ScriptableMap(Map map) {
039: this .map = map;
040: }
041:
042: public String getClassName() {
043: return "Map";
044: }
045:
046: public boolean has(String name, Scriptable start) {
047: return this .map.containsKey(name);
048: }
049:
050: /**
051: * no numeric properties
052: */
053: public boolean has(int index, Scriptable start) {
054: return false;
055: }
056:
057: public Object get(String name, Scriptable start) {
058: if (this .map.containsKey(name))
059: return this .map.get(name);
060:
061: return NOT_FOUND;
062: }
063:
064: public Object get(int index, Scriptable start) {
065: return NOT_FOUND;
066: }
067:
068: public void put(String name, Scriptable start, Object value) {
069: if (value instanceof NativeJavaObject) {
070: value = ((NativeJavaObject) value).unwrap();
071: }
072: map.put(name, value);
073: }
074:
075: public void put(int index, Scriptable start, Object value) {
076: }
077:
078: public void delete(String id) {
079: map.remove(id);
080: }
081:
082: public void delete(int index) {
083: }
084:
085: public Scriptable getPrototype() {
086: return prototype;
087: }
088:
089: public void setPrototype(Scriptable prototype) {
090: this .prototype = prototype;
091: }
092:
093: public Scriptable getParentScope() {
094: return parent;
095: }
096:
097: public void setParentScope(Scriptable parent) {
098: this .parent = parent;
099: }
100:
101: public Object[] getIds() {
102: return this .map.keySet().toArray();
103: }
104:
105: public Object getDefaultValue(Class typeHint) {
106: return this .map.toString();
107: }
108:
109: public boolean hasInstance(Scriptable value) {
110: Scriptable proto = value.getPrototype();
111: while (proto != null) {
112: if (proto.equals(this ))
113: return true;
114: proto = proto.getPrototype();
115: }
116:
117: return false;
118: }
119:
120: /**
121: * Return the java.util.Map that is wrapped by this class.
122: */
123: public Object unwrap() {
124: return this.map;
125: }
126:
127: }
|