KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > dom > util > HashTableStack


1 /*
2
3    Copyright 2000 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.dom.util;
19
20 /**
21  * This class represents a stack of HashTable objects.
22  *
23  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
24  * @version $Id: HashTableStack.java,v 1.4 2004/10/23 17:11:03 deweese Exp $
25  */

26 public class HashTableStack {
27     /**
28      * The current link.
29      */

30     protected Link current = new Link(null);
31
32     /**
33      * Creates a new HashTableStack object.
34      */

35     public HashTableStack() {
36     }
37
38     /**
39      * Pushes a new table on the stack.
40      */

41     public void push() {
42         current.pushCount++;
43     }
44
45     /**
46      * Removes the table on the top of the stack.
47      */

48     public void pop() {
49         if (current.pushCount-- == 0) {
50             current = current.next;
51         }
52     }
53
54     /**
55      * Creates a mapping in the table on the top of the stack.
56      */

57     public String JavaDoc put(String JavaDoc s, String JavaDoc v) {
58         if (current.pushCount != 0) {
59             current.pushCount--;
60             current = new Link(current);
61         }
62         if (s.length() == 0) current.defaultStr = v;
63     return (String JavaDoc)current.table.put(s, v);
64     }
65     
66     /**
67      * Gets an item in the table on the top of the stack.
68      */

69     public String JavaDoc get(String JavaDoc s) {
70         if (s.length() == 0) return current.defaultStr;
71
72     for (Link l = current; l != null; l = l.next) {
73         String JavaDoc uri = (String JavaDoc)l.table.get(s);
74         if (uri != null) {
75         return uri;
76         }
77     }
78     return null;
79     }
80     
81     /**
82      * To store the hashtables.
83      */

84     protected static class Link {
85     /**
86      * The table.
87      */

88     public HashTable table;
89     
90     /**
91      * The next link.
92      */

93     public Link next;
94
95         /**
96          * The default namespace for this part of the stack.
97          */

98         public String JavaDoc defaultStr;
99
100         /**
101          * The count of pushes since this link was
102          * added.
103          */

104         public int pushCount = 0;
105     
106     /**
107      * Creates a new link.
108      */

109     public Link(Link n) {
110         table = new HashTable();
111         next = n;
112             if (next != null)
113                 defaultStr = next.defaultStr;
114     }
115     }
116 }
117
Popular Tags