Clover coverage report - dom4j - 1.5
Coverage timestamp: vr sep 3 2004 20:47:03 GMT+01:00
file stats: LOC: 242   Methods: 18
NCLOC: 116   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Namespace.java 81,8% 86,8% 83,3% 84,9%
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: Namespace.java,v 1.20 2004/06/25 08:03:33 maartenc Exp $
 8    */
 9   
 10    package org.dom4j;
 11   
 12    import org.dom4j.tree.AbstractNode;
 13    import org.dom4j.tree.DefaultNamespace;
 14    import org.dom4j.tree.NamespaceCache;
 15   
 16    /** <p><code>Namespace</code> is a Flyweight Namespace that can be shared amongst nodes.</p>
 17    *
 18    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
 19    * @version $Revision: 1.20 $
 20    */
 21    public class Namespace extends AbstractNode {
 22   
 23    /** Cache of Namespace instances */
 24    protected static final NamespaceCache cache = new NamespaceCache();
 25   
 26    /** XML Namespace */
 27    public static final Namespace XML_NAMESPACE
 28    = cache.get("xml", "http://www.w3.org/XML/1998/namespace");
 29   
 30    /** No Namespace present */
 31    public static final Namespace NO_NAMESPACE
 32    = cache.get("", "");
 33   
 34   
 35    /** The prefix mapped to this namespace */
 36    private String prefix;
 37   
 38    /** The URI for this namespace */
 39    private String uri;
 40   
 41    /** A cached version of the hashcode for efficiency */
 42    private int hashCode;
 43   
 44   
 45    /** A helper method to return the Namespace instance
 46    * for the given prefix and URI
 47    *
 48    * @return an interned Namespace object
 49    */
 50  197688 public static Namespace get(String prefix, String uri) {
 51  197688 return cache.get(prefix, uri);
 52    }
 53   
 54    /** A helper method to return the Namespace instance
 55    * for no prefix and the URI
 56    *
 57    * @return an interned Namespace object
 58    */
 59  20 public static Namespace get(String uri) {
 60  20 return cache.get(uri);
 61    }
 62   
 63    /** @param prefix is the prefix for this namespace
 64    * @param uri is the URI for this namespace
 65    */
 66  20996 public Namespace(String prefix, String uri) {
 67  20996 this.prefix = (prefix != null) ? prefix : "";
 68  20996 this.uri = (uri != null) ? uri : "";;
 69    }
 70   
 71   
 72  1080 public short getNodeType() {
 73  1080 return NAMESPACE_NODE;
 74    }
 75   
 76    /** @return the hash code based on the qualified name and the URI of the
 77    * namespace.
 78    */
 79  82820 public int hashCode() {
 80  82820 if ( hashCode == 0 ) {
 81  470 hashCode = createHashCode();
 82    }
 83  82820 return hashCode;
 84    }
 85   
 86    /** Factory method to create the hashcode allowing derived classes to change the behaviour */
 87  470 protected int createHashCode() {
 88  470 int hashCode = uri.hashCode() ^ prefix.hashCode();
 89  470 if ( hashCode == 0 ) {
 90  12 hashCode = 0xbabe;
 91    }
 92  470 return hashCode;
 93    }
 94   
 95    /**
 96    * Checks whether this Namespace equals the given Namespace. Two Namespaces
 97    * are equals if their URI and prefix are equal.
 98    */
 99  30032 public boolean equals(Object object) {
 100  30032 if ( this == object ) {
 101  9566 return true;
 102    }
 103  20466 else if ( object instanceof Namespace ) {
 104  20406 Namespace that = (Namespace) object;
 105   
 106    // we cache hash codes so this should be quick
 107  20406 if ( hashCode() == that.hashCode() ) {
 108  19450 return uri.equals( that.getURI() )
 109    && prefix.equals( that.getPrefix() );
 110    }
 111    }
 112  1016 return false;
 113    }
 114   
 115  0 public String getText() {
 116  0 return uri;
 117    }
 118   
 119  0 public String getStringValue() {
 120  0 return uri;
 121    }
 122   
 123    /** @return the prefix for this <code>Namespace</code>.
 124    */
 125  131604 public String getPrefix() {
 126  131604 return prefix;
 127    }
 128   
 129    /** @return the URI for this <code>Namespace</code>.
 130    */
 131  669518 public String getURI() {
 132  669518 return uri;
 133    }
 134   
 135   
 136  24 public String getXPathNameStep() {
 137  24 if (prefix != null && !"".equals( prefix )) {
 138  12 return "namespace::" + prefix;
 139    }
 140  12 return "namespace::*[name()='']";
 141    }
 142   
 143  12 public String getPath(Element context) {
 144  12 StringBuffer path = new StringBuffer(10);
 145  12 Element parent = getParent();
 146  12 if (parent != null && parent != context) {
 147  0 path.append( parent.getPath( context ) );
 148  0 path.append( '/' );
 149    }
 150  12 path.append( getXPathNameStep() );
 151  12 return path.toString();
 152    }
 153   
 154  12 public String getUniquePath(Element context) {
 155  12 StringBuffer path = new StringBuffer(10);
 156  12 Element parent = getParent();
 157  12 if (parent != null && parent != context) {
 158  0 path.append( parent.getUniquePath( context ) );
 159  0 path.append( '/' );
 160    }
 161  12 path.append( getXPathNameStep() );
 162  12 return path.toString();
 163    }
 164   
 165  6 public String toString() {
 166  6 return super.toString() + " [Namespace: prefix " + getPrefix()
 167    + " mapped to URI \"" + getURI() + "\"]";
 168    }
 169   
 170  52 public String asXML() {
 171  52 StringBuffer asxml = new StringBuffer(10);
 172  52 String prefix = getPrefix();
 173  52 if ( prefix != null && prefix.length() > 0 ) {
 174  28 asxml.append("xmlns:");
 175  28 asxml.append(prefix);
 176  28 asxml.append("=\"");
 177    }
 178    else {
 179  24 asxml.append("xmlns=\"");
 180    }
 181  52 asxml.append(getURI());
 182  52 asxml.append("\"");
 183  52 return asxml.toString();
 184    }
 185   
 186  0 public void accept(Visitor visitor) {
 187  0 visitor.visit(this);
 188    }
 189   
 190  152 protected Node createXPathResult(Element parent) {
 191  152 return new DefaultNamespace( parent, getPrefix(), getURI() );
 192    }
 193   
 194    }
 195   
 196   
 197   
 198   
 199    /*
 200    * Redistribution and use of this software and associated documentation
 201    * ("Software"), with or without modification, are permitted provided
 202    * that the following conditions are met:
 203    *
 204    * 1. Redistributions of source code must retain copyright
 205    * statements and notices. Redistributions must also contain a
 206    * copy of this document.
 207    *
 208    * 2. Redistributions in binary form must reproduce the
 209    * above copyright notice, this list of conditions and the
 210    * following disclaimer in the documentation and/or other
 211    * materials provided with the distribution.
 212    *
 213    * 3. The name "DOM4J" must not be used to endorse or promote
 214    * products derived from this Software without prior written
 215    * permission of MetaStuff, Ltd. For written permission,
 216    * please contact dom4j-info@metastuff.com.
 217    *
 218    * 4. Products derived from this Software may not be called "DOM4J"
 219    * nor may "DOM4J" appear in their names without prior written
 220    * permission of MetaStuff, Ltd. DOM4J is a registered
 221    * trademark of MetaStuff, Ltd.
 222    *
 223    * 5. Due credit should be given to the DOM4J Project -
 224    * http://www.dom4j.org
 225    *
 226    * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
 227    * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
 228    * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 229    * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 230    * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 231    * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 232    * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 233    * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 234    * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 235    * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 236    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 237    * OF THE POSSIBILITY OF SUCH DAMAGE.
 238    *
 239    * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
 240    *
 241    * $Id: Namespace.java,v 1.20 2004/06/25 08:03:33 maartenc Exp $
 242    */