1 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 ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 25 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 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 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 return null; 135 } 136 } 137 | Popular Tags |