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 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| public class DatatypeAttribute extends AbstractAttribute implements SerializationContext, ValidationContext { |
31 |
| |
32 |
| |
33 |
| private Element parent; |
34 |
| |
35 |
| |
36 |
| private QName qname; |
37 |
| |
38 |
| |
39 |
| private XSDatatype datatype; |
40 |
| |
41 |
| |
42 |
| private Object data; |
43 |
| |
44 |
| |
45 |
| private String text; |
46 |
| |
47 |
| |
48 |
0
| public DatatypeAttribute(QName qname,XSDatatype datatype) {
|
49 |
0
| this.qname = qname;
|
50 |
0
| this.datatype = datatype;
|
51 |
| } |
52 |
| |
53 |
268
| public String toString() {
|
54 |
268
| return getClass().getName() + hashCode()
|
55 |
| + " [Attribute: name " + getQualifiedName() |
56 |
| + " value \"" + getValue() + "\" data: " + getData() + "]"; |
57 |
| } |
58 |
| |
59 |
1290
| public DatatypeAttribute(QName qname,XSDatatype datatype,String text) {
|
60 |
1290
| this.qname = qname;
|
61 |
1290
| this.datatype = datatype;
|
62 |
1290
| this.text = text;
|
63 |
1290
| this.data = convertToValue(text);
|
64 |
| } |
65 |
| |
66 |
| |
67 |
| |
68 |
0
| public XSDatatype getXSDatatype() {
|
69 |
0
| return datatype;
|
70 |
| } |
71 |
| |
72 |
| |
73 |
| |
74 |
0
| public String getNamespacePrefix(String uri) {
|
75 |
0
| Element parent = getParent();
|
76 |
0
| if (parent != null) {
|
77 |
0
| Namespace namespace = parent.getNamespaceForURI(uri);
|
78 |
0
| if ( namespace != null ) {
|
79 |
0
| return namespace.getPrefix();
|
80 |
| } |
81 |
| } |
82 |
0
| return null;
|
83 |
| } |
84 |
| |
85 |
| |
86 |
| |
87 |
0
| public String getBaseUri() {
|
88 |
| |
89 |
0
| return null;
|
90 |
| } |
91 |
| |
92 |
0
| public boolean isNotation(String notationName) {
|
93 |
| |
94 |
0
| return false;
|
95 |
| } |
96 |
| |
97 |
0
| public boolean isUnparsedEntity(String entityName) {
|
98 |
| |
99 |
0
| return true;
|
100 |
| } |
101 |
| |
102 |
0
| public String resolveNamespacePrefix(String prefix) {
|
103 |
| |
104 |
| |
105 |
0
| if ( prefix.equals( getNamespacePrefix() ) ) {
|
106 |
0
| return getNamespaceURI();
|
107 |
| } |
108 |
| else { |
109 |
0
| Element parent = getParent();
|
110 |
0
| if ( parent != null ) {
|
111 |
0
| Namespace namespace = parent.getNamespaceForPrefix( prefix );
|
112 |
0
| if ( namespace != null ) {
|
113 |
0
| return namespace.getURI();
|
114 |
| } |
115 |
| } |
116 |
| } |
117 |
0
| return null;
|
118 |
| } |
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
772
| public QName getQName() {
|
126 |
772
| return qname;
|
127 |
| } |
128 |
| |
129 |
542
| public String getValue() {
|
130 |
542
| return text;
|
131 |
| } |
132 |
| |
133 |
8
| public void setValue(String text) {
|
134 |
8
| validate(text);
|
135 |
| |
136 |
4
| this.text = text;
|
137 |
4
| this.data = convertToValue(text);
|
138 |
| } |
139 |
| |
140 |
416
| public Object getData() {
|
141 |
416
| return data;
|
142 |
| } |
143 |
| |
144 |
0
| public void setData(Object data) {
|
145 |
0
| String s = datatype.convertToLexicalValue( data, this );
|
146 |
0
| validate(s);
|
147 |
0
| this.text = s;
|
148 |
0
| this.data = data;
|
149 |
| } |
150 |
| |
151 |
274
| public Element getParent() {
|
152 |
274
| return parent;
|
153 |
| } |
154 |
| |
155 |
1290
| public void setParent(Element parent) {
|
156 |
1290
| this.parent = parent;
|
157 |
| } |
158 |
| |
159 |
0
| public boolean supportsParent() {
|
160 |
0
| return true;
|
161 |
| } |
162 |
| |
163 |
0
| public boolean isReadOnly() {
|
164 |
0
| return false;
|
165 |
| } |
166 |
| |
167 |
| |
168 |
| |
169 |
| |
170 |
8
| protected void validate(String text) throws IllegalArgumentException {
|
171 |
8
| try {
|
172 |
8
| datatype.checkValid(text, this);
|
173 |
| } |
174 |
| catch (DatatypeException e) { |
175 |
4
| throw new IllegalArgumentException( e.getMessage() );
|
176 |
| } |
177 |
| } |
178 |
| |
179 |
1294
| protected Object convertToValue(String text) {
|
180 |
1294
| if ( datatype instanceof DatabindableDatatype ) {
|
181 |
1294
| DatabindableDatatype bindable = (DatabindableDatatype) datatype;
|
182 |
1294
| return bindable.createJavaObject( text, this );
|
183 |
| } |
184 |
| else { |
185 |
0
| 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 |
| |