Clover coverage report - dom4j - 1.6.1
Coverage timestamp: ma mei 16 2005 14:23:01 GMT+01:00
file stats: LOC: 1.684   Methods: 115
NCLOC: 1.027   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AbstractElement.java 38,2% 48,1% 53% 45,9%
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.io.IOException;
 11    import java.io.StringWriter;
 12    import java.io.Writer;
 13    import java.util.ArrayList;
 14    import java.util.Collections;
 15    import java.util.Iterator;
 16    import java.util.List;
 17    import java.util.Map;
 18   
 19    import org.dom4j.Attribute;
 20    import org.dom4j.CDATA;
 21    import org.dom4j.CharacterData;
 22    import org.dom4j.Comment;
 23    import org.dom4j.Document;
 24    import org.dom4j.DocumentFactory;
 25    import org.dom4j.Element;
 26    import org.dom4j.Entity;
 27    import org.dom4j.IllegalAddException;
 28    import org.dom4j.Namespace;
 29    import org.dom4j.Node;
 30    import org.dom4j.ProcessingInstruction;
 31    import org.dom4j.QName;
 32    import org.dom4j.Text;
 33    import org.dom4j.Visitor;
 34    import org.dom4j.io.OutputFormat;
 35    import org.dom4j.io.XMLWriter;
 36   
 37    import org.xml.sax.Attributes;
 38   
 39    /**
 40    * <p>
 41    * <code>AbstractElement</code> is an abstract base class for tree
 42    * implementors to use for implementation inheritence.
 43    * </p>
 44    *
 45    * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
 46    * @version $Revision: 1.80 $
 47    */
 48    public abstract class AbstractElement extends AbstractBranch implements
 49    org.dom4j.Element {
 50    /** The <code>DocumentFactory</code> instance used by default */
 51    private static final DocumentFactory DOCUMENT_FACTORY = DocumentFactory
 52    .getInstance();
 53   
 54    protected static final List EMPTY_LIST = Collections.EMPTY_LIST;
 55   
 56    protected static final Iterator EMPTY_ITERATOR = EMPTY_LIST.iterator();
 57   
 58    protected static final boolean VERBOSE_TOSTRING = false;
 59   
 60    protected static final boolean USE_STRINGVALUE_SEPARATOR = false;
 61   
 62  96009 public AbstractElement() {
 63    }
 64   
 65  105403 public short getNodeType() {
 66  105403 return ELEMENT_NODE;
 67    }
 68   
 69  0 public boolean isRootElement() {
 70  0 Document document = getDocument();
 71   
 72  0 if (document != null) {
 73  0 Element root = document.getRootElement();
 74   
 75  0 if (root == this) {
 76  0 return true;
 77    }
 78    }
 79   
 80  0 return false;
 81    }
 82   
 83  1 public void setName(String name) {
 84  1 setQName(getDocumentFactory().createQName(name));
 85    }
 86   
 87  0 public void setNamespace(Namespace namespace) {
 88  0 setQName(getDocumentFactory().createQName(getName(), namespace));
 89    }
 90   
 91    /**
 92    * Returns the XPath expression to match this Elements name which is
 93    * getQualifiedName() if there is a namespace prefix defined or if no
 94    * namespace is present then it is getName() or if a namespace is defined
 95    * with no prefix then the expression is [name()='X'] where X = getName().
 96    *
 97    * @return DOCUMENT ME!
 98    */
 99  66 public String getXPathNameStep() {
 100  66 String uri = getNamespaceURI();
 101   
 102  66 if ((uri == null) || (uri.length() == 0)) {
 103  56 return getName();
 104    }
 105   
 106  10 String prefix = getNamespacePrefix();
 107   
 108  10 if ((prefix == null) || (prefix.length() == 0)) {
 109  8 return "*[name()='" + getName() + "']";
 110    }
 111   
 112  2 return getQualifiedName();
 113    }
 114   
 115  31 public String getPath(Element context) {
 116  31 if (this == context) {
 117  1 return ".";
 118    }
 119   
 120  30 Element parent = getParent();
 121   
 122  30 if (parent == null) {
 123  11 return "/" + getXPathNameStep();
 124  19 } else if (parent == context) {
 125  7 return getXPathNameStep();
 126    }
 127   
 128  12 return parent.getPath(context) + "/" + getXPathNameStep();
 129    }
 130   
 131  36 public String getUniquePath(Element context) {
 132  36 Element parent = getParent();
 133   
 134  36 if (parent == null) {
 135  13 return "/" + getXPathNameStep();
 136    }
 137   
 138  23 StringBuffer buffer = new StringBuffer();
 139   
 140  23 if (parent != context) {
 141  16 buffer.append(parent.getUniquePath(context));
 142   
 143  16 buffer.append("/");
 144    }
 145   
 146  23 buffer.append(getXPathNameStep());
 147   
 148  23 List mySiblings = parent.elements(getQName());
 149   
 150  23 if (mySiblings.size() > 1) {
 151  13 int idx = mySiblings.indexOf(this);
 152   
 153  13 if (idx >= 0) {
 154  13 buffer.append("[");
 155   
 156  13 buffer.append(Integer.toString(++idx));
 157   
 158  13 buffer.append("]");
 159    }
 160    }
 161   
 162  23 return buffer.toString();
 163    }
 164   
 165  2041 public String asXML() {
 166  2041 try {
 167  2041 StringWriter out = new StringWriter();
 168  2041 XMLWriter writer = new XMLWriter(out, new OutputFormat());
 169   
 170  2041 writer.write(this);
 171  2041 writer.flush();
 172   
 173  2041 return out.toString();
 174    } catch (IOException e) {
 175  0 throw new RuntimeException("IOException while generating "
 176    + "textual representation: " + e.getMessage());
 177    }
 178    }
 179   
 180  0 public void write(Writer out) throws IOException {
 181  0 XMLWriter writer = new XMLWriter(out, new OutputFormat());
 182  0 writer.write(this);
 183    }
 184   
 185    /**
 186    * <p>
 187    * <code>accept</code> method is the <code>Visitor Pattern</code>
 188    * method.
 189    * </p>
 190    *
 191    * @param visitor
 192    * <code>Visitor</code> is the visitor.
 193    */
 194  0 public void accept(Visitor visitor) {
 195  0 visitor.visit(this);
 196   
 197    // visit attributes
 198  0 for (int i = 0, size = attributeCount(); i < size; i++) {
 199  0 Attribute attribute = attribute(i);
 200   
 201  0 visitor.visit(attribute);
 202    }
 203   
 204    // visit content
 205  0 for (int i = 0, size = nodeCount(); i < size; i++) {
 206  0 Node node = node(i);
 207   
 208  0 node.accept(visitor);
 209    }
 210    }
 211   
 212  85700 public String toString() {
 213  85700 String uri = getNamespaceURI();
 214   
 215  85700 if ((uri != null) && (uri.length() > 0)) {
 216  2512 if (VERBOSE_TOSTRING) {
 217  0 return super.toString() + " [Element: <" + getQualifiedName()
 218    + " uri: " + uri + " attributes: " + attributeList()
 219    + " content: " + contentList() + " />]";
 220    } else {
 221  2512 return super.toString() + " [Element: <" + getQualifiedName()
 222    + " uri: " + uri + " attributes: " + attributeList()
 223    + "/>]";
 224    }
 225    } else {
 226  83188 if (VERBOSE_TOSTRING) {
 227  0 return super.toString() + " [Element: <" + getQualifiedName()
 228    + " attributes: " + attributeList() + " content: "
 229    + contentList() + " />]";
 230    } else {
 231  83188 return super.toString() + " [Element: <" + getQualifiedName()
 232    + " attributes: " + attributeList() + "/>]";
 233    }
 234    }
 235    }
 236   
 237    // QName methods
 238    // -------------------------------------------------------------------------
 239  120477 public Namespace getNamespace() {
 240  120477 return getQName().getNamespace();
 241    }
 242   
 243  84347 public String getName() {
 244  84347 return getQName().getName();
 245    }
 246   
 247  8774 public String getNamespacePrefix() {
 248  8774 return getQName().getNamespacePrefix();
 249    }
 250   
 251  172894 public String getNamespaceURI() {
 252  172894 return getQName().getNamespaceURI();
 253    }
 254   
 255  120578 public String getQualifiedName() {
 256  120578 return getQName().getQualifiedName();
 257    }
 258   
 259  0 public Object getData() {
 260  0 return getText();
 261    }
 262   
 263  0 public void setData(Object data) {
 264    // ignore this method
 265    }
 266   
 267    // Node methods
 268    // -------------------------------------------------------------------------
 269  0 public Node node(int index) {
 270  0 if (index >= 0) {
 271  0 List list = contentList();
 272   
 273  0 if (index >= list.size()) {
 274  0 return null;
 275    }
 276   
 277  0 Object node = list.get(index);
 278   
 279  0 if (node != null) {
 280  0 if (node instanceof Node) {
 281  0 return (Node) node;
 282    } else {
 283  0 return getDocumentFactory().createText(node.toString());
 284    }
 285    }
 286    }
 287   
 288  0 return null;
 289    }
 290   
 291  0 public int indexOf(Node node) {
 292  0 return contentList().indexOf(node);
 293    }
 294   
 295  1 public int nodeCount() {
 296  1 return contentList().size();
 297    }
 298   
 299  0 public Iterator nodeIterator() {
 300  0 return contentList().iterator();
 301    }
 302   
 303    // Element methods
 304    // -------------------------------------------------------------------------
 305  0 public Element element(String name) {
 306  0 List list = contentList();
 307   
 308  0 int size = list.size();
 309   
 310  0 for (int i = 0; i < size; i++) {
 311  0 Object object = list.get(i);
 312   
 313  0 if (object instanceof Element) {
 314  0 Element element = (Element) object;
 315   
 316  0 if (name.equals(element.getName())) {
 317  0 return element;
 318    }
 319    }
 320    }
 321   
 322  0 return null;
 323    }
 324   
 325  0 public Element element(QName qName) {
 326  0 List list = contentList();
 327   
 328  0 int size = list.size();
 329   
 330  0 for (int i = 0; i < size; i++) {
 331  0 Object object = list.get(i);
 332   
 333  0 if (object instanceof Element) {
 334  0 Element element = (Element) object;
 335   
 336  0 if (qName.equals(element.getQName())) {
 337  0 return element;
 338    }
 339    }
 340    }
 341   
 342  0 return null;
 343    }
 344   
 345  0 public Element element(String name, Namespace namespace) {
 346  0 return element(getDocumentFactory().createQName(name, namespace));
 347    }
 348   
 349  79 public List elements() {
 350  79 List list = contentList();
 351   
 352  79 BackedList answer = createResultList();
 353   
 354  79 int size = list.size();
 355   
 356  79 for (int i = 0; i < size; i++) {
 357  354 Object object = list.get(i);
 358   
 359  354 if (object instanceof Element) {
 360  145 answer.addLocal(object);
 361    }
 362    }
 363   
 364  79 return answer;
 365    }
 366   
 367  201 public List elements(String name) {
 368  201 List list = contentList();
 369   
 370  201 BackedList answer = createResultList();
 371   
 372  201 int size = list.size();
 373   
 374  201 for (int i = 0; i < size; i++) {
 375  1975 Object object = list.get(i);
 376   
 377  1975 if (object instanceof Element) {
 378  901 Element element = (Element) object;
 379   
 380  901 if (name.equals(element.getName())) {
 381  336 answer.addLocal(element);
 382    }
 383    }
 384    }
 385   
 386  201 return answer;
 387    }
 388   
 389  13972 public List elements(QName qName) {
 390  13972 List list = contentList();
 391   
 392  13972 BackedList answer = createResultList();
 393   
 394  13972 int size = list.size();
 395   
 396  13972 for (int i = 0; i < size; i++) {
 397  42383 Object object = list.get(i);
 398   
 399  42383 if (object instanceof Element) {
 400  14661 Element element = (Element) object;
 401   
 402  14661 if (qName.equals(element.getQName())) {
 403  3653 answer.addLocal(element);
 404    }
 405    }
 406    }
 407   
 408  13972 return answer;
 409    }
 410   
 411  0 public List elements(String name, Namespace namespace) {
 412  0 return elements(getDocumentFactory().createQName(name, namespace));
 413    }
 414   
 415  59 public Iterator elementIterator() {
 416  59 List list = elements();
 417   
 418  59 return list.iterator();
 419    }
 420   
 421  191 public Iterator elementIterator(String name) {
 422  191 List list = elements(name);
 423   
 424  191 return list.iterator();
 425    }
 426   
 427  13948 public Iterator elementIterator(QName qName) {
 428  13948 List list = elements(qName);
 429   
 430  13948 return list.iterator();
 431    }
 432   
 433  0 public Iterator elementIterator(String name, Namespace ns) {
 434  0 return elementIterator(getDocumentFactory().createQName(name, ns));
 435    }
 436   
 437    // Attribute methods
 438    // -------------------------------------------------------------------------
 439  0 public List attributes() {
 440  0 return new ContentListFacade(this, attributeList());
 441    }
 442   
 443  0 public Iterator attributeIterator() {
 444  0 return attributeList().iterator();
 445    }
 446   
 447  0 public Attribute attribute(int index) {
 448  0 return (Attribute) attributeList().get(index);
 449    }
 450   
 451  1 public int attributeCount() {
 452  1 return attributeList().size();
 453    }
 454   
 455  0 public Attribute attribute(String name) {
 456  0 List list = attributeList();
 457   
 458  0 int size = list.size();
 459   
 460  0 for (int i = 0; i < size; i++) {
 461  0 Attribute attribute = (Attribute) list.get(i);
 462   
 463  0 if (name.equals(attribute.getName())) {
 464  0 return attribute;
 465    }
 466    }
 467   
 468  0 return null;
 469    }
 470   
 471  0 public Attribute attribute(QName qName) {
 472  0 List list = attributeList();
 473   
 474  0