KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > dom > util > DocumentDescriptor


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

18 package org.apache.batik.dom.util;
19
20 import org.w3c.dom.Element JavaDoc;
21
22 import org.apache.batik.util.CleanerThread;
23
24 /**
25  * This class contains informations about a document.
26  *
27  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
28  * @version $Id: DocumentDescriptor.java,v 1.6 2005/02/22 09:13:02 cam Exp $
29  */

30 public class DocumentDescriptor {
31         
32     /**
33      * The table initial capacity
34      */

35     protected final static int INITIAL_CAPACITY = 101;
36
37     /**
38      * The underlying array
39      */

40     protected Entry[] table;
41         
42     /**
43      * The number of entries
44      */

45     protected int count;
46         
47     /**
48      * Creates a new table.
49      */

50     public DocumentDescriptor() {
51     table = new Entry[INITIAL_CAPACITY];
52     }
53
54     /**
55      * Returns the number of elements in the document.
56      */

57     public int getNumberOfElements() {
58     synchronized (this) {
59             return count;
60         }
61     }
62     
63     /**
64      * Returns the location in the source file of the end element.
65      * @return zero if the information is unknown.
66      */

67     public int getLocationLine(Element JavaDoc elt) {
68         synchronized (this) {
69             int hash = elt.hashCode() & 0x7FFFFFFF;
70             int index = hash % table.length;
71     
72             for (Entry e = table[index]; e != null; e = e.next) {
73                 if (e.hash != hash)
74                     continue;
75                 Object JavaDoc o = e.get();
76                 if (o == elt)
77                     return e.locationLine;
78             }
79         }
80         return 0;
81     }
82     
83     /**
84      * Sets the location in the source file of the end element.
85      */

86     public void setLocationLine(Element JavaDoc elt, int line) {
87         synchronized (this) {
88             int hash = elt.hashCode() & 0x7FFFFFFF;
89             int index = hash % table.length;
90     
91             for (Entry e = table[index]; e != null; e = e.next) {
92                 if (e.hash != hash)
93                     continue;
94                 Object JavaDoc o = e.get();
95                 if (o == elt)
96                     e.locationLine = line;
97             }
98     
99             // The key is not in the hash table
100
int len = table.length;
101             if (count++ >= (len * 3) >>> 2) {
102                 rehash();
103                 index = hash % table.length;
104             }
105     
106             Entry e = new Entry(hash, elt, line, table[index]);
107             table[index] = e;
108         }
109     }
110
111     /**
112      * Rehash the table
113      */

114     protected void rehash () {
115     Entry[] oldTable = table;
116     
117     table = new Entry[oldTable.length * 2 + 1];
118     
119     for (int i = oldTable.length-1; i >= 0; i--) {
120         for (Entry old = oldTable[i]; old != null;) {
121         Entry e = old;
122         old = old.next;
123         
124         int index = e.hash % table.length;
125         e.next = table[index];
126         table[index] = e;
127         }
128     }
129     }
130
131     protected void removeEntry(Entry e) {
132         synchronized (this) {
133             int hash = e.hash;
134             int index = hash % table.length;
135             Entry curr = table[index];
136             Entry prev = null;
137             while (curr != e) {
138                 prev = curr;
139                 curr = curr.next;
140             }
141             if (curr == null) return; // already remove???
142

143             if (prev == null)
144                 // First entry.
145
table[index] = curr.next;
146             else
147                 prev.next = curr.next;
148             count--;
149         }
150     }
151
152     /**
153      * To manage collisions
154      */

155     protected class Entry extends CleanerThread.WeakReferenceCleared {
156     /**
157      * The hash code
158      */

159     public int hash;
160     
161     /**
162      * The line number.
163      */

164     public int locationLine;
165     
166     /**
167      * The next entry
168      */

169     public Entry next;
170     
171     /**
172      * Creates a new entry
173      */

174     public Entry(int hash, Element JavaDoc element, int locationLine, Entry next) {
175             super(element);
176         this.hash = hash;
177         this.locationLine = locationLine;
178         this.next = next;
179     }
180
181         public void cleared() {
182             removeEntry(this);
183         }
184     }
185 }
186
Popular Tags