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. package com.sun.org.apache.xerces.internal.util;
  58. import java.util.Hashtable;
  59. import java.util.Enumeration;
  60. import com.sun.org.apache.xerces.internal.xni.Augmentations;
  61. /**
  62. * This class provides an implementation for Augmentations interface.
  63. * Augmentations interface defines a hashtable of additional data that could
  64. * be passed along the document pipeline. The information can contain extra
  65. * arguments or infoset augmentations, for example PSVI. This additional
  66. * information is identified by a String key.
  67. * <p>
  68. *
  69. * @author Elena Litani, IBM
  70. * @version $Id: AugmentationsImpl.java,v 1.8 2003/09/23 21:42:31 mrglavas Exp $
  71. */
  72. public class AugmentationsImpl implements Augmentations{
  73. private AugmentationsItemsContainer fAugmentationsContainer =
  74. new SmallContainer();
  75. /**
  76. * Add additional information identified by a key to the Augmentations structure.
  77. *
  78. * @param key Identifier, can't be <code>null</code>
  79. * @param item Additional information
  80. *
  81. * @return the previous value of the specified key in the Augmentations strucutre,
  82. * or <code>null</code> if it did not have one.
  83. */
  84. public Object putItem (String key, Object item){
  85. Object oldValue = fAugmentationsContainer.putItem(key, item);
  86. if (oldValue == null && fAugmentationsContainer.isFull()) {
  87. fAugmentationsContainer = fAugmentationsContainer.expand();
  88. }
  89. return oldValue;
  90. }
  91. /**
  92. * Get information identified by a key from the Augmentations structure
  93. *
  94. * @param key Identifier, can't be <code>null</code>
  95. *
  96. * @return the value to which the key is mapped in the Augmentations structure;
  97. * <code>null</code> if the key is not mapped to any value.
  98. */
  99. public Object getItem(String key){
  100. return fAugmentationsContainer.getItem(key);
  101. }
  102. /**
  103. * Remove additional info from the Augmentations structure
  104. *
  105. * @param key Identifier, can't be <code>null</code>
  106. */
  107. public Object removeItem (String key){
  108. return fAugmentationsContainer.removeItem(key);
  109. }
  110. /**
  111. * Returns an enumeration of the keys in the Augmentations structure
  112. *
  113. */
  114. public Enumeration keys (){
  115. return fAugmentationsContainer.keys();
  116. }
  117. /**
  118. * Remove all objects from the Augmentations structure.
  119. */
  120. public void removeAllItems() {
  121. fAugmentationsContainer.clear();
  122. }
  123. public String toString() {
  124. return fAugmentationsContainer.toString();
  125. }
  126. abstract class AugmentationsItemsContainer {
  127. abstract public Object putItem(Object key, Object item);
  128. abstract public Object getItem(Object key);
  129. abstract public Object removeItem(Object key);
  130. abstract public Enumeration keys();
  131. abstract public void clear();
  132. abstract public boolean isFull();
  133. abstract public AugmentationsItemsContainer expand();
  134. }
  135. class SmallContainer extends AugmentationsItemsContainer {
  136. final static int SIZE_LIMIT = 10;
  137. final Object[] fAugmentations = new Object[SIZE_LIMIT*2];
  138. int fNumEntries = 0;
  139. public Enumeration keys() {
  140. return new SmallContainerKeyEnumeration();
  141. }
  142. public Object getItem(Object key) {
  143. for (int i = 0; i < fNumEntries*2; i = i + 2) {
  144. if (fAugmentations[i].equals(key)) {
  145. return fAugmentations[i+1];
  146. }
  147. }
  148. return null;
  149. }
  150. public Object putItem(Object key, Object item) {
  151. for (int i = 0; i < fNumEntries*2; i = i + 2) {
  152. if (fAugmentations[i].equals(key)) {
  153. Object oldValue = fAugmentations[i+1];
  154. fAugmentations[i+1] = item;
  155. return oldValue;
  156. }
  157. }
  158. fAugmentations[fNumEntries*2] = key;
  159. fAugmentations[fNumEntries*2+1] = item;
  160. fNumEntries++;
  161. return null;
  162. }
  163. public Object removeItem(Object key) {
  164. for (int i = 0; i < fNumEntries*2; i = i + 2) {
  165. if (fAugmentations[i].equals(key)) {
  166. Object oldValue = fAugmentations[i+1];
  167. for (int j = i; j < fNumEntries*2 - 2; j = j + 2) {
  168. fAugmentations[j] = fAugmentations[j+2];
  169. fAugmentations[j+1] = fAugmentations[j+3];
  170. }
  171. fAugmentations[fNumEntries*2-2] = null;
  172. fAugmentations[fNumEntries*2-1] = null;
  173. fNumEntries--;
  174. return oldValue;
  175. }
  176. }
  177. return null;
  178. }
  179. public void clear() {
  180. for (int i = 0; i < fNumEntries*2; i = i + 2) {
  181. fAugmentations[i] = null;
  182. fAugmentations[i+1] = null;
  183. }
  184. fNumEntries = 0;
  185. }
  186. public boolean isFull() {
  187. return (fNumEntries == SIZE_LIMIT);
  188. }
  189. public AugmentationsItemsContainer expand() {
  190. LargeContainer expandedContainer = new LargeContainer();
  191. for (int i = 0; i < fNumEntries*2; i = i + 2) {
  192. expandedContainer.putItem(fAugmentations[i],
  193. fAugmentations[i+1]);
  194. }
  195. return expandedContainer;
  196. }
  197. public String toString() {
  198. StringBuffer buff = new StringBuffer();
  199. buff.append("SmallContainer - fNumEntries == " + fNumEntries);
  200. for (int i = 0; i < SIZE_LIMIT*2; i=i+2) {
  201. buff.append("\nfAugmentations[");
  202. buff.append(i);
  203. buff.append("] == ");
  204. buff.append(fAugmentations[i]);
  205. buff.append("; fAugmentations[");
  206. buff.append(i+1);
  207. buff.append("] == ");
  208. buff.append(fAugmentations[i+1]);
  209. }
  210. return buff.toString();
  211. }
  212. class SmallContainerKeyEnumeration implements Enumeration {
  213. Object [] enumArray = new Object[fNumEntries];
  214. int next = 0;
  215. SmallContainerKeyEnumeration() {
  216. for (int i = 0; i < fNumEntries; i++) {
  217. enumArray[i] = fAugmentations[i*2];
  218. }
  219. }
  220. public boolean hasMoreElements() {
  221. return next < enumArray.length;
  222. }
  223. public Object nextElement() {
  224. if (next >= enumArray.length) {
  225. throw new java.util.NoSuchElementException();
  226. }
  227. Object nextVal = enumArray[next];
  228. enumArray[next] = null;
  229. next++;
  230. return nextVal;
  231. }
  232. }
  233. }
  234. class LargeContainer extends AugmentationsItemsContainer {
  235. final Hashtable fAugmentations = new Hashtable();
  236. public Object getItem(Object key) {
  237. return fAugmentations.get(key);
  238. }
  239. public Object putItem(Object key, Object item) {
  240. return fAugmentations.put(key, item);
  241. }
  242. public Object removeItem(Object key) {
  243. return fAugmentations.remove(key);
  244. }
  245. public Enumeration keys() {
  246. return fAugmentations.keys();
  247. }
  248. public void clear() {
  249. fAugmentations.clear();
  250. }
  251. public boolean isFull() {
  252. return false;
  253. }
  254. public AugmentationsItemsContainer expand() {
  255. return this;
  256. }
  257. public String toString() {
  258. StringBuffer buff = new StringBuffer();
  259. buff.append("LargeContainer");
  260. Enumeration keys = fAugmentations.keys();
  261. while (keys.hasMoreElements()) {
  262. Object key = keys.nextElement();
  263. buff.append("\nkey == ");
  264. buff.append(key);
  265. buff.append("; value == ");
  266. buff.append(fAugmentations.get(key));
  267. }
  268. return buff.toString();
  269. }
  270. }
  271. }