1
2
3
4
5
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 public DatatypeElement(QName qname, XSDatatype datatype) {
42 super(qname);
43 this.datatype = datatype;
44 }
45
46 public DatatypeElement(QName qname, int attributeCount, XSDatatype type) {
47 super(qname, attributeCount);
48 this.datatype = type;
49 }
50
51 public String toString() {
52 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 public XSDatatype getXSDatatype() {
63 return datatype;
64 }
65
66
67
68 public String getNamespacePrefix(String uri) {
69 Namespace namespace = getNamespaceForURI(uri);
70
71 return (namespace != null) ? namespace.getPrefix() : null;
72 }
73
74
75
76 public String getBaseUri() {
77
78 return null;
79 }
80
81 public boolean isNotation(String notationName) {
82
83 return false;
84 }
85
86 public boolean isUnparsedEntity(String entityName) {
87
88 return true;
89 }
90
91 public String resolveNamespacePrefix(String prefix) {
92 Namespace namespace = getNamespaceForPrefix(prefix);
93
94 if (namespace != null) {
95 return namespace.getURI();
96 }
97
98 return null;
99 }
100
101
102
103 public Object getData() {
104 if (data == null) {
105 String text = getTextTrim();
106
107 if ((text != null) && (text.length() > 0)) {
108 if (datatype instanceof DatabindableDatatype) {
109 DatabindableDatatype bind = (DatabindableDatatype) datatype;
110 data = bind.createJavaObject(text, this);
111 } else {
112 data = datatype.createValue(text, this);
113 }
114 }
115 }
116
117 return data;
118 }
119
120 public void setData(Object data) {
121 String s = datatype.convertToLexicalValue(data, this);
122 validate(s);
123 this.data = data;
124 setText(s);
125 }
126
127 public Element addText(String text) {
128 validate(text);
129
130 return super.addText(text);
131 }
132
133 public void setText(String text) {
134 validate(text);
135 super.setText(text);
136 }
137
138
139
140
141 /***
142 * Override to force lazy recreation of data object
143 *
144 * @param node
145 * DOCUMENT ME!
146 */
147 protected void childAdded(Node node) {
148 data = null;
149 super.childAdded(node);
150 }
151
152 /***
153 * Override to force lazy recreation of data object
154 *
155 * @param node
156 * DOCUMENT ME!
157 */
158 protected void childRemoved(Node node) {
159 data = null;
160 super.childRemoved(node);
161 }
162
163 protected void validate(String text) throws IllegalArgumentException {
164 try {
165 datatype.checkValid(text, this);
166 } catch (DatatypeException e) {
167 throw new IllegalArgumentException(e.getMessage());
168 }
169 }
170 }
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207