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.QName;
19 import org.dom4j.tree.AbstractAttribute;
20 import org.relaxng.datatype.DatatypeException;
21 import org.relaxng.datatype.ValidationContext;
22
23 /*** <p><code>DatatypeAttribute</code> represents an Attribute which supports the
24 * <a href="http://www.w3.org/TR/xmlschema-2/">XML Schema Data Types</a>
25 * specification.</p>
26 *
27 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
28 * @version $Revision: 1.7 $
29 */
30 public class DatatypeAttribute extends AbstractAttribute implements SerializationContext, ValidationContext {
31
32 /*** The parent <code>Element</code> of the <code>Attribute</code> */
33 private Element parent;
34
35 /*** The <code>QName</code> for this element */
36 private QName qname;
37
38 /*** The <code>XSDatatype</code> of the <code>Attribute</code> */
39 private XSDatatype datatype;
40
41 /*** The data (Object) value of the <code>Attribute</code> */
42 private Object data;
43
44 /*** The text value of the <code>Attribute</code> */
45 private String text;
46
47
48 public DatatypeAttribute(QName qname,XSDatatype datatype) {
49 this.qname = qname;
50 this.datatype = datatype;
51 }
52
53 public String toString() {
54 return getClass().getName() + hashCode()
55 + " [Attribute: name " + getQualifiedName()
56 + " value \"" + getValue() + "\" data: " + getData() + "]";
57 }
58
59 public DatatypeAttribute(QName qname,XSDatatype datatype,String text) {
60 this.qname = qname;
61 this.datatype = datatype;
62 this.text = text;
63 this.data = convertToValue(text);
64 }
65
66
67 /*** Returns the MSV XSDatatype for this node */
68 public XSDatatype getXSDatatype() {
69 return datatype;
70 }
71
72
73
74 public String getNamespacePrefix(String uri) {
75 Element parent = getParent();
76 if (parent != null) {
77 Namespace namespace = parent.getNamespaceForURI(uri);
78 if ( namespace != null ) {
79 return namespace.getPrefix();
80 }
81 }
82 return null;
83 }
84
85
86
87 public String getBaseUri() {
88
89 return null;
90 }
91
92 public boolean isNotation(String notationName) {
93
94 return false;
95 }
96
97 public boolean isUnparsedEntity(String entityName) {
98
99 return true;
100 }
101
102 public String resolveNamespacePrefix(String prefix) {
103
104
105 if ( prefix.equals( getNamespacePrefix() ) ) {
106 return getNamespaceURI();
107 }
108 else {
109 Element parent = getParent();
110 if ( parent != null ) {
111 Namespace namespace = parent.getNamespaceForPrefix( prefix );
112 if ( namespace != null ) {
113 return namespace.getURI();
114 }
115 }
116 }
117 return null;
118 }
119
120
121
122
123
124
125 public QName getQName() {
126 return qname;
127 }
128
129 public String getValue() {
130 return text;
131 }
132
133 public void setValue(String text) {
134 validate(text);
135
136 this.text = text;
137 this.data = convertToValue(text);
138 }
139
140 public Object getData() {
141 return data;
142 }
143
144 public void setData(Object data) {
145 String s = datatype.convertToLexicalValue( data, this );
146 validate(s);
147 this.text = s;
148 this.data = data;
149 }
150
151 public Element getParent() {
152 return parent;
153 }
154
155 public void setParent(Element parent) {
156 this.parent = parent;
157 }
158
159 public boolean supportsParent() {
160 return true;
161 }
162
163 public boolean isReadOnly() {
164 return false;
165 }
166
167
168
169
170 protected void validate(String text) throws IllegalArgumentException {
171 try {
172 datatype.checkValid(text, this);
173 }
174 catch (DatatypeException e) {
175 throw new IllegalArgumentException( e.getMessage() );
176 }
177 }
178
179 protected Object convertToValue(String text) {
180 if ( datatype instanceof DatabindableDatatype ) {
181 DatabindableDatatype bindable = (DatabindableDatatype) datatype;
182 return bindable.createJavaObject( text, this );
183 }
184 else {
185 return datatype.createValue( text, this );
186 }
187 }
188 }
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236