Clover coverage report - dom4j - 1.5
Coverage timestamp: vr sep 3 2004 20:47:03 GMT+01:00
file stats: LOC: 236   Methods: 20
NCLOC: 118   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DatatypeAttribute.java 8,3% 41,3% 50% 38,5%
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: DatatypeAttribute.java,v 1.7 2004/06/25 08:03:34 maartenc Exp $
 8    */
 9   
 10    package org.dom4j.datatype;
 11   
 12    import com.sun.msv.datatype.DatabindableDatatype;
 13    import com.sun.msv.datatype.SerializationContext;
 14    import com.sun.msv.datatype.xsd.XSDatatype;
 15   
 16    import org.dom4j.Element;
 17    import org.dom4j.Namespace;
 18    import org.dom4j.QName;
 19    import org.dom4j.tree.AbstractAttribute;
 20    import org.relaxng.datatype.DatatypeException;
 21    import org.relaxng.datatype.ValidationContext;
 22   
 23    /** <p><code>DatatypeAttribute</code> represents an Attribute which supports the
 24    * <a href="http://www.w3.org/TR/xmlschema-2/">XML Schema Data Types</a>
 25    * specification.</p>
 26    *
 27    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
 28    * @version $Revision: 1.7 $
 29    */
 30    public class DatatypeAttribute extends AbstractAttribute implements SerializationContext, ValidationContext {
 31   
 32    /** The parent <code>Element</code> of the <code>Attribute</code> */
 33    private Element parent;
 34   
 35    /** The <code>QName</code> for this element */
 36    private QName qname;
 37   
 38    /** The <code>XSDatatype</code> of the <code>Attribute</code> */
 39    private XSDatatype datatype;
 40   
 41    /** The data (Object) value of the <code>Attribute</code> */
 42    private Object data;
 43   
 44    /** The text value of the <code>Attribute</code> */
 45    private String text;
 46   
 47   
 48  0 public DatatypeAttribute(QName qname,XSDatatype datatype) {
 49  0 this.qname = qname;
 50  0 this.datatype = datatype;
 51    }
 52   
 53  268 public String toString() {
 54  268 return getClass().getName() + hashCode()
 55    + " [Attribute: name " + getQualifiedName()
 56    + " value \"" + getValue() + "\" data: " + getData() + "]";
 57    }
 58   
 59  1290 public DatatypeAttribute(QName qname,XSDatatype datatype,String text) {
 60  1290 this.qname = qname;
 61  1290 this.datatype = datatype;
 62  1290 this.text = text;
 63  1290 this.data = convertToValue(text);
 64    }
 65   
 66   
 67    /** Returns the MSV XSDatatype for this node */
 68  0 public XSDatatype getXSDatatype() {
 69  0 return datatype;
 70    }
 71   
 72    // SerializationContext interface
 73    //-------------------------------------------------------------------------
 74  0 public String getNamespacePrefix(String uri) {
 75  0 Element parent = getParent();
 76  0 if (parent != null) {
 77  0 Namespace namespace = parent.getNamespaceForURI(uri);
 78  0 if ( namespace != null ) {
 79  0 return namespace.getPrefix();
 80    }
 81    }
 82  0 return null;
 83    }
 84   
 85    // ValidationContext interface
 86    //-------------------------------------------------------------------------
 87  0 public String getBaseUri() {
 88    // XXXX: could we use a Document for this?
 89  0 return null;
 90    }
 91   
 92  0 public boolean isNotation(String notationName) {
 93    // XXXX: no way to do this yet in dom4j so assume false
 94  0 return false;
 95    }
 96   
 97  0 public boolean isUnparsedEntity(String entityName) {
 98    // XXXX: no way to do this yet in dom4j so assume valid
 99  0 return true;
 100    }
 101   
 102  0 public String resolveNamespacePrefix(String prefix) {
 103    // first lets see if this is our attribute's prefix
 104   
 105  0 if ( prefix.equals( getNamespacePrefix() ) ) {
 106  0 return getNamespaceURI();
 107    }
 108    else {
 109  0 Element parent = getParent();
 110  0 if ( parent != null ) {
 111  0 Namespace namespace = parent.getNamespaceForPrefix( prefix );
 112  0 if ( namespace != null ) {
 113  0 return namespace.getURI();
 114    }
 115    }
 116    }
 117  0 return null;
 118    }
 119   
 120   
 121   
 122    // Attribute interface
 123    //-------------------------------------------------------------------------
 124   
 125  772 public QName getQName() {
 126  772 return qname;
 127    }
 128   
 129  542 public String getValue() {
 130  542 return text;
 131    }
 132   
 133  8 public void setValue(String text) {
 134  8 validate(text);
 135   
 136  4 this.text = text;
 137  4 this.data = convertToValue(text);
 138    }
 139   
 140  416 public Object getData() {
 141  416 return data;
 142    }
 143   
 144  0 public void setData(Object data) {
 145  0 String s = datatype.convertToLexicalValue( data, this );
 146  0 validate(s);
 147  0 this.text = s;
 148  0 this.data = data;
 149    }
 150   
 151  274 public Element getParent() {
 152  274 return parent;
 153    }
 154   
 155  1290 public void setParent(Element parent) {
 156  1290 this.parent = parent;
 157    }
 158   
 159  0 public boolean supportsParent() {
 160  0 return true;
 161    }
 162   
 163  0 public boolean isReadOnly() {
 164  0 return false;
 165    }
 166   
 167   
 168    // Implementation methods
 169    //-------------------------------------------------------------------------
 170  8 protected void validate(String text) throws IllegalArgumentException {
 171  8 try {
 172  8 datatype.checkValid(text, this);
 173    }
 174    catch (DatatypeException e) {
 175  4 throw new IllegalArgumentException( e.getMessage() );
 176    }
 177    }
 178   
 179  1294 protected Object convertToValue(String text) {
 180  1294 if ( datatype instanceof DatabindableDatatype ) {
 181  1294 DatabindableDatatype bindable = (DatabindableDatatype) datatype;
 182  1294 return bindable.createJavaObject( text, this );
 183    }
 184    else {
 185  0 return datatype.createValue( text, this );
 186    }
 187    }
 188    }
 189   
 190   
 191   
 192   
 193    /*
 194    * Redistribution and use of this software and associated documentation
 195    * ("Software"), with or without modification, are permitted provided
 196    * that the following conditions are met:
 197    *
 198    * 1. Redistributions of source code must retain copyright
 199    * statements and notices. Redistributions must also contain a
 200    * copy of this document.
 201    *
 202    * 2. Redistributions in binary form must reproduce the
 203    * above copyright notice, this list of conditions and the
 204    * following disclaimer in the documentation and/or other
 205    * materials provided with the distribution.
 206    *
 207    * 3. The name "DOM4J" must not be used to endorse or promote
 208    * products derived from this Software without prior written
 209    * permission of MetaStuff, Ltd. For written permission,
 210    * please contact dom4j-info@metastuff.com.
 211    *
 212    * 4. Products derived from this Software may not be called "DOM4J"
 213    * nor may "DOM4J" appear in their names without prior written
 214    * permission of MetaStuff, Ltd. DOM4J is a registered
 215    * trademark of MetaStuff, Ltd.
 216    *
 217    * 5. Due credit should be given to the DOM4J Project -
 218    * http://www.dom4j.org
 219    *
 220    * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
 221    * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
 222    * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 223    * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 224    * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 225    * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 226    * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 227    * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 228    * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 229    * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 230    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 231    * OF THE POSSIBILITY OF SUCH DAMAGE.
 232    *
 233    * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
 234    *
 235    * $Id: DatatypeAttribute.java,v 1.7 2004/06/25 08:03:34 maartenc Exp $
 236    */