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