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

file stats: | LOC: | 207 |  | Methods: | 16
| NCLOC: | 95 |  | Classes: | 1

Source file | Conditionals | Statements | Methods | TOTAL

DatatypeElement.java

40%

60,5%

50%

54,7%

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