|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| package org.dom4j.util; |
|
11 |
| |
|
12 |
| import java.util.ArrayList; |
|
13 |
| import java.util.HashMap; |
|
14 |
| import java.util.Iterator; |
|
15 |
| import java.util.List; |
|
16 |
| import java.util.Map; |
|
17 |
| |
|
18 |
| import org.dom4j.Attribute; |
|
19 |
| import org.dom4j.Element; |
|
20 |
| import org.dom4j.Node; |
|
21 |
| import org.dom4j.QName; |
|
22 |
| import org.dom4j.tree.BackedList; |
|
23 |
| import org.dom4j.tree.DefaultElement; |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| public class IndexedElement extends DefaultElement { |
|
33 |
| |
|
34 |
| |
|
35 |
| private Map elementIndex; |
|
36 |
| |
|
37 |
| |
|
38 |
| private Map attributeIndex; |
|
39 |
| |
|
40 |
| |
|
41 |
0
| public IndexedElement(String name) {
|
|
42 |
0
| super(name);
|
|
43 |
| } |
|
44 |
| |
|
45 |
10
| public IndexedElement(QName qname) {
|
|
46 |
10
| super(qname);
|
|
47 |
| } |
|
48 |
| |
|
49 |
0
| public IndexedElement(QName qname, int attributeCount) {
|
|
50 |
0
| super(qname, attributeCount);
|
|
51 |
| } |
|
52 |
| |
|
53 |
8
| public Attribute attribute(String name) {
|
|
54 |
8
| return (Attribute) attributeIndex().get(name);
|
|
55 |
| } |
|
56 |
| |
|
57 |
0
| public Attribute attribute(QName qName) {
|
|
58 |
0
| return (Attribute) attributeIndex().get(qName);
|
|
59 |
| } |
|
60 |
| |
|
61 |
0
| public Element element(String name) {
|
|
62 |
0
| return asElement( elementIndex().get(name) );
|
|
63 |
| } |
|
64 |
| |
|
65 |
0
| public Element element(QName qName) {
|
|
66 |
0
| return asElement( elementIndex().get(qName) );
|
|
67 |
| } |
|
68 |
| |
|
69 |
0
| public List elements(String name) {
|
|
70 |
0
| return asElementList( elementIndex().get(name) );
|
|
71 |
| } |
|
72 |
| |
|
73 |
10
| public List elements(QName qName) {
|
|
74 |
10
| return asElementList( elementIndex().get(qName) );
|
|
75 |
| } |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
0
| protected Element asElement(Object object) {
|
|
82 |
0
| if ( object instanceof Element ) {
|
|
83 |
0
| return (Element) object;
|
|
84 |
| } |
|
85 |
0
| else if ( object != null ) {
|
|
86 |
0
| List list = (List) object;
|
|
87 |
0
| if ( list.size() >= 1 ) {
|
|
88 |
0
| return (Element) list.get(0);
|
|
89 |
| } |
|
90 |
| } |
|
91 |
0
| return null;
|
|
92 |
| } |
|
93 |
| |
|
94 |
10
| protected List asElementList(Object object) {
|
|
95 |
10
| if ( object instanceof Element ) {
|
|
96 |
0
| return createSingleResultList( object );
|
|
97 |
| } |
|
98 |
10
| else if ( object != null ) {
|
|
99 |
2
| List list = (List) object;
|
|
100 |
2
| BackedList answer = createResultList();
|
|
101 |
2
| for ( int i = 0, size = list.size(); i < size; i++ ) {
|
|
102 |
4
| answer.addLocal( list.get(i) );
|
|
103 |
| } |
|
104 |
2
| return answer;
|
|
105 |
| } |
|
106 |
8
| return createEmptyList();
|
|
107 |
| } |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
0
| protected Iterator asElementIterator(Object object) {
|
|
113 |
0
| return asElementList(object).iterator();
|
|
114 |
| } |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
0
| protected void addNode(Node node) {
|
|
120 |
0
| super.addNode(node);
|
|
121 |
0
| if ( elementIndex != null && node instanceof Element ) {
|
|
122 |
0
| addToElementIndex( (Element) node );
|
|
123 |
| } |
|
124 |
0
| else if ( attributeIndex != null && node instanceof Attribute ) {
|
|
125 |
0
| addToAttributeIndex( (Attribute) node );
|
|
126 |
| } |
|
127 |
| } |
|
128 |
| |
|
129 |
0
| protected boolean removeNode(Node node) {
|
|
130 |
0
| if ( super.removeNode(node) ) {
|
|
131 |
0
| if ( elementIndex != null && node instanceof Element ) {
|
|
132 |
0
| removeFromElementIndex( (Element) node );
|
|
133 |
| } |
|
134 |
0
| else if ( attributeIndex != null && node instanceof Attribute ) {
|
|
135 |
0
| removeFromAttributeIndex( (Attribute) node );
|
|
136 |
| } |
|
137 |
0
| return true;
|
|
138 |
| } |
|
139 |
0
| return false;
|
|
140 |
| } |
|
141 |
| |
|
142 |
8
| protected Map attributeIndex() {
|
|
143 |
8
| if ( attributeIndex == null ) {
|
|
144 |
4
| attributeIndex = createAttributeIndex();
|
|
145 |
4
| for (Iterator iter = attributeIterator(); iter.hasNext(); ) {
|
|
146 |
0
| addToAttributeIndex( (Attribute) iter.next() );
|
|
147 |
| } |
|
148 |
| } |
|
149 |
8
| return attributeIndex;
|
|
150 |
| } |
|
151 |
| |
|
152 |
10
| protected Map elementIndex() {
|
|
153 |
10
| if ( elementIndex == null ) {
|
|
154 |
10
| elementIndex = createElementIndex();
|
|
155 |
10
| for (Iterator iter = elementIterator(); iter.hasNext(); ) {
|
|
156 |
8
| addToElementIndex( (Element) iter.next() );
|
|
157 |
| } |
|
158 |
| } |
|
159 |
10
| return elementIndex;
|
|
160 |
| } |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
4
| protected Map createAttributeIndex() {
|
|
165 |
4
| Map answer = createIndex();
|
|
166 |
4
| return answer;
|
|
167 |
| } |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
10
| protected Map createElementIndex() {
|
|
172 |
10
| Map answer = createIndex();
|
|
173 |
10
| return answer;
|
|
174 |
| } |
|
175 |
| |
|
176 |
8
| protected void addToElementIndex(Element element) {
|
|
177 |
8
| QName qName = element.getQName();
|
|
178 |
8
| String name = qName.getName();
|
|
179 |
8
| addToElementIndex(qName, element);
|
|
180 |
8
| addToElementIndex(name, element);
|
|
181 |
| } |
|
182 |
| |
|
183 |
16
| protected void addToElementIndex(Object key, Element value) {
|
|
184 |
16
| Object oldValue = elementIndex.get(key);
|
|
185 |
16
| if (oldValue == null) {
|
|
186 |
12
| elementIndex.put(key, value);
|
|
187 |
| } |
|
188 |
| else { |
|
189 |
4
| if ( oldValue instanceof List ) {
|
|
190 |
0
| List list = (List) oldValue;
|
|
191 |
0
| list.add(value);
|
|
192 |
| } |
|
193 |
| else { |
|
194 |
4
| List list = createList();
|
|
195 |
4
| list.add(oldValue);
|
|
196 |
4
| list.add(value);
|
|
197 |
4
| elementIndex.put(key, list);
|
|
198 |
| } |
|
199 |
| } |
|
200 |
| } |
|
201 |
| |
|
202 |
0
| protected void removeFromElementIndex(Element element) {
|
|
203 |
0
| QName qName = element.getQName();
|
|
204 |
0
| String name = qName.getName();
|
|
205 |
0
| removeFromElementIndex(qName, element);
|
|
206 |
0
| removeFromElementIndex(name, element);
|
|
207 |
| } |
|
208 |
| |
|
209 |
0
| protected void removeFromElementIndex(Object key, Element value) {
|
|
210 |
0
| Object oldValue = elementIndex.get(key);
|
|
211 |
0
| if ( oldValue instanceof List ) {
|
|
212 |
0
| List list = (List) oldValue;
|
|
213 |
0
| list.remove(value);
|
|
214 |
| } |
|
215 |
| else { |
|
216 |
0
| elementIndex.remove(key);
|
|
217 |
| } |
|
218 |
| } |
|
219 |
| |
|
220 |
| |
|
221 |
0
| protected void addToAttributeIndex(Attribute attribute) {
|
|
222 |
0
| QName qName = attribute.getQName();
|
|
223 |
0
| String name = qName.getName();
|
|
224 |
0
| addToAttributeIndex(qName, attribute);
|
|
225 |
0
| addToAttributeIndex(name, attribute);
|
|
226 |
| } |
|
227 |
| |
|
228 |
0
| protected void addToAttributeIndex(Object key, Attribute value) {
|
|
229 |
0
| Object oldValue = attributeIndex.get(key);
|
|
230 |
0
| if (oldValue != null) {
|
|
231 |
0
| attributeIndex.put(key, value);
|
|
232 |
| } |
|
233 |
| } |
|
234 |
| |
|
235 |
0
| protected void removeFromAttributeIndex(Attribute attribute) {
|
|
236 |
0
| QName qName = attribute.getQName();
|
|
237 |
0
| String name = qName.getName();
|
|
238 |
0
| removeFromAttributeIndex(qName, attribute);
|
|
239 |
0
| removeFromAttributeIndex(name, attribute);
|
|
240 |
| } |
|
241 |
| |
|
242 |
0
| protected void removeFromAttributeIndex(Object key, Attribute value) {
|
|
243 |
0
| Object oldValue = attributeIndex.get(key);
|
|
244 |
0
| if ( oldValue != null && oldValue.equals( value ) ) {
|
|
245 |
0
| attributeIndex.remove(key);
|
|
246 |
| } |
|
247 |
| } |
|
248 |
| |
|
249 |
| |
|
250 |
| |
|
251 |
14
| protected Map createIndex() {
|
|
252 |
14
| return new HashMap();
|
|
253 |
| } |
|
254 |
| |
|
255 |
| |
|
256 |
| |
|
257 |
4
| protected List createList() {
|
|
258 |
4
| return new ArrayList();
|
|
259 |
| } |
|
260 |
| } |
|
261 |
| |
|
262 |
| |
|
263 |
| |
|
264 |
| |
|
265 |
| |
|
266 |
| |
|
267 |
| |
|
268 |
| |
|
269 |
| |
|
270 |
| |
|
271 |
| |
|
272 |
| |
|
273 |
| |
|
274 |
| |
|
275 |
| |
|
276 |
| |
|
277 |
| |
|
278 |
| |
|
279 |
| |
|
280 |
| |
|
281 |
| |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
| |
|
286 |
| |
|
287 |
| |
|
288 |
| |
|
289 |
| |
|
290 |
| |
|
291 |
| |
|
292 |
| |
|
293 |
| |
|
294 |
| |
|
295 |
| |
|
296 |
| |
|
297 |
| |
|
298 |
| |
|
299 |
| |
|
300 |
| |
|
301 |
| |
|
302 |
| |
|
303 |
| |
|
304 |
| |
|
305 |
| |
|
306 |
| |
|
307 |
| |
|
308 |
| |