KickJava   Java API By Example, From Geeks To Geeks.

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


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.Node;
17 import org.dom4j.QName;
18 import org.dom4j.tree.DefaultElement;
19
20 import org.relaxng.datatype.DatatypeException;
21 import org.relaxng.datatype.ValidationContext;
22
23 /**
24  * <p>
25  * <code>DatatypeElement</code> represents an Element which supports the <a
26  * HREF="http://www.w3.org/TR/xmlschema-2/">XML Schema Data Types </a>
27  * specification.
28  * </p>
29  *
30  * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan </a>
31  * @version $Revision: 1.9 $
32  */

33 public class DatatypeElement extends DefaultElement implements
34         SerializationContext, ValidationContext {
35     /** The <code>XSDatatype</code> of the <code>Attribute</code> */
36     private XSDatatype datatype;
37
38     /** The data (Object) value of the <code>Attribute</code> */
39     private Object JavaDoc data;
40
41     public DatatypeElement(QName qname, XSDatatype datatype) {
42         super(qname);
43         this.datatype = datatype;
44     }
45
46     public DatatypeElement(QName qname, int attributeCount, XSDatatype type) {
47         super(qname, attributeCount);
48         this.datatype = type;
49     }
50
51     public String JavaDoc toString() {
52         return getClass().getName() + hashCode() + " [Element: <"
53                 + getQualifiedName() + " attributes: " + attributeList()
54                 + " data: " + getData() + " />]";
55     }
56
57     /**
58      * Returns the MSV XSDatatype for this node
59      *
60      * @return DOCUMENT ME!
61      */

62     public XSDatatype getXSDatatype() {
63         return datatype;
64     }
65
66     // SerializationContext interface
67
// -------------------------------------------------------------------------
68
public String JavaDoc getNamespacePrefix(String JavaDoc uri) {
69         Namespace namespace = getNamespaceForURI(uri);
70
71         return (namespace != null) ? namespace.getPrefix() : null;
72     }
73
74     // ValidationContext interface
75
// -------------------------------------------------------------------------
76
public String JavaDoc getBaseUri() {
77         // XXXX: could we use a Document for this?
78
return null;
79     }
80
81     public boolean isNotation(String JavaDoc notationName) {
82         // XXXX: no way to do this yet in dom4j so assume false
83
return false;
84     }
85
86     public boolean isUnparsedEntity(String JavaDoc entityName) {
87         // XXXX: no way to do this yet in dom4j so assume valid
88
return true;
89     }
90
91     public String JavaDoc resolveNamespacePrefix(String JavaDoc prefix) {
92         Namespace namespace = getNamespaceForPrefix(prefix);
93
94         if (namespace != null) {
95             return namespace.getURI();
96         }
97
98         return null;
99     }
100
101     // Element interface
102
// -------------------------------------------------------------------------
103
public Object JavaDoc getData() {
104         if (data == null) {
105             String JavaDoc text = getTextTrim();
106
107             if ((text != null) && (text.length() > 0)) {
108                 if (datatype instanceof DatabindableDatatype) {
109                     DatabindableDatatype bind = (DatabindableDatatype) datatype;
110                     data = bind.createJavaObject(text, this);
111                 } else {
112                     data = datatype.createValue(text, this);
113                 }
114             }
115         }
116
117         return data;
118     }
119
120     public void setData(Object JavaDoc data) {
121         String JavaDoc s = datatype.convertToLexicalValue(data, this);
122         validate(s);
123         this.data = data;
124         setText(s);
125     }
126
127     public Element addText(String JavaDoc text) {
128         validate(text);
129
130         return super.addText(text);
131     }
132
133     public void setText(String JavaDoc text) {
134         validate(text);
135         super.setText(text);
136     }
137
138     // Implementation methods
139
// -------------------------------------------------------------------------
140

141     /**
142      * Override to force lazy recreation of data object
143      *
144      * @param node
145      * DOCUMENT ME!
146      */

147     protected void childAdded(Node node) {
148         data = null;
149         super.childAdded(node);
150     }
151
152     /**
153      * Override to force lazy recreation of data object
154      *
155      * @param node
156      * DOCUMENT ME!
157      */

158     protected void childRemoved(Node node) {
159         data = null;
160         super.childRemoved(node);
161     }
162
163     protected void validate(String JavaDoc text) throws IllegalArgumentException JavaDoc {
164         try {
165             datatype.checkValid(text, this);
166         } catch (DatatypeException e) {
167             throw new IllegalArgumentException JavaDoc(e.getMessage());
168         }
169     }
170 }
171
172 /*
173  * Redistribution and use of this software and associated documentation
174  * ("Software"), with or without modification, are permitted provided that the
175  * following conditions are met:
176  *
177  * 1. Redistributions of source code must retain copyright statements and
178  * notices. Redistributions must also contain a copy of this document.
179  *
180  * 2. Redistributions in binary form must reproduce the above copyright notice,
181  * this list of conditions and the following disclaimer in the documentation
182  * and/or other materials provided with the distribution.
183  *
184  * 3. The name "DOM4J" must not be used to endorse or promote products derived
185  * from this Software without prior written permission of MetaStuff, Ltd. For
186  * written permission, please contact dom4j-info@metastuff.com.
187  *
188  * 4. Products derived from this Software may not be called "DOM4J" nor may
189  * "DOM4J" appear in their names without prior written permission of MetaStuff,
190  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
191  *
192  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
193  *
194  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
195  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
196  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
197  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
198  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
199  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
200  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
201  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
202  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
203  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
204  * POSSIBILITY OF SUCH DAMAGE.
205  *
206  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
207  */

208
Popular Tags