KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > datatype > DatatypeAttribute


1 /*
2  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
3  *
4  * This software is open source.
5  * See the bottom of this file for the licence.
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 JavaDoc data;
45
46     /** The text value of the <code>Attribute</code> */
47     private String JavaDoc 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 JavaDoc text) {
55         this.qname = qname;
56         this.datatype = datatype;
57         this.text = text;
58         this.data = convertToValue(text);
59     }
60
61     public String JavaDoc 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     // SerializationContext interface
77
// -------------------------------------------------------------------------
78
public String JavaDoc getNamespacePrefix(String JavaDoc 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     // ValidationContext interface
93
// -------------------------------------------------------------------------
94
public String JavaDoc getBaseUri() {
95         // XXXX: could we use a Document for this?
96
return null;
97     }
98
99     public boolean isNotation(String JavaDoc notationName) {
100         // XXXX: no way to do this yet in dom4j so assume false
101
return false;
102     }
103
104     public boolean isUnparsedEntity(String JavaDoc entityName) {
105         // XXXX: no way to do this yet in dom4j so assume valid
106
return true;
107     }
108
109     public String JavaDoc resolveNamespacePrefix(String JavaDoc prefix) {
110         // first lets see if this is our attribute's prefix
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     // Attribute interface
130
// -------------------------------------------------------------------------
131
public QName getQName() {
132         return qname;
133     }
134
135     public String JavaDoc getValue() {
136         return text;
137     }
138
139     public void setValue(String JavaDoc value) {
140         validate(value);
141
142         this.text = value;
143         this.data = convertToValue(value);
144     }
145
146     public Object JavaDoc getData() {
147         return data;
148     }
149
150     public void setData(Object JavaDoc data) {
151         String JavaDoc 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     // Implementation methods
174
// -------------------------------------------------------------------------
175
protected void validate(String JavaDoc txt) throws IllegalArgumentException JavaDoc {
176         try {
177             datatype.checkValid(txt, this);
178         } catch (DatatypeException e) {
179             throw new IllegalArgumentException JavaDoc(e.getMessage());
180         }
181     }
182
183     protected Object JavaDoc convertToValue(String JavaDoc 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  * Redistribution and use of this software and associated documentation
196  * ("Software"), with or without modification, are permitted provided that the
197  * following conditions are met:
198  *
199  * 1. Redistributions of source code must retain copyright statements and
200  * notices. Redistributions must also contain a copy of this document.
201  *
202  * 2. Redistributions in binary form must reproduce the above copyright notice,
203  * this list of conditions and the following disclaimer in the documentation
204  * and/or other materials provided with the distribution.
205  *
206  * 3. The name "DOM4J" must not be used to endorse or promote products derived
207  * from this Software without prior written permission of MetaStuff, Ltd. For
208  * written permission, please contact dom4j-info@metastuff.com.
209  *
210  * 4. Products derived from this Software may not be called "DOM4J" nor may
211  * "DOM4J" appear in their names without prior written permission of MetaStuff,
212  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
213  *
214  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
215  *
216  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
217  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
218  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
219  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
220  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
221  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
222  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
223  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
224  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
225  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
226  * POSSIBILITY OF SUCH DAMAGE.
227  *
228  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
229  */

230
Popular Tags