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.QName; 17 import org.dom4j.tree.AbstractAttribute; 18 19 import org.relaxng.datatype.DatatypeException; 20 import org.relaxng.datatype.ValidationContext; 21 22 32 public class DatatypeAttribute extends AbstractAttribute implements 33 SerializationContext, ValidationContext { 34 35 private Element parent; 36 37 38 private QName qname; 39 40 41 private XSDatatype datatype; 42 43 44 private Object data; 45 46 47 private String 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 text) { 55 this.qname = qname; 56 this.datatype = datatype; 57 this.text = text; 58 this.data = convertToValue(text); 59 } 60 61 public String toString() { 62 return getClass().getName() + hashCode() + " [Attribute: name " 63 + getQualifiedName() + " value \"" + getValue() + "\" data: " 64 + getData() + "]"; 65 } 66 67 72 public XSDatatype getXSDatatype() { 73 return datatype; 74 } 75 76 public String getNamespacePrefix(String 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 public String getBaseUri() { 95 return null; 97 } 98 99 public boolean isNotation(String notationName) { 100 return false; 102 } 103 104 public boolean isUnparsedEntity(String entityName) { 105 return true; 107 } 108 109 public String resolveNamespacePrefix(String prefix) { 110 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 public QName getQName() { 132 return qname; 133 } 134 135 public String getValue() { 136 return text; 137 } 138 139 public void setValue(String value) { 140 validate(value); 141 142 this.text = value; 143 this.data = convertToValue(value); 144 } 145 146 public Object getData() { 147 return data; 148 } 149 150 public void setData(Object data) { 151 String 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 protected void validate(String txt) throws IllegalArgumentException { 176 try { 177 datatype.checkValid(txt, this); 178 } catch (DatatypeException e) { 179 throw new IllegalArgumentException (e.getMessage()); 180 } 181 } 182 183 protected Object convertToValue(String 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 230 | Popular Tags |