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