KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > xs > XSDeclarationPool


1 /*
2  * Copyright 2001, 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.impl.xs;
18
19 import org.apache.xerces.impl.dv.xs.XSSimpleTypeDecl;
20
21 /**
22  * This class is pool that enables caching of XML Schema declaration objects.
23  * Before a compiled grammar object is garbage collected,
24  * the implementation will add all XML Schema component
25  * declarations to the pool.
26  * Note: The cashing mechanism is not implemented yet.
27  *
28  * @xerces.internal
29  *
30  * @author Elena Litani, IBM
31  * @version $Id: XSDeclarationPool.java,v 1.9 2004/10/06 15:14:55 mrglavas Exp $
32  */

33 public final class XSDeclarationPool {
34     /** Chunk shift (8). */
35     private static final int CHUNK_SHIFT = 8; // 2^8 = 256
36

37     /** Chunk size (1 << CHUNK_SHIFT). */
38     private static final int CHUNK_SIZE = 1 << CHUNK_SHIFT;
39
40     /** Chunk mask (CHUNK_SIZE - 1). */
41     private static final int CHUNK_MASK = CHUNK_SIZE - 1;
42
43     /** Initial chunk count (). */
44     private static final int INITIAL_CHUNK_COUNT = (1 << (10 - CHUNK_SHIFT)); // 2^10 = 1k
45

46     /** Element declaration pool*/
47     private XSElementDecl fElementDecl[][] = new XSElementDecl[INITIAL_CHUNK_COUNT][];
48     private int fElementDeclIndex = 0;
49
50     /** Particle declaration pool */
51     private XSParticleDecl fParticleDecl[][] = new XSParticleDecl[INITIAL_CHUNK_COUNT][];
52     private int fParticleDeclIndex = 0;
53
54     /** Particle declaration pool */
55     private XSModelGroupImpl fModelGroup[][] = new XSModelGroupImpl[INITIAL_CHUNK_COUNT][];
56     private int fModelGroupIndex = 0;
57
58     /** Attribute declaration pool */
59     private XSAttributeDecl fAttrDecl[][] = new XSAttributeDecl[INITIAL_CHUNK_COUNT][];
60     private int fAttrDeclIndex = 0;
61
62     /** ComplexType declaration pool */
63     private XSComplexTypeDecl fCTDecl[][] = new XSComplexTypeDecl[INITIAL_CHUNK_COUNT][];
64     private int fCTDeclIndex = 0;
65
66     /** SimpleType declaration pool */
67     private XSSimpleTypeDecl fSTDecl[][] = new XSSimpleTypeDecl[INITIAL_CHUNK_COUNT][];
68     private int fSTDeclIndex = 0;
69
70     /** AttributeUse declaration pool */
71     private XSAttributeUseImpl fAttributeUse[][] = new XSAttributeUseImpl[INITIAL_CHUNK_COUNT][];
72     private int fAttributeUseIndex = 0;
73
74     public final XSElementDecl getElementDecl(){
75         int chunk = fElementDeclIndex >> CHUNK_SHIFT;
76         int index = fElementDeclIndex & CHUNK_MASK;
77         ensureElementDeclCapacity(chunk);
78         if (fElementDecl[chunk][index] == null) {
79             fElementDecl[chunk][index] = new XSElementDecl();
80         } else {
81             fElementDecl[chunk][index].reset();
82         }
83         fElementDeclIndex++;
84         return fElementDecl[chunk][index];
85     }
86
87     public final XSAttributeDecl getAttributeDecl(){
88         int chunk = fAttrDeclIndex >> CHUNK_SHIFT;
89         int index = fAttrDeclIndex & CHUNK_MASK;
90         ensureAttrDeclCapacity(chunk);
91         if (fAttrDecl[chunk][index] == null) {
92             fAttrDecl[chunk][index] = new XSAttributeDecl();
93         } else {
94             fAttrDecl[chunk][index].reset();
95         }
96         fAttrDeclIndex++;
97         return fAttrDecl[chunk][index];
98
99     }
100
101     public final XSAttributeUseImpl getAttributeUse(){
102         int chunk = fAttributeUseIndex >> CHUNK_SHIFT;
103         int index = fAttributeUseIndex & CHUNK_MASK;
104         ensureAttributeUseCapacity(chunk);
105         if (fAttributeUse[chunk][index] == null) {
106             fAttributeUse[chunk][index] = new XSAttributeUseImpl();
107         } else {
108             fAttributeUse[chunk][index].reset();
109         }
110         fAttributeUseIndex++;
111         return fAttributeUse[chunk][index];
112
113     }
114     
115     public final XSComplexTypeDecl getComplexTypeDecl(){
116         int chunk = fCTDeclIndex >> CHUNK_SHIFT;
117         int index = fCTDeclIndex & CHUNK_MASK;
118         ensureCTDeclCapacity(chunk);
119         if (fCTDecl[chunk][index] == null) {
120
121             fCTDecl[chunk][index] = new XSComplexTypeDecl();
122         } else {
123             fCTDecl[chunk][index].reset();
124         }
125         fCTDeclIndex++;
126         return fCTDecl[chunk][index];
127     }
128
129     public final XSSimpleTypeDecl getSimpleTypeDecl(){
130         int chunk = fSTDeclIndex >> CHUNK_SHIFT;
131         int index = fSTDeclIndex & CHUNK_MASK;
132         ensureSTDeclCapacity(chunk);
133         if (fSTDecl[chunk][index] == null) {
134             fSTDecl[chunk][index] = new XSSimpleTypeDecl();
135         } else {
136             fSTDecl[chunk][index].reset();
137         }
138         fSTDeclIndex++;
139         return fSTDecl[chunk][index];
140
141     }
142
143     public final XSParticleDecl getParticleDecl(){
144         int chunk = fParticleDeclIndex >> CHUNK_SHIFT;
145         int index = fParticleDeclIndex & CHUNK_MASK;
146         ensureParticleDeclCapacity(chunk);
147         if (fParticleDecl[chunk][index] == null) {
148             fParticleDecl[chunk][index] = new XSParticleDecl();
149         } else {
150             fParticleDecl[chunk][index].reset();
151         }
152         fParticleDeclIndex++;
153         return fParticleDecl[chunk][index];
154     }
155
156     public final XSModelGroupImpl getModelGroup(){
157         int chunk = fModelGroupIndex >> CHUNK_SHIFT;
158         int index = fModelGroupIndex & CHUNK_MASK;
159         ensureModelGroupCapacity(chunk);
160         if (fModelGroup[chunk][index] == null) {
161             fModelGroup[chunk][index] = new XSModelGroupImpl();
162         } else {
163             fModelGroup[chunk][index].reset();
164         }
165         fModelGroupIndex++;
166         return fModelGroup[chunk][index];
167     }
168
169     // REVISIT: do we need decl pool for group declarations, attribute group,
170
// notations?
171
// it seems like each schema would use a small number of those
172
// components, so it probably is not worth keeping those components
173
// in the pool.
174

175     private boolean ensureElementDeclCapacity(int chunk) {
176         if (chunk >= fElementDecl.length) {
177             fElementDecl = resize(fElementDecl, fElementDecl.length * 2);
178         } else if (fElementDecl[chunk] != null) {
179             return false;
180         }
181
182         fElementDecl[chunk] = new XSElementDecl[CHUNK_SIZE];
183         return true;
184     }
185
186     private static XSElementDecl[][] resize(XSElementDecl array[][], int newsize) {
187         XSElementDecl newarray[][] = new XSElementDecl[newsize][];
188         System.arraycopy(array, 0, newarray, 0, array.length);
189         return newarray;
190     }
191
192     private boolean ensureParticleDeclCapacity(int chunk) {
193         if (chunk >= fParticleDecl.length) {
194             fParticleDecl = resize(fParticleDecl, fParticleDecl.length * 2);
195         } else if (fParticleDecl[chunk] != null) {
196             return false;
197         }
198
199         fParticleDecl[chunk] = new XSParticleDecl[CHUNK_SIZE];
200         return true;
201     }
202
203     private boolean ensureModelGroupCapacity(int chunk) {
204         if (chunk >= fModelGroup.length) {
205             fModelGroup = resize(fModelGroup, fModelGroup.length * 2);
206         } else if (fModelGroup[chunk] != null) {
207             return false;
208         }
209
210         fModelGroup[chunk] = new XSModelGroupImpl[CHUNK_SIZE];
211         return true;
212     }
213
214     private static XSParticleDecl[][] resize(XSParticleDecl array[][], int newsize) {
215         XSParticleDecl newarray[][] = new XSParticleDecl[newsize][];
216         System.arraycopy(array, 0, newarray, 0, array.length);
217         return newarray;
218     }
219
220     private static XSModelGroupImpl[][] resize(XSModelGroupImpl array[][], int newsize) {
221         XSModelGroupImpl newarray[][] = new XSModelGroupImpl[newsize][];
222         System.arraycopy(array, 0, newarray, 0, array.length);
223         return newarray;
224     }
225
226     private boolean ensureAttrDeclCapacity(int chunk) {
227         if (chunk >= fAttrDecl.length) {
228             fAttrDecl = resize(fAttrDecl, fAttrDecl.length * 2);
229         } else if (fAttrDecl[chunk] != null) {
230             return false;
231         }
232
233         fAttrDecl[chunk] = new XSAttributeDecl[CHUNK_SIZE];
234         return true;
235     }
236
237     private static XSAttributeDecl[][] resize(XSAttributeDecl array[][], int newsize) {
238         XSAttributeDecl newarray[][] = new XSAttributeDecl[newsize][];
239         System.arraycopy(array, 0, newarray, 0, array.length);
240         return newarray;
241     }
242
243     private boolean ensureAttributeUseCapacity(int chunk) {
244         if (chunk >= fAttributeUse.length) {
245             fAttributeUse = resize(fAttributeUse, fAttributeUse.length * 2);
246         } else if (fAttributeUse[chunk] != null) {
247             return false;
248         }
249
250         fAttributeUse[chunk] = new XSAttributeUseImpl[CHUNK_SIZE];
251         return true;
252     }
253
254     private static XSAttributeUseImpl[][] resize(XSAttributeUseImpl array[][], int newsize) {
255         XSAttributeUseImpl newarray[][] = new XSAttributeUseImpl[newsize][];
256         System.arraycopy(array, 0, newarray, 0, array.length);
257         return newarray;
258     }
259
260     private boolean ensureSTDeclCapacity(int chunk) {
261         if (chunk >= fSTDecl.length) {
262             fSTDecl = resize(fSTDecl, fSTDecl.length * 2);
263         } else if (fSTDecl[chunk] != null) {
264             return false;
265         }
266
267         fSTDecl[chunk] = new XSSimpleTypeDecl[CHUNK_SIZE];
268         return true;
269     }
270
271     private static XSSimpleTypeDecl[][] resize(XSSimpleTypeDecl array[][], int newsize) {
272         XSSimpleTypeDecl newarray[][] = new XSSimpleTypeDecl[newsize][];
273         System.arraycopy(array, 0, newarray, 0, array.length);
274         return newarray;
275     }
276
277     private boolean ensureCTDeclCapacity(int chunk) {
278
279         if (chunk >= fCTDecl.length) {
280             fCTDecl = resize(fCTDecl, fCTDecl.length * 2);
281         } else if (fCTDecl[chunk] != null){
282             return false;
283         }
284
285         fCTDecl[chunk] = new XSComplexTypeDecl[CHUNK_SIZE];
286         return true;
287     }
288
289     private static XSComplexTypeDecl[][] resize(XSComplexTypeDecl array[][], int newsize) {
290         XSComplexTypeDecl newarray[][] = new XSComplexTypeDecl[newsize][];
291         System.arraycopy(array, 0, newarray, 0, array.length);
292         return newarray;
293     }
294
295
296
297     public void reset(){
298         fElementDeclIndex = 0;
299         fParticleDeclIndex = 0;
300         fModelGroupIndex = 0;
301         fSTDeclIndex = 0;
302         fCTDeclIndex = 0;
303         fAttrDeclIndex = 0;
304         fAttributeUseIndex = 0;
305     }
306
307
308 }
309
Popular Tags