Clover coverage report - dom4j - 1.6.1
Coverage timestamp: ma mei 16 2005 14:23:01 GMT+01:00
file stats: LOC: 1.055   Methods: 49
NCLOC: 727   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DefaultElement.java 59,8% 68,3% 75,5% 65,5%
coverage coverage
 1    /*
 2    * Copyright 2001-2005 (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   
 8    package org.dom4j.tree;
 9   
 10    import java.util.ArrayList;
 11    import java.util.Iterator;
 12    import java.util.List;
 13   
 14    import org.dom4j.Attribute;
 15    import org.dom4j.Branch;
 16    import org.dom4j.Document;
 17    import org.dom4j.DocumentFactory;
 18    import org.dom4j.Element;
 19    import org.dom4j.IllegalAddException;
 20    import org.dom4j.Namespace;
 21    import org.dom4j.Node;
 22    import org.dom4j.ProcessingInstruction;
 23    import org.dom4j.QName;
 24   
 25    /**
 26    * <p>
 27    * <code>DefaultElement</code> is the default DOM4J default implementation of
 28    * an XML element.
 29    * </p>
 30    *
 31    * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
 32    * @version $Revision: 1.59 $
 33    */
 34    public class DefaultElement extends AbstractElement {
 35    /** The <code>DocumentFactory</code> instance used by default */
 36    private static final transient DocumentFactory DOCUMENT_FACTORY =
 37    DocumentFactory.getInstance();
 38   
 39    /** The <code>QName</code> for this element */
 40    private QName qname;
 41   
 42    /**
 43    * Stores the parent branch of this node which is either a Document if this
 44    * element is the root element in a document, or another Element if it is a
 45    * child of the root document, or null if it has not been added to a
 46    * document yet.
 47    */
 48    private Branch parentBranch;
 49   
 50    /**
 51    * Stores null for no content, a Node for a single content node or a List
 52    * for multiple content nodes. The List will be lazily constructed when
 53    * required.
 54    */
 55    private Object content;
 56   
 57    /** Lazily constructes list of attributes */
 58    private Object attributes;
 59   
 60  1 public DefaultElement(String name) {
 61  1 this.qname = DOCUMENT_FACTORY.createQName(name);
 62    }
 63   
 64  96007 public DefaultElement(QName qname) {
 65  96007 this.qname = qname;
 66    }
 67   
 68  0 public DefaultElement(QName qname, int attributeCount) {
 69  0 this.qname = qname;
 70   
 71  0 if (attributeCount > 1) {
 72  0 this.attributes = new ArrayList(attributeCount);
 73    }
 74    }
 75   
 76  0 public DefaultElement(String name, Namespace namespace) {
 77  0 this.qname = DOCUMENT_FACTORY.createQName(name, namespace);
 78    }
 79   
 80  131539 public Element getParent() {
 81  131539 Element result = null;
 82   
 83  131539 if (parentBranch instanceof Element) {
 84  35288 result = (Element) parentBranch;
 85    }
 86   
 87  131539 return result;
 88    }
 89   
 90  94507 public void setParent(Element parent) {
 91  94507 if (parentBranch instanceof Element || (parent != null)) {
 92  92978 parentBranch = parent;
 93    }
 94    }
 95   
 96  13010 public Document getDocument() {
 97  13010 if (parentBranch instanceof Document) {
 98  6597 return (Document) parentBranch;
 99  6413 } else if (parentBranch instanceof Element) {
 100  117 Element parent = (Element) parentBranch;
 101   
 102  117 return parent.getDocument();
 103    }
 104   
 105  6296 return null;
 106    }
 107   
 108  15752 public void setDocument(Document document) {
 109  15752 if (parentBranch instanceof Document || (document != null)) {
 110  12600 parentBranch = document;
 111    }
 112    }
 113   
 114  10 public boolean supportsParent() {
 115  10 return true;
 116    }
 117   
 118  619575 public QName getQName() {
 119  619575 return qname;
 120    }
 121   
 122  1502 public void setQName(QName name) {
 123  1502 this.qname = name;
 124    }
 125   
 126  183 public String getText() {
 127  183 final Object contentShadow = content;
 128   
 129  183 if (contentShadow instanceof List) {
 130  80 return super.getText();
 131    } else {
 132  103 if (contentShadow != null) {
 133  93 return getContentAsText(contentShadow);
 134    } else {
 135  10 return "";
 136    }
 137    }
 138    }
 139   
 140  8 public String getStringValue() {
 141  8 final Object contentShadow = content;
 142   
 143  8 if (contentShadow instanceof List) {
 144  6 List list = (List) contentShadow;
 145   
 146  6 int size = list.size();
 147   
 148  6 if (size > 0) {
 149  6 if (size == 1) {
 150    // optimised to avoid StringBuffer creation
 151  4 return getContentAsStringValue(list.get(0));
 152    } else {
 153  2 StringBuffer buffer = new StringBuffer();
 154   
 155  2 for (int i = 0; i < size; i++) {
 156  9 Object node = list.get(i);
 157   
 158  9 String string = getContentAsStringValue(node);
 159   
 160  9 if (string.length() > 0) {
 161  7 if (USE_STRINGVALUE_SEPARATOR) {
 162  0 if (buffer.length() > 0) {
 163  0 buffer.append(' ');
 164    }
 165    }
 166   
 167  7 buffer.append(string);
 168    }
 169    }
 170   
 171  2 return buffer.toString();
 172    }
 173    }
 174    } else {
 175  2 if (contentShadow != null) {
 176  0 return getContentAsStringValue(contentShadow);
 177    }
 178    }
 179   
 180  2 return "";
 181    }
 182   
 183  129 public Object clone() {
 184  129 DefaultElement answer = (DefaultElement) super.clone();
 185   
 186  129 if (answer != this) {
 187  129 answer.content = null;
 188   
 189  129 answer.attributes = null;
 190   
 191  129 answer.appendAttributes(this);
 192   
 193  129 answer.appendContent(this);
 194    }
 195   
 196  129 return answer;
 197    }
 198   
 199  8764 public Namespace getNamespaceForPrefix(String prefix) {
 200  8764 if (prefix == null) {
 201  9 prefix = "";
 202    }
 203   
 204  8764 if (prefix.equals(getNamespacePrefix())) {
 205  3721 return getNamespace();
 206  5043 } else if (prefix.equals("xml")) {
 207  0 return Namespace.XML_NAMESPACE;
 208    } else {
 209  5043 final Object contentShadow = content;
 210   
 211  5043 if (contentShadow instanceof List) {
 212  5040 List list = (List) contentShadow;
 213   
 214  5040 int size = list.size();
 215   
 216  5040 for (int i = 0; i < size; i++) {
 217  5100 Object object = list.get(i);
 218   
 219  5100 if (object instanceof Namespace) {
 220  5055 Namespace namespace = (Namespace) object;
 221   
 222  5055 if (prefix.equals(namespace.getPrefix())) {
 223  5023 return namespace;
 224    }
 225    }
 226    }
 227  3 } else if (contentShadow instanceof Namespace) {
 228  0 Namespace namespace = (Namespace) contentShadow;
 229   
 230  0 if (prefix.equals(namespace.getPrefix())) {
 231  0 return namespace;
 232    }
 233    }
 234    }
 235   
 236  20 Element parent = getParent();
 237   
 238  20 if (parent != null) {
 239  19 Namespace answer = parent.getNamespaceForPrefix(prefix);
 240   
 241  19 if (answer != null) {
 242  19 return answer;
 243    }
 244    }
 245   
 246  1 if ((prefix == null) || (prefix.length() <= 0)) {
 247  0 return Namespace.NO_NAMESPACE;
 248    }
 249   
 250  1 return null;
 251    }
 252   
 253  9 public Namespace getNamespaceForURI(String uri) {
 254  9 if ((uri == null) || (uri.length() <= 0)) {
 255  0 return Namespace.NO_NAMESPACE;
 256  9 } else if (uri.equals(getNamespaceURI())) {
 257  0 return getNamespace();
 258    } else {
 259  9 final Object contentShadow = content;
 260   
 261  9 if (contentShadow instanceof List) {
 262  9 List list = (List) contentShadow;
 263   
 264  9 int size = list.size();
 265   
 266  9 for (int i = 0; i < size; i++) {
 267  21 Object object = list.get(i);
 268   
 269  21 if (object instanceof Namespace) {
 270  12 Namespace namespace = (Namespace) object;
 271   
 272  12 if (uri.equals(namespace.getURI())) {
 273  6 return namespace;
 274    }
 275    }
 276    }
 277  0 } else if (contentShadow instanceof Namespace) {
 278  0 Namespace namespace = (Namespace) contentShadow;
 279   
 280  0 if (uri.equals(namespace.getURI())) {
 281  0 return namespace;
 282    }
 283    }
 284   
 285  3 Element parent = getParent();
 286   
 287  3 if (parent != null) {
 288  3 return parent.getNamespaceForURI(uri);
 289    }
 290   
 291  0 return null;
 292    }
 293    }
 294   
 295  9903 public List declaredNamespaces() {
 296  9903 BackedList answer = createResultList();
 297   
 298    // if (getNamespaceURI().length() > 0) {
 299    //
 300    // answer.addLocal(getNamespace());
 301    //
 302    // }
 303  9903 final Object contentShadow = content;
 304   
 305  9903 if (contentShadow instanceof List) {
 306  9903 List list = (List) contentShadow;
 307   
 308  9903 int size = list.size();
 309   
 310  9903 for (int i = 0; i < size; i++) {
 311  31929 Object object = list.get(i);
 312   
 313  31929 if (object instanceof Namespace) {
 314  821 answer.addLocal(object);
 315    }
 316    }
 317    } else {
 318  0 if (contentShadow instanceof Namespace) {
 319  0 answer.addLocal(contentShadow);
 320    }
 321    }
 322   
 323  9903 return answer;
 324    }
 325   
 326  15 public List additionalNamespaces() {
 327  15 final Object contentShadow = content;
 328   
 329  15 if (contentShadow instanceof List) {
 330  13 List list = (List) contentShadow;
 331   
 332  13 int size = list.size();
 333   
 334  13 BackedList answer = createResultList();
 335   
 336  13 for (int i = 0; i < size; i++) {
 337  89 Object object = list.get(i);
 338   
 339  89 if (object instanceof Namespace) {
 340  25 Namespace namespace = (Namespace) object;
 341   
 342  25 if (!namespace.equals(getNamespace())) {
 343  13 answer.addLocal(namespace);
 344    }
 345    }
 346    }
 347   
 348  13 return answer;
 349    } else {
 350  2 if (contentShadow instanceof Namespace) {
 351  1 Namespace namespace = (Namespace) contentShadow;
 352   
 353  1 if (namespace.equals(getNamespace())) {
 354  1 return createEmptyList();
 355    }
 356   
 357  0 return createSingleResultList(namespace);
 358    } else {
 359  1 return createEmptyList();
 360    }
 361    }
 362    }
 363   
 364  0 public List additionalNamespaces(String defaultNamespaceURI) {
 365  0 final Object contentShadow = content;
 366   
 367  0 if (contentShadow instanceof List) {
 368  0 List list = (List) contentShadow;
 369   
 370  0 BackedList answer = createResultList();
 371   
 372  0 int size = list.size();
 373   
 374  0 for (int i = 0; i < size; i++) {
 375  0 Object object = list.get(i);
 376   
 377  0 if (object instanceof Namespace) {
 378  0 Namespace namespace = (Namespace) object;
 379   
 380  0 if (!defaultNamespaceURI.equals(namespace.getURI())) {
 381  0 answer.addLocal(namespace);
 382    }
 383    }
 384    }
 385   
 386  0 return answer;
 387    } else {
 388  0 if (contentShadow instanceof Namespace) {
 389  0 Namespace namespace = (Namespace) contentShadow;
 390   
 391  0 if (!defaultNamespaceURI.equals(namespace.getURI())) {
 392  0 return createSingleResultList(namespace);
 393    }
 394    }
 395    }
 396   
 397  0 return createEmptyList();
 398    }
 399   
 400    // Processing instruction API
 401  0 public List processingInstructions() {
 402  0 final Object contentShadow = content;
 403   
 404  0 if (contentShadow instanceof List) {
 405  0 List list = (List) contentShadow;
 406   
 407  0 BackedList answer = createResultList();
 408   
 409  0 int size = list.size();
 410   
 411  0 for (int i = 0; i < size; i++) {
 412  0 Object object = list.get(i);
 413   
 414  0 if (object instanceof ProcessingInstruction) {
 415  0 answer.addLocal(object);
 416    }
 417    }
 418   
 419  0 return answer;
 420    } else {
 421  0 if (contentShadow instanceof ProcessingInstruction) {
 422  0 return createSingleResultList(contentShadow);
 423    }
 424   
 425  0 return createEmptyList();
 426    }
 427    }
 428   
 429  0 public List processingInstructions(String target) {
 430  0 final Object shadow = content;
 431   
 432  0 if (shadow instanceof List) {
 433  0 List list = (List) shadow;
 434   
 435  0 BackedList answer = createResultList();
 436   
 437  0 int size = list.size();
 438   
 439  0 for (int i = 0; i < size; i++) {
 440  0 Object object = list.get(i);
 441   
 442  0 if (object instanceof ProcessingInstruction) {
 443  0 ProcessingInstruction pi = (ProcessingInstruction) object;
 444   
 445  0 if (target.equals(pi.getName())) {
 446  0 answer.addLocal(pi);
 447    }
 448    }
 449    }
 450   
 451  0 return answer;
 452    } else {
 453  0 if (shadow instanceof ProcessingInstruction) {
 454  0 ProcessingInstruction pi = (ProcessingInstruction) shadow;
 455   
 456  0 if (target.equals(pi.getName())) {
 457  0 return createSingleResultList(pi);
 458    }
 459    }
 460   
 461  0 return createEmptyList();
 462    }
 463    }