Clover coverage report - dom4j - 1.5
Coverage timestamp: vr sep 3 2004 20:47:03 GMT+01:00
file stats: LOC: 264   Methods: 25
NCLOC: 162   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AbstractDocument.java 31,8% 64,8% 72% 60,2%
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: AbstractDocument.java,v 1.29 2004/07/11 10:49:37 maartenc Exp $
 8    */
 9   
 10    package org.dom4j.tree;
 11   
 12    import java.io.ByteArrayOutputStream;
 13    import java.io.IOException;
 14    import java.io.Writer;
 15    import java.util.Iterator;
 16    import java.util.List;
 17    import java.util.Map;
 18   
 19    import org.dom4j.Comment;
 20    import org.dom4j.Document;
 21    import org.dom4j.DocumentType;
 22    import org.dom4j.Element;
 23    import org.dom4j.IllegalAddException;
 24    import org.dom4j.Node;
 25    import org.dom4j.ProcessingInstruction;
 26    import org.dom4j.QName;
 27    import org.dom4j.Text;
 28    import org.dom4j.Visitor;
 29    import org.dom4j.io.XMLWriter;
 30   
 31    /** <p><code>AbstractDocument</code> is an abstract base class for
 32    * tree implementors to use for implementation inheritence.</p>
 33    *
 34    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
 35    * @version $Revision: 1.29 $
 36    */
 37    public abstract class AbstractDocument extends AbstractBranch implements Document {
 38   
 39  12002 public AbstractDocument() {
 40    }
 41   
 42  54 public short getNodeType() {
 43  54 return DOCUMENT_NODE;
 44    }
 45   
 46  2 public String getPath(Element context) {
 47  2 return "/";
 48    }
 49   
 50  2 public String getUniquePath(Element context) {
 51  2 return "/";
 52    }
 53   
 54  0 public Document getDocument() {
 55  0 return this;
 56    }
 57   
 58  0 public String getXMLEncoding() {
 59  0 return null;
 60    }
 61   
 62  0 public String getStringValue() {
 63  0 Element root = getRootElement();
 64  0 return ( root != null ) ? root.getStringValue() : "";
 65    }
 66   
 67  96 public String asXML() {
 68  96 try {
 69  96 ByteArrayOutputStream out = new ByteArrayOutputStream();
 70  96 XMLWriter writer = new XMLWriter(out, outputFormat);
 71  96 writer.write(this);
 72  96 return out.toString();
 73    }
 74    catch (IOException e) {
 75  0 throw new RuntimeException("IOException while generating textual representation: " + e.getMessage());
 76    }
 77    }
 78   
 79  0 public void write(Writer out) throws IOException {
 80  0 XMLWriter writer = new XMLWriter( out, outputFormat );
 81  0 writer.write(this);
 82    }
 83   
 84    /** <p><code>accept</code> method is the <code>Visitor Pattern</code> method.
 85    * </p>
 86    *
 87    * @param visitor <code>Visitor</code> is the visitor.
 88    */
 89  0 public void accept(Visitor visitor) {
 90  0 visitor.visit(this);
 91   
 92  0 DocumentType docType = getDocType();
 93  0 if ( docType != null ) {
 94  0 visitor.visit( docType );
 95    }
 96   
 97    // visit content
 98  0 List content = content();
 99  0 if (content != null) {
 100  0 for ( Iterator iter = content.iterator(); iter.hasNext(); ) {
 101  0 Object object = iter.next();
 102  0 if (object instanceof String) {
 103  0 Text text = getDocumentFactory().createText((String) object);
 104  0 visitor.visit(text);
 105    }
 106    else {
 107  0 Node node = (Node) object;
 108  0 node.accept(visitor);
 109    }
 110    }
 111    }
 112    }
 113   
 114  302 public String toString() {
 115  302 return super.toString() + " [Document: name " + getName() + "]";
 116    }
 117   
 118  208 public void normalize() {
 119  208 Element element = getRootElement();
 120  208 if ( element != null ) {
 121  208 element.normalize();
 122    }
 123    }
 124   
 125  44 public Document addComment(String comment) {
 126  44 Comment node = getDocumentFactory().createComment( comment );
 127  44 add( node );
 128  44 return this;
 129    }
 130   
 131  42 public Document addProcessingInstruction(String target, String data) {
 132  42 ProcessingInstruction node = getDocumentFactory().createProcessingInstruction( target, data );
 133  42 add( node );
 134  42 return this;
 135    }
 136   
 137  0 public Document addProcessingInstruction(String target, Map data) {
 138  0 ProcessingInstruction node = getDocumentFactory().createProcessingInstruction( target, data );
 139  0 add( node );
 140  0 return this;
 141    }
 142   
 143  338 public Element addElement(String name) {
 144  338 Element element = getDocumentFactory().createElement(name);
 145  338 add(element);
 146  338 return element;
 147    }
 148   
 149  4 public Element addElement(String qualifiedName, String namespaceURI) {
 150  4 Element element = getDocumentFactory().createElement(qualifiedName, namespaceURI);
 151  4 add(element);
 152  4 return element;
 153    }
 154   
 155  11642 public Element addElement(QName qName) {
 156  11642 Element element = getDocumentFactory().createElement(qName);
 157  11642 add(element);
 158  11642 return element;
 159    }
 160   
 161  6 public void setRootElement(Element rootElement) {
 162  6 clearContent();
 163  6 if ( rootElement != null ) {
 164  6 super.add(rootElement);
 165  6 rootElementAdded(rootElement);
 166    }
 167    }
 168   
 169  12012 public void add(Element element) {
 170  12012 checkAddElementAllowed(element);
 171  12010 super.add(element);
 172  12010 rootElementAdded(element);
 173    }
 174   
 175  4 public boolean remove(Element element) {
 176  4 boolean answer = super.remove(element);
 177  4 Element root = getRootElement();
 178  4 if ( root != null && answer ) {
 179  0 setRootElement(null);
 180    }
 181  4 element.setDocument(null);
 182  4 return answer;
 183    }
 184   
 185  0 public Node asXPathResult(Element parent) {
 186  0 return this;
 187    }
 188   
 189  12104 protected void childAdded(Node node) {
 190  12104 if (node != null ) {
 191  12104 node.setDocument(this);
 192    }
 193    }
 194   
 195  8 protected void childRemoved(Node node) {
 196  8 if ( node != null ) {
 197  8 node.setDocument(null);
 198    }
 199    }
 200   
 201  12012 protected void checkAddElementAllowed(Element element) {
 202  12012 Element root = getRootElement();
 203  12012 if ( root != null ) {
 204  2 throw new IllegalAddException(
 205    this,
 206    element,
 207    "Cannot add another element to this Document as it already has "
 208    + " a root element of: " + root.getQualifiedName()
 209    );
 210    }
 211    }
 212   
 213    /** Called to set the root element variable */
 214    protected abstract void rootElementAdded(Element rootElement);
 215   
 216    }
 217   
 218   
 219   
 220   
 221    /*
 222    * Redistribution and use of this software and associated documentation
 223    * ("Software"), with or without modification, are permitted provided
 224    * that the following conditions are met:
 225    *
 226    * 1. Redistributions of source code must retain copyright
 227    * statements and notices. Redistributions must also contain a
 228    * copy of this document.
 229    *
 230    * 2. Redistributions in binary form must reproduce the
 231    * above copyright notice, this list of conditions and the
 232    * following disclaimer in the documentation and/or other
 233    * materials provided with the distribution.
 234    *
 235    * 3. The name "DOM4J" must not be used to endorse or promote
 236    * products derived from this Software without prior written
 237    * permission of MetaStuff, Ltd. For written permission,
 238    * please contact dom4j-info@metastuff.com.
 239    *
 240    * 4. Products derived from this Software may not be called "DOM4J"
 241    * nor may "DOM4J" appear in their names without prior written
 242    * permission of MetaStuff, Ltd. DOM4J is a registered
 243    * trademark of MetaStuff, Ltd.
 244    *
 245    * 5. Due credit should be given to the DOM4J Project -
 246    * http://www.dom4j.org
 247    *
 248    * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
 249    * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
 250    * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 251    * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 252    * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 253    * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 254    * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 255    * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 256    * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 257    * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 258    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 259    * OF THE POSSIBILITY OF SUCH DAMAGE.
 260    *
 261    * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
 262    *
 263    * $Id: AbstractDocument.java,v 1.29 2004/07/11 10:49:37 maartenc Exp $
 264    */