KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > xml > fastinfoset > util > DuplicateAttributeVerifier


1
2 package com.sun.xml.fastinfoset.util;
3
4 import org.jvnet.fastinfoset.FastInfosetException;
5 import com.sun.xml.fastinfoset.CommonResourceBundle;
6
7
8 public class DuplicateAttributeVerifier {
9     public static final int MAP_SIZE = 256;
10     
11     public int _currentIteration;
12     
13     private static class Entry {
14         private int iteration;
15         private int value;
16         
17         private Entry hashNext;
18         
19         private Entry poolNext;
20     }
21     
22     private Entry[] _map;
23        
24     public final Entry _poolHead;
25     public Entry _poolCurrent;
26     private Entry _poolTail;
27     
28     
29     public DuplicateAttributeVerifier() {
30         _poolTail = _poolHead = new Entry();
31     }
32     
33     public final void clear() {
34         _currentIteration = 0;
35
36         Entry e = _poolHead;
37         while (e != null) {
38             e.iteration = 0;
39             e = e.poolNext;
40         }
41         
42         reset();
43     }
44     
45     public final void reset() {
46         _poolCurrent = _poolHead;
47     }
48     
49     private final void increasePool(int capacity) {
50         if (_map == null) {
51             _map = new Entry[MAP_SIZE];
52             _poolCurrent = _poolHead;
53         } else {
54             final Entry tail = _poolTail;
55             for (int i = 0; i < capacity; i++) {
56                 final Entry e = new Entry();
57                 _poolTail.poolNext = e;
58                 _poolTail = e;
59             }
60
61             _poolCurrent = tail.poolNext;
62         }
63     }
64     
65     public final void checkForDuplicateAttribute(int hash, int value) throws FastInfosetException {
66         if (_poolCurrent == null) {
67             increasePool(16);
68         }
69         
70         // Get next free entry
71
final Entry newEntry = _poolCurrent;
72         _poolCurrent = _poolCurrent.poolNext;
73
74         final Entry head = _map[hash];
75         if (head == null || head.iteration < _currentIteration) {
76             newEntry.hashNext = null;
77             _map[hash] = newEntry;
78             newEntry.iteration = _currentIteration;
79             newEntry.value = value;
80         } else {
81             Entry e = head;
82             do {
83                 if (e.value == value) {
84                     reset();
85                     throw new FastInfosetException(CommonResourceBundle.getInstance().getString("message.duplicateAttribute"));
86                 }
87             } while ((e = e.hashNext) != null);
88             
89             newEntry.hashNext = head;
90             _map[hash] = newEntry;
91             newEntry.iteration = _currentIteration;
92             newEntry.value = value;
93         }
94     }
95 }
96
Popular Tags