View Javadoc
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   * @version $Id: XSDeclarationPool.java,v 1.7 2010-11-01 04:39:55 joehw Exp $
37   */
38  public final class XSDeclarationPool {
39      /** Chunk shift (8). */
40      private static final int CHUNK_SHIFT = 8; // 2^8 = 256
41  
42      /** Chunk size (1 << CHUNK_SHIFT). */
43      private static final int CHUNK_SIZE = 1 << CHUNK_SHIFT;
44  
45      /** Chunk mask (CHUNK_SIZE - 1). */
46      private static final int CHUNK_MASK = CHUNK_SIZE - 1;
47  
48      /** Initial chunk count (). */
49      private static final int INITIAL_CHUNK_COUNT = (1 << (10 - CHUNK_SHIFT)); // 2^10 = 1k
50  
51      /** Element declaration pool*/
52      private XSElementDecl fElementDecl[][] = new XSElementDecl[INITIAL_CHUNK_COUNT][];
53      private int fElementDeclIndex = 0;
54  
55      /** Particle declaration pool */
56      private XSParticleDecl fParticleDecl[][] = new XSParticleDecl[INITIAL_CHUNK_COUNT][];
57      private int fParticleDeclIndex = 0;
58  
59      /** Particle declaration pool */
60      private XSModelGroupImpl fModelGroup[][] = new XSModelGroupImpl[INITIAL_CHUNK_COUNT][];
61      private int fModelGroupIndex = 0;
62  
63      /** Attribute declaration pool */
64      private XSAttributeDecl fAttrDecl[][] = new XSAttributeDecl[INITIAL_CHUNK_COUNT][];
65      private int fAttrDeclIndex = 0;
66  
67      /** ComplexType declaration pool */
68      private XSComplexTypeDecl fCTDecl[][] = new XSComplexTypeDecl[INITIAL_CHUNK_COUNT][];
69      private int fCTDeclIndex = 0;
70  
71      /** SimpleType declaration pool */
72      private XSSimpleTypeDecl fSTDecl[][] = new XSSimpleTypeDecl[INITIAL_CHUNK_COUNT][];
73      private int fSTDeclIndex = 0;
74  
75      /** AttributeUse declaration pool */
76      private XSAttributeUseImpl fAttributeUse[][] = new XSAttributeUseImpl[INITIAL_CHUNK_COUNT][];
77      private int fAttributeUseIndex = 0;
78  
79      private SchemaDVFactoryImpl dvFactory;
80      public void setDVFactory(SchemaDVFactoryImpl dvFactory) {
81          this.dvFactory = dvFactory;
82      }
83  
84      public final  XSElementDecl getElementDecl(){
85          int     chunk       = fElementDeclIndex >> CHUNK_SHIFT;
86          int     index       = fElementDeclIndex &  CHUNK_MASK;
87          ensureElementDeclCapacity(chunk);
88          if (fElementDecl[chunk][index] == null) {
89              fElementDecl[chunk][index] = new XSElementDecl();
90          } else {
91              fElementDecl[chunk][index].reset();
92          }
93          fElementDeclIndex++;
94          return fElementDecl[chunk][index];
95      }
96  
97      public final XSAttributeDecl getAttributeDecl(){
98          int     chunk       = fAttrDeclIndex >> CHUNK_SHIFT;
99          int     index       = fAttrDeclIndex &  CHUNK_MASK;
100         ensureAttrDeclCapacity(chunk);
101         if (fAttrDecl[chunk][index] == null) {
102             fAttrDecl[chunk][index] = new XSAttributeDecl();
103         } else {
104             fAttrDecl[chunk][index].reset();
105         }
106         fAttrDeclIndex++;
107         return fAttrDecl[chunk][index];
108 
109     }
110 
111     public final XSAttributeUseImpl getAttributeUse(){
112         int     chunk       = fAttributeUseIndex >> CHUNK_SHIFT;
113         int     index       = fAttributeUseIndex &  CHUNK_MASK;
114         ensureAttributeUseCapacity(chunk);
115         if (fAttributeUse[chunk][index] == null) {
116             fAttributeUse[chunk][index] = new XSAttributeUseImpl();
117         } else {
118             fAttributeUse[chunk][index].reset();
119         }
120         fAttributeUseIndex++;
121         return fAttributeUse[chunk][index];
122 
123     }
124 
125     public final XSComplexTypeDecl getComplexTypeDecl(){
126         int     chunk       = fCTDeclIndex >> CHUNK_SHIFT;
127         int     index       = fCTDeclIndex &  CHUNK_MASK;
128         ensureCTDeclCapacity(chunk);
129         if (fCTDecl[chunk][index] == null) {
130 
131             fCTDecl[chunk][index] = new XSComplexTypeDecl();
132         } else {
133             fCTDecl[chunk][index].reset();
134         }
135         fCTDeclIndex++;
136         return fCTDecl[chunk][index];
137     }
138 
139     public final XSSimpleTypeDecl getSimpleTypeDecl(){
140         int     chunk       = fSTDeclIndex >> CHUNK_SHIFT;
141         int     index       = fSTDeclIndex &  CHUNK_MASK;
142         ensureSTDeclCapacity(chunk);
143         if (fSTDecl[chunk][index] == null) {
144             fSTDecl[chunk][index] = dvFactory.newXSSimpleTypeDecl();
145         } else {
146             fSTDecl[chunk][index].reset();
147         }
148         fSTDeclIndex++;
149         return fSTDecl[chunk][index];
150 
151     }
152 
153     public final XSParticleDecl getParticleDecl(){
154         int     chunk       = fParticleDeclIndex >> CHUNK_SHIFT;
155         int     index       = fParticleDeclIndex &  CHUNK_MASK;
156         ensureParticleDeclCapacity(chunk);
157         if (fParticleDecl[chunk][index] == null) {
158             fParticleDecl[chunk][index] = new XSParticleDecl();
159         } else {
160             fParticleDecl[chunk][index].reset();
161         }
162         fParticleDeclIndex++;
163         return fParticleDecl[chunk][index];
164     }
165 
166     public final XSModelGroupImpl getModelGroup(){
167         int     chunk       = fModelGroupIndex >> CHUNK_SHIFT;
168         int     index       = fModelGroupIndex &  CHUNK_MASK;
169         ensureModelGroupCapacity(chunk);
170         if (fModelGroup[chunk][index] == null) {
171             fModelGroup[chunk][index] = new XSModelGroupImpl();
172         } else {
173             fModelGroup[chunk][index].reset();
174         }
175         fModelGroupIndex++;
176         return fModelGroup[chunk][index];
177     }
178 
179     // REVISIT: do we need decl pool for group declarations, attribute group,
180     //          notations?
181     //          it seems like each schema would use a small number of those
182     //          components, so it probably is not worth keeping those components
183     //          in the pool.
184 
185     private boolean ensureElementDeclCapacity(int chunk) {
186         if (chunk >= fElementDecl.length) {
187             fElementDecl = resize(fElementDecl, fElementDecl.length * 2);
188         } else if (fElementDecl[chunk] != null) {
189             return false;
190         }
191 
192         fElementDecl[chunk] = new XSElementDecl[CHUNK_SIZE];
193         return true;
194     }
195 
196     private static XSElementDecl[][] resize(XSElementDecl array[][], int newsize) {
197         XSElementDecl newarray[][] = new XSElementDecl[newsize][];
198         System.arraycopy(array, 0, newarray, 0, array.length);
199         return newarray;
200     }
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 
209         fParticleDecl[chunk] = new XSParticleDecl[CHUNK_SIZE];
210         return true;
211     }
212 
213     private boolean ensureModelGroupCapacity(int chunk) {
214         if (chunk >= fModelGroup.length) {
215             fModelGroup = resize(fModelGroup, fModelGroup.length * 2);
216         } else if (fModelGroup[chunk] != null) {
217             return false;
218         }
219 
220         fModelGroup[chunk] = new XSModelGroupImpl[CHUNK_SIZE];
221         return true;
222     }
223 
224     private static XSParticleDecl[][] resize(XSParticleDecl array[][], int newsize) {
225         XSParticleDecl newarray[][] = new XSParticleDecl[newsize][];
226         System.arraycopy(array, 0, newarray, 0, array.length);
227         return newarray;
228     }
229 
230     private static XSModelGroupImpl[][] resize(XSModelGroupImpl array[][], int newsize) {
231         XSModelGroupImpl newarray[][] = new XSModelGroupImpl[newsize][];
232         System.arraycopy(array, 0, newarray, 0, array.length);
233         return newarray;
234     }
235 
236     private boolean ensureAttrDeclCapacity(int chunk) {
237         if (chunk >= fAttrDecl.length) {
238             fAttrDecl = resize(fAttrDecl, fAttrDecl.length * 2);
239         } else if (fAttrDecl[chunk] != null) {
240             return false;
241         }
242 
243         fAttrDecl[chunk] = new XSAttributeDecl[CHUNK_SIZE];
244         return true;
245     }
246 
247     private static XSAttributeDecl[][] resize(XSAttributeDecl array[][], int newsize) {
248         XSAttributeDecl newarray[][] = new XSAttributeDecl[newsize][];
249         System.arraycopy(array, 0, newarray, 0, array.length);
250         return newarray;
251     }
252 
253     private boolean ensureAttributeUseCapacity(int chunk) {
254         if (chunk >= fAttributeUse.length) {
255             fAttributeUse = resize(fAttributeUse, fAttributeUse.length * 2);
256         } else if (fAttributeUse[chunk] != null) {
257             return false;
258         }
259 
260         fAttributeUse[chunk] = new XSAttributeUseImpl[CHUNK_SIZE];
261         return true;
262     }
263 
264     private static XSAttributeUseImpl[][] resize(XSAttributeUseImpl array[][], int newsize) {
265         XSAttributeUseImpl newarray[][] = new XSAttributeUseImpl[newsize][];
266         System.arraycopy(array, 0, newarray, 0, array.length);
267         return newarray;
268     }
269 
270     private boolean ensureSTDeclCapacity(int chunk) {
271         if (chunk >= fSTDecl.length) {
272             fSTDecl = resize(fSTDecl, fSTDecl.length * 2);
273         } else if (fSTDecl[chunk] != null) {
274             return false;
275         }
276 
277         fSTDecl[chunk] = new XSSimpleTypeDecl[CHUNK_SIZE];
278         return true;
279     }
280 
281     private static XSSimpleTypeDecl[][] resize(XSSimpleTypeDecl array[][], int newsize) {
282         XSSimpleTypeDecl newarray[][] = new XSSimpleTypeDecl[newsize][];
283         System.arraycopy(array, 0, newarray, 0, array.length);
284         return newarray;
285     }
286 
287     private boolean ensureCTDeclCapacity(int chunk) {
288 
289         if (chunk >= fCTDecl.length) {
290             fCTDecl = resize(fCTDecl, fCTDecl.length * 2);
291         } else if (fCTDecl[chunk] != null){
292             return false;
293         }
294 
295         fCTDecl[chunk] = new XSComplexTypeDecl[CHUNK_SIZE];
296         return true;
297     }
298 
299     private static XSComplexTypeDecl[][] resize(XSComplexTypeDecl array[][], int newsize) {
300         XSComplexTypeDecl newarray[][] = new XSComplexTypeDecl[newsize][];
301         System.arraycopy(array, 0, newarray, 0, array.length);
302         return newarray;
303     }
304 
305 
306 
307     public void reset(){
308         fElementDeclIndex = 0;
309         fParticleDeclIndex = 0;
310         fModelGroupIndex = 0;
311         fSTDeclIndex = 0;
312         fCTDeclIndex = 0;
313         fAttrDeclIndex = 0;
314         fAttributeUseIndex = 0;
315     }
316 
317 
318 }