| 1 56 57 package org.jdom; 58 59 import java.io.*; 60 import java.util.*; 61 62 import org.jdom.filter.*; 63 64 81 public class Element extends Content implements Parent { 82 83 private static final String CVS_ID = 84 "@(#) $RCSfile: Element.java,v $ $Revision: 1.154 $ $Date: 2004/12/11 00:27:38 $ $Name: $"; 85 86 private static final int INITIAL_ARRAY_SIZE = 5; 87 88 89 protected String name; 90 91 92 protected transient Namespace namespace; 93 94 96 protected transient List additionalNamespaces; 97 98 101 105 AttributeList attributes = new AttributeList(this); 106 107 111 ContentList content = new ContentList(this); 112 113 128 protected Element() { } 129 130 139 public Element(String name, Namespace namespace) { 140 setName(name); 141 setNamespace(namespace); 142 } 143 144 151 public Element(String name) { 152 this(name, (Namespace) null); 153 } 154 155 166 public Element(String name, String uri) { 167 this(name, Namespace.getNamespace("", uri)); 168 } 169 170 182 public Element(String name, String prefix, String uri) { 183 this(name, Namespace.getNamespace(prefix, uri)); 184 } 185 186 191 public String getName() { 192 return name; 193 } 194 195 203 public Element setName(String name) { 204 String reason = Verifier.checkElementName(name); 205 if (reason != null) { 206 throw new IllegalNameException(name, "element", reason); 207 } 208 this.name = name; 209 return this; 210 } 211 212 217 public Namespace getNamespace() { 218 return namespace; 219 } 220 221 228 public Element setNamespace(Namespace namespace) { 229 if (namespace == null) { 230 namespace = Namespace.NO_NAMESPACE; 231 } 232 233 this.namespace = namespace; 234 return this; 235 } 236 237 243 public String getNamespacePrefix() { 244 return namespace.getPrefix(); 245 } 246 247 254 public String getNamespaceURI() { 255 return namespace.getURI(); 256 } 257 258 269 public Namespace getNamespace(String prefix) { 270 if (prefix == null) { 271 return null; 272 } 273 274 if (prefix.equals("xml")) { 275 return Namespace.XML_NAMESPACE; 277 } 278 279 if (prefix.equals(getNamespacePrefix())) { 281 return getNamespace(); 282 } 283 284 if (additionalNamespaces != null) { 286 for (int i = 0; i < additionalNamespaces.size(); i++) { 287 Namespace ns = (Namespace) additionalNamespaces.get(i); 288 if (prefix.equals(ns.getPrefix())) { 289 return ns; 290 } 291 } 292 } 293 294 if (parent instanceof Element) { 296 return ((Element)parent).getNamespace(prefix); 297 } 298 299 return null; 300 } 301 302 310 public String getQualifiedName() { 311 if (namespace.getPrefix().equals("")) { 314 return getName(); 315 } 316 317 return new StringBuffer (namespace.getPrefix()) 318 .append(':') 319 .append(name) 320 .toString(); 321 } 322 323 335 public void addNamespaceDeclaration(Namespace additional) { 336 337 String reason = Verifier.checkNamespaceCollision(additional, this); 340 if (reason != null) { 341 throw new IllegalAddException(this, additional, reason); 342 } 343 344 if (additionalNamespaces == null) { 345 additionalNamespaces = new ArrayList(INITIAL_ARRAY_SIZE); 346 } 347 348 additionalNamespaces.add(additional); 349 } 350 351 361 public void removeNamespaceDeclaration(Namespace additionalNamespace) { 362 if (additionalNamespaces == null) { 363 return; 364 } 365 additionalNamespaces.remove(additionalNamespace); 366 } 367 368 378 public List getAdditionalNamespaces() { 379 if (additionalNamespaces == null) { 383 return Collections.EMPTY_LIST; 384 } 385 return Collections.unmodifiableList(additionalNamespaces); 386 } 387 388 396 public String getValue() { 397 StringBuffer buffer = new StringBuffer (); 398 399 Iterator itr = getContent().iterator(); 400 while (itr.hasNext()) { 401 Content child = (Content) itr.next(); 402 if (child instanceof Element || child instanceof Text) { 403 buffer.append(child.getValue()); 404 } 405 } 406 return buffer.toString(); 407 } 408 409 416 public boolean isRootElement() { 417 return parent instanceof Document; 418 } 419 420 public int getContentSize() { 421 return content.size(); 422 } 423 424 public int indexOf(Content child) { 425 return content.indexOf(child); 426 } 427 428 438 439 450 public String getText() { 451 if (content.size() == 0) { 452 return ""; 453 } 454 455 if (content.size() == 1) { 457 Object obj = content.get(0); 458 if (obj instanceof Text) { 459 return ((Text) obj).getText(); 460 } else { 461 return ""; 462 } 463 } 464 465 StringBuffer textContent = new StringBuffer (); 467 boolean hasText = false; 468 469 for (int i = 0; i < content.size(); i++) { 470 Object obj = content.get(i); 471 if (obj instanceof Text) { 472 textContent.append(((Text) obj).getText()); 473 hasText = true; 474 } 475 } 476 477 if (!hasText) { 478 return ""; 479 } 480 else { 481 return textContent.toString(); 482 } 483 } 484 485 493 public String getTextTrim() { 494 return getText().trim(); 495 } 496 497 506 public String getTextNormalize() { 507 return Text.normalizeString(getText()); 508 } 509 510 519 public String getChildText(String name) { 520 Element child = getChild(name); 521 if (child == null) { 522 return null; 523 } 524 return child.getText(); 525 } 526 527 536 public String getChildTextTrim(String name) { 537 Element child = getChild(name); 538 if (child == null) { 539 return null; 540 } 541 return child.getTextTrim(); 542 } 543 544 553 public String getChildTextNormalize(String name) { 554 Element child = getChild(name); 555 if (child == null) { 556 return null; 557 } 558 return child.getTextNormalize(); 559 } 560 561 570 public String getChildText(String name, Namespace ns) { 571 Element child = getChild(name, ns); 572 if (child == null) { 573 return null; 574 } 575 return child.getText(); 576 } 577 578 587 public String getChildTextTrim(String name, Namespace ns) { 588 Element child = getChild(name, ns); 589 if (child == null) { 590 return null; 591 } 592 return child.getTextTrim(); 593 } 594 595 604 public String getChildTextNormalize(String name, Namespace ns) { 605 Element child = getChild(name, ns); 606 if (child == null) { 607 return null; 608 } 609 return child.getTextNormalize(); 610 } 611 612 626 public Element setText(String text) { 627 content.clear(); 628 629 if (text != null) { 630 addContent(new Text(text)); 631 } 632 633 return this; 634 } 635 636 658 public List getContent() { 659 return content; 660 } 661 662 674 public List getContent(Filter filter) { 675 return content.getView(filter); 676 } 677 678 683 public List removeContent() { 684 List old = new ArrayList(content); 685 content.clear(); 686 return old; 687 } 688 689 695 public List removeContent(Filter filter) { 696 List old = new ArrayList(); 697 Iterator itr = content.getView(filter).iterator(); 698 while (itr.hasNext()) { 699 Content child = (Content) itr.next(); 700 old.add(child); 701 itr.remove(); 702 } 703 return old; 704 } 705 706 741 public Element setContent(Collection newContent) { 742 content.clearAndSet(newContent); 743 return this; 744 } 745 746 761 public Element setContent(int index, Content child) { 762 content.set(index, child); 763 return this; 764 } 765 766 782 public Element setContent(int index, Collection collection) { 783 content.remove(index); 784 content.addAll(index, collection); 785 return this; 786 } 787 788 798 public Element addContent(String str) { 799 return addContent(new Text(str)); 800 } 801 802 808 public Element addContent(Content child) { 809 content.add(child); 810 return this; 811 } 812 813 824 public Element addContent(Collection collection) { 825 content.addAll(collection); 826 return this; 827 } 828 829 839 public Element addContent(int index, Content child) { 840 content.add(index, child); 841 return this; 842 } 843 844 858 public Element addContent(int index, Collection c) { 859 content.addAll(index, c); 860 return this; 861 } 862 863 public List cloneContent() { 864 int size = getContentSize(); 865 List list = new ArrayList(size); 866 for (int i = 0; i < size; i++) { 867 Content child = getContent(i); 868 list.add(child.clone()); 869 } 870 return list; 871 } 872 873 public Content getContent(int index) { 874 return (Content) content.get(index); 875 } 876 877 882 public boolean removeContent(Content child) { 883 return content.remove(child); 884 } 885 886 public Content removeContent(int index) { 887 return (Content) content.remove(index); 888 } 889 890 917 public Element setContent(Content child) { 918 content.clear(); 919 content.add(child); 920 return this; 921 } 922 923 924 931 public boolean isAncestor(Element element) { 932 Object p = element.getParent(); 933 while (p instanceof Element) { 934 if (p == this) { 935 return true; 936 } 937 p = ((Element) p).getParent(); 938 } 939 return false; 940 } 941 942 953 public List getAttributes() { 954 return attributes; 955 } 956 957 966 public Attribute getAttribute(String name) { 967 return getAttribute(name, Namespace.NO_NAMESPACE); 968 } 969 970 980 public Attribute getAttribute(String name, Namespace ns) { 981 return (Attribute) attributes.get(name, ns); 982 } 983 984 994 public String getAttributeValue(String name) { 995 return getAttributeValue(name, Namespace.NO_NAMESPACE); 996 } 997 998 |