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

file stats: | LOC: | 229 |  | Methods: | 20
| NCLOC: | 117 |  | Classes: | 1

Source file | Conditionals | Statements | Methods | TOTAL

DatatypeAttribute.java

8,3%

41,3%

50%

38,5%

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