Clover coverage report - dom4j - 1.5
Coverage timestamp: vr sep 3 2004 20:47:03 GMT+01:00
file stats: LOC: 199   Methods: 16
NCLOC: 94   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
BeanElement.java 70% 64,7% 56,2% 63,3%
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: BeanElement.java,v 1.13 2004/06/25 08:03:34 maartenc Exp $
 8    */
 9   
 10    package org.dom4j.bean;
 11   
 12    import java.util.List;
 13   
 14    import org.xml.sax.Attributes;
 15   
 16    import org.dom4j.Attribute;
 17    import org.dom4j.DocumentFactory;
 18    import org.dom4j.Element;
 19    import org.dom4j.Namespace;
 20    import org.dom4j.QName;
 21    import org.dom4j.tree.DefaultElement;
 22    import org.dom4j.tree.NamespaceStack;
 23   
 24   
 25   
 26    /** <p><code>BeanElement</code> uses a Java Bean to store its attributes.</p>
 27    *
 28    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
 29    * @version $Revision: 1.13 $
 30    */
 31    public class BeanElement extends DefaultElement {
 32   
 33    /** The <code>DocumentFactory</code> instance used by default */
 34    private static final DocumentFactory DOCUMENT_FACTORY = BeanDocumentFactory.getInstance();
 35   
 36    /** The JavaBean which defines my attributes */
 37    private Object bean;
 38   
 39   
 40  0 public BeanElement(String name, Object bean) {
 41  0 this( DOCUMENT_FACTORY.createQName(name), bean );
 42    }
 43   
 44  0 public BeanElement(String name,Namespace namespace, Object bean) {
 45  0 this( DOCUMENT_FACTORY.createQName(name, namespace), bean );
 46    }
 47   
 48  0 public BeanElement(QName qname, Object bean) {
 49  0 super( qname);
 50  0 this.bean = bean;
 51    }
 52   
 53  6 public BeanElement(QName qname) {
 54  6 super( qname);
 55    }
 56   
 57    /** @return the JavaBean associated with this element
 58    */
 59  8 public Object getData() {
 60  8 return bean;
 61    }
 62   
 63  4 public void setData(Object bean) {
 64  4 this.bean = bean;
 65   
 66    // force the attributeList to be lazily
 67    // created next time an attribute related
 68    // method is called again.
 69  4 setAttributeList(null);
 70    }
 71   
 72  4 public Attribute attribute(String name) {
 73  4 return getBeanAttributeList().attribute(name);
 74    }
 75   
 76  0 public Attribute attribute(QName qname) {
 77  0 return getBeanAttributeList().attribute(qname);
 78    }
 79   
 80  4 public Element addAttribute(String name, String value) {
 81  4 Attribute attribute = attribute(name);
 82  4 if (attribute != null ) {
 83  4 attribute.setValue(value);
 84    }
 85  4 return this;
 86    }
 87   
 88  0 public Element addAttribute(QName qName, String value) {
 89  0 Attribute attribute = attribute(qName);
 90  0 if (attribute != null ) {
 91  0 attribute.setValue(value);
 92    }
 93  0 return this;
 94    }
 95   
 96  0 public void setAttributes(List attributes) {
 97  0 throw new UnsupportedOperationException( "setAttributes(List) is not supported yet!" );
 98    }
 99   
 100   
 101    //Method overridden from AbstractElement
 102  6 public void setAttributes(Attributes attributes,
 103    NamespaceStack namespaceStack,
 104    boolean noNamespaceAttributes) {
 105  6 String className = attributes.getValue("class");
 106  6 if (className != null) {
 107  4 try {
 108  4 Class beanClass = Class.forName(className,
 109    true,
 110    BeanElement.class.getClassLoader());
 111  4 this.setData(beanClass.newInstance());
 112   
 113  4 for( int i=0; i<attributes.getLength(); i++){
 114  8 String attributeName = attributes.getLocalName(i);
 115  8 if( !"class".equalsIgnoreCase(attributeName) ){
 116  4 addAttribute(attributeName, attributes.getValue(i));
 117    }
 118    }
 119    }
 120    catch (Exception ex) {
 121    //What to do here?
 122  0 ( (BeanDocumentFactory)this.getDocumentFactory()).handleException(ex);
 123    }
 124    }
 125    else {
 126  2 super.setAttributes(attributes, namespaceStack, noNamespaceAttributes);
 127    }
 128    }
 129   
 130   
 131   
 132    // Implementation methods
 133    //-------------------------------------------------------------------------
 134  10 protected DocumentFactory getDocumentFactory() {
 135  10 return DOCUMENT_FACTORY;
 136    }
 137   
 138  4 protected BeanAttributeList getBeanAttributeList() {
 139  4 return (BeanAttributeList) attributeList();
 140    }
 141   
 142    /** A Factory Method pattern which lazily creates
 143    * a List implementation used to store content
 144    */
 145  4 protected List createAttributeList() {
 146  4 return new BeanAttributeList(this);
 147    }
 148  0 protected List createAttributeList(int size) {
 149  0 return new BeanAttributeList(this);
 150    }
 151    }
 152   
 153   
 154   
 155   
 156    /*
 157    * Redistribution and use of this software and associated documentation
 158    * ("Software"), with or without modification, are permitted provided
 159    * that the following conditions are met:
 160    *
 161    * 1. Redistributions of source code must retain copyright
 162    * statements and notices. Redistributions must also contain a
 163    * copy of this document.
 164    *
 165    * 2. Redistributions in binary form must reproduce the
 166    * above copyright notice, this list of conditions and the
 167    * following disclaimer in the documentation and/or other
 168    * materials provided with the distribution.
 169    *
 170    * 3. The name "DOM4J" must not be used to endorse or promote
 171    * products derived from this Software without prior written
 172    * permission of MetaStuff, Ltd. For written permission,
 173    * please contact dom4j-info@metastuff.com.
 174    *
 175    * 4. Products derived from this Software may not be called "DOM4J"
 176    * nor may "DOM4J" appear in their names without prior written
 177    * permission of MetaStuff, Ltd. DOM4J is a registered
 178    * trademark of MetaStuff, Ltd.
 179    *
 180    * 5. Due credit should be given to the DOM4J Project -
 181    * http://dom4j.org/
 182    *
 183    * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
 184    * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
 185    * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 186    * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 187    * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 188    * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 189    * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 190    * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 191    * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 192    * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 193    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 194    * OF THE POSSIBILITY OF SUCH DAMAGE.
 195    *
 196    * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
 197    *
 198    * $Id: BeanElement.java,v 1.13 2004/06/25 08:03:34 maartenc Exp $
 199    */