1 package org.columba.core.xml; 17 18 import java.util.Enumeration ; 22 import java.util.Hashtable ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Observable ; 26 import java.util.Vector ; 27 28 32 68 public class XmlElement extends Observable implements Cloneable { 69 70 private static final java.util.logging.Logger LOG = 71 java.util.logging.Logger.getLogger("org.columba.core.xml"); 73 String name; 74 75 String data; 76 77 Hashtable attributes; 78 79 List subElements; 80 81 XmlElement parent; 82 83 89 public XmlElement() { 90 subElements = new Vector (); 91 this.attributes = new Hashtable (10); 92 } 93 94 103 public XmlElement(String name) { 104 this.name = name; 105 this.attributes = new Hashtable (10); 106 subElements = new Vector (); 107 data = ""; 108 } 109 110 121 public XmlElement(String name, Hashtable attributes) { 122 this.name = name; 123 this.attributes = attributes; 124 subElements = new Vector (); 125 } 126 127 138 public XmlElement(String name, String data) { 139 this.name = name; 140 this.data = data; 141 subElements = new Vector (); 142 this.attributes = new Hashtable (10); 143 } 144 145 155 public Object addAttribute(String name, String value) { 156 if ((value != null) && (name != null)) { 157 Object returnValue = (attributes.put(name, value)); 158 159 return returnValue; 160 } 161 162 return null; 163 } 164 165 173 public String getAttribute(String name) { 174 return ((String ) attributes.get(name)); 175 } 176 177 public String getAttribute(String name, String defaultValue) { 178 if (getAttribute(name) == null) { 179 addAttribute(name, defaultValue); 180 } 181 182 return getAttribute(name); 183 } 184 185 193 public Hashtable getAttributes() { 194 return attributes; 195 } 196 197 205 public void setAttributes(Hashtable attrs) { 206 attributes = attrs; 207 } 208 209 215 public Enumeration getAttributeNames() { 216 return (attributes.keys()); 217 } 218 219 227 public boolean addElement(XmlElement e) { 228 e.setParent(this); 229 230 return (subElements.add(e)); 231 } 232 233 public XmlElement removeElement(XmlElement e) { 234 XmlElement child = null; 235 236 for (int i = 0; i < subElements.size(); i++) { 237 child = (XmlElement) subElements.get(i); 238 239 if (child == e) { 243 subElements.remove(i); 244 } 245 } 246 247 return (child); 248 } 249 250 public XmlElement removeElement(int index) { 251 return (XmlElement) subElements.remove(index); 252 } 253 254 public void removeAllElements() { 255 subElements.clear(); 256 } 257 258 266 public void removeFromParent() { 267 if (parent == null) { 268 return; 269 } 270 271 parent.removeElement(this); 272 parent = null; 273 } 274 275 public void append(XmlElement e) { 276 e.removeFromParent(); 277 278 addElement(e); 279 } 280 281 288 public void insertElement(XmlElement e, int index) { 289 e.removeFromParent(); 290 291 subElements.add(index, e); 292 e.setParent(this); 293 } 294 295 301 public List getElements() { 302 return subElements; 303 } 304 305 public int count() { 306 return subElements.size(); 307 } 308 309 317 public XmlElement getElement(String path) { 318 int i = path.indexOf('/'); 319 String topName; 320 String subName; 321 322 if (i == 0) { 323 path = path.substring(1); 324 i = path.indexOf('/'); 325 } 326 327 if (i > 0) { 328 topName = path.substring(0, i); 329 subName = path.substring(i + 1); 330 } else { 331 topName = path; 332 subName = null; 333 } 334 335 int j; 336 337 for (j = 0; j < subElements.size(); j++) { 338 if (((XmlElement) subElements.get(j)).getName().equals(topName)) { 339 if (subName != null) { 340 return (((XmlElement) subElements.get(j)) 341 .getElement(subName)); 342 } else { 343 return ((XmlElement) subElements.get(j)); 344 } 345 } 346 } 347 348 return null; 349 } 350 351 public XmlElement getElement(int index) { 352 return (XmlElement) subElements.get(index); 353 } 354 355 363 public XmlElement addSubElement(String path) { 364 XmlElement parent = this; 365 XmlElement child; 366 String name; 367 368 while (path.indexOf('/') != -1) { 369 name = path.substring(0, path.indexOf('/')); 370 path = path.substring(path.indexOf('/') + 1); 371 372 if (name.length() == 0) 374 continue; 375 376 if (parent.getElement(name) != null) { 377 parent = parent.getElement(name); 378 } else { 379 child = new XmlElement(name); 380 381 parent.addElement(child); 382 parent = child; 383 } 384 385 } 386 387 child = new XmlElement(path); 388 parent.addElement(child); 389 390 return child; 391 } 392 393 401 public XmlElement addSubElement(XmlElement e) { 402 e.setParent(this); 403 subElements.add(e); 404 405 return e; 406 } 407 408 417 public XmlElement addSubElement(String name, String data) { 418 XmlElement e = new XmlElement(name); 419 e.setData(data); 420 e.setParent(this); 421 subElements.add(e); 422 423 return e; 424 } 425 426 433 public void setParent(XmlElement parent) { 434 this.parent = parent; 435 } 436 437 443 public XmlElement getParent() { 444 return parent; 445 } 446 447 454 public void setData(String d) { 455 data = d; 456 } 457 458 464 public String getData() { 465 return data; 466 } 467 468 474 public String getName() { 475 return name; 476 } 477 478 485 486 493 494 503 504 522 523 532 533 546 public static void printNode(XmlElement node, String indent) { 547 String data = node.getData(); 548 549 if ((data == null) || data.equals("")) { 550 LOG.info(indent + node.getName()); 551 } else { 552 LOG.info(indent + node.getName() + " = '" + data + "'"); } 554 555 for (Enumeration enumeration = node.getAttributes().keys(); enumeration 557 .hasMoreElements();) { 558 String key = (String ) enumeration.nextElement(); 559 String value = node.getAttribute(key); 560 LOG.info(indent + key + ":" + value); } 562 563 List subs = node.getElements(); 564 565 for (Iterator it = subs.iterator(); it.hasNext();) { 566 printNode((XmlElement) it.next(), indent + " "); 567 568 } 571 } 572 573 574 public Object clone() { 575 try { 576 XmlElement clone = (XmlElement) super.clone(); 580 if (attributes != null) { 581 clone.setAttributes((Hashtable ) getAttributes().clone()); 582 } 583 584 if (subElements != null) { 585 clone.subElements = new Vector (); 586 587 List childs = getElements(); 588 XmlElement child; 589 590 for (Iterator it = childs.iterator(); it.hasNext();) { 591 child = (XmlElement) it.next(); 592 593 clone.addSubElement((XmlElement) child.clone()); 596 } 597 } 598 599 return clone; 600 } catch (CloneNotSupportedException cnse) { 601 throw new InternalError ("Could not clone XmlElement: " + cnse); 602 } 603 } 604 605 611 public void setName(String name) { 612 this.name = name; 613 } 614 615 620 public void notifyObservers() { 621 setChanged(); 622 super.notifyObservers(); 623 } 624 625 637 private boolean equals(Object obj1, Object obj2) { 638 boolean equal = false; 639 640 if ((obj1 == null) && (obj2 == null)) { 641 equal = true; 642 } else if ((obj1 != null) && (obj2 != null)) { 643 equal = obj1.equals(obj2); 644 } 645 646 return equal; 647 } 648 649 650 public boolean equals(Object obj) { 651 boolean equal = false; 652 653 if ((obj != null) && (obj instanceof XmlElement)) { 654 XmlElement other = (XmlElement) obj; 655 656 if (equals(attributes, other.attributes) 657 && equals(data, other.data) && equals(name, other.name) 658 && equals(subElements, other.subElements)) { 659 equal = true; 660 } 661 } 662 663 return equal; 664 } 665 666 667 public int hashCode() { 668 int hashCode = 23; 670 671 if (attributes != null) { 672 hashCode += (attributes.hashCode() * 13); 673 } 674 675 if (data != null) { 676 hashCode += (data.hashCode() * 17); 677 } 678 679 if (name != null) { 680 hashCode += (name.hashCode() * 29); 681 } 682 683 if (subElements != null) { 684 hashCode += (subElements.hashCode() * 57); 685 } 686 687 return hashCode; 688 } 689 690 693 public String toString() { 694 return getName(); 695 } 696 } 697 698 | Popular Tags |