1 /*
2 * Copyright 2001-2004 (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 * $Id: DatatypeElement.java,v 1.7 2004/06/25 08:03:34 maartenc Exp $
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.Node;
19 import org.dom4j.QName;
20 import org.dom4j.tree.DefaultElement;
21 import org.relaxng.datatype.DatatypeException;
22 import org.relaxng.datatype.ValidationContext;
23
24 /*** <p><code>DatatypeElement</code> represents an Element which supports the
25 * <a href="http://www.w3.org/TR/xmlschema-2/">XML Schema Data Types</a>
26 * specification.</p>
27 *
28 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
29 * @version $Revision: 1.7 $
30 */
31 public class DatatypeElement extends DefaultElement implements SerializationContext, ValidationContext {
32
33 /*** The <code>XSDatatype</code> of the <code>Attribute</code> */
34 private XSDatatype datatype;
35
36 /*** The data (Object) value of the <code>Attribute</code> */
37 private Object data;
38
39
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 datatype) {
47 super(qname, attributeCount);
48 this.datatype = datatype;
49 }
50
51 public String toString() {
52 return getClass().getName() + hashCode()
53 + " [Element: <" + getQualifiedName()
54 + " attributes: " + attributeList()
55 + " data: " + getData() + " />]";
56 }
57
58 /*** Returns the MSV XSDatatype for this node */
59 public XSDatatype getXSDatatype() {
60 return datatype;
61 }
62
63 // SerializationContext interface
64 //-------------------------------------------------------------------------
65 public String getNamespacePrefix(String uri) {
66 Namespace namespace = getNamespaceForURI(uri);
67 return (namespace != null) ? namespace.getPrefix() : null;
68 }
69
70 // ValidationContext interface
71 //-------------------------------------------------------------------------
72 public String getBaseUri() {
73 // XXXX: could we use a Document for this?
74 return null;
75 }
76
77 public boolean isNotation(String notationName) {
78 // XXXX: no way to do this yet in dom4j so assume false
79 return false;
80 }
81
82 public boolean isUnparsedEntity(String entityName) {
83 // XXXX: no way to do this yet in dom4j so assume valid
84 return true;
85 }
86
87 public String resolveNamespacePrefix(String prefix) {
88 Namespace namespace = getNamespaceForPrefix( prefix );
89 if ( namespace != null ) {
90 return namespace.getURI();
91 }
92 return null;
93 }
94
95
96 // Element interface
97 //-------------------------------------------------------------------------
98 public Object getData() {
99 if ( data == null ) {
100 String text = getTextTrim();
101 if ( text != null && text.length() > 0 ) {
102 if ( datatype instanceof DatabindableDatatype ) {
103 DatabindableDatatype bindable = (DatabindableDatatype) datatype;
104 data = bindable.createJavaObject( text, this );
105 }
106 else {
107 data = datatype.createValue( text, this );
108 }
109 }
110 }
111 return data;
112 }
113
114 public void setData(Object data) {
115 String s = datatype.convertToLexicalValue( data, this );
116 validate(s);
117 this.data = data;
118 setText( s );
119 }
120
121 public Element addText(String text) {
122 validate(text);
123 return super.addText(text);
124 }
125
126 public void setText(String text) {
127 validate(text);
128 super.setText(text);
129 }
130 // Implementation methods
131 //-------------------------------------------------------------------------
132 /*** Override to force lazy recreation of data object */
133 protected void childAdded(Node node) {
134 data = null;
135 super.childAdded(node);
136 }
137
138 /*** Override to force lazy recreation of data object */
139 protected void childRemoved(Node node) {
140 data = null;
141 super.childRemoved(node);
142 }
143
144 protected void validate(String text) throws IllegalArgumentException {
145 try {
146 datatype.checkValid(text, this);
147 }
148 catch (DatatypeException e) {
149 throw new IllegalArgumentException( e.getMessage() );
150 }
151 }
152 }
153
154
155
156
157 /*
158 * Redistribution and use of this software and associated documentation
159 * ("Software"), with or without modification, are permitted provided
160 * that the following conditions are met:
161 *
162 * 1. Redistributions of source code must retain copyright
163 * statements and notices. Redistributions must also contain a
164 * copy of this document.
165 *
166 * 2. Redistributions in binary form must reproduce the
167 * above copyright notice, this list of conditions and the
168 * following disclaimer in the documentation and/or other
169 * materials provided with the distribution.
170 *
171 * 3. The name "DOM4J" must not be used to endorse or promote
172 * products derived from this Software without prior written
173 * permission of MetaStuff, Ltd. For written permission,
174 * please contact dom4j-info@metastuff.com.
175 *
176 * 4. Products derived from this Software may not be called "DOM4J"
177 * nor may "DOM4J" appear in their names without prior written
178 * permission of MetaStuff, Ltd. DOM4J is a registered
179 * trademark of MetaStuff, Ltd.
180 *
181 * 5. Due credit should be given to the DOM4J Project -
182 * http://www.dom4j.org
183 *
184 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
185 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
186 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
187 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
188 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
189 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
190 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
191 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
192 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
193 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
194 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
195 * OF THE POSSIBILITY OF SUCH DAMAGE.
196 *
197 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
198 *
199 * $Id: DatatypeElement.java,v 1.7 2004/06/25 08:03:34 maartenc Exp $
200 */