1 package gnu.expr;
2 import javax.script.*;
3 import java.util.*;
4 import gnu.mapping.*;
5 
6 /** An implementation of javax.script.Bindings that uses a SimpleEnvironment.
7  */
8 
9 public class KawaScriptBindings
10   extends AbstractMap<String,Object>
11   implements Bindings
12 {
13   final SimpleEnvironment environment;
14 
KawaScriptBindings(SimpleEnvironment environment)15   public KawaScriptBindings (SimpleEnvironment environment)
16   {
17     this.environment = environment;
18   }
19 
asSymbol(String name)20   Symbol asSymbol (String name)
21   {
22     if (name.length() > 0 && name.charAt(0) == '{')
23       return Symbol.parse(name);
24     // FIXME should handle prefix:name here.
25     return environment.getSymbol(name);
26   }
27 
get(Object key)28   public Object get(Object key)
29   {
30     return environment.get(key);
31   }
32 
put(String key, Object value)33   public Object put (String key, Object value)
34   {
35     return environment.put(key, value);
36   }
37 
38   class Entry implements Map.Entry<String,Object>
39   {
Entry(Location nloc)40     public Entry(Location nloc) { this.nloc = nloc; }
41     Location nloc;
getKey()42     public String getKey () { return nloc.getKeySymbol().toString(); }
getValue()43     public Object getValue() { return nloc.getValue(); }
setValue(Object v)44     public Object setValue(Object v) { return nloc.setValue(v); }
hashCode()45     public int hashCode() { return nloc.hashCode(); }
equals(Object o)46     public boolean equals(Object o)
47     {
48       if (! (o instanceof Entry))
49         return false;
50       Entry e2 = (Entry) o;
51       return ((getKey()==null ?
52                e2.getKey()==null : getKey().equals(e2.getKey())) &&
53               (getValue()==null ?
54                e2.getValue()==null : getValue().equals(e2.getValue())));
55     }
56   }
57 
58   class EntrySet extends AbstractSet<Map.Entry<String,Object>>
59   {
iterator()60     public Iterator<Map.Entry<String,Object>> iterator()
61     {
62       return new Iterator<Map.Entry<String,Object>>()
63         {
64           LocationEnumeration locEnum = environment.enumerateLocations();
65           public boolean hasNext() { return locEnum.hasNext(); }
66           public Map.Entry<String,Object> next()
67             { return new Entry(locEnum.next()); }
68           public void remove() { locEnum.remove(); }
69         };
70     }
size()71     public int size() { return environment.size(); }
72   }
73 
entrySet()74   public Set<Map.Entry<String,Object>> entrySet()
75   {
76     return new EntrySet();
77   }
78 }
79