1
2
3
4
5
6
7
8
9
10 package org.dom4j.datatype;
11
12 import com.sun.msv.datatype.DatabindableDatatype;
13 import com.sun.msv.datatype.SerializationContext;
14 import com.sun.msv.datatype.xsd.XSDatatype;
15
16 import org.dom4j.Element;
17 import org.dom4j.Namespace;
18 import org.dom4j.Node;
19 import org.dom4j.QName;
20 import org.dom4j.tree.DefaultElement;
21 import org.relaxng.datatype.DatatypeException;
22 import org.relaxng.datatype.ValidationContext;
23
24 /*** <p><code>DatatypeElement</code> represents an Element which supports the
25 * <a href="http://www.w3.org/TR/xmlschema-2/">XML Schema Data Types</a>
26 * specification.</p>
27 *
28 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
29 * @version $Revision: 1.7 $
30 */
31 public class DatatypeElement extends DefaultElement implements SerializationContext, ValidationContext {
32
33 /*** The <code>XSDatatype</code> of the <code>Attribute</code> */
34 private XSDatatype datatype;
35
36 /*** The data (Object) value of the <code>Attribute</code> */
37 private Object data;
38
39
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 datatype) {
47 super(qname, attributeCount);
48 this.datatype = datatype;
49 }
50
51 public String toString() {
52 return getClass().getName() + hashCode()
53 + " [Element: <" + getQualifiedName()
54 + " attributes: " + attributeList()
55 + " data: " + getData() + " />]";
56 }
57
58 /*** Returns the MSV XSDatatype for this node */
59 public XSDatatype getXSDatatype() {
60 return datatype;
61 }
62
63
64
65 public String getNamespacePrefix(String uri) {
66 Namespace namespace = getNamespaceForURI(uri);
67 return (namespace != null) ? namespace.getPrefix() : null;
68 }
69
70
71
72 public String getBaseUri() {
73
74 return null;
75 }
76
77 public boolean isNotation(String notationName) {
78
79 return false;
80 }
81
82 public boolean isUnparsedEntity(String entityName) {
83
84 return true;
85 }
86
87 public String resolveNamespacePrefix(String prefix) {
88 Namespace namespace = getNamespaceForPrefix( prefix );
89 if ( namespace != null ) {
90 return namespace.getURI();
91 }
92 return null;
93 }
94
95
96
97
98 public Object getData() {
99 if ( data == null ) {
100 String text = getTextTrim();
101 if ( text != null && text.length() > 0 ) {
102 if ( datatype instanceof DatabindableDatatype ) {
103 DatabindableDatatype bindable = (DatabindableDatatype) datatype;
104 data = bindable.createJavaObject( text, this );
105 }
106 else {
107 data = datatype.createValue( text, this );
108 }
109 }
110 }
111 return data;
112 }
113
114 public void setData(Object data) {
115 String s = datatype.convertToLexicalValue( data, this );
116 validate(s);
117 this.data = data;
118 setText( s );
119 }
120
121 public Element addText(String text) {
122 validate(text);
123 return super.addText(text);
124 }
125
126 public void setText(String text) {
127 validate(text);
128 super.setText(text);
129 }
130
131
132 /*** Override to force lazy recreation of data object */
133 protected void childAdded(Node node) {
134 data = null;
135 super.childAdded(node);
136 }
137
138 /*** Override to force lazy recreation of data object */
139 protected void childRemoved(Node node) {
140 data = null;
141 super.childRemoved(node);
142 }
143
144 protected void validate(String text) throws IllegalArgumentException {
145 try {
146 datatype.checkValid(text, this);
147 }
148 catch (DatatypeException e) {
149 throw new IllegalArgumentException( e.getMessage() );
150 }
151 }
152 }
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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