2
* Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
4
* This software is open source.
5
* See the bottom of this file for the licence.
8
package org.dom4j.datatype;
10
import com.sun.msv.datatype.DatabindableDatatype;
11
import com.sun.msv.datatype.SerializationContext;
12
import com.sun.msv.datatype.xsd.XSDatatype;
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;
20
import org.relaxng.datatype.DatatypeException;
21
import org.relaxng.datatype.ValidationContext;
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>
30
* @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
31
* @version $Revision: 1.9 $
33
public class DatatypeElement extends DefaultElement implements
34
SerializationContext, ValidationContext {
35
/** The <code>XSDatatype</code> of the <code>Attribute</code> */
36
private XSDatatype datatype;
38
/** The data (Object) value of the <code>Attribute</code> */
41
public DatatypeElement(QName qname, XSDatatype datatype) {
43
this.datatype = datatype;
46
public DatatypeElement(QName qname, int attributeCount, XSDatatype type) {
47
super(qname, attributeCount);
51
public String toString() {
52
return getClass().getName() + hashCode() + " [Element: <"
53
+ getQualifiedName() + " attributes: " + attributeList()
54
+ " data: " + getData() + " />]";
58
* Returns the MSV XSDatatype for this node
60
* @return DOCUMENT ME!
62
public XSDatatype getXSDatatype() {
66
// SerializationContext interface
67
// -------------------------------------------------------------------------
68
public String getNamespacePrefix(String uri) {
69
Namespace namespace = getNamespaceForURI(uri);
71
return (namespace != null) ? namespace.getPrefix() : null;
74
// ValidationContext interface
75
// -------------------------------------------------------------------------
76
public String getBaseUri() {
77
// XXXX: could we use a Document for this?
81
public boolean isNotation(String notationName) {
82
// XXXX: no way to do this yet in dom4j so assume false
86
public boolean isUnparsedEntity(String entityName) {
87
// XXXX: no way to do this yet in dom4j so assume valid
91
public String resolveNamespacePrefix(String prefix) {
92
Namespace namespace = getNamespaceForPrefix(prefix);
94
if (namespace != null) {
95
return namespace.getURI();
102
// -------------------------------------------------------------------------
103
public Object getData() {
105
String text = getTextTrim();
107
if ((text != null) && (text.length() > 0)) {
108
if (datatype instanceof DatabindableDatatype) {
109
DatabindableDatatype bind = (DatabindableDatatype) datatype;
110
data = bind.createJavaObject(text, this);
112
data = datatype.createValue(text, this);
120
public void setData(Object data) {
121
String s = datatype.convertToLexicalValue(data, this);
127
public Element addText(String text) {
130
return super.addText(text);
133
public void setText(String text) {
138
// Implementation methods
139
// -------------------------------------------------------------------------
142
* Override to force lazy recreation of data object
147
protected void childAdded(Node node) {
149
super.childAdded(node);
153
* Override to force lazy recreation of data object
158
protected void childRemoved(Node node) {
160
super.childRemoved(node);
163
protected void validate(String text) throws IllegalArgumentException {
165
datatype.checkValid(text, this);
166
} catch (DatatypeException e) {
167
throw new IllegalArgumentException(e.getMessage());
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:
177
* 1. Redistributions of source code must retain copyright statements and
178
* notices. Redistributions must also contain a copy of this document.
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.
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.
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.
192
* 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
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.
206
* Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.