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) 2001, 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;
  58. import com.sun.org.apache.xerces.internal.impl.dv.xs.XSSimpleTypeDecl;
  59. /**
  60. * This class is pool that enables caching of XML Schema declaration objects.
  61. * Before a compiled grammar object is garbage collected,
  62. * the implementation will add all XML Schema component
  63. * declarations to the pool.
  64. * Note: The cashing mechanism is not implemented yet.
  65. *
  66. * @author Elena Litani, IBM
  67. * @version $Id: XSDeclarationPool.java,v 1.7 2002/11/09 22:18:06 sandygao Exp $
  68. */
  69. public final class XSDeclarationPool {
  70. /** Chunk shift (8). */
  71. private static final int CHUNK_SHIFT = 8; // 2^8 = 256
  72. /** Chunk size (1 << CHUNK_SHIFT). */
  73. private static final int CHUNK_SIZE = 1 << CHUNK_SHIFT;
  74. /** Chunk mask (CHUNK_SIZE - 1). */
  75. private static final int CHUNK_MASK = CHUNK_SIZE - 1;
  76. /** Initial chunk count (). */
  77. private static final int INITIAL_CHUNK_COUNT = (1 << (10 - CHUNK_SHIFT)); // 2^10 = 1k
  78. /** Element declaration pool*/
  79. private XSElementDecl fElementDecl[][] = new XSElementDecl[INITIAL_CHUNK_COUNT][];
  80. private int fElementDeclIndex = 0;
  81. /** Particle declaration pool */
  82. private XSParticleDecl fParticleDecl[][] = new XSParticleDecl[INITIAL_CHUNK_COUNT][];
  83. private int fParticleDeclIndex = 0;
  84. /** Particle declaration pool */
  85. private XSModelGroupImpl fModelGroup[][] = new XSModelGroupImpl[INITIAL_CHUNK_COUNT][];
  86. private int fModelGroupIndex = 0;
  87. /** Attribute declaration pool */
  88. private XSAttributeDecl fAttrDecl[][] = new XSAttributeDecl[INITIAL_CHUNK_COUNT][];
  89. private int fAttrDeclIndex = 0;
  90. /** ComplexType declaration pool */
  91. private XSComplexTypeDecl fCTDecl[][] = new XSComplexTypeDecl[INITIAL_CHUNK_COUNT][];
  92. private int fCTDeclIndex = 0;
  93. /** SimpleType declaration pool */
  94. private XSSimpleTypeDecl fSTDecl[][] = new XSSimpleTypeDecl[INITIAL_CHUNK_COUNT][];
  95. private int fSTDeclIndex = 0;
  96. /** AttributeUse declaration pool */
  97. private XSAttributeUseImpl fAttributeUse[][] = new XSAttributeUseImpl[INITIAL_CHUNK_COUNT][];
  98. private int fAttributeUseIndex = 0;
  99. public final XSElementDecl getElementDecl(){
  100. int chunk = fElementDeclIndex >> CHUNK_SHIFT;
  101. int index = fElementDeclIndex & CHUNK_MASK;
  102. ensureElementDeclCapacity(chunk);
  103. if (fElementDecl[chunk][index] == null) {
  104. fElementDecl[chunk][index] = new XSElementDecl();
  105. } else {
  106. fElementDecl[chunk][index].reset();
  107. }
  108. fElementDeclIndex++;
  109. return fElementDecl[chunk][index];
  110. }
  111. public final XSAttributeDecl getAttributeDecl(){
  112. int chunk = fAttrDeclIndex >> CHUNK_SHIFT;
  113. int index = fAttrDeclIndex & CHUNK_MASK;
  114. ensureAttrDeclCapacity(chunk);
  115. if (fAttrDecl[chunk][index] == null) {
  116. fAttrDecl[chunk][index] = new XSAttributeDecl();
  117. } else {
  118. fAttrDecl[chunk][index].reset();
  119. }
  120. fAttrDeclIndex++;
  121. return fAttrDecl[chunk][index];
  122. }
  123. public final XSAttributeUseImpl getAttributeUse(){
  124. int chunk = fAttributeUseIndex >> CHUNK_SHIFT;
  125. int index = fAttributeUseIndex & CHUNK_MASK;
  126. ensureAttributeUseCapacity(chunk);
  127. if (fAttributeUse[chunk][index] == null) {
  128. fAttributeUse[chunk][index] = new XSAttributeUseImpl();
  129. } else {
  130. fAttributeUse[chunk][index].reset();
  131. }
  132. fAttributeUseIndex++;
  133. return fAttributeUse[chunk][index];
  134. }
  135. public final XSComplexTypeDecl getComplexTypeDecl(){
  136. int chunk = fCTDeclIndex >> CHUNK_SHIFT;
  137. int index = fCTDeclIndex & CHUNK_MASK;
  138. ensureCTDeclCapacity(chunk);
  139. if (fCTDecl[chunk][index] == null) {
  140. fCTDecl[chunk][index] = new XSComplexTypeDecl();
  141. } else {
  142. fCTDecl[chunk][index].reset();
  143. }
  144. fCTDeclIndex++;
  145. return fCTDecl[chunk][index];
  146. }
  147. public final XSSimpleTypeDecl getSimpleTypeDecl(){
  148. int chunk = fSTDeclIndex >> CHUNK_SHIFT;
  149. int index = fSTDeclIndex & CHUNK_MASK;
  150. ensureSTDeclCapacity(chunk);
  151. if (fSTDecl[chunk][index] == null) {
  152. fSTDecl[chunk][index] = new XSSimpleTypeDecl();
  153. } else {
  154. fSTDecl[chunk][index].reset();
  155. }
  156. fSTDeclIndex++;
  157. return fSTDecl[chunk][index];
  158. }
  159. public final XSParticleDecl getParticleDecl(){
  160. int chunk = fParticleDeclIndex >> CHUNK_SHIFT;
  161. int index = fParticleDeclIndex & CHUNK_MASK;
  162. ensureParticleDeclCapacity(chunk);
  163. if (fParticleDecl[chunk][index] == null) {
  164. fParticleDecl[chunk][index] = new XSParticleDecl();
  165. } else {
  166. fParticleDecl[chunk][index].reset();
  167. }
  168. fParticleDeclIndex++;
  169. return fParticleDecl[chunk][index];
  170. }
  171. public final XSModelGroupImpl getModelGroup(){
  172. int chunk = fModelGroupIndex >> CHUNK_SHIFT;
  173. int index = fModelGroupIndex & CHUNK_MASK;
  174. ensureModelGroupCapacity(chunk);
  175. if (fModelGroup[chunk][index] == null) {
  176. fModelGroup[chunk][index] = new XSModelGroupImpl();
  177. } else {
  178. fModelGroup[chunk][index].reset();
  179. }
  180. fModelGroupIndex++;
  181. return fModelGroup[chunk][index];
  182. }
  183. // REVISIT: do we need decl pool for group declarations, attribute group,
  184. // notations?
  185. // it seems like each schema would use a small number of those
  186. // components, so it probably is not worth keeping those components
  187. // in the pool.
  188. private boolean ensureElementDeclCapacity(int chunk) {
  189. if (chunk >= fElementDecl.length) {
  190. fElementDecl = resize(fElementDecl, fElementDecl.length * 2);
  191. } else if (fElementDecl[chunk] != null) {
  192. return false;
  193. }
  194. fElementDecl[chunk] = new XSElementDecl[CHUNK_SIZE];
  195. return true;
  196. }
  197. private static XSElementDecl[][] resize(XSElementDecl array[][], int newsize) {
  198. XSElementDecl newarray[][] = new XSElementDecl[newsize][];
  199. System.arraycopy(array, 0, newarray, 0, array.length);
  200. return newarray;
  201. }
  202. private boolean ensureParticleDeclCapacity(int chunk) {
  203. if (chunk >= fParticleDecl.length) {
  204. fParticleDecl = resize(fParticleDecl, fParticleDecl.length * 2);
  205. } else if (fParticleDecl[chunk] != null) {
  206. return false;
  207. }
  208. fParticleDecl[chunk] = new XSParticleDecl[CHUNK_SIZE];
  209. return true;
  210. }
  211. private boolean ensureModelGroupCapacity(int chunk) {
  212. if (chunk >= fModelGroup.length) {
  213. fModelGroup = resize(fModelGroup, fModelGroup.length * 2);
  214. } else if (fModelGroup[chunk] != null) {
  215. return false;
  216. }
  217. fModelGroup[chunk] = new XSModelGroupImpl[CHUNK_SIZE];
  218. return true;
  219. }
  220. private static XSParticleDecl[][] resize(XSParticleDecl array[][], int newsize) {
  221. XSParticleDecl newarray[][] = new XSParticleDecl[newsize][];
  222. System.arraycopy(array, 0, newarray, 0, array.length);
  223. return newarray;
  224. }
  225. private static XSModelGroupImpl[][] resize(XSModelGroupImpl array[][], int newsize) {
  226. XSModelGroupImpl newarray[][] = new XSModelGroupImpl[newsize][];
  227. System.arraycopy(array, 0, newarray, 0, array.length);
  228. return newarray;
  229. }
  230. private boolean ensureAttrDeclCapacity(int chunk) {
  231. if (chunk >= fAttrDecl.length) {
  232. fAttrDecl = resize(fAttrDecl, fAttrDecl.length * 2);
  233. } else if (fAttrDecl[chunk] != null) {
  234. return false;
  235. }
  236. fAttrDecl[chunk] = new XSAttributeDecl[CHUNK_SIZE];
  237. return true;
  238. }
  239. private static XSAttributeDecl[][] resize(XSAttributeDecl array[][], int newsize) {
  240. XSAttributeDecl newarray[][] = new XSAttributeDecl[newsize][];
  241. System.arraycopy(array, 0, newarray, 0, array.length);
  242. return newarray;
  243. }
  244. private boolean ensureAttributeUseCapacity(int chunk) {
  245. if (chunk >= fAttributeUse.length) {
  246. fAttributeUse = resize(fAttributeUse, fAttributeUse.length * 2);
  247. } else if (fAttributeUse[chunk] != null) {
  248. return false;
  249. }
  250. fAttributeUse[chunk] = new XSAttributeUseImpl[CHUNK_SIZE];
  251. return true;
  252. }
  253. private static XSAttributeUseImpl[][] resize(XSAttributeUseImpl array[][], int newsize) {
  254. XSAttributeUseImpl newarray[][] = new XSAttributeUseImpl[newsize][];
  255. System.arraycopy(array, 0, newarray, 0, array.length);
  256. return newarray;
  257. }
  258. private boolean ensureSTDeclCapacity(int chunk) {
  259. if (chunk >= fSTDecl.length) {
  260. fSTDecl = resize(fSTDecl, fSTDecl.length * 2);
  261. } else if (fSTDecl[chunk] != null) {
  262. return false;
  263. }
  264. fSTDecl[chunk] = new XSSimpleTypeDecl[CHUNK_SIZE];
  265. return true;
  266. }
  267. private static XSSimpleTypeDecl[][] resize(XSSimpleTypeDecl array[][], int newsize) {
  268. XSSimpleTypeDecl newarray[][] = new XSSimpleTypeDecl[newsize][];
  269. System.arraycopy(array, 0, newarray, 0, array.length);
  270. return newarray;
  271. }
  272. private boolean ensureCTDeclCapacity(int chunk) {
  273. if (chunk >= fCTDecl.length) {
  274. fCTDecl = resize(fCTDecl, fCTDecl.length * 2);
  275. } else if (fCTDecl[chunk] != null){
  276. return false;
  277. }
  278. fCTDecl[chunk] = new XSComplexTypeDecl[CHUNK_SIZE];
  279. return true;
  280. }
  281. private static XSComplexTypeDecl[][] resize(XSComplexTypeDecl array[][], int newsize) {
  282. XSComplexTypeDecl newarray[][] = new XSComplexTypeDecl[newsize][];
  283. System.arraycopy(array, 0, newarray, 0, array.length);
  284. return newarray;
  285. }
  286. public void reset(){
  287. fElementDeclIndex = 0;
  288. fParticleDeclIndex = 0;
  289. fModelGroupIndex = 0;
  290. fSTDeclIndex = 0;
  291. fCTDeclIndex = 0;
  292. fAttrDeclIndex = 0;
  293. fAttributeUseIndex = 0;
  294. }
  295. }