1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.opensaml.xml.util;
18
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.Map;
22 import java.util.Set;
23 import java.util.concurrent.ConcurrentHashMap;
24 import java.util.concurrent.CopyOnWriteArraySet;
25
26 import javax.xml.namespace.QName;
27
28 import org.opensaml.xml.Configuration;
29 import org.opensaml.xml.XMLObject;
30
31
32
33
34
35
36
37 public class AttributeMap implements Map<QName, String> {
38
39
40 private XMLObject attributeOwner;
41
42
43 private Map<QName, String> attributes;
44
45
46
47 private Set<QName> idAttribNames;
48
49
50
51
52
53
54
55
56 public AttributeMap(XMLObject newOwner) throws NullPointerException {
57 if (newOwner == null) {
58 throw new NullPointerException("Attribute owner XMLObject may not be null");
59 }
60
61 attributeOwner = newOwner;
62 attributes = new ConcurrentHashMap<QName, String>();
63 idAttribNames = new CopyOnWriteArraySet<QName>();
64 }
65
66
67 public String put(QName attributeName, String value) {
68 String oldValue = get(attributeName);
69 if (value != oldValue) {
70 releaseDOM();
71 attributes.put(attributeName, value);
72 if (isIDAttribute(attributeName) || Configuration.isIDAttribute(attributeName)) {
73 attributeOwner.getIDIndex().deregisterIDMapping(oldValue);
74 attributeOwner.getIDIndex().registerIDMapping(value, attributeOwner);
75 }
76 }
77
78 return oldValue;
79 }
80
81
82 public void clear() {
83 for (QName attributeName : attributes.keySet()) {
84 remove(attributeName);
85 }
86 }
87
88
89
90
91
92
93 public Set<QName> keySet() {
94 return Collections.unmodifiableSet(attributes.keySet());
95 }
96
97
98 public int size() {
99 return attributes.size();
100 }
101
102
103 public boolean isEmpty() {
104 return attributes.isEmpty();
105 }
106
107
108 public boolean containsKey(Object key) {
109 return attributes.containsKey(key);
110 }
111
112
113 public boolean containsValue(Object value) {
114 return attributes.containsValue(value);
115 }
116
117
118 public String get(Object key) {
119 return attributes.get(key);
120 }
121
122
123 public String remove(Object key) {
124 String removedValue = attributes.remove(key);
125 if (removedValue != null) {
126 releaseDOM();
127 QName attributeName = (QName) key;
128 if (isIDAttribute(attributeName) || Configuration.isIDAttribute(attributeName)) {
129 attributeOwner.getIDIndex().deregisterIDMapping(removedValue);
130 }
131 }
132
133 return removedValue;
134 }
135
136
137 public void putAll(Map<? extends QName, ? extends String> t) {
138 if (t != null && t.size() > 0) {
139 for (Entry<? extends QName, ? extends String> entry : t.entrySet()) {
140 put(entry.getKey(), entry.getValue());
141 }
142 }
143 }
144
145
146
147
148
149
150 public Collection<String> values() {
151 return Collections.unmodifiableCollection(attributes.values());
152 }
153
154
155
156
157
158
159 public Set<Entry<QName, String>> entrySet() {
160 return Collections.unmodifiableSet(attributes.entrySet());
161 }
162
163
164
165
166
167
168 public void registerID(QName attributeName) {
169 if (! idAttribNames.contains(attributeName)) {
170 idAttribNames.add(attributeName);
171 }
172
173
174
175 if (containsKey(attributeName)) {
176 attributeOwner.getIDIndex().registerIDMapping(get(attributeName), attributeOwner);
177 }
178 }
179
180
181
182
183
184
185 public void deregisterID(QName attributeName) {
186 if (idAttribNames.contains(attributeName)) {
187 idAttribNames.remove(attributeName);
188 }
189
190
191
192 if (containsKey(attributeName)) {
193 attributeOwner.getIDIndex().deregisterIDMapping(get(attributeName));
194 }
195 }
196
197
198
199
200
201
202
203
204 public boolean isIDAttribute(QName attributeName) {
205 return idAttribNames.contains(attributeName);
206 }
207
208
209
210
211 private void releaseDOM() {
212 attributeOwner.releaseDOM();
213 attributeOwner.releaseParentDOM(true);
214 }
215 }