1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.bea.xml.stream.util;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.Set;
23 import java.util.Map;
24 import java.util.Iterator;
25
26 /***
27 * Maintains a table for namespace scope
28 *
29 *
30 * values = map from strings to stacks
31 * [a]->[value1,0],[value2,0]
32 *
33 * table = a stack of bindings
34 */
35 public class SymbolTable {
36 private int depth;
37 private Stack table;
38 private Map values;
39
40 public SymbolTable() {
41 depth = 0;
42 table = new Stack();
43 values = new HashMap();
44 }
45
46 public void clear() {
47 depth = 0;
48 table.clear();
49 values.clear();
50 }
51
52
53
54
55 public int getDepth() {
56 return depth;
57 }
58
59 public boolean withinElement() {
60 return depth > 0;
61 }
62
63
64
65
66 public void put (String name, String value) {
67 table.push(new Symbol(name,value,depth));
68 if (!values.containsKey(name)) {
69 Stack valueStack = new Stack();
70 valueStack.push(value);
71 values.put(name,valueStack);
72 } else {
73 Stack valueStack = (Stack) values.get(name);
74 valueStack.push(value);
75 }
76 }
77
78
79
80
81 public String get (String name)
82 {
83 Stack valueStack = (Stack) values.get(name);
84 if (valueStack==null || valueStack.isEmpty())
85 return null;
86 return (String) valueStack.peek();
87 }
88
89 public Set getAll(String name)
90 {
91 HashSet result = new HashSet();
92 Iterator i = table.iterator();
93 while (i.hasNext()) {
94 Symbol s = (Symbol) i.next();
95 if (name.equals(s.getName()))
96 result.add(s.getValue());
97 }
98 return result;
99 }
100
101
102
103
104 public void openScope() {
105 depth++;
106 }
107
108
109
110
111
112
113 public void closeScope() {
114
115 Symbol symbol = (Symbol) table.peek();
116 int symbolDepth = symbol.depth;
117
118
119 while (symbolDepth == depth && !table.isEmpty()) {
120 symbol = (Symbol) table.pop();
121
122
123 Stack valueStack = (Stack) values.get(symbol.name);
124 valueStack.pop();
125
126
127 if (!table.isEmpty()) {
128 symbol = (Symbol) table.peek();
129 symbolDepth = symbol.depth;
130 } else break;
131 }
132 depth--;
133 }
134
135 public String toString() {
136 Iterator i = table.iterator();
137 String retVal="";
138 while(i.hasNext()) {
139 Symbol symbol = (Symbol) i.next();
140 retVal = retVal + symbol + "\n";
141 }
142 return retVal;
143 }
144
145 public static void main(String args[])
146 throws Exception
147 {
148 SymbolTable st = new SymbolTable();
149 st.openScope();
150 st.put("x","foo");
151 st.put("y","bar");
152 System.out.println("1 x:"+st.get("x"));
153 System.out.println("1 y:"+st.get("y"));
154 st.openScope();
155 st.put("x","bar");
156 st.put("y","foo");
157 st.openScope();
158 st.put("x","barbie");
159 st.openScope();
160 st.closeScope();
161
162 System.out.println("3 x:"+st.get("x"));
163 st.closeScope();
164 System.out.println("2 x:"+st.get("x"));
165 System.out.println("2 y:"+st.get("y"));
166 System.out.print(st);
167 st.closeScope();
168 System.out.println("1 x:"+st.get("x"));
169 System.out.println("1 y:"+st.get("y"));
170 st.closeScope();
171 System.out.print(st);
172 }
173 }