Clover coverage report - dom4j - 1.5
Coverage timestamp: vr sep 3 2004 20:47:03 GMT+01:00
file stats: LOC: 1.836   Methods: 49
NCLOC: 768   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DefaultElement.java 59,5% 68,3% 75,5% 65,4%
coverage coverage
 1    /*
 2    * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
 3    *
 4    * This software is open source.
 5    * See the bottom of this file for the licence.
 6    *
 7    * $Id: DefaultElement.java,v 1.56 2004/08/04 18:22:39 maartenc Exp $
 8    */
 9   
 10    package org.dom4j.tree;
 11   
 12    import java.util.ArrayList;
 13    import java.util.Iterator;
 14    import java.util.List;
 15   
 16    import org.dom4j.Attribute;
 17    import org.dom4j.Branch;
 18    import org.dom4j.Document;
 19    import org.dom4j.DocumentFactory;
 20    import org.dom4j.Element;
 21    import org.dom4j.IllegalAddException;
 22    import org.dom4j.Namespace;
 23    import org.dom4j.Node;
 24    import org.dom4j.ProcessingInstruction;
 25    import org.dom4j.QName;
 26   
 27    /**
 28    * <p>
 29    * <code>DefaultElement</code> is the default DOM4J default implementation of
 30    * an XML element.
 31    * </p>
 32    *
 33    * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
 34    * @version $Revision: 1.56 $
 35    */
 36   
 37    public class DefaultElement extends AbstractElement {
 38   
 39    /** The <code>DocumentFactory</code> instance used by default */
 40   
 41    private static transient final DocumentFactory DOCUMENT_FACTORY = DocumentFactory
 42    .getInstance();
 43   
 44    /** The <code>QName</code> for this element */
 45   
 46    private QName qname;
 47   
 48    /**
 49    * Stores the parent branch of this node which is either a Document if this
 50    * element is the root element in a document, or another Element if it is a
 51    * child of the root document, or null if it has not been added to a
 52    * document yet.
 53    */
 54   
 55    private Branch parentBranch;
 56   
 57    /**
 58    * Stores null for no content, a Node for a single content node or a List
 59    * for multiple content nodes. The List will be lazily constructed when
 60    * required.
 61    */
 62   
 63    private Object content;
 64   
 65    /** Lazily constructes list of attributes */
 66   
 67    private Object attributes;
 68   
 69  2 public DefaultElement(String name) {
 70   
 71  2 this.qname = DOCUMENT_FACTORY.createQName(name);
 72   
 73    }
 74   
 75  184656 public DefaultElement(QName qname) {
 76   
 77  184656 this.qname = qname;
 78   
 79    }
 80   
 81  0 public DefaultElement(QName qname, int attributeCount) {
 82   
 83  0 this.qname = qname;
 84   
 85  0 if (attributeCount > 1) {
 86   
 87  0 this.attributes = new ArrayList(attributeCount);
 88   
 89    }
 90   
 91    }
 92   
 93  0 public DefaultElement(String name, Namespace namespace) {
 94   
 95  0 this.qname = DOCUMENT_FACTORY.createQName(name, namespace);
 96   
 97    }
 98   
 99  178386 public Element getParent() {
 100   
 101  178386 return (parentBranch instanceof Element) ? (Element) parentBranch
 102    : null;
 103   
 104    }
 105   
 106  182190 public void setParent(Element parent) {
 107   
 108  182190 if (parentBranch instanceof Element || parent != null) {
 109   
 110  179136 parentBranch = parent;
 111   
 112    }
 113   
 114    }
 115   
 116  12420 public Document getDocument() {
 117   
 118  12420 if (parentBranch instanceof Document) {
 119   
 120  270 return (Document) parentBranch;
 121   
 122    }
 123   
 124  12147 else if (parentBranch instanceof Element) {
 125   
 126  124 Element parent = (Element) parentBranch;
 127   
 128  124 return parent.getDocument();
 129   
 130    }
 131   
 132  12026 return null;
 133   
 134    }
 135   
 136  30354 public void setDocument(Document document) {
 137   
 138  30354 if (parentBranch instanceof Document || document != null) {
 139   
 140  24066 parentBranch = document;
 141   
 142    }
 143   
 144    }
 145   
 146  20 public boolean supportsParent() {
 147   
 148  20 return true;
 149   
 150    }
 151   
 152  1164630 public QName getQName() {
 153   
 154  1164630 return qname;
 155   
 156    }
 157   
 158  3004 public void setQName(QName qname) {
 159   
 160  3004 this.qname = qname;
 161   
 162    }
 163   
 164  372 public String getText() {
 165  372 final Object contentShadow = content;
 166  372 if (contentShadow instanceof List) {
 167   
 168  156 return super.getText();
 169   
 170    }
 171   
 172    else {
 173   
 174  216 if (contentShadow != null) {
 175   
 176  196 return getContentAsText(contentShadow);
 177   
 178    }
 179   
 180    else {
 181   
 182  20 return "";
 183   
 184    }
 185   
 186    }
 187   
 188    }
 189   
 190  2452 public String getStringValue() {
 191  2452 final Object contentShadow = content;
 192   
 193  2452 if (contentShadow instanceof List) {
 194   
 195  2322 List list = (List) contentShadow;
 196   
 197  2322 int size = list.size();
 198   
 199  2322 if (size > 0) {
 200   
 201  2322 if (size == 1) {
 202   
 203    // optimised to avoid StringBuffer creation
 204   
 205  2160 return getContentAsStringValue(list.get(0));
 206   
 207    }
 208   
 209    else {
 210   
 211  162 StringBuffer buffer = new StringBuffer();
 212   
 213  162 for (int i = 0; i < size; i++) {
 214   
 215  330 Object node = list.get(i);
 216   
 217  330 String string = getContentAsStringValue(node);
 218   
 219  330 if (string.length() > 0) {
 220   
 221  328 if (USE_STRINGVALUE_SEPARATOR) {
 222   
 223  0 if (buffer.length() > 0) {
 224   
 225  0 buffer.append(' ');
 226   
 227    }
 228   
 229    }
 230   
 231  328 buffer.append(string);
 232   
 233    }
 234   
 235    }
 236   
 237  162 return buffer.toString();
 238   
 239    }
 240   
 241    }
 242   
 243    }
 244   
 245    else {
 246   
 247  130 if (contentShadow != null) {
 248   
 249  130 return getContentAsStringValue(contentShadow);
 250   
 251    }
 252   
 253    }
 254   
 255  0 return "";
 256   
 257    }
 258   
 259  250 public Object clone() {
 260   
 261  250 DefaultElement answer = (DefaultElement) super.clone();
 262   
 263  250 if (answer != this) {
 264   
 265  250 answer.content = null;
 266   
 267  250 answer.attributes = null;
 268   
 269  250 answer.appendAttributes(this);
 270   
 271  250 answer.appendContent(this);
 272   
 273    }
 274   
 275  250 return answer;
 276   
 277    }
 278   
 279  15442 public Namespace getNamespaceForPrefix(String prefix) {
 280   
 281  15442 if (prefix == null) {
 282   
 283  18 prefix = "";
 284   
 285    }
 286   
 287  15442 if (prefix.equals(getNamespacePrefix())) {
 288   
 289  5354 return getNamespace();
 290   
 291    }
 292   
 293  10088 else if (prefix.equals("xml")) {
 294   
 295  0 return Namespace.XML_NAMESPACE;
 296   
 297    }
 298   
 299    else {
 300  10088 final Object contentShadow = content;
 301   
 302  10088 if (contentShadow instanceof List) {
 303   
 304  10082 List list = (List) contentShadow;
 305   
 306  10082 int size = list.size();
 307   
 308  10082 for (int i = 0; i < size; i++) {
 309   
 310  10208 Object object = list.get(i);
 311   
 312  10208 if (object instanceof Namespace) {
 313   
 314  10112 Namespace namespace = (Namespace) object;
 315   
 316  10112 if (prefix.equals(namespace.getPrefix())) {
 317   
 318  10046 return namespace;
 319   
 320    }
 321   
 322    }
 323   
 324    }
 325   
 326    }
 327   
 328  6 else if (contentShadow instanceof Namespace) {
 329   
 330  0 Namespace namespace = (Namespace) contentShadow;
 331   
 332  0 if (prefix.equals(namespace.getPrefix())) {
 333   
 334  0 return namespace;
 335   
 336    }
 337   
 338    }
 339   
 340    }
 341   
 342  42 Element parent = getParent();
 343   
 344  42 if (parent != null) {
 345   
 346  38 Namespace answer = parent.getNamespaceForPrefix(prefix);
 347   
 348  38 if (answer != null) {
 349   
 350  38 return answer;
 351   
 352    }
 353   
 354    }
 355   
 356  4 if (prefix == null || prefix.length() <= 0) {
 357   
 358  0 return Namespace.NO_NAMESPACE;
 359   
 360    }
 361   
 362  4 return null;
 363   
 364    }
 365   
 366  18 public Namespace getNamespaceForURI(String uri) {
 367   
 368  18 if (uri == null || uri.length() <= 0) {
 369   
 370  0 return Namespace.NO_NAMESPACE;
 371   
 372    }
 373   
 374  18 else if (uri.equals(getNamespaceURI())) {
 375   
 376  0 return getNamespace();
 377   
 378    }
 379   
 380    else {
 381  18 final Object contentShadow = content;
 382   
 383  18 if (contentShadow instanceof List) {
 384   
 385  18 List list = (List) contentShadow;
 386   
 387  18 int size = list.size();
 388   
 389  18 for (int i = 0; i < size; i++) {
 390   
 391  42 Object object = list.get(i);
 392   
 393  42 if (object instanceof Namespace) {
 394   
 395  24 Namespace namespace = (Namespace) object;
 396   
 397  24 if (uri.equals(namespace.getURI())) {
 398   
 399  12 return namespace;
 400   
 401    }
 402   
 403    }
 404   
 405    }
 406   
 407    }
 408   
 409  0 else if (contentShadow instanceof Namespace) {
 410   
 411  0 Namespace namespace = (Namespace) contentShadow;
 412   
 413  0 if (uri.equals(namespace.getURI())) {
 414   
 415  0 return namespace;
 416   
 417    }
 418   
 419    }
 420   
 421  6 Element parent = getParent();
 422   
 423  6 if (parent != null) {
 424   
 425  6 return parent.getNamespaceForURI(uri);
 426   
 427    }
 428   
 429  0 return null;
 430   
 431    }
 432   
 433    }
 434   
 435  19748 public List declaredNamespaces() {
 436   
 437  19748 BackedList answer = createResultList();
 438   
 439    // if (getNamespaceURI().length() > 0) {
 440    //
 441    // answer.addLocal(getNamespace());
 442    //
 443    // }
 444   
 445  19748 final Object contentShadow = content;
 446  19748 if (contentShadow instanceof List) {
 447   
 448  19748 List list = (List) contentShadow;
 449   
 450  19748 int size = list.size();
 451   
 452  19748 for (int i = 0; i < size; i++) {
 453   
 454  56264 Object object = list.get(i);
 455   
 456  56264 if (object instanceof Namespace) {
 457   
 458  256 answer.addLocal(object);
 459   
 460    }
 461   
 462    }
 463   
 464    }
 465   
 466    else {
 467   
 468  0 if (contentShadow instanceof Namespace) {
 469   
 470  0 answer.addLocal(contentShadow);
 471   
 472    }
 473   
 474    }
 475   
 476  19748 return answer;
 477   
 478    }
 479   
 480  30 public List additionalNamespaces() {
 481  30 final Object contentShadow = content;
 482   
 483  30 if (contentShadow instanceof List) {
 484   
 485  26 List list = (List) contentShadow;
 486   
 487  26 int size = list.size();
 488   
 489  26 BackedList answer = createResultList();
 490   
 491  26 for (int i = 0; i < size; i++) {
 492   
 493  178 Object object = list.get(i);
 494   
 495  178 if (object instanceof Namespace) {
 496   
 497  50 Namespace namespace = (Namespace) object;
 498   
 499  50 if (!namespace.equals(getNamespace())) {
 500   
 501  26 answer.addLocal(namespace);
 502    }
 503   
 504    }
 505   
 506    }
 507   
 508  26 return answer;
 509   
 510    }
 511   
 512    else {
 513   
 514  4 if (contentShadow instanceof Namespace) {
 515   
 516  2 Namespace namespace = (Namespace) contentShadow;
 517   
 518  2 if (namespace.equals(getNamespace())) {
 519   
 520  2 return createEmptyList();
 521   
 522    }
 523   
 524  0 return createSingleResultList(namespace);
 525   
 526    }
 527   
 528    else {
 529   
 530  2 return createEmptyList();
 531   
 532    }
 533   
 534    }
 535   
 536    }
 537   
 538  0 public List additionalNamespaces(String defaultNamespaceURI) {
 539   
 540  0 final Object contentShadow = content;
 541  0 if (contentShadow instanceof List) {
 542   
 543  0 List list = (List) contentShadow;
 544   
 545  0 BackedList answer = createResultList();
 546   
 547  0 int size = list.size();
 548   
 549  0 for (int i = 0; i < size; i++) {
 550   
 551  0 Object object = list.get(i);
 552   
 553  0 if (object instanceof Namespace) {
 554   
 555  0 Namespace namespace = (Namespace) object;
 556   
 557  0 if (!defaultNamespaceURI.equals(namespace.getURI())) {
 558   
 559  0 answer.addLocal(namespace);
 560   
 561    }
 562   
 563    }
 564   
 565    }
 566   
 567  0 return answer;
 568   
 569    }
 570   
 571    else {
 572   
 573  0 if (contentShadow instanceof Namespace) {
 574   
 575  0 Namespace namespace = (Namespace) contentShadow;
 576   
 577  0 if (!defaultNamespaceURI.equals(namespace.getURI())) {
 578   
 579  0 return createSingleResultList(namespace);
 580   
 581    }
 582   
 583    }
 584   
 585    }
 586   
 587  0 return createEmptyList();
 588   
 589    }
 590   
 591    // Processing instruction API
 592   
 593  0 public List processingInstructions() {
 594   
 595  0 final Object contentShadow = content;
 596  0 if (contentShadow instanceof List) {
 597   
 598  0 List list = (List) contentShadow;
 599   
 600  0 BackedList answer = createResultList();
 601   
 602  0 int size = list.size();
 603   
 604  0 for (int i = 0; i < size; i++) {
 605   
 606  0 Object object = list.get(i);
 607   
 608  0 if (object instanceof ProcessingInstruction) {
 609   
 610  0 answer.addLocal(object);
 611   
 612    }
 613   
 614    }
 615   
 616  0 return answer;
 617   
 618    }
 619   
 620    else {
 621   
 622  0 if (contentShadow instanceof ProcessingInstruction) {
 623   
 624  0 return createSingleResultList(contentShadow);
 625   
 626    }
 627   
 628  0 return createEmptyList();
 629   
 630    }
 631   
 632    }
 633   
 634  0 public List processingInstructions(String target) {
 635   
 636  0 final Object contentShadow = content;
 637  0 if (contentShadow instanceof List) {
 638   
 639  0 List list = (List) contentShadow;
 640   
 641  0 BackedList answer = createResultList();
 642   
 643  0 int size = list.size();
 644   
 645  0 for (int i = 0; i < size; i++) {
 646   
 647  0 Object object = list.get(i);
 648   
 649  0 if (object instanceof ProcessingInstruction) {
 650   
 651  0 ProcessingInstruction pi = (ProcessingInstruction) object;
 652   
 653  0 if (target.equals(pi.getName())) {
 654   
 655  0 answer.addLocal(pi);
 656   
 657    }
 658   
 659    }
 660   
 661    }
 662   
 663  0 return answer;
 664   
 665    }
 666   
 667    else {
 668   
 669  0 if (contentShadow instanceof ProcessingInstruction) {
 670   
 671  0 ProcessingInstruction pi = (ProcessingInstruction) contentShadow;
 672   
 673  0 if (target.equals(pi.getName())) {
 674   
 675  0 return createSingleResultList(pi);
 676   
 677    }
 678   
 679    }
 680   
 681  0 return createEmptyList();
 682   
 683    }
 684   
 685    }
 686   
 687  0 public ProcessingInstruction processingInstruction(String target) {
 688  0 final Object contentShadow = content;
 689   
 690  0 if (contentShadow instanceof List) {
 691   
 692  0 List list = (List) contentShadow;
 693   
 694  0 int size = list.size();
 695   
 696  0 for (int i = 0; i < size; i++) {
 697   
 698  0 Object object = list.get(i);
 699   
 700  0 if (object instanceof ProcessingInstruction) {
 701   
 702  0 ProcessingInstruction pi = (ProcessingInstruction) object;
 703   
 704  0 if (target.equals(pi.getName())) {
 705   
 706  0 return pi;
 707   
 708    }
 709   
 710    }
 711   
 712    }
 713   
 714    }
 715   
 716    else {
 717   
 718  0 if (contentShadow instanceof ProcessingInstruction) {
 719   
 720  0 ProcessingInstruction pi = (ProcessingInstruction) contentShadow;
 721   
 722  0 if (target.equals(pi.getName())) {
 723   
 724  0 return pi;
 725   
 726    }
 727   
 728    }
 729   
 730    }
 731   
 732  0 return null;
 733   
 734    }
 735   
 736  0 public boolean removeProcessingInstruction(String target) {
 737  0 final Object contentShadow = content;
 738   
 739  0 if (contentShadow instanceof List) {
 740   
 741  0 List list = (List) contentShadow;
 742   
 743  0 for (Iterator iter = list.iterator(); iter.hasNext();) {
 744   
 745  0 Object object = iter.next();
 746   
 747  0 if (object instanceof ProcessingInstruction) {
 748   
 749  0 ProcessingInstruction pi = (ProcessingInstruction) object;
 750   
 751  0 if (target.equals(pi.getName())) {
 752   
 753  0 iter.remove();
 754   
 755  0 return true;
 756   
 757    }
 758   
 759    }
 760   
 761    }
 762   
 763    }
 764   
 765    else {
 766   
 767  0 if (contentShadow instanceof ProcessingInstruction) {
 768   
 769  0 ProcessingInstruction pi = (ProcessingInstruction) contentShadow;
 770   
 771  0 if (target.equals(pi.getName())) {
 772   
 773  0 this.content = null;
 774   
 775  0 return true;
 776   
 777    }
 778   
 779    }
 780   
 781    }
 782   
 783  0 return false;
 784   
 785    }
 786   
 787  250 public Element element(String name) {
 788  250 final Object contentShadow = content;
 789   
 790  250 if (contentShadow instanceof List) {
 791   
 792  62 List list = (List) contentShadow;
 793   
 794  62 int size = list.size();
 795   
 796  168 for (int i = 0; i < size; i++) {
 797   
 798  168 Object object = list.get(i);
 799   
 800  168 if (object instanceof Element) {
 801   
 802  74 Element element = (Element) object;
 803   
 804  74 if (name.equals(element.getName())) {
 805   
 806  62 return element;
 807   
 808    }
 809   
 810    }
 811   
 812    }
 813   
 814    }
 815   
 816    else {
 817   
 818  188 if (contentShadow instanceof Element) {
 819   
 820  8 Element element = (Element) contentShadow;
 821   
 822  8 if (name.equals(element.getName())) {
 823   
 824  8 return element;
 825   
 826    }
 827   
 828    }
 829   
 830    }
 831   
 832  180 return null;
 833   
 834    }
 835   
 836  5930 public Element element(QName qName) {
 837  5930 final Object contentShadow = content;
 838   
 839  5930 if (contentShadow instanceof List) {
 840   
 841  5410 List list = (List) contentShadow;
 842   
 843  5410 int size = list.size();
 844   
 845  5410 for (int i = 0; i < size; i++) {
 846   
 847  42520 Object object = list.get(i);
 848   
 849  42520 if (object instanceof Element) {
 850   
 851  23360 Element element = (Element) object;
 852   
 853  23360 if (qName.equals(element.getQName())) {
 854   
 855  4580 return element;
 856   
 857    }
 858   
 859    }
 860   
 861    }
 862   
 863    }
 864   
 865    else {
 866   
 867  520 if (contentShadow instanceof Element) {
 868   
 869  2 Element element = (Element) contentShadow;
 870   
 871  2 if (qName.equals(element.getQName())) {
 872   
 873  2 return element;
 874   
 875    }
 876   
 877    }
 878   
 879    }
 880   
 881  1348 return null;
 882   
 883    }
 884   
 885  0 public Element element(String name, Namespace namespace) {
 886   
 887  0 return element(getDocumentFactory().createQName(name, namespace));
 888   
 889    }
 890   
 891    // public List elements() {
 892    // final Object contentShadow = content;
 893    //
 894    // if (contentShadow instanceof List) {
 895    //
 896    // List list = (List) contentShadow;
 897    //
 898    // BackedList answer = createResultList();
 899    //
 900    // int size = list.size();
 901    //
 902    // for (int i = 0; i < size; i++) {
 903    //
 904    // Object object = list.get(i);
 905    //
 906    // if (object instanceof Element) {
 907    //
 908    // answer.addLocal(object);
 909    //
 910    // }
 911    //
 912    // }
 913    //
 914    // return answer;
 915    //
 916    // }
 917    //
 918    // else {
 919    //
 920    // if (contentShadow instanceof Element) {
 921    //
 922    // Element element = (Element) contentShadow;
 923    //
 924    // return createSingleResultList(element);
 925    //
 926    // }
 927    //
 928    // return createEmptyList();
 929    //
 930    // }
 931    //
 932    // }
 933    //
 934    // public List elements(String name) {
 935    // final Object contentShadow = content;
 936    //
 937    // if (contentShadow instanceof List) {
 938    //
 939    // List list = (List) contentShadow;
 940    //
 941    // BackedList answer = createResultList();
 942    //
 943    // int size = list.size();
 944    //
 945    // for (int i = 0; i < size; i++) {
 946    //
 947    // Object object = list.get(i);
 948    //
 949    // if (object instanceof Element) {
 950    //
 951    // Element element = (Element) object;
 952    //
 953    // if (name.equals(element.getName())) {
 954    //
 955    // answer.addLocal(element);
 956    //
 957    // }
 958    //
 959    // }
 960    //
 961    // }
 962    //
 963    // return answer;
 964    //
 965    // }
 966    //
 967    // else {
 968    //
 969    // if (contentShadow instanceof Element) {
 970    //
 971    // Element element = (Element) contentShadow;
 972    //
 973    // if (name.equals(element.getName())) {
 974    //
 975    // return createSingleResultList(element);
 976    //
 977    // }
 978    //
 979    // }
 980    //
 981    // return createEmptyList();
 982    //
 983    // }
 984    //
 985    // }
 986    //
 987    // public List elements(QName qName) {
 988    // final Object contentShadow = content;
 989    //
 990    // if (contentShadow instanceof List) {
 991    //
 992    // List list = (List) contentShadow;
 993    //
 994    // BackedList answer = createResultList();
 995    //
 996    // int size = list.size();
 997    //
 998    // for (int i = 0; i < size; i++) {
 999    //
 1000    // Object object = list.get(i);
 1001    //
 1002    // if (object instanceof Element) {
 1003    //
 1004    // Element element = (Element) object;
 1005    //
 1006    // if (qName.equals(element.getQName())) {
 1007    //
 1008    // answer.addLocal(element);
 1009    //
 1010    // }
 1011    //
 1012    // }
 1013    //
 1014    // }
 1015    //
 1016    // return answer;
 1017    //
 1018    // }
 1019    //
 1020    // else {
 1021    //
 1022    // if (contentShadow instanceof Element) {
 1023    //
 1024    // Element element = (Element) contentShadow;
 1025    //
 1026    // if (qName.equals(element.getQName())) {
 1027    //
 1028    // return createSingleResultList(element);
 1029    //
 1030    // }
 1031    //
 1032    // }
 1033    //
 1034    // return createEmptyList();
 1035    //
 1036    // }
 1037    //
 1038    // }
 1039    //
 1040    // public List elements(String name, Namespace namespace) {
 1041    //
 1042    // return elements(getDocumentFactory().createQName(name, namespace));
 1043    //
 1044    // }
 1045    //
 1046    // public Iterator elementIterator() {
 1047    // final Object contentShadow = content;
 1048    //
 1049    // if (contentShadow instanceof List) {
 1050    //
 1051    // List list = (List) contentShadow;
 1052    //
 1053    // return new ElementIterator(list.iterator());
 1054    //
 1055    // }
 1056    //
 1057    // else {
 1058    //
 1059    // if (contentShadow instanceof Element) {
 1060    //
 1061    // Element element = (Element) contentShadow;
 1062    //
 1063    // return createSingleIterator(element);
 1064    //
 1065    // }
 1066    //
 1067    // return EMPTY_ITERATOR;
 1068    //
 1069    // }
 1070    //
 1071    // }
 1072    //
 1073    // public Iterator elementIterator(String name) {
 1074    // final Object contentShadow = content;
 1075    //
 1076    // if (contentShadow instanceof List) {
 1077    //
 1078    // List list = (List) contentShadow;
 1079    //
 1080    // return new ElementNameIterator(list.iterator(), name);
 1081    //
 1082    // }
 1083    //
 1084    // else {
 1085    //
 1086    // if (contentShadow instanceof Element) {
 1087    //
 1088    // Element element = (Element) contentShadow;
 1089    //
 1090    // if (name.equals(element.getName())) {
 1091    //
 1092    // return createSingleIterator(element);
 1093    //
 1094    // }
 1095    //
 1096    // }
 1097    //
 1098    // return EMPTY_ITERATOR;
 1099    //
 1100    // }
 1101    //
 1102    // }
 1103    //
 1104    // public Iterator elementIterator(QName qName) {
 1105    // final Object contentShadow = content;
 1106    //
 1107    // if (contentShadow instanceof List) {
 1108    //
 1109    // List list = (List) contentShadow;
 1110    //
 1111    // return new ElementQNameIterator(list.iterator(), qName);
 1112    //
 1113    // }
 1114    //
 1115    // else {
 1116    //
 1117    // if (contentShadow instanceof Element) {
 1118    //
 1119    // Element element = (Element) contentShadow;
 1120    //
 1121    // if (qName.equals(element.getQName())) {
 1122    //
 1123    // return createSingleIterator(element);
 1124    //
 1125    // }
 1126    //
 1127    // }
 1128    //
 1129    // return EMPTY_ITERATOR;
 1130    //
 1131    // }
 1132    //
 1133    // }
 1134    //
 1135    // public Iterator elementIterator(String name, Namespace namespace) {
 1136    //
 1137    // return elementIterator(getDocumentFactory().createQName(name,
 1138    // namespace));
 1139    //
 1140    // }
 1141    //
 1142  2 public void setContent(List content) {
 1143   
 1144  2 if (content instanceof ContentListFacade) {
 1145   
 1146  2 content = ((ContentListFacade) content).getBackingList();
 1147   
 1148    }
 1149   
 1150  2 if (content == null) {
 1151   
 1152  0 this.content = null;
 1153   
 1154    }
 1155   
 1156    else {
 1157   
 1158  2 int size = content.size();
 1159   
 1160  2 List newContent = createContentList(size);
 1161   
 1162  2 for (int i = 0; i < size; i++) {
 1163   
 1164  4 Object object = content.get(i);
 1165   
 1166  4 if (object instanceof Node) {
 1167   
 1168  4 Node node = (Node) object;
 1169   
 1170  4 Element parent = node.getParent();
 1171   
 1172  4 if (parent != null && parent != this) {
 1173   
 1174  4 node = (Node) node.clone();
 1175   
 1176    }
 1177   
 1178  4 newContent.add(node);
 1179   
 1180  4 childAdded(node);
 1181   
 1182    }
 1183   
 1184  0 else if (object != null) {
 1185   
 1186  0 String text = object.toString();
 1187   
 1188  0 Node node = getDocumentFactory().createText(text);
 1189   
 1190  0 newContent.add(node);
 1191   
 1192  0 childAdded(node);
 1193   
 1194    }
 1195   
 1196    }
 1197   
 1198  2 contentRemoved();
 1199   
 1200  2 this.content = newContent;
 1201   
 1202    }
 1203   
 1204    }
 1205   
 1206  0 public void clearContent() {
 1207   
 1208  0 if (content != null) {
 1209   
 1210  0 contentRemoved();
 1211   
 1212  0 content = null;
 1213   
 1214    }
 1215   
 1216    }
 1217   
 1218  624566 public Node node(int index) {
 1219   
 1220  624566 if (index >= 0) {
 1221   
 1222  624566 final Object contentShadow = content;
 1223  624566 Object node;
 1224   
 1225  624566 if (contentShadow instanceof List) {
 1226   
 1227  576654 List list = (List) contentShadow;
 1228   
 1229  576654 if (index >= list.size()) {
 1230   
 1231  0 return null;
 1232   
 1233    }
 1234   
 1235  576654 node = list.get(index);
 1236   
 1237    } else {
 1238  47912 node = (index == 0) ? contentShadow : null;
 1239    }
 1240   
 1241  624566 if (node != null) {
 1242   
 1243  624566 if (node instanceof Node) {
 1244   
 1245  624566 return (Node) node;
 1246   
 1247    }
 1248   
 1249    else {
 1250   
 1251  0 return new DefaultText(node.toString());
 1252   
 1253    }
 1254   
 1255    }
 1256   
 1257    }
 1258   
 1259  0 return null;
 1260   
 1261    }
 1262   
 1263  0 public int indexOf(Node node) {
 1264  0 final Object contentShadow = content;
 1265   
 1266  0 if (contentShadow instanceof List) {
 1267   
 1268  0 List list = (List) contentShadow;
 1269   
 1270  0 return list.indexOf(node);
 1271   
 1272    }
 1273   
 1274    else {
 1275   
 1276  0 return (contentShadow != null && contentShadow.equals(node)) ? 0
 1277    : -1;
 1278   
 1279    }
 1280   
 1281    }
 1282   
 1283  223300 public int nodeCount() {
 1284  223300 final Object contentShadow = content;
 1285   
 1286  223300 if (contentShadow instanceof List) {
 1287   
 1288  175092 List list = (List) contentShadow;
 1289   
 1290  175092 return list.size();
 1291   
 1292    }
 1293   
 1294    else {
 1295   
 1296  48208 return (contentShadow != null) ? 1 : 0;
 1297   
 1298    }
 1299   
 1300    }
 1301   
 1302  180980 public Iterator nodeIterator() {
 1303  180980 final Object contentShadow = content;
 1304   
 1305  180980 if (contentShadow instanceof List) {
 1306   
 1307  56302 List list = (List) contentShadow;
 1308   
 1309  56302 return list.iterator();
 1310   
 1311    }
 1312   
 1313    else {
 1314   
 1315  124678 if (contentShadow != null) {
 1316   
 1317  123942 return createSingleIterator(contentShadow);
 1318   
 1319    }
 1320   
 1321    else {
 1322   
 1323  736 return EMPTY_ITERATOR;
 1324   
 1325    }
 1326   
 1327    }
 1328   
 1329    }
 1330   
 1331  10 public List attributes() {
 1332   
 1333  10 return new ContentListFacade(this, attributeList());
 1334   
 1335    }
 1336   
 1337  0 public void setAttributes(List attributes) {
 1338   
 1339  0 if (attributes instanceof ContentListFacade) {
 1340   
 1341  0 attributes = ((ContentListFacade) attributes).getBackingList();
 1342   
 1343    }
 1344  0 this.attributes = attributes;
 1345   
 1346    }
 1347   
 1348  10060 public Iterator attributeIterator() {
 1349  10060 final Object attributesShadow = this.attributes;
 1350  10060 if (attributesShadow instanceof List) {
 1351   
 1352  5192 List list = (List) attributesShadow;
 1353   
 1354  5192 return list.iterator();
 1355   
 1356    }
 1357   
 1358  4868 else if (attributesShadow != null) {
 1359   
 1360  1996 return createSingleIterator(attributesShadow);
 1361   
 1362    }
 1363   
 1364    else {
 1365   
 1366  2872 return EMPTY_ITERATOR;
 1367   
 1368    }
 1369   
 1370    }
 1371   
 1372  41714 public Attribute attribute(int index) {
 1373  41714 final Object attributesShadow = this.attributes;
 1374  41714 if (attributesShadow instanceof List) {
 1375   
 1376  37320 List list = (List) attributesShadow;
 1377   
 1378  37320 return (Attribute) list.get(index);
 1379   
 1380    }
 1381   
 1382  4394 else if (attributesShadow != null && index == 0) {
 1383   
 1384  4394 return (Attribute) attributesShadow;
 1385   
 1386    }
 1387   
 1388    else {
 1389   
 1390  0 return null;
 1391   
 1392    }
 1393   
 1394    }
 1395   
 1396  197950 public int attributeCount() {
 1397  197950 final Object attributesShadow = this.attributes;
 1398   
 1399  197950 if (attributesShadow instanceof List) {
 1400   
 1401  112358 List list = (List) attributesShadow;
 1402   
 1403  112358 return list.size();
 1404   
 1405    }
 1406   
 1407    else {
 1408   
 1409  85592 return (attributesShadow != null) ? 1 : 0;
 1410   
 1411    }
 1412   
 1413    }
 1414   
 1415  5648 public Attribute attribute(String name) {
 1416  5648 final Object attributesShadow = this.attributes;
 1417   
 1418  5648 if (attributesShadow instanceof List) {
 1419   
 1420  2402 List list = (List) attributesShadow;
 1421   
 1422  2402 int size = list.size();
 1423   
 1424  2402 for (int i = 0; i < size; i++) {
 1425   
 1426  4198 Attribute attribute = (Attribute) list.get(i);
 1427   
 1428  4198 if (name.equals(attribute.getName())) {
 1429   
 1430  1948 return attribute;
 1431   
 1432    }
 1433   
 1434    }
 1435   
 1436    }
 1437   
 1438  3246 else if (attributesShadow != null) {
 1439   
 1440  2430 Attribute attribute = (Attribute) attributesShadow;
 1441   
 1442  2430 if (name.equals(attribute.getName())) {
 1443   
 1444  1174 return attribute;
 1445   
 1446    }
 1447   
 1448    }
 1449   
 1450  2526 return null;
 1451   
 1452    }
 1453   
 1454  37384 public Attribute attribute(QName qName) {
 1455  37384 final Object attributesShadow = this.attributes;
 1456   
 1457  37384 if (attributesShadow instanceof List) {
 1458   
 1459  31082 List list = (List) attributesShadow;
 1460   
 1461  31082 int size = list.size();
 1462   
 1463  31082 for (int i = 0; i < size; i++) {
 1464   
 1465  36454 Attribute attribute = (Attribute) list.get(i);
 1466   
 1467  36454 if (qName.equals(attribute.getQName())) {
 1468   
 1469  30782 return attribute;
 1470   
 1471    }
 1472   
 1473    }
 1474   
 1475    }
 1476   
 1477  6302 else if (attributesShadow != null) {
 1478   
 1479  1236 Attribute attribute = (Attribute) attributesShadow;
 1480   
 1481  1236 if (qName.equals(attribute.getQName())) {
 1482   
 1483  462 return attribute;
 1484   
 1485    }
 1486   
 1487    }
 1488   
 1489  6140 return null;
 1490   
 1491    }
 1492   
 1493  0 public Attribute attribute(String name, Namespace namespace) {
 1494   
 1495  0 return attribute(getDocumentFactory().createQName(name, namespace));
 1496   
 1497    }
 1498   
 1499  17578 public void add(Attribute attribute) {
 1500   
 1501  17578 if (attribute.getParent() != null) {
 1502   
 1503  0 String message = "The Attribute already has an existing parent \""
 1504    + attribute.getParent().getQualifiedName() + "\"";
 1505   
 1506  0 throw new IllegalAddException(this, attribute, message);
 1507   
 1508    }
 1509   
 1510  17578 if (attribute.getValue() == null) {
 1511   
 1512    // try remove a previous attribute with the same
 1513   
 1514    // name since adding an attribute with a null value
 1515   
 1516    // is equivalent to removing it.
 1517   
 1518  4 Attribute oldAttribute = attribute(attribute.getQName());
 1519   
 1520  4 if (oldAttribute != null) {
 1521   
 1522  2 remove(oldAttribute);
 1523   
 1524    }
 1525   
 1526    }
 1527   
 1528    else {
 1529   
 1530  17574 if (attributes == null) {
 1531   
 1532  15994 attributes = attribute;
 1533   
 1534    }
 1535   
 1536    else {
 1537   
 1538  1580 attributeList().add(attribute);
 1539   
 1540    }
 1541   
 1542  17574 childAdded(attribute);
 1543   
 1544    }
 1545   
 1546    }
 1547   
 1548  12 public boolean remove(Attribute attribute) {
 1549   
 1550  12 boolean answer = false;
 1551  12 final Object attributesShadow = this.attributes;
 1552   
 1553  12 if (attributesShadow instanceof List) {
 1554   
 1555  6 List list = (List) attributesShadow;
 1556   
 1557  6 answer = list.remove(attribute);
 1558   
 1559  6 if (!answer) {
 1560   
 1561    // we may have a copy of the attribute
 1562   
 1563  0 Attribute copy = attribute(attribute.getQName());
 1564   
 1565  0 if (copy != null) {
 1566   
 1567  0 list.remove(copy);
 1568   
 1569  0 answer = true;
 1570   
 1571    }
 1572   
 1573    }
 1574   
 1575    }
 1576   
 1577  6 else if (attributesShadow != null) {
 1578   
 1579  6 if (attribute.equals(attributesShadow)) {
 1580   
 1581  6 this.attributes = null;
 1582   
 1583  6 answer = true;
 1584   
 1585    }
 1586   
 1587    else {
 1588   
 1589    // we may have a copy of the attribute
 1590   
 1591  0 Attribute other = (Attribute) attributesShadow;
 1592   
 1593  0 if (attribute.getQName().equals(other.getQName())) {
 1594   
 1595  0 attributes = null;
 1596   
 1597  0 answer = true;
 1598   
 1599    }
 1600   
 1601    }
 1602   
 1603    }
 1604   
 1605  12 if (answer) {
 1606   
 1607  12 childRemoved(attribute);
 1608   
 1609    }
 1610   
 1611  12 return answer;
 1612   
 1613    }
 1614   
 1615    // Implementation methods
 1616   
 1617    //-------------------------------------------------------------------------
 1618   
 1619  402176 protected void addNewNode(Node node) {
 1620  402176 final Object contentShadow = content;
 1621   
 1622  402176 if (contentShadow == null) {
 1623   
 1624  176908 this.content = node;
 1625   
 1626    }
 1627   
 1628    else {
 1629   
 1630  225268 if (contentShadow instanceof List) {
 1631   
 1632  182988 List list = (List) contentShadow;
 1633   
 1634  182988 list.add(node);
 1635   
 1636    }
 1637   
 1638    else {
 1639   
 1640  42280 List list = createContentList();
 1641   
 1642  42280 list.add(contentShadow);
 1643   
 1644  42280 list.add(node);
 1645   
 1646  42280 this.content = list;
 1647   
 1648    }
 1649   
 1650    }
 1651   
 1652  402176 childAdded(node);
 1653   
 1654    }
 1655   
 1656  6734 protected boolean removeNode(Node node) {
 1657   
 1658  6734 boolean answer = false;
 1659  6734 final Object contentShadow = content;
 1660   
 1661  6734 if (contentShadow != null) {
 1662   
 1663  6734 if (contentShadow == node) {
 1664   
 1665  2 this.content = null;
 1666   
 1667  2 answer = true;
 1668   
 1669    }
 1670   
 1671  6732 else if (contentShadow instanceof List) {
 1672   
 1673  6732 List list = (List) contentShadow;
 1674   
 1675  6732 answer = list.remove(node);
 1676   
 1677    }
 1678   
 1679    }
 1680   
 1681  6734 if (answer) {
 1682   
 1683  6734 childRemoved(node);
 1684   
 1685    }
 1686   
 1687  6734 return answer;
 1688   
 1689    }
 1690   
 1691  167050 protected List contentList() {
 1692  167050 final Object contentShadow = content;
 1693   
 1694  167050 if (contentShadow instanceof List) {
 1695   
 1696  113296 return (List) contentShadow;
 1697   
 1698    }
 1699   
 1700    else {
 1701   
 1702  53754 List list = createContentList();
 1703   
 1704  53754 if (contentShadow != null) {
 1705   
 1706  47544 list.add(contentShadow);
 1707   
 1708    }
 1709   
 1710  53754 this.content = list;
 1711   
 1712  53754 return list;
 1713   
 1714    }
 1715   
 1716    }
 1717   
 1718  154526 protected List attributeList() {
 1719  154526 final Object attributesShadow = this.attributes;
 1720   
 1721  154526 if (attributesShadow instanceof List) {
 1722   
 1723  102444 return (List) attributesShadow;
 1724   
 1725    }
 1726   
 1727  52082 else if (attributesShadow != null) {
 1728   
 1729  15342 List list = createAttributeList();
 1730   
 1731  15342 list.add(attributesShadow);
 1732   
 1733  15342 this.attributes = list;
 1734   
 1735  15342 return list;
 1736   
 1737    }
 1738   
 1739    else {
 1740   
 1741  36740 List list = createAttributeList();
 1742   
 1743  36740 this.attributes = list;
 1744   
 1745  36740 return list;
 1746   
 1747    }
 1748   
 1749    }
 1750   
 1751  2992 protected List attributeList(int size) {
 1752  2992 final Object attributesShadow = this.attributes;
 1753  2992 if (attributesShadow instanceof List) {
 1754   
 1755  0 return (List) attributesShadow;
 1756   
 1757    }
 1758   
 1759  2992 else if (attributesShadow != null) {
 1760   
 1761  0 List list = createAttributeList(size);
 1762   
 1763  0 list.add(attributesShadow);
 1764   
 1765  0 this.attributes = list;
 1766   
 1767  0 return list;
 1768   
 1769    }
 1770   
 1771    else {
 1772   
 1773  2992 List list = createAttributeList(size);
 1774   
 1775  2992 this.attributes = list;
 1776   
 1777  2992 return list;
 1778   
 1779    }
 1780   
 1781    }
 1782   
 1783  4 protected void setAttributeList(List attributes) {
 1784   
 1785  4 this.attributes = attributes;
 1786   
 1787    }
 1788   
 1789  416858 protected DocumentFactory getDocumentFactory() {
 1790   
 1791  416858 DocumentFactory factory = qname.getDocumentFactory();
 1792   
 1793  416858 return (factory != null) ? factory : DOCUMENT_FACTORY;
 1794   
 1795    }
 1796   
 1797    }
 1798   
 1799    /*
 1800    * Redistribution and use of this software and associated documentation
 1801    * ("Software"), with or without modification, are permitted provided that the
 1802    * following conditions are met:
 1803    *
 1804    * 1. Redistributions of source code must retain copyright statements and
 1805    * notices. Redistributions must also contain a copy of this document.
 1806    *
 1807    * 2. Redistributions in binary form must reproduce the above copyright notice,
 1808    * this list of conditions and the following disclaimer in the documentation
 1809    * and/or other materials provided with the distribution.
 1810    *
 1811    * 3. The name "DOM4J" must not be used to endorse or promote products derived
 1812    * from this Software without prior written permission of MetaStuff, Ltd. For
 1813    * written permission, please contact dom4j-info@metastuff.com.
 1814    *
 1815    * 4. Products derived from this Software may not be called "DOM4J" nor may
 1816    * "DOM4J" appear in their names without prior written permission of MetaStuff,
 1817    * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
 1818    *
 1819    * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
 1820    *
 1821    * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
 1822    * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 1823    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 1824    * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
 1825    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 1826    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 1827    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 1828    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 1829    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 1830    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 1831    * POSSIBILITY OF SUCH DAMAGE.
 1832    *
 1833    * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
 1834    *
 1835    * $Id: DefaultElement.java,v 1.56 2004/08/04 18:22:39 maartenc Exp $
 1836    */