KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > util > AugmentationsImpl


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package com.sun.org.apache.xerces.internal.util;
59
60 import java.util.Hashtable JavaDoc;
61 import java.util.Enumeration JavaDoc;
62
63 import com.sun.org.apache.xerces.internal.xni.Augmentations;
64
65 /**
66  * This class provides an implementation for Augmentations interface.
67  * Augmentations interface defines a hashtable of additional data that could
68  * be passed along the document pipeline. The information can contain extra
69  * arguments or infoset augmentations, for example PSVI. This additional
70  * information is identified by a String key.
71  * <p>
72  *
73  * @author Elena Litani, IBM
74  * @version $Id: AugmentationsImpl.java,v 1.8 2003/09/23 21:42:31 mrglavas Exp $
75  */

76 public class AugmentationsImpl implements Augmentations{
77     
78     private AugmentationsItemsContainer fAugmentationsContainer =
79                                         new SmallContainer();
80     
81     /**
82      * Add additional information identified by a key to the Augmentations structure.
83      *
84      * @param key Identifier, can't be <code>null</code>
85      * @param item Additional information
86      *
87      * @return the previous value of the specified key in the Augmentations strucutre,
88      * or <code>null</code> if it did not have one.
89      */

90     public Object JavaDoc putItem (String JavaDoc key, Object JavaDoc item){
91         Object JavaDoc oldValue = fAugmentationsContainer.putItem(key, item);
92
93         if (oldValue == null && fAugmentationsContainer.isFull()) {
94             fAugmentationsContainer = fAugmentationsContainer.expand();
95         }
96
97         return oldValue;
98     }
99
100
101     /**
102      * Get information identified by a key from the Augmentations structure
103      *
104      * @param key Identifier, can't be <code>null</code>
105      *
106      * @return the value to which the key is mapped in the Augmentations structure;
107      * <code>null</code> if the key is not mapped to any value.
108      */

109     public Object JavaDoc getItem(String JavaDoc key){
110         return fAugmentationsContainer.getItem(key);
111     }
112     
113     
114     /**
115      * Remove additional info from the Augmentations structure
116      *
117      * @param key Identifier, can't be <code>null</code>
118      */

119     public Object JavaDoc removeItem (String JavaDoc key){
120         return fAugmentationsContainer.removeItem(key);
121     }
122
123     /**
124      * Returns an enumeration of the keys in the Augmentations structure
125      *
126      */

127     public Enumeration JavaDoc keys (){
128         return fAugmentationsContainer.keys();
129     }
130
131     /**
132      * Remove all objects from the Augmentations structure.
133      */

134     public void removeAllItems() {
135         fAugmentationsContainer.clear();
136     }
137
138     public String JavaDoc toString() {
139         return fAugmentationsContainer.toString();
140     }
141
142     abstract class AugmentationsItemsContainer {
143         abstract public Object JavaDoc putItem(Object JavaDoc key, Object JavaDoc item);
144         abstract public Object JavaDoc getItem(Object JavaDoc key);
145         abstract public Object JavaDoc removeItem(Object JavaDoc key);
146         abstract public Enumeration JavaDoc keys();
147         abstract public void clear();
148         abstract public boolean isFull();
149         abstract public AugmentationsItemsContainer expand();
150     }
151
152     class SmallContainer extends AugmentationsItemsContainer {
153         final static int SIZE_LIMIT = 10;
154         final Object JavaDoc[] fAugmentations = new Object JavaDoc[SIZE_LIMIT*2];
155         int fNumEntries = 0;
156
157         public Enumeration JavaDoc keys() {
158             return new SmallContainerKeyEnumeration();
159         }
160
161         public Object JavaDoc getItem(Object JavaDoc key) {
162             for (int i = 0; i < fNumEntries*2; i = i + 2) {
163                 if (fAugmentations[i].equals(key)) {
164                     return fAugmentations[i+1];
165                 }
166             }
167
168             return null;
169         }
170
171         public Object JavaDoc putItem(Object JavaDoc key, Object JavaDoc item) {
172             for (int i = 0; i < fNumEntries*2; i = i + 2) {
173                 if (fAugmentations[i].equals(key)) {
174                     Object JavaDoc oldValue = fAugmentations[i+1];
175                     fAugmentations[i+1] = item;
176
177                     return oldValue;
178                 }
179             }
180
181             fAugmentations[fNumEntries*2] = key;
182             fAugmentations[fNumEntries*2+1] = item;
183             fNumEntries++;
184
185             return null;
186         }
187
188
189         public Object JavaDoc removeItem(Object JavaDoc key) {
190             for (int i = 0; i < fNumEntries*2; i = i + 2) {
191                 if (fAugmentations[i].equals(key)) {
192                     Object JavaDoc oldValue = fAugmentations[i+1];
193
194                     for (int j = i; j < fNumEntries*2 - 2; j = j + 2) {
195                         fAugmentations[j] = fAugmentations[j+2];
196                         fAugmentations[j+1] = fAugmentations[j+3];
197                     }
198
199                     fAugmentations[fNumEntries*2-2] = null;
200                     fAugmentations[fNumEntries*2-1] = null;
201                     fNumEntries--;
202
203                     return oldValue;
204                 }
205             }
206
207             return null;
208         }
209
210         public void clear() {
211             for (int i = 0; i < fNumEntries*2; i = i + 2) {
212                 fAugmentations[i] = null;
213                 fAugmentations[i+1] = null;
214             }
215
216             fNumEntries = 0;
217         }
218
219         public boolean isFull() {
220             return (fNumEntries == SIZE_LIMIT);
221         }
222
223         public AugmentationsItemsContainer expand() {
224             LargeContainer expandedContainer = new LargeContainer();
225
226             for (int i = 0; i < fNumEntries*2; i = i + 2) {
227                 expandedContainer.putItem(fAugmentations[i],
228                                           fAugmentations[i+1]);
229             }
230
231             return expandedContainer;
232         }
233
234         public String JavaDoc toString() {
235             StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
236             buff.append("SmallContainer - fNumEntries == " + fNumEntries);
237
238             for (int i = 0; i < SIZE_LIMIT*2; i=i+2) {
239                 buff.append("\nfAugmentations[");
240                 buff.append(i);
241                 buff.append("] == ");
242                 buff.append(fAugmentations[i]);
243                 buff.append("; fAugmentations[");
244                 buff.append(i+1);
245                 buff.append("] == ");
246                 buff.append(fAugmentations[i+1]);
247             }
248
249             return buff.toString();
250         }
251
252         class SmallContainerKeyEnumeration implements Enumeration JavaDoc {
253             Object JavaDoc [] enumArray = new Object JavaDoc[fNumEntries];
254             int next = 0;
255
256             SmallContainerKeyEnumeration() {
257                 for (int i = 0; i < fNumEntries; i++) {
258                     enumArray[i] = fAugmentations[i*2];
259                 }
260             }
261
262             public boolean hasMoreElements() {
263                 return next < enumArray.length;
264             }
265
266             public Object JavaDoc nextElement() {
267                 if (next >= enumArray.length) {
268                     throw new java.util.NoSuchElementException JavaDoc();
269                 }
270
271                 Object JavaDoc nextVal = enumArray[next];
272                 enumArray[next] = null;
273                 next++;
274
275                 return nextVal;
276             }
277         }
278     }
279
280     class LargeContainer extends AugmentationsItemsContainer {
281         final Hashtable JavaDoc fAugmentations = new Hashtable JavaDoc();
282
283         public Object JavaDoc getItem(Object JavaDoc key) {
284             return fAugmentations.get(key);
285         }
286
287         public Object JavaDoc putItem(Object JavaDoc key, Object JavaDoc item) {
288             return fAugmentations.put(key, item);
289         }
290
291         public Object JavaDoc removeItem(Object JavaDoc key) {
292             return fAugmentations.remove(key);
293         }
294
295         public Enumeration JavaDoc keys() {
296             return fAugmentations.keys();
297         }
298
299         public void clear() {
300             fAugmentations.clear();
301         }
302
303         public boolean isFull() {
304             return false;
305         }
306
307         public AugmentationsItemsContainer expand() {
308             return this;
309         }
310
311         public String JavaDoc toString() {
312             StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
313             buff.append("LargeContainer");
314             Enumeration JavaDoc keys = fAugmentations.keys();
315
316             while (keys.hasMoreElements()) {
317                 Object JavaDoc key = keys.nextElement();
318                 buff.append("\nkey == ");
319                 buff.append(key);
320                 buff.append("; value == ");
321                 buff.append(fAugmentations.get(key));
322             }
323
324             return buff.toString();
325         }
326     }
327 }
328
Popular Tags