1 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 33 public class DatatypeElement extends DefaultElement implements 34 SerializationContext, ValidationContext { 35 36 private XSDatatype datatype; 37 38 39 private Object 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 toString() { 52 return getClass().getName() + hashCode() + " [Element: <" 53 + getQualifiedName() + " attributes: " + attributeList() 54 + " data: " + getData() + " />]"; 55 } 56 57 62 public XSDatatype getXSDatatype() { 63 return datatype; 64 } 65 66 public String getNamespacePrefix(String uri) { 69 Namespace namespace = getNamespaceForURI(uri); 70 71 return (namespace != null) ? namespace.getPrefix() : null; 72 } 73 74 public String getBaseUri() { 77 return null; 79 } 80 81 public boolean isNotation(String notationName) { 82 return false; 84 } 85 86 public boolean isUnparsedEntity(String entityName) { 87 return true; 89 } 90 91 public String resolveNamespacePrefix(String prefix) { 92 Namespace namespace = getNamespaceForPrefix(prefix); 93 94 if (namespace != null) { 95 return namespace.getURI(); 96 } 97 98 return null; 99 } 100 101 public Object getData() { 104 if (data == null) { 105 String 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 data) { 121 String s = datatype.convertToLexicalValue(data, this); 122 validate(s); 123 this.data = data; 124 setText(s); 125 } 126 127 public Element addText(String text) { 128 validate(text); 129 130 return super.addText(text); 131 } 132 133 public void setText(String text) { 134 validate(text); 135 super.setText(text); 136 } 137 138 141 147 protected void childAdded(Node node) { 148 data = null; 149 super.childAdded(node); 150 } 151 152 158 protected void childRemoved(Node node) { 159 data = null; 160 super.childRemoved(node); 161 } 162 163 protected void validate(String text) throws IllegalArgumentException { 164 try { 165 datatype.checkValid(text, this); 166 } catch (DatatypeException e) { 167 throw new IllegalArgumentException (e.getMessage()); 168 } 169 } 170 } 171 172 208 | Popular Tags |