KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > state > ContextImpl


1 package org.sapia.soto.state;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Map JavaDoc;
7 import java.util.Stack JavaDoc;
8
9
10 /**
11  * @author Yanick Duchesne
12  * <dl>
13  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
14  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
15  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
16  * </dl>
17  */

18 public class ContextImpl implements Context {
19   private Map JavaDoc _scopes = new HashMap JavaDoc();
20   private List JavaDoc _scopeList = new ArrayList JavaDoc();
21   protected Stack JavaDoc _stack = new Stack JavaDoc();
22
23   public ContextImpl() {
24   }
25
26   /**
27    * @param scope the name of the scope to which the given map is associated.
28    * @param vals a <code>Map</code>.
29    */

30   public void addScope(String JavaDoc scope, Scope vals) throws IllegalStateException JavaDoc {
31     if (_scopes.get(scope) != null) {
32       throw new IllegalStateException JavaDoc("Scope already exists: " + scope);
33     }
34
35     _scopes.put(scope, vals);
36     _scopeList.add(vals);
37   }
38
39   /**
40    * Returns the map that corresponds to the given scope.
41    *
42    * @param name the name of a scope.
43    * @return the <code>Scope</code> that corresponds to the desired scope, or <code>null</code>
44    * if none was found for the given scope.
45    */

46   protected Scope getScope(String JavaDoc name) {
47     return (Scope) _scopes.get(name);
48   }
49
50   /**
51    * @see org.sapia.soto.state.Context#get(java.lang.Object)
52    */

53   public Object JavaDoc get(Object JavaDoc key) {
54     Scope scope;
55     Object JavaDoc 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   /**
69    * @see org.sapia.soto.state.Context#get(java.lang.Object, java.lang.String[])
70    */

71   public Object JavaDoc get(Object JavaDoc key, String JavaDoc[] scopes) {
72     Scope scope;
73     Object JavaDoc 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   /**
87    * @see org.sapia.soto.state.Context#get(java.lang.Object, java.lang.String)
88    */

89   public Object JavaDoc get(Object JavaDoc key, String JavaDoc 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   /**
108    * @see org.sapia.soto.state.Context#getScopes()
109    */

110   public Map JavaDoc getScopes() {
111     return _scopes;
112   }
113
114   /**
115    * @see org.sapia.soto.state.Context#push(java.lang.Object)
116    */

117   public void push(Object JavaDoc o) {
118     _stack.push(o);
119   }
120
121   /**
122    * @see org.sapia.soto.state.Context#pop()
123    */

124   public Object JavaDoc pop() {
125     return _stack.pop();
126   }
127
128   /**
129    * @see org.sapia.soto.state.Context#currentObject()
130    */

131   public Object JavaDoc currentObject() throws IllegalStateException JavaDoc {
132     if (_stack.size() == 0) {
133       throw new IllegalStateException JavaDoc("No object on context stack");
134     }
135
136     return _stack.peek();
137   }
138
139   /**
140    * @see org.sapia.soto.state.Context#hasCurrentObject()
141    */

142   public boolean hasCurrentObject() {
143     return _stack.size() > 0;
144   }
145 }
146
Popular Tags