View Javadoc

1   /*   Copyright 2004 BEA Systems, Inc.
2    *
3    *   Licensed under the Apache License, Version 2.0 (the "License");
4    *   you may not use this file except in compliance with the License.
5    *   You may obtain a copy of the License at
6    *
7    *       http://www.apache.org/licenses/LICENSE-2.0
8    *
9    *   Unless required by applicable law or agreed to in writing, software
10   *   distributed under the License is distributed on an "AS IS" BASIS,
11   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   *   See the License for the specific language governing permissions and
13   *   limitations under the License.
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    // Gets the current depth
54    //
55    public int getDepth() {
56      return depth;
57    }
58  
59    public boolean withinElement() {
60      return depth > 0;
61    }
62  
63    //
64    // Adds a name/value pair
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    // Gets the value for a variable
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   // add a new highest level scope to the table
103   // 
104   public void openScope() {
105     depth++;
106   }
107 
108   //
109   // remove the highest level scope from the table
110   // 
111 
112   
113   public void closeScope() {
114     // Get the top binding
115     Symbol symbol = (Symbol) table.peek();
116     int symbolDepth = symbol.depth;
117 
118     // check if it needs to be popped of the table
119     while (symbolDepth == depth && !table.isEmpty()) {
120       symbol = (Symbol) table.pop();
121 
122       // pop its value as well
123       Stack valueStack = (Stack) values.get(symbol.name);
124       valueStack.pop();
125 
126       // check the next binding
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 }