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 1009 public String getAttributeValue(String name, String def) { 1010 return getAttributeValue(name, Namespace.NO_NAMESPACE, def); 1011 } 1012 1013 1024 public String getAttributeValue(String name, Namespace ns) { 1025 return getAttributeValue(name, ns, null); 1026 } 1027 1028 1040 public String getAttributeValue(String name, Namespace ns, String def) { 1041 Attribute attribute = (Attribute) attributes.get(name, ns); 1042 return (attribute == null) ? def : attribute.getValue(); 1043 } 1044 1045 1088 public Element setAttributes(List newAttributes) { 1089 attributes.clearAndSet(newAttributes); 1090 return this; 1091 } 1092 1093 1108 public Element setAttribute(String name, String value) { 1109 return setAttribute(new Attribute(name, value)); 1110 } 1111 1112 1131 public Element setAttribute(String name, String value, Namespace ns) { 1132 return setAttribute(new Attribute(name, value, ns)); 1133 } 1134 1135 1147 public Element setAttribute(Attribute attribute) { 1148 attributes.add(attribute); 1149 return this; 1150 } 1151 1152 1161 public boolean removeAttribute(String name) { 1162 return removeAttribute(name, Namespace.NO_NAMESPACE); 1163 } 1164 1165 1176 public boolean removeAttribute(String name, Namespace ns) { 1177 return attributes.remove(name, ns); 1178 } 1179 1180 1188 public boolean removeAttribute(Attribute attribute) { 1189 return attributes.remove(attribute); 1190 } 1191 1192 1204 public String toString() { 1205 StringBuffer stringForm = new StringBuffer (64) 1206 .append("[Element: <") 1207 .append(getQualifiedName()); 1208 1209 String nsuri = getNamespaceURI(); 1210 if (!nsuri.equals("")) { 1211 stringForm 1212 .append(" [Namespace: ") 1213 .append(nsuri) 1214 .append("]"); 1215 } 1216 stringForm.append("/>]"); 1217 1218 return stringForm.toString(); 1219 } 1220 1221 1230 public Object clone() { 1231 1232 1234 final Element element = (Element) super.clone(); 1235 1236 1239 1245 element.content = new ContentList(element); 1248 element.attributes = new AttributeList(element); 1249 1250 if (attributes != null) { 1252 for(int i = 0; i < attributes.size(); i++) { 1253 final Attribute attribute = (Attribute) attributes.get(i); 1254 element.attributes.add(attribute.clone()); 1255 } 1256 } 1257 1258 if (additionalNamespaces != null) { 1260 element.additionalNamespaces = new ArrayList(additionalNamespaces); 1261 } 1262 1263 if (content != null) { 1265 for(int i = 0; i < content.size(); i++) { 1266 final Content c = (Content) content.get(i); 1267 element.content.add(c.clone()); 1268 } 1269 } 1270 1271 return element; 1272 } 1273 1274 1275 private void writeObject(ObjectOutputStream out) throws IOException { 1278 1279 out.defaultWriteObject(); 1280 1281 out.writeObject(namespace.getPrefix()); 1284 out.writeObject(namespace.getURI()); 1285 1286 if (additionalNamespaces == null) { 1287 out.write(0); 1288 } 1289 else { 1290 int size = additionalNamespaces.size(); 1291 out.write(size); 1292 for (int i = 0; i < size; i++) { 1293 Namespace additional = (Namespace) additionalNamespaces.get(i); 1294 out.writeObject(additional.getPrefix()); 1295 out.writeObject(additional.getURI()); 1296 } 1297 } 1298 } 1299 1300 private void readObject(ObjectInputStream in) 1301 throws IOException, ClassNotFoundException { 1302 1303 in.defaultReadObject(); 1304 1305 namespace = Namespace.getNamespace( 1306 (String )in.readObject(), (String )in.readObject()); 1307 1308 int size = in.read(); 1309 1310 if (size != 0) { 1311 additionalNamespaces = new ArrayList(size); 1312 for (int i = 0; i < size; i++) { 1313 Namespace additional = Namespace.getNamespace( 1314 (String )in.readObject(), (String )in.readObject()); 1315 additionalNamespaces.add(additional); 1316 } 1317 } 1318 } 1319 1320 1325 public Iterator getDescendants() { 1326 return new DescendantIterator(this); 1327 } 1328 1329 1338 public Iterator getDescendants(Filter filter) { 1339 return new FilterIterator(new DescendantIterator(this), filter); 1340 } 1341 1342 1343 1344 1375 public List getChildren() { 1376 return content.getView(new ElementFilter()); 1377 } 1378 1379 1395 public List getChildren(String name) { 1396 return getChildren(name, Namespace.NO_NAMESPACE); 1397 } 1398 1399 1416 public List getChildren(String name, Namespace ns) { 1417 return content.getView(new ElementFilter(name, ns)); 1418 } 1419 1420 1430 public Element getChild(String name, Namespace ns) { 1431 List elements = content.getView(new ElementFilter(name, ns)); 1432 Iterator i = elements.iterator(); 1433 if (i.hasNext()) { 1434 return (Element) i.next(); 1435 } 1436 return null; 1437 } 1438 1439 1448 public Element getChild(String name) { 1449 return getChild(name, Namespace.NO_NAMESPACE); 1450 } 1451 1452 1462 public boolean removeChild(String name) { 1463 return removeChild(name, Namespace.NO_NAMESPACE); 1464 } 1465 1466 1477 public boolean removeChild(String name, Namespace ns) { 1478 List old = content.getView(new ElementFilter(name, ns)); 1479 Iterator i = old.iterator(); 1480 if (i.hasNext()) { 1481 i.next(); 1482 i.remove(); 1483 return true; 1484 } 1485 1486 return false; 1487 } 1488 1489 1499 public boolean removeChildren(String name) { 1500 return removeChildren(name, Namespace.NO_NAMESPACE); 1501 } 1502 1503 1514 public boolean removeChildren(String name, Namespace ns) { 1515 boolean deletedSome = false; 1516 1517 List old = content.getView(new ElementFilter(name, ns)); 1518 Iterator i = old.iterator(); 1519 while (i.hasNext()) { 1520 i.next(); 1521 i.remove(); 1522 deletedSome = true; 1523 } 1524 1525 return deletedSome; 1526 } 1527 1528} 1529 | Popular Tags |