Project dom4j 1.5.2 [5/2/05 10:13 PM]
 
Coverage - org/dom4j/datatype/DatatypeAttribute.java
  /*
   * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
   * 
   * This software is open source. 
   * See the bottom of this file for the licence.
   * 
   * $Id: DatatypeAttribute.java,v 1.7 2004/06/25 08:03:34 maartenc Exp $
   */
 
  package org.dom4j.datatype;
 
  import com.sun.msv.datatype.DatabindableDatatype;
  import com.sun.msv.datatype.SerializationContext;
  import com.sun.msv.datatype.xsd.XSDatatype;
 
  import org.dom4j.Element;
  import org.dom4j.Namespace;
  import org.dom4j.QName;
  import org.dom4j.tree.AbstractAttribute;
  import org.relaxng.datatype.DatatypeException;
  import org.relaxng.datatype.ValidationContext;
 
  /** <p><code>DatatypeAttribute</code> represents an Attribute which supports the
    * <a href="http://www.w3.org/TR/xmlschema-2/">XML Schema Data Types</a>
    * specification.</p>
    *
    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
    * @version $Revision: 1.7 $
    */
  public class DatatypeAttribute extends AbstractAttribute implements SerializationContext, ValidationContext {
 
      /** The parent <code>Element</code> of the <code>Attribute</code> */
      private Element parent;
      
      /** The <code>QName</code> for this element */
      private QName qname;
      
      /** The <code>XSDatatype</code> of the <code>Attribute</code> */
      private XSDatatype datatype;
      
      /** The data (Object) value of the <code>Attribute</code> */
      private Object data;
 
      /** The text value of the <code>Attribute</code> */
      private String text;
 
      
0x       public DatatypeAttribute(QName qname,XSDatatype datatype) {
0x           this.qname = qname;
0x           this.datatype = datatype;
0x       }
 
      public String toString() {
0x           return getClass().getName() + hashCode() 
              + " [Attribute: name " + getQualifiedName() 
              + " value \"" + getValue() + "\" data: " + getData() + "]";
      }
 
8x       public DatatypeAttribute(QName qname,XSDatatype datatype,String text) { 
8x           this.qname = qname;
8x           this.datatype = datatype;
8x           this.text = text;
8x           this.data = convertToValue(text);
8x       }
      
 
      /** Returns the MSV XSDatatype for this node */
      public XSDatatype getXSDatatype() {
0x           return datatype;
      }
      
      // SerializationContext interface
      //-------------------------------------------------------------------------  
      public String getNamespacePrefix(String uri) {
0x           Element parent = getParent();
0/2 0x           if (parent != null) {
0x               Namespace namespace = parent.getNamespaceForURI(uri);
0/2 0x               if ( namespace != null ) {
0x                   return namespace.getPrefix();
              }
          }
0x           return null;
      }
      
      // ValidationContext interface
      //-------------------------------------------------------------------------
      public String getBaseUri() {
          // XXXX: could we use a Document for this?
0x           return null;
      }
      
      public boolean isNotation(String notationName) {
          // XXXX: no way to do this yet in dom4j so assume false
0x           return false;
      }
              
      public boolean isUnparsedEntity(String entityName) {
          // XXXX: no way to do this yet in dom4j so assume valid
0x           return true;
      }
      
      public String resolveNamespacePrefix(String prefix) {
          // first lets see if this is our attribute's prefix
          
0/2 0x           if ( prefix.equals( getNamespacePrefix() ) ) {
0x               return getNamespaceURI();
          }
          else {
0x               Element parent = getParent();
0/2 0x               if ( parent != null ) {
0x                   Namespace namespace = parent.getNamespaceForPrefix( prefix );
0/2 0x                   if ( namespace != null ) {
0x                       return namespace.getURI();
                  }
              }
          }
0x           return null;
      }
      
      
      
      // Attribute interface
      //-------------------------------------------------------------------------
      
      public QName getQName() {
8x           return qname;
      }
      
      public String getValue() {
0x           return text;
      }
      
      public void setValue(String text) {
0x           validate(text);
          
0x           this.text = text;        
0x           this.data = convertToValue(text);
0x       }
      
      public Object getData() {
8x           return data;
      }
      
      public void setData(Object data) {
0x           String s = datatype.convertToLexicalValue( data, this );
0x           validate(s);
0x           this.text = s;        
0x           this.data = data;
0x       }
      
      public Element getParent() {
0x           return parent;
      }
 
      public void setParent(Element parent) {
8x           this.parent = parent;
8x       }
      
      public boolean supportsParent() {
0x           return true;
      }
      
      public boolean isReadOnly() {
0x           return false;
      }
      
      
      // Implementation methods
      //-------------------------------------------------------------------------       
      protected void validate(String text) throws IllegalArgumentException {
          try {
0x               datatype.checkValid(text, this);
          }
          catch (DatatypeException e) {
0x               throw new IllegalArgumentException( e.getMessage() );
0x           }
0x       }
      
      protected Object convertToValue(String text) {
1/2 8x           if ( datatype instanceof DatabindableDatatype ) {
8x               DatabindableDatatype bindable = (DatabindableDatatype) datatype;
8x               return bindable.createJavaObject( text, this );
          }
          else {
0x               return datatype.createValue( text, this );
          }
      }
  }
 
 
 
 
  /*
   * Redistribution and use of this software and associated documentation
   * ("Software"), with or without modification, are permitted provided
   * that the following conditions are met:
   *
   * 1. Redistributions of source code must retain copyright
   *    statements and notices.  Redistributions must also contain a
   *    copy of this document.
   *
   * 2. Redistributions in binary form must reproduce the
   *    above copyright notice, this list of conditions and the
   *    following disclaimer in the documentation and/or other
   *    materials provided with the distribution.
   *
   * 3. The name "DOM4J" must not be used to endorse or promote
   *    products derived from this Software without prior written
   *    permission of MetaStuff, Ltd.  For written permission,
   *    please contact dom4j-info@metastuff.com.
   *
   * 4. Products derived from this Software may not be called "DOM4J"
   *    nor may "DOM4J" appear in their names without prior written
   *    permission of MetaStuff, Ltd. DOM4J is a registered
   *    trademark of MetaStuff, Ltd.
   *
   * 5. Due credit should be given to the DOM4J Project - 
   *    http://www.dom4j.org
   *
   * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
   * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   * OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
   *
   * $Id: DatatypeAttribute.java,v 1.7 2004/06/25 08:03:34 maartenc Exp $
   */