1. /*
  2. * Copyright 1999-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. * $Id: CustomStringPool.java,v 1.5 2004/02/16 23:06:11 minchau Exp $
  18. */
  19. package com.sun.org.apache.xml.internal.dtm.ref;
  20. import java.util.Hashtable;
  21. /** <p>CustomStringPool is an example of appliction provided data structure
  22. * for a DTM implementation to hold symbol references, e.g. elelment names.
  23. * It will follow the DTMDStringPool interface and use two simple methods
  24. * indexToString(int i) and stringToIndex(Sring s) to map between a set of
  25. * string values and a set of integer index values. Therefore, an application
  26. * may improve DTM processing speed by substituting the DTM symbol resolution
  27. * tables with application specific quick symbol resolution tables.</p>
  28. *
  29. * %REVIEW% The only difference between this an DTMStringPool seems to be that
  30. * it uses a java.lang.Hashtable full of Integers rather than implementing its
  31. * own hashing. Joe deliberately avoided that approach when writing
  32. * DTMStringPool, since it is both much more memory-hungry and probably slower
  33. * -- especially in JDK 1.1.x, where Hashtable is synchronized. We need to
  34. * either justify this implementation or discard it.
  35. *
  36. * <p>Status: In progress, under discussion.</p>
  37. * */
  38. public class CustomStringPool extends DTMStringPool {
  39. //final Vector m_intToString;
  40. //static final int HASHPRIME=101;
  41. //int[] m_hashStart=new int[HASHPRIME];
  42. final Hashtable m_stringToInt = new Hashtable();
  43. public static final int NULL=-1;
  44. public CustomStringPool()
  45. {
  46. super();
  47. /*m_intToString=new Vector();
  48. System.out.println("In constructor m_intToString is " +
  49. ((null == m_intToString) ? "null" : "not null"));*/
  50. //m_stringToInt=new Hashtable();
  51. //removeAllElements();
  52. }
  53. public void removeAllElements()
  54. {
  55. m_intToString.removeAllElements();
  56. if (m_stringToInt != null)
  57. m_stringToInt.clear();
  58. }
  59. /** @return string whose value is uniquely identified by this integer index.
  60. * @throws java.lang.ArrayIndexOutOfBoundsException
  61. * if index doesn't map to a string.
  62. * */
  63. public String indexToString(int i)
  64. throws java.lang.ArrayIndexOutOfBoundsException
  65. {
  66. return(String) m_intToString.elementAt(i);
  67. }
  68. /** @return integer index uniquely identifying the value of this string. */
  69. public int stringToIndex(String s)
  70. {
  71. if (s==null) return NULL;
  72. Integer iobj=(Integer)m_stringToInt.get(s);
  73. if (iobj==null) {
  74. m_intToString.addElement(s);
  75. iobj=new Integer(m_intToString.size());
  76. m_stringToInt.put(s,iobj);
  77. }
  78. return iobj.intValue();
  79. }
  80. }