001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.impl.xs;
019:
020: import org.apache.xerces.impl.dv.xs.XSSimpleTypeDecl;
021:
022: /**
023: * This class is pool that enables caching of XML Schema declaration objects.
024: * Before a compiled grammar object is garbage collected,
025: * the implementation will add all XML Schema component
026: * declarations to the pool.
027: * Note: The cashing mechanism is not implemented yet.
028: *
029: * @xerces.internal
030: *
031: * @author Elena Litani, IBM
032: * @version $Id: XSDeclarationPool.java 446734 2006-09-15 20:51:23Z mrglavas $
033: */
034: public final class XSDeclarationPool {
035: /** Chunk shift (8). */
036: private static final int CHUNK_SHIFT = 8; // 2^8 = 256
037:
038: /** Chunk size (1 << CHUNK_SHIFT). */
039: private static final int CHUNK_SIZE = 1 << CHUNK_SHIFT;
040:
041: /** Chunk mask (CHUNK_SIZE - 1). */
042: private static final int CHUNK_MASK = CHUNK_SIZE - 1;
043:
044: /** Initial chunk count (). */
045: private static final int INITIAL_CHUNK_COUNT = (1 << (10 - CHUNK_SHIFT)); // 2^10 = 1k
046:
047: /** Element declaration pool*/
048: private XSElementDecl fElementDecl[][] = new XSElementDecl[INITIAL_CHUNK_COUNT][];
049: private int fElementDeclIndex = 0;
050:
051: /** Particle declaration pool */
052: private XSParticleDecl fParticleDecl[][] = new XSParticleDecl[INITIAL_CHUNK_COUNT][];
053: private int fParticleDeclIndex = 0;
054:
055: /** Particle declaration pool */
056: private XSModelGroupImpl fModelGroup[][] = new XSModelGroupImpl[INITIAL_CHUNK_COUNT][];
057: private int fModelGroupIndex = 0;
058:
059: /** Attribute declaration pool */
060: private XSAttributeDecl fAttrDecl[][] = new XSAttributeDecl[INITIAL_CHUNK_COUNT][];
061: private int fAttrDeclIndex = 0;
062:
063: /** ComplexType declaration pool */
064: private XSComplexTypeDecl fCTDecl[][] = new XSComplexTypeDecl[INITIAL_CHUNK_COUNT][];
065: private int fCTDeclIndex = 0;
066:
067: /** SimpleType declaration pool */
068: private XSSimpleTypeDecl fSTDecl[][] = new XSSimpleTypeDecl[INITIAL_CHUNK_COUNT][];
069: private int fSTDeclIndex = 0;
070:
071: /** AttributeUse declaration pool */
072: private XSAttributeUseImpl fAttributeUse[][] = new XSAttributeUseImpl[INITIAL_CHUNK_COUNT][];
073: private int fAttributeUseIndex = 0;
074:
075: public final XSElementDecl getElementDecl() {
076: int chunk = fElementDeclIndex >> CHUNK_SHIFT;
077: int index = fElementDeclIndex & CHUNK_MASK;
078: ensureElementDeclCapacity(chunk);
079: if (fElementDecl[chunk][index] == null) {
080: fElementDecl[chunk][index] = new XSElementDecl();
081: } else {
082: fElementDecl[chunk][index].reset();
083: }
084: fElementDeclIndex++;
085: return fElementDecl[chunk][index];
086: }
087:
088: public final XSAttributeDecl getAttributeDecl() {
089: int chunk = fAttrDeclIndex >> CHUNK_SHIFT;
090: int index = fAttrDeclIndex & CHUNK_MASK;
091: ensureAttrDeclCapacity(chunk);
092: if (fAttrDecl[chunk][index] == null) {
093: fAttrDecl[chunk][index] = new XSAttributeDecl();
094: } else {
095: fAttrDecl[chunk][index].reset();
096: }
097: fAttrDeclIndex++;
098: return fAttrDecl[chunk][index];
099:
100: }
101:
102: public final XSAttributeUseImpl getAttributeUse() {
103: int chunk = fAttributeUseIndex >> CHUNK_SHIFT;
104: int index = fAttributeUseIndex & CHUNK_MASK;
105: ensureAttributeUseCapacity(chunk);
106: if (fAttributeUse[chunk][index] == null) {
107: fAttributeUse[chunk][index] = new XSAttributeUseImpl();
108: } else {
109: fAttributeUse[chunk][index].reset();
110: }
111: fAttributeUseIndex++;
112: return fAttributeUse[chunk][index];
113:
114: }
115:
116: public final XSComplexTypeDecl getComplexTypeDecl() {
117: int chunk = fCTDeclIndex >> CHUNK_SHIFT;
118: int index = fCTDeclIndex & CHUNK_MASK;
119: ensureCTDeclCapacity(chunk);
120: if (fCTDecl[chunk][index] == null) {
121:
122: fCTDecl[chunk][index] = new XSComplexTypeDecl();
123: } else {
124: fCTDecl[chunk][index].reset();
125: }
126: fCTDeclIndex++;
127: return fCTDecl[chunk][index];
128: }
129:
130: public final XSSimpleTypeDecl getSimpleTypeDecl() {
131: int chunk = fSTDeclIndex >> CHUNK_SHIFT;
132: int index = fSTDeclIndex & CHUNK_MASK;
133: ensureSTDeclCapacity(chunk);
134: if (fSTDecl[chunk][index] == null) {
135: fSTDecl[chunk][index] = new XSSimpleTypeDecl();
136: } else {
137: fSTDecl[chunk][index].reset();
138: }
139: fSTDeclIndex++;
140: return fSTDecl[chunk][index];
141:
142: }
143:
144: public final XSParticleDecl getParticleDecl() {
145: int chunk = fParticleDeclIndex >> CHUNK_SHIFT;
146: int index = fParticleDeclIndex & CHUNK_MASK;
147: ensureParticleDeclCapacity(chunk);
148: if (fParticleDecl[chunk][index] == null) {
149: fParticleDecl[chunk][index] = new XSParticleDecl();
150: } else {
151: fParticleDecl[chunk][index].reset();
152: }
153: fParticleDeclIndex++;
154: return fParticleDecl[chunk][index];
155: }
156:
157: public final XSModelGroupImpl getModelGroup() {
158: int chunk = fModelGroupIndex >> CHUNK_SHIFT;
159: int index = fModelGroupIndex & CHUNK_MASK;
160: ensureModelGroupCapacity(chunk);
161: if (fModelGroup[chunk][index] == null) {
162: fModelGroup[chunk][index] = new XSModelGroupImpl();
163: } else {
164: fModelGroup[chunk][index].reset();
165: }
166: fModelGroupIndex++;
167: return fModelGroup[chunk][index];
168: }
169:
170: // REVISIT: do we need decl pool for group declarations, attribute group,
171: // notations?
172: // it seems like each schema would use a small number of those
173: // components, so it probably is not worth keeping those components
174: // in the pool.
175:
176: private boolean ensureElementDeclCapacity(int chunk) {
177: if (chunk >= fElementDecl.length) {
178: fElementDecl = resize(fElementDecl, fElementDecl.length * 2);
179: } else if (fElementDecl[chunk] != null) {
180: return false;
181: }
182:
183: fElementDecl[chunk] = new XSElementDecl[CHUNK_SIZE];
184: return true;
185: }
186:
187: private static XSElementDecl[][] resize(XSElementDecl array[][],
188: int newsize) {
189: XSElementDecl newarray[][] = new XSElementDecl[newsize][];
190: System.arraycopy(array, 0, newarray, 0, array.length);
191: return newarray;
192: }
193:
194: private boolean ensureParticleDeclCapacity(int chunk) {
195: if (chunk >= fParticleDecl.length) {
196: fParticleDecl = resize(fParticleDecl,
197: fParticleDecl.length * 2);
198: } else if (fParticleDecl[chunk] != null) {
199: return false;
200: }
201:
202: fParticleDecl[chunk] = new XSParticleDecl[CHUNK_SIZE];
203: return true;
204: }
205:
206: private boolean ensureModelGroupCapacity(int chunk) {
207: if (chunk >= fModelGroup.length) {
208: fModelGroup = resize(fModelGroup, fModelGroup.length * 2);
209: } else if (fModelGroup[chunk] != null) {
210: return false;
211: }
212:
213: fModelGroup[chunk] = new XSModelGroupImpl[CHUNK_SIZE];
214: return true;
215: }
216:
217: private static XSParticleDecl[][] resize(XSParticleDecl array[][],
218: int newsize) {
219: XSParticleDecl newarray[][] = new XSParticleDecl[newsize][];
220: System.arraycopy(array, 0, newarray, 0, array.length);
221: return newarray;
222: }
223:
224: private static XSModelGroupImpl[][] resize(
225: XSModelGroupImpl array[][], int newsize) {
226: XSModelGroupImpl newarray[][] = new XSModelGroupImpl[newsize][];
227: System.arraycopy(array, 0, newarray, 0, array.length);
228: return newarray;
229: }
230:
231: private boolean ensureAttrDeclCapacity(int chunk) {
232: if (chunk >= fAttrDecl.length) {
233: fAttrDecl = resize(fAttrDecl, fAttrDecl.length * 2);
234: } else if (fAttrDecl[chunk] != null) {
235: return false;
236: }
237:
238: fAttrDecl[chunk] = new XSAttributeDecl[CHUNK_SIZE];
239: return true;
240: }
241:
242: private static XSAttributeDecl[][] resize(
243: XSAttributeDecl array[][], int newsize) {
244: XSAttributeDecl newarray[][] = new XSAttributeDecl[newsize][];
245: System.arraycopy(array, 0, newarray, 0, array.length);
246: return newarray;
247: }
248:
249: private boolean ensureAttributeUseCapacity(int chunk) {
250: if (chunk >= fAttributeUse.length) {
251: fAttributeUse = resize(fAttributeUse,
252: fAttributeUse.length * 2);
253: } else if (fAttributeUse[chunk] != null) {
254: return false;
255: }
256:
257: fAttributeUse[chunk] = new XSAttributeUseImpl[CHUNK_SIZE];
258: return true;
259: }
260:
261: private static XSAttributeUseImpl[][] resize(
262: XSAttributeUseImpl array[][], int newsize) {
263: XSAttributeUseImpl newarray[][] = new XSAttributeUseImpl[newsize][];
264: System.arraycopy(array, 0, newarray, 0, array.length);
265: return newarray;
266: }
267:
268: private boolean ensureSTDeclCapacity(int chunk) {
269: if (chunk >= fSTDecl.length) {
270: fSTDecl = resize(fSTDecl, fSTDecl.length * 2);
271: } else if (fSTDecl[chunk] != null) {
272: return false;
273: }
274:
275: fSTDecl[chunk] = new XSSimpleTypeDecl[CHUNK_SIZE];
276: return true;
277: }
278:
279: private static XSSimpleTypeDecl[][] resize(
280: 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(
299: 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: public void reset() {
306: fElementDeclIndex = 0;
307: fParticleDeclIndex = 0;
308: fModelGroupIndex = 0;
309: fSTDeclIndex = 0;
310: fCTDeclIndex = 0;
311: fAttrDeclIndex = 0;
312: fAttributeUseIndex = 0;
313: }
314:
315: }
|