KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > impl > xs > XSDeclarationPool


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
58 package com.sun.org.apache.xerces.internal.impl.xs;
59
60 import com.sun.org.apache.xerces.internal.impl.dv.xs.XSSimpleTypeDecl;
61
62 /**
63  * This class is pool that enables caching of XML Schema declaration objects.
64  * Before a compiled grammar object is garbage collected,
65  * the implementation will add all XML Schema component
66  * declarations to the pool.
67  * Note: The cashing mechanism is not implemented yet.
68  *
69  * @author Elena Litani, IBM
70  * @version $Id: XSDeclarationPool.java,v 1.7 2002/11/09 22:18:06 sandygao Exp $
71  */

72 public final class XSDeclarationPool {
73     /** Chunk shift (8). */
74     private static final int CHUNK_SHIFT = 8; // 2^8 = 256
75

76     /** Chunk size (1 << CHUNK_SHIFT). */
77     private static final int CHUNK_SIZE = 1 << CHUNK_SHIFT;
78
79     /** Chunk mask (CHUNK_SIZE - 1). */
80     private static final int CHUNK_MASK = CHUNK_SIZE - 1;
81
82     /** Initial chunk count (). */
83     private static final int INITIAL_CHUNK_COUNT = (1 << (10 - CHUNK_SHIFT)); // 2^10 = 1k
84

85     /** Element declaration pool*/
86     private XSElementDecl fElementDecl[][] = new XSElementDecl[INITIAL_CHUNK_COUNT][];
87     private int fElementDeclIndex = 0;
88
89     /** Particle declaration pool */
90     private XSParticleDecl fParticleDecl[][] = new XSParticleDecl[INITIAL_CHUNK_COUNT][];
91     private int fParticleDeclIndex = 0;
92
93     /** Particle declaration pool */
94     private XSModelGroupImpl fModelGroup[][] = new XSModelGroupImpl[INITIAL_CHUNK_COUNT][];
95     private int fModelGroupIndex = 0;
96
97     /** Attribute declaration pool */
98     private XSAttributeDecl fAttrDecl[][] = new XSAttributeDecl[INITIAL_CHUNK_COUNT][];
99     private int fAttrDeclIndex = 0;
100
101     /** ComplexType declaration pool */
102     private XSComplexTypeDecl fCTDecl[][] = new XSComplexTypeDecl[INITIAL_CHUNK_COUNT][];
103     private int fCTDeclIndex = 0;
104
105     /** SimpleType declaration pool */
106     private XSSimpleTypeDecl fSTDecl[][] = new XSSimpleTypeDecl[INITIAL_CHUNK_COUNT][];
107     private int fSTDeclIndex = 0;
108
109     /** AttributeUse declaration pool */
110     private XSAttributeUseImpl fAttributeUse[][] = new XSAttributeUseImpl[INITIAL_CHUNK_COUNT][];
111     private int fAttributeUseIndex = 0;
112
113     public final XSElementDecl getElementDecl(){
114         int chunk = fElementDeclIndex >> CHUNK_SHIFT;
115         int index = fElementDeclIndex & CHUNK_MASK;
116         ensureElementDeclCapacity(chunk);
117         if (fElementDecl[chunk][index] == null) {
118             fElementDecl[chunk][index] = new XSElementDecl();
119         } else {
120             fElementDecl[chunk][index].reset();
121         }
122         fElementDeclIndex++;
123         return fElementDecl[chunk][index];
124     }
125
126     public final XSAttributeDecl getAttributeDecl(){
127         int chunk = fAttrDeclIndex >> CHUNK_SHIFT;
128         int index = fAttrDeclIndex & CHUNK_MASK;
129         ensureAttrDeclCapacity(chunk);
130         if (fAttrDecl[chunk][index] == null) {
131             fAttrDecl[chunk][index] = new XSAttributeDecl();
132         } else {
133             fAttrDecl[chunk][index].reset();
134         }
135         fAttrDeclIndex++;
136         return fAttrDecl[chunk][index];
137
138     }
139
140     public final XSAttributeUseImpl getAttributeUse(){
141         int chunk = fAttributeUseIndex >> CHUNK_SHIFT;
142         int index = fAttributeUseIndex & CHUNK_MASK;
143         ensureAttributeUseCapacity(chunk);
144         if (fAttributeUse[chunk][index] == null) {
145             fAttributeUse[chunk][index] = new XSAttributeUseImpl();
146         } else {
147             fAttributeUse[chunk][index].reset();
148         }
149         fAttributeUseIndex++;
150         return fAttributeUse[chunk][index];
151
152     }
153     
154     public final XSComplexTypeDecl getComplexTypeDecl(){
155         int chunk = fCTDeclIndex >> CHUNK_SHIFT;
156         int index = fCTDeclIndex & CHUNK_MASK;
157         ensureCTDeclCapacity(chunk);
158         if (fCTDecl[chunk][index] == null) {
159
160             fCTDecl[chunk][index] = new XSComplexTypeDecl();
161         } else {
162             fCTDecl[chunk][index].reset();
163         }
164         fCTDeclIndex++;
165         return fCTDecl[chunk][index];
166     }
167
168     public final XSSimpleTypeDecl getSimpleTypeDecl(){
169         int chunk = fSTDeclIndex >> CHUNK_SHIFT;
170         int index = fSTDeclIndex & CHUNK_MASK;
171         ensureSTDeclCapacity(chunk);
172         if (fSTDecl[chunk][index] == null) {
173             fSTDecl[chunk][index] = new XSSimpleTypeDecl();
174         } else {
175             fSTDecl[chunk][index].reset();
176         }
177         fSTDeclIndex++;
178         return fSTDecl[chunk][index];
179
180     }
181
182     public final XSParticleDecl getParticleDecl(){
183         int chunk = fParticleDeclIndex >> CHUNK_SHIFT;
184         int index = fParticleDeclIndex & CHUNK_MASK;
185         ensureParticleDeclCapacity(chunk);
186         if (fParticleDecl[chunk][index] == null) {
187             fParticleDecl[chunk][index] = new XSParticleDecl();
188         } else {
189             fParticleDecl[chunk][index].reset();
190         }
191         fParticleDeclIndex++;
192         return fParticleDecl[chunk][index];
193     }
194
195     public final XSModelGroupImpl getModelGroup(){
196         int chunk = fModelGroupIndex >> CHUNK_SHIFT;
197         int index = fModelGroupIndex & CHUNK_MASK;
198         ensureModelGroupCapacity(chunk);
199         if (fModelGroup[chunk][index] == null) {
200             fModelGroup[chunk][index] = new XSModelGroupImpl();
201         } else {
202             fModelGroup[chunk][index].reset();
203         }
204         fModelGroupIndex++;
205         return fModelGroup[chunk][index];
206     }
207
208     // REVISIT: do we need decl pool for group declarations, attribute group,
209
// notations?
210
// it seems like each schema would use a small number of those
211
// components, so it probably is not worth keeping those components
212
// in the pool.
213

214     private boolean ensureElementDeclCapacity(int chunk) {
215         if (chunk >= fElementDecl.length) {
216             fElementDecl = resize(fElementDecl, fElementDecl.length * 2);
217         } else if (fElementDecl[chunk] != null) {
218             return false;
219         }
220
221         fElementDecl[chunk] = new XSElementDecl[CHUNK_SIZE];
222         return true;
223     }
224
225     private static XSElementDecl[][] resize(XSElementDecl array[][], int newsize) {
226         XSElementDecl newarray[][] = new XSElementDecl[newsize][];
227         System.arraycopy(array, 0, newarray, 0, array.length);
228         return newarray;
229     }
230
231     private boolean ensureParticleDeclCapacity(int chunk) {
232         if (chunk >= fParticleDecl.length) {
233             fParticleDecl = resize(fParticleDecl, fParticleDecl.length * 2);
234         } else if (fParticleDecl[chunk] != null) {
235             return false;
236         }
237
238         fParticleDecl[chunk] = new XSParticleDecl[CHUNK_SIZE];
239         return true;
240     }
241
242     private boolean ensureModelGroupCapacity(int chunk) {
243         if (chunk >= fModelGroup.length) {
244             fModelGroup = resize(fModelGroup, fModelGroup.length * 2);
245         } else if (fModelGroup[chunk] != null) {
246             return false;
247         }
248
249         fModelGroup[chunk] = new XSModelGroupImpl[CHUNK_SIZE];
250         return true;
251     }
252
253     private static XSParticleDecl[][] resize(XSParticleDecl array[][], int newsize) {
254         XSParticleDecl newarray[][] = new XSParticleDecl[newsize][];
255         System.arraycopy(array, 0, newarray, 0, array.length);
256         return newarray;
257     }
258
259     private static XSModelGroupImpl[][] resize(XSModelGroupImpl array[][], int newsize) {
260         XSModelGroupImpl newarray[][] = new XSModelGroupImpl[newsize][];
261         System.arraycopy(array, 0, newarray, 0, array.length);
262         return newarray;
263     }
264
265     private boolean ensureAttrDeclCapacity(int chunk) {
266         if (chunk >= fAttrDecl.length) {
267             fAttrDecl = resize(fAttrDecl, fAttrDecl.length * 2);
268         } else if (fAttrDecl[chunk] != null) {
269             return false;
270         }
271
272         fAttrDecl[chunk] = new XSAttributeDecl[CHUNK_SIZE];
273         return true;
274     }
275
276     private static XSAttributeDecl[][] resize(XSAttributeDecl array[][], int newsize) {
277         XSAttributeDecl newarray[][] = new XSAttributeDecl[newsize][];
278         System.arraycopy(array, 0, newarray, 0, array.length);
279         return newarray;
280     }
281
282     private boolean ensureAttributeUseCapacity(int chunk) {
283         if (chunk >= fAttributeUse.length) {
284             fAttributeUse = resize(fAttributeUse, fAttributeUse.length * 2);
285         } else if (fAttributeUse[chunk] != null) {
286             return false;
287         }
288
289         fAttributeUse[chunk] = new XSAttributeUseImpl[CHUNK_SIZE];
290         return true;
291     }
292
293     private static XSAttributeUseImpl[][] resize(XSAttributeUseImpl array[][], int newsize) {
294         XSAttributeUseImpl newarray[][] = new XSAttributeUseImpl[newsize][];
295         System.arraycopy(array, 0, newarray, 0, array.length);
296         return newarray;
297     }
298
299     private boolean ensureSTDeclCapacity(int chunk) {
300         if (chunk >= fSTDecl.length) {
301             fSTDecl = resize(fSTDecl, fSTDecl.length * 2);
302         } else if (fSTDecl[chunk] != null) {
303             return false;
304         }
305
306         fSTDecl[chunk] = new XSSimpleTypeDecl[CHUNK_SIZE];
307         return true;
308     }
309
310     private static XSSimpleTypeDecl[][] resize(XSSimpleTypeDecl array[][], int newsize) {
311         XSSimpleTypeDecl newarray[][] = new XSSimpleTypeDecl[newsize][];
312         System.arraycopy(array, 0, newarray, 0, array.length);
313         return newarray;
314     }
315
316     private boolean ensureCTDeclCapacity(int chunk) {
317
318         if (chunk >= fCTDecl.length) {
319             fCTDecl = resize(fCTDecl, fCTDecl.length * 2);
320         } else if (fCTDecl[chunk] != null){
321             return false;
322         }
323
324         fCTDecl[chunk] = new XSComplexTypeDecl[CHUNK_SIZE];
325         return true;
326     }
327
328     private static XSComplexTypeDecl[][] resize(XSComplexTypeDecl array[][], int newsize) {
329         XSComplexTypeDecl newarray[][] = new XSComplexTypeDecl[newsize][];
330         System.arraycopy(array, 0, newarray, 0, array.length);
331         return newarray;
332     }
333
334
335
336     public void reset(){
337         fElementDeclIndex = 0;
338         fParticleDeclIndex = 0;
339         fModelGroupIndex = 0;
340         fSTDeclIndex = 0;
341         fCTDeclIndex = 0;
342         fAttrDeclIndex = 0;
343         fAttributeUseIndex = 0;
344     }
345
346
347 }
348
Popular Tags