1 7 package org.enhydra.xml; 8 9 import java.util.ArrayList ; 10 import java.util.HashMap ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 14 import org.w3c.dom.DOMException ; 15 import org.w3c.dom.Document ; 16 import org.w3c.dom.Element ; 17 import org.w3c.dom.Node ; 18 import org.w3c.dom.NodeList ; 19 20 31 public class HashMapElement extends ElementImpl { 32 33 34 37 protected HashMap children = null; 38 39 40 43 public HashMapElement() { 44 super(); 45 children = new HashMap (); 46 } 47 48 49 55 public HashMapElement(HashMapElement element) { 56 super(element); 57 children = element.children; 58 } 59 60 61 68 public HashMapElement(Document ownerDoc, String name) { 69 super(ownerDoc, name); 70 this.children = new HashMap (); 71 } 72 73 74 83 protected HashMapElement(Document ownerDoc, String nodeName, short type, String value) { 84 super(ownerDoc, nodeName, type, value); 85 } 86 87 88 94 public HashMapElement(Node node) { 95 this(node, true); 96 } 97 98 107 public HashMapElement(Node node, boolean deep) { 108 super(node, false); 109 children = new HashMap (); 110 if (deep) 111 initNodeImplChildren(node); 112 } 113 114 115 122 protected Node newElementInstance(Node node) { 123 return new HashMapElement(node); 124 } 125 126 134 public static Element newInstance(Document document) { 135 Node root = document.getDocumentElement(); 136 return new HashMapElement(root); 137 } 138 139 153 public Node insertBefore(Node newChild, Node refChild) { 154 super.insertBefore(newChild, refChild); 155 156 if (newChild.getNodeType() == ELEMENT_NODE) { 157 HashMapElement newChildNode = (HashMapElement) newChild; 158 159 List list = (List ) children.get(newChildNode.getTagName()); 160 if (list == null) 161 list = new ArrayList (); 162 list.add(newChildNode); 163 children.put(newChildNode.getTagName(), list); 164 } 165 return newChild; 166 } 167 168 169 182 public Node replaceChild(Node newChild, Node oldChild) { 183 super.replaceChild(newChild, oldChild); 184 185 if (oldChild.getNodeType() == ELEMENT_NODE) { 186 HashMapElement oldChildNode = (HashMapElement) oldChild; 187 List list = (List ) children.get(oldChildNode.getTagName()); 188 if (list != null) { 189 int index = list.indexOf(oldChildNode); 190 if (index != -1) 191 list.remove(index); 192 if (list.size() == 0) 193 children.remove(oldChildNode.getTagName()); 194 } 195 } 196 if (newChild.getNodeType() == ELEMENT_NODE) { 197 HashMapElement newChildNode = (HashMapElement) newChild; 198 List list = (List ) children.get(newChildNode.getTagName()); 199 if (list == null) 200 list = new ArrayList (); 201 list.add(newChildNode); 202 children.put(newChildNode.getTagName(), list); 203 } 204 205 return oldChild; 206 } 207 208 209 220 public Node removeChild(Node oldChild) { 221 super.removeChild(oldChild); 222 223 if (oldChild.getNodeType() == ELEMENT_NODE) { 224 HashMapElement oldChildNode = (HashMapElement) oldChild; 225 226 List list = (List ) children.get(oldChildNode.getTagName()); 227 if (list != null) { 228 int index = list.indexOf(oldChildNode); 229 if (index != -1) 230 list.remove(index); 231 if (list.size() == 0) 232 children.remove(oldChildNode.getTagName()); 233 } 234 } 235 return oldChild; 236 } 237 238 239 253 public Node cloneNode(boolean deep) { 254 return new HashMapElement(this, deep); 255 } 256 257 258 259 267 public NodeList getElementsByTagName(String name) { 268 List list = new ArrayList (); 269 270 if (nodeName.equals(name)) { 272 list.add(this); 273 } 274 275 getElementsByTagName(name, list); 276 return new NodeListImpl(list); 277 } 278 279 280 private void getElementsByTagName(String name, List list) { 281 if (numChildren == 0) 282 return; 283 List fList = (List ) children.get(name); 284 if (fList != null); 285 list.addAll(fList); 286 for (Iterator iter = children.values().iterator(); iter.hasNext();) { 287 fList = (List ) iter.next(); 288 for (int i = 0; i < fList.size(); i++) { 289 ((HashMapElement) fList.get(i)).getElementsByTagName( 290 name, 291 list); 292 } 293 } 294 } 295 296 297 302 public boolean hasElementChildNodes() { 303 return children.size() > 0; 304 } 305 306 307 314 public NodeList getChildrenByTagName(String name) { 315 List list = (List ) this.children.get(name); 316 if (list != null) 317 return new NodeListImpl(list); 318 return null; 319 } 320 321 322 329 public Element getFirstChildByTagName(String name) { 330 NodeList children = getChildrenByTagName(name); 331 if (children != null && children.getLength() > 0) 332 return (HashMapElement) children.item(0); 333 return null; 334 } 335 336 337 342 public Element getNextSameNameNode() { 343 try { 344 HashMapElement parent = (HashMapElement) this.getParentNode(); 345 List tagList = (List ) parent.children.get(this.nodeName); 346 int index = tagList.indexOf(this); 347 if (++index <= tagList.size()) 348 return (HashMapElement) tagList.get(index); 349 } catch (NullPointerException e) { 350 throw new NodeDOMException( 351 DOMException.NOT_FOUND_ERR, 352 "Root node doesn't have a successor"); 353 } 354 return null; 355 } 356 357 358 363 public String getText() { 364 String text = ""; 365 Node child = this.getFirstChild(); 366 while (child != null) { 367 if (child.getNodeType() == Node.TEXT_NODE) 368 text += child.getNodeValue(); 369 child = child.getNextSibling(); 370 } 371 if (!text.equals("")) 372 return text; 373 return null; 374 } 375 376 377 383 public void setText(String text) { 384 Node child = this.getFirstChild(); 385 if (child != null) { 386 child.setNodeValue(text); 387 child = child.getNextSibling(); 388 while (child != null) { 389 if (child.getNodeType() == Node.TEXT_NODE) { 390 Node temp = child; 391 child = child.getNextSibling(); 392 this.removeChild(temp); 393 } else { 394 child = child.getNextSibling(); 395 } 396 } 397 } 398 } 399 400 401 } | Popular Tags |