1 57 58 package com.sun.org.apache.xerces.internal.impl.xs.dom; 59 60 import com.sun.org.apache.xerces.internal.dom.TextImpl; 61 import com.sun.org.apache.xerces.internal.dom.AttrNSImpl; 62 63 64 74 public final class DOMNodePool { 75 76 private static final int CHUNK_SHIFT = 8; 78 79 private static final int CHUNK_SIZE = 1 << CHUNK_SHIFT; 80 81 82 private static final int CHUNK_MASK = CHUNK_SIZE - 1; 83 84 85 private static final int INITIAL_CHUNK_COUNT = (1 << (10 - CHUNK_SHIFT)); 87 88 private ElementNSImpl fElements[][] = new ElementNSImpl[INITIAL_CHUNK_COUNT][]; 89 private int fElementIndex = 0; 90 91 92 93 private TextImpl fTextNode[][] = new TextImpl[INITIAL_CHUNK_COUNT][]; 94 private int fTextNodeIndex = 0; 95 96 97 98 private AttrNSImpl fAttrNode[][] = new AttrNSImpl[INITIAL_CHUNK_COUNT][]; 99 private int fAttrNodeIndex = 0; 100 101 102 103 109 public final ElementNSImpl getElementNode(){ 110 int chunk = fElementIndex >> CHUNK_SHIFT; 111 int index = fElementIndex & CHUNK_MASK; 112 ensureElementsCapacity(chunk); 113 if (fElements[chunk][index] == null) { 114 fElements[chunk][index] = new ElementNSImpl(); 115 } 116 fElementIndex++; 117 return fElements[chunk][index]; 118 } 119 120 private void ensureElementsCapacity(int chunk) { 121 if (fElements.length <= chunk) { 122 fElements = resize(fElements, fElements.length * 2); 123 } 124 else if (fElements[chunk] != null) { 125 return; 126 } 127 128 fElements[chunk] = new ElementNSImpl[CHUNK_SIZE]; 129 return; 130 } 131 132 private static ElementNSImpl[][] resize(ElementNSImpl array[][], int newsize) { 133 ElementNSImpl newarray[][] = new ElementNSImpl[newsize][]; 134 System.arraycopy(array, 0, newarray, 0, array.length); 135 return newarray; 136 } 137 138 144 public final TextImpl getTextNode(){ 145 int chunk = fTextNodeIndex >> CHUNK_SHIFT; 146 int index = fTextNodeIndex & CHUNK_MASK; 147 ensureTextCapacity(chunk); 148 if (fTextNode[chunk][index] == null) { 149 fTextNode[chunk][index] = new TextImpl(); 150 } 151 fTextNodeIndex++; 152 return fTextNode[chunk][index]; 153 } 154 155 private void ensureTextCapacity(int chunk) { 156 if (fTextNode.length <= chunk) { 157 fTextNode = resize(fTextNode, fTextNode.length * 2); 158 } 159 else if (fTextNode[chunk] != null) { 160 return; 161 } 162 163 fTextNode[chunk] = new TextImpl[CHUNK_SIZE]; 164 return; 165 } 166 167 private static TextImpl[][] resize(TextImpl array[][], int newsize) { 168 TextImpl newarray[][] = new TextImpl[newsize][]; 169 System.arraycopy(array, 0, newarray, 0, array.length); 170 return newarray; 171 } 172 173 179 public final AttrNSImpl getAttrNode(){ 180 int chunk = fAttrNodeIndex >> CHUNK_SHIFT; 181 int index = fAttrNodeIndex & CHUNK_MASK; 182 ensureAttrsCapacity(chunk); 183 if (fAttrNode[chunk][index] == null) { 184 fAttrNode[chunk][index] = new AttrNSImpl(); 185 } 186 fAttrNodeIndex++; 187 return fAttrNode[chunk][index]; 188 } 189 190 private void ensureAttrsCapacity(int chunk) { 191 if (fAttrNode.length <= chunk) { 192 fAttrNode = resize(fAttrNode, fAttrNode.length * 2); 193 } 194 else if (fAttrNode[chunk] != null) { 195 return; 196 } 197 198 fAttrNode[chunk] = new AttrNSImpl[CHUNK_SIZE]; 199 return; 200 } 201 202 private static AttrNSImpl[][] resize(AttrNSImpl array[][], int newsize) { 203 AttrNSImpl newarray[][] = new AttrNSImpl[newsize][]; 204 System.arraycopy(array, 0, newarray, 0, array.length); 205 return newarray; 206 } 207 208 209 212 public void reset(){ 213 fElementIndex = 0; 214 fTextNodeIndex = 0; 215 fAttrNodeIndex = 0; 216 } 217 218 219 } 220 | Popular Tags |