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.QName; |
17 |
| import org.dom4j.tree.AbstractAttribute; |
18 |
| |
19 |
| import org.relaxng.datatype.DatatypeException; |
20 |
| import org.relaxng.datatype.ValidationContext; |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| public class DatatypeAttribute extends AbstractAttribute implements |
33 |
| SerializationContext, ValidationContext { |
34 |
| |
35 |
| private Element parent; |
36 |
| |
37 |
| |
38 |
| private QName qname; |
39 |
| |
40 |
| |
41 |
| private XSDatatype datatype; |
42 |
| |
43 |
| |
44 |
| private Object data; |
45 |
| |
46 |
| |
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 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
0
| public XSDatatype getXSDatatype() {
|
73 |
0
| return datatype;
|
74 |
| } |
75 |
| |
76 |
| |
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 |
| |
93 |
| |
94 |
0
| public String getBaseUri() {
|
95 |
| |
96 |
0
| return null;
|
97 |
| } |
98 |
| |
99 |
0
| public boolean isNotation(String notationName) {
|
100 |
| |
101 |
0
| return false;
|
102 |
| } |
103 |
| |
104 |
0
| public boolean isUnparsedEntity(String entityName) {
|
105 |
| |
106 |
0
| return true;
|
107 |
| } |
108 |
| |
109 |
0
| public String resolveNamespacePrefix(String prefix) {
|
110 |
| |
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 |
| |
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 |
| |
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 |
| |
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 |
| |