1 package org.sapia.soto.state; 2 3 import java.util.ArrayList ; 4 import java.util.HashMap ; 5 import java.util.List ; 6 import java.util.Map ; 7 import java.util.Stack ; 8 9 10 18 public class ContextImpl implements Context { 19 private Map _scopes = new HashMap (); 20 private List _scopeList = new ArrayList (); 21 protected Stack _stack = new Stack (); 22 23 public ContextImpl() { 24 } 25 26 30 public void addScope(String scope, Scope vals) throws IllegalStateException { 31 if (_scopes.get(scope) != null) { 32 throw new IllegalStateException ("Scope already exists: " + scope); 33 } 34 35 _scopes.put(scope, vals); 36 _scopeList.add(vals); 37 } 38 39 46 protected Scope getScope(String name) { 47 return (Scope) _scopes.get(name); 48 } 49 50 53 public Object get(Object key) { 54 Scope scope; 55 Object toReturn = null; 56 57 for (int i = 0; i < _scopeList.size(); i++) { 58 scope = (Scope) _scopeList.get(i); 59 60 if ((toReturn = scope.getVal(key)) != null) { 61 return toReturn; 62 } 63 } 64 65 return toReturn; 66 } 67 68 71 public Object get(Object key, String [] scopes) { 72 Scope scope; 73 Object toReturn = null; 74 75 for (int i = 0; i < scopes.length; i++) { 76 scope = getScope(scopes[i]); 77 78 if ((scope != null) && ((toReturn = scope.getVal(key)) != null)) { 79 return toReturn; 80 } 81 } 82 83 return toReturn; 84 } 85 86 89 public Object get(Object key, String scope) { 90 if ((scope.length() == 1) && (scope.charAt(0) == STACK_SCOPE)) { 91 if (_stack.size() > 0) { 92 return _stack.peek(); 93 } else { 94 return null; 95 } 96 } 97 98 Scope sc = getScope(scope); 99 100 if (sc != null) { 101 return sc.getVal(key); 102 } 103 104 return null; 105 } 106 107 110 public Map getScopes() { 111 return _scopes; 112 } 113 114 117 public void push(Object o) { 118 _stack.push(o); 119 } 120 121 124 public Object pop() { 125 return _stack.pop(); 126 } 127 128 131 public Object currentObject() throws IllegalStateException { 132 if (_stack.size() == 0) { 133 throw new IllegalStateException ("No object on context stack"); 134 } 135 136 return _stack.peek(); 137 } 138 139 142 public boolean hasCurrentObject() { 143 return _stack.size() > 0; 144 } 145 } 146 | Popular Tags |