1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package net.sf.stax;
25
26 import java.util.Map;
27 import java.util.Set;
28 import java.util.List;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.Iterator;
32 import java.util.ArrayList;
33
34 import org.xml.sax.SAXException;
35
36 /***
37 * Default implementation of <b>IdMapper</b>.
38 *
39 * @author Thomas Down
40 * @author Matthew Pocock
41 * @author Michael Heuer
42 * @version $Revision: 1.2 $ $Date: 2006/01/02 20:37:34 $
43 */
44 class IdMapperImpl
45 implements IdMapper
46 {
47 /*** List of global listeners. */
48 private final List globalListeners;
49
50 /*** Map of IdListeners keyed by ids. */
51 private final Map idListeners;
52
53 /*** Map of ids to items. */
54 private final Map idToItem;
55
56
57 /***
58 * Create a new default implementation of <b>IdMapper</b>.
59 */
60 public IdMapperImpl()
61 {
62 globalListeners = new ArrayList();
63 idListeners = new HashMap();
64 idToItem = new HashMap();
65 }
66
67 /***
68 * Create a new default implementation of <b>IdMapper</b>
69 * with the specified map of existing ids to items.
70 *
71 * @param existingIds map of existing ids to items.
72 */
73 public IdMapperImpl(final Map existingIds)
74 {
75 this();
76 idToItem.putAll(existingIds);
77 }
78
79
80 /***
81 * Return the set of unresolved ids.
82 *
83 * @return the set of unresolved ids
84 */
85 public Set getUnresolvedIds()
86 {
87 return idListeners.keySet();
88 }
89
90 /*** @see IdMapper */
91 public void registerItemForId(final Object id, final Object item)
92 throws SAXException
93 {
94
95 if (idToItem.containsKey(id))
96 {
97 throw new SAXException("id already mapped: " + id + " -> " + idToItem.get(id));
98 }
99
100
101 idToItem.put(id, item);
102
103
104 for (Iterator i = globalListeners.iterator(); i.hasNext();)
105 {
106 IdListener l = (IdListener) i.next();
107 l.idRegistered(id, item);
108 }
109
110
111 Object listeners = idListeners.get(id);
112 if (listeners != null)
113 {
114
115 if (listeners instanceof Set)
116 {
117 for (Iterator i = ((Set) listeners).iterator(); i.hasNext();)
118 {
119 IdListener l = (IdListener) i.next();
120 l.idRegistered(id, item);
121 }
122 }
123 else
124 {
125
126 IdListener l = (IdListener) listeners;
127 l.idRegistered(id, item);
128 }
129 }
130 idListeners.remove(id);
131 }
132
133
134 /*** @see IdMapper */
135 public void addIdListener(final IdListener listener)
136 throws SAXException
137 {
138
139 globalListeners.add(listener);
140
141
142 for (Iterator i = idToItem.keySet().iterator(); i.hasNext();)
143 {
144 Object id = i.next();
145 listener.idRegistered(id, idToItem.get(id));
146 }
147 }
148
149 /*** @see IdMapper */
150 public void removeIdListener(final IdListener listener)
151 throws SAXException
152 {
153
154 }
155
156 /*** @see IdMapper */
157 public void addIdListener(final IdListener listener, final Object id)
158 throws SAXException
159 {
160 if (idToItem.containsKey(id))
161 {
162
163 listener.idRegistered(id, idToItem.get(id));
164 }
165 else
166 {
167
168 Object ls = idListeners.get(id);
169 if (ls == null)
170 {
171 idListeners.put(id, listener);
172 }
173 else if (ls instanceof IdListener)
174 {
175 Set lSet = new HashSet();
176 lSet.add(ls);
177 lSet.add(listener);
178 idListeners.put(id, lSet);
179 }
180 else
181 {
182 Set lSet = (Set) ls;
183 lSet.add(listener);
184 }
185 }
186 }
187
188 /*** @see IdMapper */
189 public void removeIdListener(final IdListener listener, final Object id)
190 throws SAXException
191 {
192
193 }
194 }