1 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 ; 23 24 29 public class ScriptableMap implements Scriptable, Wrapper { 30 31 private Map map; 32 private Scriptable prototype, parent; 33 34 public ScriptableMap() { 35 } 36 37 public ScriptableMap(Map map) { 38 this.map = map; 39 } 40 41 public String getClassName() { 42 return "Map"; 43 } 44 45 public boolean has(String name, Scriptable start) { 46 return this.map.containsKey(name); 47 } 48 49 52 public boolean has(int index, Scriptable start) { 53 return false; 54 } 55 56 public Object get(String 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 get(int index, Scriptable start) { 64 return NOT_FOUND; 65 } 66 67 public void put(String name, Scriptable start, Object 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 value) { 75 } 76 77 public void delete(String 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 [] getIds() { 101 return this.map.keySet().toArray(); 102 } 103 104 public Object getDefaultValue(Class 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 122 public Object unwrap() { 123 return this.map; 124 } 125 126 } 127 | Popular Tags |