dom4j - 1.6.1

Clover coverage report - dom4j - 1.6.1
Coverage timestamp: ma mei 16 2005 14:23:01 GMT+01:00
FRAMES NO FRAMES

file stats: | LOC: | 194 |  | Methods: | 16
| NCLOC: | 93 |  | Classes: | 1

Source file | Conditionals | Statements | Methods | TOTAL

BeanElement.java

70%

64,7%

56,2%

63,3%

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