KickJava   Java API By Example, From Geeks To Geeks.

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


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) 2002 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.dom;
59
60 import com.sun.org.apache.xerces.internal.dom.TextImpl;
61 import com.sun.org.apache.xerces.internal.dom.AttrNSImpl;
62
63
64 /**
65  * This class is pool that enables caching of DOM nodes, such as Element, Attr,
66  * Text, that are used to parse and later traverse XML Schemas.
67  * The pool is reset before a new set of schemas is traversed.
68  * Note: pool is not reset during traversals of imported/included
69  * schemas.
70  *
71  * @author Elena Litani, IBM
72  * @version $Id: DOMNodePool.java,v 1.3 2002/11/20 00:49:47 twl Exp $
73  */

74 public final class DOMNodePool {
75     /** Chunk shift (8). */
76     private static final int CHUNK_SHIFT = 8; // 2^8 = 256
77

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

87     /** Element nodes pool*/
88     private ElementNSImpl fElements[][] = new ElementNSImpl[INITIAL_CHUNK_COUNT][];
89     private int fElementIndex = 0;
90
91
92     /** Text nodes pool*/
93     private TextImpl fTextNode[][] = new TextImpl[INITIAL_CHUNK_COUNT][];
94     private int fTextNodeIndex = 0;
95
96
97     /** Attribute nodes pool*/
98     private AttrNSImpl fAttrNode[][] = new AttrNSImpl[INITIAL_CHUNK_COUNT][];
99     private int fAttrNodeIndex = 0;
100
101     
102
103     /**
104      * This method creates a new element node or provides a
105      * free element node if such exists in the pool.
106      *
107      * @return usable element node
108      */

109     public final ElementNSImpl getElementNode(){
110         int chunk = fElementIndex >> CHUNK_SHIFT;
111         int index = fElementIndex & CHUNK_MASK;
112         ensureElementsCapacity(chunk);
113         if (fElements[chunk][index] == null) {
114             fElements[chunk][index] = new ElementNSImpl();
115         }
116         fElementIndex++;
117         return fElements[chunk][index];
118     }
119
120     private void ensureElementsCapacity(int chunk) {
121         if (fElements.length <= chunk) {
122             fElements = resize(fElements, fElements.length * 2);
123         }
124         else if (fElements[chunk] != null) {
125             return;
126         }
127
128         fElements[chunk] = new ElementNSImpl[CHUNK_SIZE];
129         return;
130     }
131
132     private static ElementNSImpl[][] resize(ElementNSImpl array[][], int newsize) {
133         ElementNSImpl newarray[][] = new ElementNSImpl[newsize][];
134         System.arraycopy(array, 0, newarray, 0, array.length);
135         return newarray;
136     }
137
138     /**
139      * This methods creates text node or provides a free
140      * text node if such exists in the pool.
141      *
142      * @return a usable TextNode
143      */

144     public final TextImpl getTextNode(){
145         int chunk = fTextNodeIndex >> CHUNK_SHIFT;
146         int index = fTextNodeIndex & CHUNK_MASK;
147         ensureTextCapacity(chunk);
148         if (fTextNode[chunk][index] == null) {
149             fTextNode[chunk][index] = new TextImpl();
150         }
151         fTextNodeIndex++;
152         return fTextNode[chunk][index];
153     }
154
155     private void ensureTextCapacity(int chunk) {
156         if (fTextNode.length <= chunk) {
157             fTextNode = resize(fTextNode, fTextNode.length * 2);
158         }
159         else if (fTextNode[chunk] != null) {
160             return;
161         }
162
163         fTextNode[chunk] = new TextImpl[CHUNK_SIZE];
164         return;
165     }
166
167     private static TextImpl[][] resize(TextImpl array[][], int newsize) {
168         TextImpl newarray[][] = new TextImpl[newsize][];
169         System.arraycopy(array, 0, newarray, 0, array.length);
170         return newarray;
171     }
172
173     /**
174      * This methods creates attribute node or provides a free
175      * attribute node if such exists in the pool.
176      *
177      * @return a usable attribute node
178      */

179     public final AttrNSImpl getAttrNode(){
180         int chunk = fAttrNodeIndex >> CHUNK_SHIFT;
181         int index = fAttrNodeIndex & CHUNK_MASK;
182         ensureAttrsCapacity(chunk);
183         if (fAttrNode[chunk][index] == null) {
184             fAttrNode[chunk][index] = new AttrNSImpl();
185         }
186         fAttrNodeIndex++;
187         return fAttrNode[chunk][index];
188     }
189
190     private void ensureAttrsCapacity(int chunk) {
191         if (fAttrNode.length <= chunk) {
192             fAttrNode = resize(fAttrNode, fAttrNode.length * 2);
193         }
194         else if (fAttrNode[chunk] != null) {
195             return;
196         }
197
198         fAttrNode[chunk] = new AttrNSImpl[CHUNK_SIZE];
199         return;
200     }
201
202     private static AttrNSImpl[][] resize(AttrNSImpl array[][], int newsize) {
203         AttrNSImpl newarray[][] = new AttrNSImpl[newsize][];
204         System.arraycopy(array, 0, newarray, 0, array.length);
205         return newarray;
206     }
207
208
209     /**
210      * Reset the pool. The nodes in the pool become 'free' nodes.
211      */

212     public void reset(){
213         fElementIndex = 0;
214         fTextNodeIndex = 0;
215         fAttrNodeIndex = 0;
216     }
217
218
219 }
220
Popular Tags