author | mkos |
Sun, 30 Dec 2012 00:00:00 +0100 | |
changeset 22678 | ac1ea46be942 |
parent 12009 | 4abb694f273a |
permissions | -rw-r--r-- |
12009 | 1 |
/* |
22678
ac1ea46be942
8029237: Update copyright year to match last edit in jaxws repository for 2012
mkos
parents:
12009
diff
changeset
|
2 |
* Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved. |
12009 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
* |
|
25 |
* THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. |
|
26 |
*/ |
|
27 |
||
28 |
package com.sun.xml.internal.fastinfoset.util; |
|
29 |
||
30 |
import com.sun.xml.internal.fastinfoset.EncodingConstants; |
|
31 |
import com.sun.xml.internal.fastinfoset.CommonResourceBundle; |
|
32 |
import java.util.Iterator; |
|
33 |
import java.util.NoSuchElementException; |
|
34 |
import com.sun.xml.internal.org.jvnet.fastinfoset.FastInfosetException; |
|
35 |
||
36 |
public class PrefixArray extends ValueArray { |
|
37 |
public static final int PREFIX_MAP_SIZE = 64; |
|
38 |
||
39 |
private int _initialCapacity; |
|
40 |
||
41 |
public String[] _array; |
|
42 |
||
43 |
private PrefixArray _readOnlyArray; |
|
44 |
||
45 |
private static class PrefixEntry { |
|
46 |
private PrefixEntry next; |
|
47 |
private int prefixId; |
|
48 |
} |
|
49 |
||
50 |
private PrefixEntry[] _prefixMap = new PrefixEntry[PREFIX_MAP_SIZE]; |
|
51 |
||
52 |
private PrefixEntry _prefixPool; |
|
53 |
||
54 |
private static class NamespaceEntry { |
|
55 |
private NamespaceEntry next; |
|
56 |
private int declarationId; |
|
57 |
private int namespaceIndex; |
|
58 |
||
59 |
private String prefix; |
|
60 |
private String namespaceName; |
|
61 |
private int prefixEntryIndex; |
|
62 |
} |
|
63 |
||
64 |
private NamespaceEntry _namespacePool; |
|
65 |
||
66 |
private NamespaceEntry[] _inScopeNamespaces; |
|
67 |
||
68 |
public int[] _currentInScope; |
|
69 |
||
70 |
public int _declarationId; |
|
71 |
||
72 |
public PrefixArray(int initialCapacity, int maximumCapacity) { |
|
73 |
_initialCapacity = initialCapacity; |
|
74 |
_maximumCapacity = maximumCapacity; |
|
75 |
||
76 |
_array = new String[initialCapacity]; |
|
77 |
// Sizes of _inScopeNamespaces and _currentInScope need to be two |
|
78 |
// greater than _array because 0 represents the empty string and |
|
79 |
// 1 represents the xml prefix |
|
80 |
_inScopeNamespaces = new NamespaceEntry[initialCapacity + 2]; |
|
81 |
_currentInScope = new int[initialCapacity + 2]; |
|
82 |
||
83 |
increaseNamespacePool(initialCapacity); |
|
84 |
increasePrefixPool(initialCapacity); |
|
85 |
||
86 |
initializeEntries(); |
|
87 |
} |
|
88 |
||
89 |
public PrefixArray() { |
|
90 |
this(DEFAULT_CAPACITY, MAXIMUM_CAPACITY); |
|
91 |
} |
|
92 |
||
93 |
private final void initializeEntries() { |
|
94 |
_inScopeNamespaces[0] = _namespacePool; |
|
95 |
_namespacePool = _namespacePool.next; |
|
96 |
_inScopeNamespaces[0].next = null; |
|
97 |
_inScopeNamespaces[0].prefix = ""; |
|
98 |
_inScopeNamespaces[0].namespaceName = ""; |
|
99 |
_inScopeNamespaces[0].namespaceIndex = _currentInScope[0] = 0; |
|
100 |
||
101 |
int index = KeyIntMap.indexFor(KeyIntMap.hashHash(_inScopeNamespaces[0].prefix.hashCode()), _prefixMap.length); |
|
102 |
_prefixMap[index] = _prefixPool; |
|
103 |
_prefixPool = _prefixPool.next; |
|
104 |
_prefixMap[index].next = null; |
|
105 |
_prefixMap[index].prefixId = 0; |
|
106 |
||
107 |
||
108 |
_inScopeNamespaces[1] = _namespacePool; |
|
109 |
_namespacePool = _namespacePool.next; |
|
110 |
_inScopeNamespaces[1].next = null; |
|
111 |
_inScopeNamespaces[1].prefix = EncodingConstants.XML_NAMESPACE_PREFIX; |
|
112 |
_inScopeNamespaces[1].namespaceName = EncodingConstants.XML_NAMESPACE_NAME; |
|
113 |
_inScopeNamespaces[1].namespaceIndex = _currentInScope[1] = 1; |
|
114 |
||
115 |
index = KeyIntMap.indexFor(KeyIntMap.hashHash(_inScopeNamespaces[1].prefix.hashCode()), _prefixMap.length); |
|
116 |
if (_prefixMap[index] == null) { |
|
117 |
_prefixMap[index] = _prefixPool; |
|
118 |
_prefixPool = _prefixPool.next; |
|
119 |
_prefixMap[index].next = null; |
|
120 |
} else { |
|
121 |
final PrefixEntry e = _prefixMap[index]; |
|
122 |
_prefixMap[index] = _prefixPool; |
|
123 |
_prefixPool = _prefixPool.next; |
|
124 |
_prefixMap[index].next = e; |
|
125 |
} |
|
126 |
_prefixMap[index].prefixId = 1; |
|
127 |
} |
|
128 |
||
129 |
private final void increaseNamespacePool(int capacity) { |
|
130 |
if (_namespacePool == null) { |
|
131 |
_namespacePool = new NamespaceEntry(); |
|
132 |
} |
|
133 |
||
134 |
for (int i = 0; i < capacity; i++) { |
|
135 |
NamespaceEntry ne = new NamespaceEntry(); |
|
136 |
ne.next = _namespacePool; |
|
137 |
_namespacePool = ne; |
|
138 |
} |
|
139 |
} |
|
140 |
||
141 |
private final void increasePrefixPool(int capacity) { |
|
142 |
if (_prefixPool == null) { |
|
143 |
_prefixPool = new PrefixEntry(); |
|
144 |
} |
|
145 |
||
146 |
for (int i = 0; i < capacity; i++) { |
|
147 |
PrefixEntry pe = new PrefixEntry(); |
|
148 |
pe.next = _prefixPool; |
|
149 |
_prefixPool = pe; |
|
150 |
} |
|
151 |
} |
|
152 |
||
153 |
public int countNamespacePool() { |
|
154 |
int i = 0; |
|
155 |
NamespaceEntry e = _namespacePool; |
|
156 |
while (e != null) { |
|
157 |
i++; |
|
158 |
e = e.next; |
|
159 |
} |
|
160 |
return i; |
|
161 |
} |
|
162 |
||
163 |
public int countPrefixPool() { |
|
164 |
int i = 0; |
|
165 |
PrefixEntry e = _prefixPool; |
|
166 |
while (e != null) { |
|
167 |
i++; |
|
168 |
e = e.next; |
|
169 |
} |
|
170 |
return i; |
|
171 |
} |
|
172 |
||
173 |
public final void clear() { |
|
174 |
for (int i = _readOnlyArraySize; i < _size; i++) { |
|
175 |
_array[i] = null; |
|
176 |
} |
|
177 |
_size = _readOnlyArraySize; |
|
178 |
} |
|
179 |
||
180 |
public final void clearCompletely() { |
|
181 |
_prefixPool = null; |
|
182 |
_namespacePool = null; |
|
183 |
||
184 |
for (int i = 0; i < _size + 2; i++) { |
|
185 |
_currentInScope[i] = 0; |
|
186 |
_inScopeNamespaces[i] = null; |
|
187 |
} |
|
188 |
||
189 |
for (int i = 0; i < _prefixMap.length; i++) { |
|
190 |
_prefixMap[i] = null; |
|
191 |
} |
|
192 |
||
193 |
increaseNamespacePool(_initialCapacity); |
|
194 |
increasePrefixPool(_initialCapacity); |
|
195 |
||
196 |
initializeEntries(); |
|
197 |
||
198 |
_declarationId = 0; |
|
199 |
||
200 |
clear(); |
|
201 |
} |
|
202 |
||
203 |
/** |
|
204 |
* Returns cloned version of internal String[]. |
|
205 |
* @return cloned version of internal String[]. |
|
206 |
*/ |
|
207 |
public final String[] getArray() { |
|
208 |
if (_array == null) return null; |
|
209 |
||
210 |
final String[] clonedArray = new String[_array.length]; |
|
211 |
System.arraycopy(_array, 0, clonedArray, 0, _array.length); |
|
212 |
return clonedArray; |
|
213 |
} |
|
214 |
||
215 |
public final void setReadOnlyArray(ValueArray readOnlyArray, boolean clear) { |
|
216 |
if (!(readOnlyArray instanceof PrefixArray)) { |
|
217 |
throw new IllegalArgumentException(CommonResourceBundle.getInstance(). |
|
218 |
getString("message.illegalClass", new Object[]{readOnlyArray})); |
|
219 |
} |
|
220 |
||
221 |
setReadOnlyArray((PrefixArray)readOnlyArray, clear); |
|
222 |
} |
|
223 |
||
224 |
public final void setReadOnlyArray(PrefixArray readOnlyArray, boolean clear) { |
|
225 |
if (readOnlyArray != null) { |
|
226 |
_readOnlyArray = readOnlyArray; |
|
227 |
_readOnlyArraySize = readOnlyArray.getSize(); |
|
228 |
||
229 |
clearCompletely(); |
|
230 |
||
231 |
// Resize according to size of read only arrays |
|
232 |
_inScopeNamespaces = new NamespaceEntry[_readOnlyArraySize + _inScopeNamespaces.length]; |
|
233 |
_currentInScope = new int[_readOnlyArraySize + _currentInScope.length]; |
|
234 |
// Intialize first two entries |
|
235 |
initializeEntries(); |
|
236 |
||
237 |
if (clear) { |
|
238 |
clear(); |
|
239 |
} |
|
240 |
||
241 |
_array = getCompleteArray(); |
|
242 |
_size = _readOnlyArraySize; |
|
243 |
} |
|
244 |
} |
|
245 |
||
246 |
public final String[] getCompleteArray() { |
|
247 |
if (_readOnlyArray == null) { |
|
248 |
// Return cloned version of internal _array |
|
249 |
return getArray(); |
|
250 |
// return _array; |
|
251 |
} else { |
|
252 |
final String[] ra = _readOnlyArray.getCompleteArray(); |
|
253 |
final String[] a = new String[_readOnlyArraySize + _array.length]; |
|
254 |
System.arraycopy(ra, 0, a, 0, _readOnlyArraySize); |
|
255 |
return a; |
|
256 |
} |
|
257 |
} |
|
258 |
||
259 |
public final String get(int i) { |
|
260 |
return _array[i]; |
|
261 |
} |
|
262 |
||
263 |
public final int add(String s) { |
|
264 |
if (_size == _array.length) { |
|
265 |
resize(); |
|
266 |
} |
|
267 |
||
268 |
_array[_size++] = s; |
|
269 |
return _size; |
|
270 |
} |
|
271 |
||
272 |
protected final void resize() { |
|
273 |
if (_size == _maximumCapacity) { |
|
274 |
throw new ValueArrayResourceException(CommonResourceBundle.getInstance().getString("message.arrayMaxCapacity")); |
|
275 |
} |
|
276 |
||
277 |
int newSize = _size * 3 / 2 + 1; |
|
278 |
if (newSize > _maximumCapacity) { |
|
279 |
newSize = _maximumCapacity; |
|
280 |
} |
|
281 |
||
282 |
final String[] newArray = new String[newSize]; |
|
283 |
System.arraycopy(_array, 0, newArray, 0, _size); |
|
284 |
_array = newArray; |
|
285 |
||
286 |
newSize += 2; |
|
287 |
final NamespaceEntry[] newInScopeNamespaces = new NamespaceEntry[newSize]; |
|
288 |
System.arraycopy(_inScopeNamespaces, 0, newInScopeNamespaces, 0, _inScopeNamespaces.length); |
|
289 |
_inScopeNamespaces = newInScopeNamespaces; |
|
290 |
||
291 |
final int[] newCurrentInScope = new int[newSize]; |
|
292 |
System.arraycopy(_currentInScope, 0, newCurrentInScope, 0, _currentInScope.length); |
|
293 |
_currentInScope = newCurrentInScope; |
|
294 |
} |
|
295 |
||
296 |
public final void clearDeclarationIds() { |
|
297 |
for (int i = 0; i < _size; i++) { |
|
298 |
final NamespaceEntry e = _inScopeNamespaces[i]; |
|
299 |
if (e != null) { |
|
300 |
e.declarationId = 0; |
|
301 |
} |
|
302 |
} |
|
303 |
||
304 |
_declarationId = 1; |
|
305 |
} |
|
306 |
||
307 |
public final void pushScope(int prefixIndex, int namespaceIndex) throws FastInfosetException { |
|
308 |
if (_namespacePool == null) { |
|
309 |
increaseNamespacePool(16); |
|
310 |
} |
|
311 |
||
312 |
final NamespaceEntry e = _namespacePool; |
|
313 |
_namespacePool = e.next; |
|
314 |
||
315 |
final NamespaceEntry current = _inScopeNamespaces[++prefixIndex]; |
|
316 |
if (current == null) { |
|
317 |
e.declarationId = _declarationId; |
|
318 |
e.namespaceIndex = _currentInScope[prefixIndex] = ++namespaceIndex; |
|
319 |
e.next = null; |
|
320 |
||
321 |
_inScopeNamespaces[prefixIndex] = e; |
|
322 |
} else if (current.declarationId < _declarationId) { |
|
323 |
e.declarationId = _declarationId; |
|
324 |
e.namespaceIndex = _currentInScope[prefixIndex] = ++namespaceIndex; |
|
325 |
e.next = current; |
|
326 |
||
327 |
current.declarationId = 0; |
|
328 |
_inScopeNamespaces[prefixIndex] = e; |
|
329 |
} else { |
|
330 |
throw new FastInfosetException(CommonResourceBundle.getInstance().getString("message.duplicateNamespaceAttribute")); |
|
331 |
} |
|
332 |
} |
|
333 |
||
334 |
public final void pushScopeWithPrefixEntry(String prefix, String namespaceName, |
|
335 |
int prefixIndex, int namespaceIndex) throws FastInfosetException { |
|
336 |
if (_namespacePool == null) { |
|
337 |
increaseNamespacePool(16); |
|
338 |
} |
|
339 |
if (_prefixPool == null) { |
|
340 |
increasePrefixPool(16); |
|
341 |
} |
|
342 |
||
343 |
final NamespaceEntry e = _namespacePool; |
|
344 |
_namespacePool = e.next; |
|
345 |
||
346 |
final NamespaceEntry current = _inScopeNamespaces[++prefixIndex]; |
|
347 |
if (current == null) { |
|
348 |
e.declarationId = _declarationId; |
|
349 |
e.namespaceIndex = _currentInScope[prefixIndex] = ++namespaceIndex; |
|
350 |
e.next = null; |
|
351 |
||
352 |
_inScopeNamespaces[prefixIndex] = e; |
|
353 |
} else if (current.declarationId < _declarationId) { |
|
354 |
e.declarationId = _declarationId; |
|
355 |
e.namespaceIndex = _currentInScope[prefixIndex] = ++namespaceIndex; |
|
356 |
e.next = current; |
|
357 |
||
358 |
current.declarationId = 0; |
|
359 |
_inScopeNamespaces[prefixIndex] = e; |
|
360 |
} else { |
|
361 |
throw new FastInfosetException(CommonResourceBundle.getInstance().getString("message.duplicateNamespaceAttribute")); |
|
362 |
} |
|
363 |
||
364 |
final PrefixEntry p = _prefixPool; |
|
365 |
_prefixPool = _prefixPool.next; |
|
366 |
p.prefixId = prefixIndex; |
|
367 |
||
368 |
e.prefix = prefix; |
|
369 |
e.namespaceName = namespaceName; |
|
370 |
e.prefixEntryIndex = KeyIntMap.indexFor(KeyIntMap.hashHash(prefix.hashCode()), _prefixMap.length); |
|
371 |
||
372 |
final PrefixEntry pCurrent = _prefixMap[e.prefixEntryIndex]; |
|
373 |
p.next = pCurrent; |
|
374 |
_prefixMap[e.prefixEntryIndex] = p; |
|
375 |
} |
|
376 |
||
377 |
public final void popScope(int prefixIndex) { |
|
378 |
final NamespaceEntry e = _inScopeNamespaces[++prefixIndex]; |
|
379 |
_inScopeNamespaces[prefixIndex] = e.next; |
|
380 |
_currentInScope[prefixIndex] = (e.next != null) ? e.next.namespaceIndex : 0; |
|
381 |
||
382 |
e.next = _namespacePool; |
|
383 |
_namespacePool = e; |
|
384 |
} |
|
385 |
||
386 |
public final void popScopeWithPrefixEntry(int prefixIndex) { |
|
387 |
final NamespaceEntry e = _inScopeNamespaces[++prefixIndex]; |
|
388 |
||
389 |
_inScopeNamespaces[prefixIndex] = e.next; |
|
390 |
_currentInScope[prefixIndex] = (e.next != null) ? e.next.namespaceIndex : 0; |
|
391 |
||
392 |
e.prefix = e.namespaceName = null; |
|
393 |
e.next = _namespacePool; |
|
394 |
_namespacePool = e; |
|
395 |
||
396 |
PrefixEntry current = _prefixMap[e.prefixEntryIndex]; |
|
397 |
if (current.prefixId == prefixIndex) { |
|
398 |
_prefixMap[e.prefixEntryIndex] = current.next; |
|
399 |
current.next = _prefixPool; |
|
400 |
_prefixPool = current; |
|
401 |
} else { |
|
402 |
PrefixEntry prev = current; |
|
403 |
current = current.next; |
|
404 |
while (current != null) { |
|
405 |
if (current.prefixId == prefixIndex) { |
|
406 |
prev.next = current.next; |
|
407 |
current.next = _prefixPool; |
|
408 |
_prefixPool = current; |
|
409 |
break; |
|
410 |
} |
|
411 |
prev = current; |
|
412 |
current = current.next; |
|
413 |
} |
|
414 |
} |
|
415 |
} |
|
416 |
||
417 |
public final String getNamespaceFromPrefix(String prefix) { |
|
418 |
final int index = KeyIntMap.indexFor(KeyIntMap.hashHash(prefix.hashCode()), _prefixMap.length); |
|
419 |
PrefixEntry pe = _prefixMap[index]; |
|
420 |
while (pe != null) { |
|
421 |
final NamespaceEntry ne = _inScopeNamespaces[pe.prefixId]; |
|
422 |
if (prefix == ne.prefix || prefix.equals(ne.prefix)) { |
|
423 |
return ne.namespaceName; |
|
424 |
} |
|
425 |
pe = pe.next; |
|
426 |
} |
|
427 |
||
428 |
return null; |
|
429 |
} |
|
430 |
||
431 |
public final String getPrefixFromNamespace(String namespaceName) { |
|
432 |
int position = 0; |
|
433 |
while (++position < _size + 2) { |
|
434 |
final NamespaceEntry ne = _inScopeNamespaces[position]; |
|
435 |
if (ne != null && namespaceName.equals(ne.namespaceName)) { |
|
436 |
return ne.prefix; |
|
437 |
} |
|
438 |
} |
|
439 |
||
440 |
return null; |
|
441 |
} |
|
442 |
||
443 |
public final Iterator getPrefixes() { |
|
444 |
return new Iterator() { |
|
445 |
int _position = 1; |
|
446 |
NamespaceEntry _ne = _inScopeNamespaces[_position]; |
|
447 |
||
448 |
public boolean hasNext() { |
|
449 |
return _ne != null; |
|
450 |
} |
|
451 |
||
452 |
public Object next() { |
|
453 |
if (_position == _size + 2) { |
|
454 |
throw new NoSuchElementException(); |
|
455 |
} |
|
456 |
||
457 |
final String prefix = _ne.prefix; |
|
458 |
moveToNext(); |
|
459 |
return prefix; |
|
460 |
} |
|
461 |
||
462 |
public void remove() { |
|
463 |
throw new UnsupportedOperationException(); |
|
464 |
} |
|
465 |
||
466 |
private final void moveToNext() { |
|
467 |
while (++_position < _size + 2) { |
|
468 |
_ne = _inScopeNamespaces[_position]; |
|
469 |
if (_ne != null) { |
|
470 |
return; |
|
471 |
} |
|
472 |
} |
|
473 |
_ne = null; |
|
474 |
} |
|
475 |
||
476 |
}; |
|
477 |
} |
|
478 |
||
479 |
public final Iterator getPrefixesFromNamespace(final String namespaceName) { |
|
480 |
return new Iterator() { |
|
481 |
String _namespaceName = namespaceName; |
|
482 |
int _position = 0; |
|
483 |
NamespaceEntry _ne; |
|
484 |
||
485 |
{ |
|
486 |
moveToNext(); |
|
487 |
} |
|
488 |
||
489 |
public boolean hasNext() { |
|
490 |
return _ne != null; |
|
491 |
} |
|
492 |
||
493 |
public Object next() { |
|
494 |
if (_position == _size + 2) { |
|
495 |
throw new NoSuchElementException(); |
|
496 |
} |
|
497 |
||
498 |
final String prefix = _ne.prefix; |
|
499 |
moveToNext(); |
|
500 |
return prefix; |
|
501 |
} |
|
502 |
||
503 |
public void remove() { |
|
504 |
throw new UnsupportedOperationException(); |
|
505 |
} |
|
506 |
||
507 |
private final void moveToNext() { |
|
508 |
while (++_position < _size + 2) { |
|
509 |
_ne = _inScopeNamespaces[_position]; |
|
510 |
if (_ne != null && _namespaceName.equals(_ne.namespaceName)) { |
|
511 |
return; |
|
512 |
} |
|
513 |
} |
|
514 |
_ne = null; |
|
515 |
} |
|
516 |
}; |
|
517 |
} |
|
518 |
} |