1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright 2001, 2002,2004 The Apache Software Foundation.
   7  *
   8  * Licensed under the Apache License, Version 2.0 (the "License");
   9  * you may not use this file except in compliance with the License.
  10  * You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 package com.sun.org.apache.xerces.internal.impl.xs;
  22 
  23 import com.sun.org.apache.xerces.internal.impl.dv.xs.SchemaDVFactoryImpl;
  24 import com.sun.org.apache.xerces.internal.impl.dv.xs.XSSimpleTypeDecl;
  25 
  26 /**
  27  * This class is pool that enables caching of XML Schema declaration objects.
  28  * Before a compiled grammar object is garbage collected,
  29  * the implementation will add all XML Schema component
  30  * declarations to the pool.
  31  * Note: The cashing mechanism is not implemented yet.
  32  *
  33  * @xerces.internal
  34  *
  35  * @author Elena Litani, IBM
  36  */
  37 public final class XSDeclarationPool {
  38     /** Chunk shift (8). */
  39     private static final int CHUNK_SHIFT = 8; // 2^8 = 256
  40 
  41     /** Chunk size (1 << CHUNK_SHIFT). */
  42     private static final int CHUNK_SIZE = 1 << CHUNK_SHIFT;
  43 
  44     /** Chunk mask (CHUNK_SIZE - 1). */
  45     private static final int CHUNK_MASK = CHUNK_SIZE - 1;
  46 
  47     /** Initial chunk count (). */
  48     private static final int INITIAL_CHUNK_COUNT = (1 << (10 - CHUNK_SHIFT)); // 2^10 = 1k
  49 
  50     /** Element declaration pool*/
  51     private XSElementDecl fElementDecl[][] = new XSElementDecl[INITIAL_CHUNK_COUNT][];
  52     private int fElementDeclIndex = 0;
  53 
  54     /** Particle declaration pool */
  55     private XSParticleDecl fParticleDecl[][] = new XSParticleDecl[INITIAL_CHUNK_COUNT][];
  56     private int fParticleDeclIndex = 0;
  57 
  58     /** Particle declaration pool */
  59     private XSModelGroupImpl fModelGroup[][] = new XSModelGroupImpl[INITIAL_CHUNK_COUNT][];
  60     private int fModelGroupIndex = 0;
  61 
  62     /** Attribute declaration pool */
  63     private XSAttributeDecl fAttrDecl[][] = new XSAttributeDecl[INITIAL_CHUNK_COUNT][];
  64     private int fAttrDeclIndex = 0;
  65 
  66     /** ComplexType declaration pool */
  67     private XSComplexTypeDecl fCTDecl[][] = new XSComplexTypeDecl[INITIAL_CHUNK_COUNT][];
  68     private int fCTDeclIndex = 0;
  69 
  70     /** SimpleType declaration pool */
  71     private XSSimpleTypeDecl fSTDecl[][] = new XSSimpleTypeDecl[INITIAL_CHUNK_COUNT][];
  72     private int fSTDeclIndex = 0;
  73 
  74     /** AttributeUse declaration pool */
  75     private XSAttributeUseImpl fAttributeUse[][] = new XSAttributeUseImpl[INITIAL_CHUNK_COUNT][];
  76     private int fAttributeUseIndex = 0;
  77 
  78     private SchemaDVFactoryImpl dvFactory;
  79     public void setDVFactory(SchemaDVFactoryImpl dvFactory) {
  80         this.dvFactory = dvFactory;
  81     }
  82 
  83     public final  XSElementDecl getElementDecl(){
  84         int     chunk       = fElementDeclIndex >> CHUNK_SHIFT;
  85         int     index       = fElementDeclIndex &  CHUNK_MASK;
  86         ensureElementDeclCapacity(chunk);
  87         if (fElementDecl[chunk][index] == null) {
  88             fElementDecl[chunk][index] = new XSElementDecl();
  89         } else {
  90             fElementDecl[chunk][index].reset();
  91         }
  92         fElementDeclIndex++;
  93         return fElementDecl[chunk][index];
  94     }
  95 
  96     public final XSAttributeDecl getAttributeDecl(){
  97         int     chunk       = fAttrDeclIndex >> CHUNK_SHIFT;
  98         int     index       = fAttrDeclIndex &  CHUNK_MASK;
  99         ensureAttrDeclCapacity(chunk);
 100         if (fAttrDecl[chunk][index] == null) {
 101             fAttrDecl[chunk][index] = new XSAttributeDecl();
 102         } else {
 103             fAttrDecl[chunk][index].reset();
 104         }
 105         fAttrDeclIndex++;
 106         return fAttrDecl[chunk][index];
 107 
 108     }
 109 
 110     public final XSAttributeUseImpl getAttributeUse(){
 111         int     chunk       = fAttributeUseIndex >> CHUNK_SHIFT;
 112         int     index       = fAttributeUseIndex &  CHUNK_MASK;
 113         ensureAttributeUseCapacity(chunk);
 114         if (fAttributeUse[chunk][index] == null) {
 115             fAttributeUse[chunk][index] = new XSAttributeUseImpl();
 116         } else {
 117             fAttributeUse[chunk][index].reset();
 118         }
 119         fAttributeUseIndex++;
 120         return fAttributeUse[chunk][index];
 121 
 122     }
 123 
 124     public final XSComplexTypeDecl getComplexTypeDecl(){
 125         int     chunk       = fCTDeclIndex >> CHUNK_SHIFT;
 126         int     index       = fCTDeclIndex &  CHUNK_MASK;
 127         ensureCTDeclCapacity(chunk);
 128         if (fCTDecl[chunk][index] == null) {
 129 
 130             fCTDecl[chunk][index] = new XSComplexTypeDecl();
 131         } else {
 132             fCTDecl[chunk][index].reset();
 133         }
 134         fCTDeclIndex++;
 135         return fCTDecl[chunk][index];
 136     }
 137 
 138     public final XSSimpleTypeDecl getSimpleTypeDecl(){
 139         int     chunk       = fSTDeclIndex >> CHUNK_SHIFT;
 140         int     index       = fSTDeclIndex &  CHUNK_MASK;
 141         ensureSTDeclCapacity(chunk);
 142         if (fSTDecl[chunk][index] == null) {
 143             fSTDecl[chunk][index] = dvFactory.newXSSimpleTypeDecl();
 144         } else {
 145             fSTDecl[chunk][index].reset();
 146         }
 147         fSTDeclIndex++;
 148         return fSTDecl[chunk][index];
 149 
 150     }
 151 
 152     public final XSParticleDecl getParticleDecl(){
 153         int     chunk       = fParticleDeclIndex >> CHUNK_SHIFT;
 154         int     index       = fParticleDeclIndex &  CHUNK_MASK;
 155         ensureParticleDeclCapacity(chunk);
 156         if (fParticleDecl[chunk][index] == null) {
 157             fParticleDecl[chunk][index] = new XSParticleDecl();
 158         } else {
 159             fParticleDecl[chunk][index].reset();
 160         }
 161         fParticleDeclIndex++;
 162         return fParticleDecl[chunk][index];
 163     }
 164 
 165     public final XSModelGroupImpl getModelGroup(){
 166         int     chunk       = fModelGroupIndex >> CHUNK_SHIFT;
 167         int     index       = fModelGroupIndex &  CHUNK_MASK;
 168         ensureModelGroupCapacity(chunk);
 169         if (fModelGroup[chunk][index] == null) {
 170             fModelGroup[chunk][index] = new XSModelGroupImpl();
 171         } else {
 172             fModelGroup[chunk][index].reset();
 173         }
 174         fModelGroupIndex++;
 175         return fModelGroup[chunk][index];
 176     }
 177 
 178     // REVISIT: do we need decl pool for group declarations, attribute group,
 179     //          notations?
 180     //          it seems like each schema would use a small number of those
 181     //          components, so it probably is not worth keeping those components
 182     //          in the pool.
 183 
 184     private boolean ensureElementDeclCapacity(int chunk) {
 185         if (chunk >= fElementDecl.length) {
 186             fElementDecl = resize(fElementDecl, fElementDecl.length * 2);
 187         } else if (fElementDecl[chunk] != null) {
 188             return false;
 189         }
 190 
 191         fElementDecl[chunk] = new XSElementDecl[CHUNK_SIZE];
 192         return true;
 193     }
 194 
 195     private static XSElementDecl[][] resize(XSElementDecl array[][], int newsize) {
 196         XSElementDecl newarray[][] = new XSElementDecl[newsize][];
 197         System.arraycopy(array, 0, newarray, 0, array.length);
 198         return newarray;
 199     }
 200 
 201     private boolean ensureParticleDeclCapacity(int chunk) {
 202         if (chunk >= fParticleDecl.length) {
 203             fParticleDecl = resize(fParticleDecl, fParticleDecl.length * 2);
 204         } else if (fParticleDecl[chunk] != null) {
 205             return false;
 206         }
 207 
 208         fParticleDecl[chunk] = new XSParticleDecl[CHUNK_SIZE];
 209         return true;
 210     }
 211 
 212     private boolean ensureModelGroupCapacity(int chunk) {
 213         if (chunk >= fModelGroup.length) {
 214             fModelGroup = resize(fModelGroup, fModelGroup.length * 2);
 215         } else if (fModelGroup[chunk] != null) {
 216             return false;
 217         }
 218 
 219         fModelGroup[chunk] = new XSModelGroupImpl[CHUNK_SIZE];
 220         return true;
 221     }
 222 
 223     private static XSParticleDecl[][] resize(XSParticleDecl array[][], int newsize) {
 224         XSParticleDecl newarray[][] = new XSParticleDecl[newsize][];
 225         System.arraycopy(array, 0, newarray, 0, array.length);
 226         return newarray;
 227     }
 228 
 229     private static XSModelGroupImpl[][] resize(XSModelGroupImpl array[][], int newsize) {
 230         XSModelGroupImpl newarray[][] = new XSModelGroupImpl[newsize][];
 231         System.arraycopy(array, 0, newarray, 0, array.length);
 232         return newarray;
 233     }
 234 
 235     private boolean ensureAttrDeclCapacity(int chunk) {
 236         if (chunk >= fAttrDecl.length) {
 237             fAttrDecl = resize(fAttrDecl, fAttrDecl.length * 2);
 238         } else if (fAttrDecl[chunk] != null) {
 239             return false;
 240         }
 241 
 242         fAttrDecl[chunk] = new XSAttributeDecl[CHUNK_SIZE];
 243         return true;
 244     }
 245 
 246     private static XSAttributeDecl[][] resize(XSAttributeDecl array[][], int newsize) {
 247         XSAttributeDecl newarray[][] = new XSAttributeDecl[newsize][];
 248         System.arraycopy(array, 0, newarray, 0, array.length);
 249         return newarray;
 250     }
 251 
 252     private boolean ensureAttributeUseCapacity(int chunk) {
 253         if (chunk >= fAttributeUse.length) {
 254             fAttributeUse = resize(fAttributeUse, fAttributeUse.length * 2);
 255         } else if (fAttributeUse[chunk] != null) {
 256             return false;
 257         }
 258 
 259         fAttributeUse[chunk] = new XSAttributeUseImpl[CHUNK_SIZE];
 260         return true;
 261     }
 262 
 263     private static XSAttributeUseImpl[][] resize(XSAttributeUseImpl array[][], int newsize) {
 264         XSAttributeUseImpl newarray[][] = new XSAttributeUseImpl[newsize][];
 265         System.arraycopy(array, 0, newarray, 0, array.length);
 266         return newarray;
 267     }
 268 
 269     private boolean ensureSTDeclCapacity(int chunk) {
 270         if (chunk >= fSTDecl.length) {
 271             fSTDecl = resize(fSTDecl, fSTDecl.length * 2);
 272         } else if (fSTDecl[chunk] != null) {
 273             return false;
 274         }
 275 
 276         fSTDecl[chunk] = new XSSimpleTypeDecl[CHUNK_SIZE];
 277         return true;
 278     }
 279 
 280     private static XSSimpleTypeDecl[][] resize(XSSimpleTypeDecl array[][], int newsize) {
 281         XSSimpleTypeDecl newarray[][] = new XSSimpleTypeDecl[newsize][];
 282         System.arraycopy(array, 0, newarray, 0, array.length);
 283         return newarray;
 284     }
 285 
 286     private boolean ensureCTDeclCapacity(int chunk) {
 287 
 288         if (chunk >= fCTDecl.length) {
 289             fCTDecl = resize(fCTDecl, fCTDecl.length * 2);
 290         } else if (fCTDecl[chunk] != null){
 291             return false;
 292         }
 293 
 294         fCTDecl[chunk] = new XSComplexTypeDecl[CHUNK_SIZE];
 295         return true;
 296     }
 297 
 298     private static XSComplexTypeDecl[][] resize(XSComplexTypeDecl array[][], int newsize) {
 299         XSComplexTypeDecl newarray[][] = new XSComplexTypeDecl[newsize][];
 300         System.arraycopy(array, 0, newarray, 0, array.length);
 301         return newarray;
 302     }
 303 
 304 
 305 
 306     public void reset(){
 307         fElementDeclIndex = 0;
 308         fParticleDeclIndex = 0;
 309         fModelGroupIndex = 0;
 310         fSTDeclIndex = 0;
 311         fCTDeclIndex = 0;
 312         fAttrDeclIndex = 0;
 313         fAttributeUseIndex = 0;
 314     }
 315 
 316 
 317 }