View Javadoc

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