KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > util > AugmentationsImpl


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

16
17 package org.apache.xerces.util;
18
19 import java.util.Hashtable JavaDoc;
20 import java.util.Enumeration JavaDoc;
21
22 import org.apache.xerces.xni.Augmentations;
23
24 /**
25  * This class provides an implementation for Augmentations interface.
26  * Augmentations interface defines a hashtable of additional data that could
27  * be passed along the document pipeline. The information can contain extra
28  * arguments or infoset augmentations, for example PSVI. This additional
29  * information is identified by a String key.
30  * <p>
31  *
32  * @author Elena Litani, IBM
33  * @version $Id: AugmentationsImpl.java,v 1.9 2004/02/24 23:15:53 mrglavas Exp $
34  */

35 public class AugmentationsImpl implements Augmentations{
36     
37     private AugmentationsItemsContainer fAugmentationsContainer =
38                                         new SmallContainer();
39     
40     /**
41      * Add additional information identified by a key to the Augmentations structure.
42      *
43      * @param key Identifier, can't be <code>null</code>
44      * @param item Additional information
45      *
46      * @return the previous value of the specified key in the Augmentations strucutre,
47      * or <code>null</code> if it did not have one.
48      */

49     public Object JavaDoc putItem (String JavaDoc key, Object JavaDoc item){
50         Object JavaDoc oldValue = fAugmentationsContainer.putItem(key, item);
51
52         if (oldValue == null && fAugmentationsContainer.isFull()) {
53             fAugmentationsContainer = fAugmentationsContainer.expand();
54         }
55
56         return oldValue;
57     }
58
59
60     /**
61      * Get information identified by a key from the Augmentations structure
62      *
63      * @param key Identifier, can't be <code>null</code>
64      *
65      * @return the value to which the key is mapped in the Augmentations structure;
66      * <code>null</code> if the key is not mapped to any value.
67      */

68     public Object JavaDoc getItem(String JavaDoc key){
69         return fAugmentationsContainer.getItem(key);
70     }
71     
72     
73     /**
74      * Remove additional info from the Augmentations structure
75      *
76      * @param key Identifier, can't be <code>null</code>
77      */

78     public Object JavaDoc removeItem (String JavaDoc key){
79         return fAugmentationsContainer.removeItem(key);
80     }
81
82     /**
83      * Returns an enumeration of the keys in the Augmentations structure
84      *
85      */

86     public Enumeration JavaDoc keys (){
87         return fAugmentationsContainer.keys();
88     }
89
90     /**
91      * Remove all objects from the Augmentations structure.
92      */

93     public void removeAllItems() {
94         fAugmentationsContainer.clear();
95     }
96
97     public String JavaDoc toString() {
98         return fAugmentationsContainer.toString();
99     }
100
101     abstract class AugmentationsItemsContainer {
102         abstract public Object JavaDoc putItem(Object JavaDoc key, Object JavaDoc item);
103         abstract public Object JavaDoc getItem(Object JavaDoc key);
104         abstract public Object JavaDoc removeItem(Object JavaDoc key);
105         abstract public Enumeration JavaDoc keys();
106         abstract public void clear();
107         abstract public boolean isFull();
108         abstract public AugmentationsItemsContainer expand();
109     }
110
111     class SmallContainer extends AugmentationsItemsContainer {
112         final static int SIZE_LIMIT = 10;
113         final Object JavaDoc[] fAugmentations = new Object JavaDoc[SIZE_LIMIT*2];
114         int fNumEntries = 0;
115
116         public Enumeration JavaDoc keys() {
117             return new SmallContainerKeyEnumeration();
118         }
119
120         public Object JavaDoc getItem(Object JavaDoc key) {
121             for (int i = 0; i < fNumEntries*2; i = i + 2) {
122                 if (fAugmentations[i].equals(key)) {
123                     return fAugmentations[i+1];
124                 }
125             }
126
127             return null;
128         }
129
130         public Object JavaDoc putItem(Object JavaDoc key, Object JavaDoc item) {
131             for (int i = 0; i < fNumEntries*2; i = i + 2) {
132                 if (fAugmentations[i].equals(key)) {
133                     Object JavaDoc oldValue = fAugmentations[i+1];
134                     fAugmentations[i+1] = item;
135
136                     return oldValue;
137                 }
138             }
139
140             fAugmentations[fNumEntries*2] = key;
141             fAugmentations[fNumEntries*2+1] = item;
142             fNumEntries++;
143
144             return null;
145         }
146
147
148         public Object JavaDoc removeItem(Object JavaDoc key) {
149             for (int i = 0; i < fNumEntries*2; i = i + 2) {
150                 if (fAugmentations[i].equals(key)) {
151                     Object JavaDoc oldValue = fAugmentations[i+1];
152
153                     for (int j = i; j < fNumEntries*2 - 2; j = j + 2) {
154                         fAugmentations[j] = fAugmentations[j+2];
155                         fAugmentations[j+1] = fAugmentations[j+3];
156                     }
157
158                     fAugmentations[fNumEntries*2-2] = null;
159                     fAugmentations[fNumEntries*2-1] = null;
160                     fNumEntries--;
161
162                     return oldValue;
163                 }
164             }
165
166             return null;
167         }
168
169         public void clear() {
170             for (int i = 0; i < fNumEntries*2; i = i + 2) {
171                 fAugmentations[i] = null;
172                 fAugmentations[i+1] = null;
173             }
174
175             fNumEntries = 0;
176         }
177
178         public boolean isFull() {
179             return (fNumEntries == SIZE_LIMIT);
180         }
181
182         public AugmentationsItemsContainer expand() {
183             LargeContainer expandedContainer = new LargeContainer();
184
185             for (int i = 0; i < fNumEntries*2; i = i + 2) {
186                 expandedContainer.putItem(fAugmentations[i],
187                                           fAugmentations[i+1]);
188             }
189
190             return expandedContainer;
191         }
192
193         public String JavaDoc toString() {
194             StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
195             buff.append("SmallContainer - fNumEntries == " + fNumEntries);
196
197             for (int i = 0; i < SIZE_LIMIT*2; i=i+2) {
198                 buff.append("\nfAugmentations[");
199                 buff.append(i);
200                 buff.append("] == ");
201                 buff.append(fAugmentations[i]);
202                 buff.append("; fAugmentations[");
203                 buff.append(i+1);
204                 buff.append("] == ");
205                 buff.append(fAugmentations[i+1]);
206             }
207
208             return buff.toString();
209         }
210
211         class SmallContainerKeyEnumeration implements Enumeration JavaDoc {
212             Object JavaDoc [] enumArray = new Object JavaDoc[fNumEntries];
213             int next = 0;
214
215             SmallContainerKeyEnumeration() {
216                 for (int i = 0; i < fNumEntries; i++) {
217                     enumArray[i] = fAugmentations[i*2];
218                 }
219             }
220
221             public boolean hasMoreElements() {
222                 return next < enumArray.length;
223             }
224
225             public Object JavaDoc nextElement() {
226                 if (next >= enumArray.length) {
227                     throw new java.util.NoSuchElementException JavaDoc();
228                 }
229
230                 Object JavaDoc nextVal = enumArray[next];
231                 enumArray[next] = null;
232                 next++;
233
234                 return nextVal;
235             }
236         }
237     }
238
239     class LargeContainer extends AugmentationsItemsContainer {
240         final Hashtable JavaDoc fAugmentations = new Hashtable JavaDoc();
241
242         public Object JavaDoc getItem(Object JavaDoc key) {
243             return fAugmentations.get(key);
244         }
245
246         public Object JavaDoc putItem(Object JavaDoc key, Object JavaDoc item) {
247             return fAugmentations.put(key, item);
248         }
249
250         public Object JavaDoc removeItem(Object JavaDoc key) {
251             return fAugmentations.remove(key);
252         }
253
254         public Enumeration JavaDoc keys() {
255             return fAugmentations.keys();
256         }
257
258         public void clear() {
259             fAugmentations.clear();
260         }
261
262         public boolean isFull() {
263             return false;
264         }
265
266         public AugmentationsItemsContainer expand() {
267             return this;
268         }
269
270         public String JavaDoc toString() {
271             StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
272             buff.append("LargeContainer");
273             Enumeration JavaDoc keys = fAugmentations.keys();
274
275             while (keys.hasMoreElements()) {
276                 Object JavaDoc key = keys.nextElement();
277                 buff.append("\nkey == ");
278                 buff.append(key);
279                 buff.append("; value == ");
280                 buff.append(fAugmentations.get(key));
281             }
282
283             return buff.toString();
284         }
285     }
286 }
287
Popular Tags