1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2001, 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) 2002 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.impl.xs.dom;
  58. import com.sun.org.apache.xerces.internal.dom.TextImpl;
  59. import com.sun.org.apache.xerces.internal.dom.AttrNSImpl;
  60. /**
  61. * This class is pool that enables caching of DOM nodes, such as Element, Attr,
  62. * Text, that are used to parse and later traverse XML Schemas.
  63. * The pool is reset before a new set of schemas is traversed.
  64. * Note: pool is not reset during traversals of imported/included
  65. * schemas.
  66. *
  67. * @author Elena Litani, IBM
  68. * @version $Id: DOMNodePool.java,v 1.3 2002/11/20 00:49:47 twl Exp $
  69. */
  70. public final class DOMNodePool {
  71. /** Chunk shift (8). */
  72. private static final int CHUNK_SHIFT = 8; // 2^8 = 256
  73. /** Chunk size (1 << CHUNK_SHIFT). */
  74. private static final int CHUNK_SIZE = 1 << CHUNK_SHIFT;
  75. /** Chunk mask (CHUNK_SIZE - 1). */
  76. private static final int CHUNK_MASK = CHUNK_SIZE - 1;
  77. /** Initial chunk count (). */
  78. private static final int INITIAL_CHUNK_COUNT = (1 << (10 - CHUNK_SHIFT)); // 2^10 = 1k
  79. /** Element nodes pool*/
  80. private ElementNSImpl fElements[][] = new ElementNSImpl[INITIAL_CHUNK_COUNT][];
  81. private int fElementIndex = 0;
  82. /** Text nodes pool*/
  83. private TextImpl fTextNode[][] = new TextImpl[INITIAL_CHUNK_COUNT][];
  84. private int fTextNodeIndex = 0;
  85. /** Attribute nodes pool*/
  86. private AttrNSImpl fAttrNode[][] = new AttrNSImpl[INITIAL_CHUNK_COUNT][];
  87. private int fAttrNodeIndex = 0;
  88. /**
  89. * This method creates a new element node or provides a
  90. * free element node if such exists in the pool.
  91. *
  92. * @return usable element node
  93. */
  94. public final ElementNSImpl getElementNode(){
  95. int chunk = fElementIndex >> CHUNK_SHIFT;
  96. int index = fElementIndex & CHUNK_MASK;
  97. ensureElementsCapacity(chunk);
  98. if (fElements[chunk][index] == null) {
  99. fElements[chunk][index] = new ElementNSImpl();
  100. }
  101. fElementIndex++;
  102. return fElements[chunk][index];
  103. }
  104. private void ensureElementsCapacity(int chunk) {
  105. if (fElements.length <= chunk) {
  106. fElements = resize(fElements, fElements.length * 2);
  107. }
  108. else if (fElements[chunk] != null) {
  109. return;
  110. }
  111. fElements[chunk] = new ElementNSImpl[CHUNK_SIZE];
  112. return;
  113. }
  114. private static ElementNSImpl[][] resize(ElementNSImpl array[][], int newsize) {
  115. ElementNSImpl newarray[][] = new ElementNSImpl[newsize][];
  116. System.arraycopy(array, 0, newarray, 0, array.length);
  117. return newarray;
  118. }
  119. /**
  120. * This methods creates text node or provides a free
  121. * text node if such exists in the pool.
  122. *
  123. * @return a usable TextNode
  124. */
  125. public final TextImpl getTextNode(){
  126. int chunk = fTextNodeIndex >> CHUNK_SHIFT;
  127. int index = fTextNodeIndex & CHUNK_MASK;
  128. ensureTextCapacity(chunk);
  129. if (fTextNode[chunk][index] == null) {
  130. fTextNode[chunk][index] = new TextImpl();
  131. }
  132. fTextNodeIndex++;
  133. return fTextNode[chunk][index];
  134. }
  135. private void ensureTextCapacity(int chunk) {
  136. if (fTextNode.length <= chunk) {
  137. fTextNode = resize(fTextNode, fTextNode.length * 2);
  138. }
  139. else if (fTextNode[chunk] != null) {
  140. return;
  141. }
  142. fTextNode[chunk] = new TextImpl[CHUNK_SIZE];
  143. return;
  144. }
  145. private static TextImpl[][] resize(TextImpl array[][], int newsize) {
  146. TextImpl newarray[][] = new TextImpl[newsize][];
  147. System.arraycopy(array, 0, newarray, 0, array.length);
  148. return newarray;
  149. }
  150. /**
  151. * This methods creates attribute node or provides a free
  152. * attribute node if such exists in the pool.
  153. *
  154. * @return a usable attribute node
  155. */
  156. public final AttrNSImpl getAttrNode(){
  157. int chunk = fAttrNodeIndex >> CHUNK_SHIFT;
  158. int index = fAttrNodeIndex & CHUNK_MASK;
  159. ensureAttrsCapacity(chunk);
  160. if (fAttrNode[chunk][index] == null) {
  161. fAttrNode[chunk][index] = new AttrNSImpl();
  162. }
  163. fAttrNodeIndex++;
  164. return fAttrNode[chunk][index];
  165. }
  166. private void ensureAttrsCapacity(int chunk) {
  167. if (fAttrNode.length <= chunk) {
  168. fAttrNode = resize(fAttrNode, fAttrNode.length * 2);
  169. }
  170. else if (fAttrNode[chunk] != null) {
  171. return;
  172. }
  173. fAttrNode[chunk] = new AttrNSImpl[CHUNK_SIZE];
  174. return;
  175. }
  176. private static AttrNSImpl[][] resize(AttrNSImpl array[][], int newsize) {
  177. AttrNSImpl newarray[][] = new AttrNSImpl[newsize][];
  178. System.arraycopy(array, 0, newarray, 0, array.length);
  179. return newarray;
  180. }
  181. /**
  182. * Reset the pool. The nodes in the pool become 'free' nodes.
  183. */
  184. public void reset(){
  185. fElementIndex = 0;
  186. fTextNodeIndex = 0;
  187. fAttrNodeIndex = 0;
  188. }
  189. }