package org.dbxml.xml.dom;

/*
 * dbXML License, Version 1.0
 *
 *
 * Copyright (c) 1999-2001 The dbXML Group, L.L.C.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 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 end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by
 *        The dbXML Group (http://www.dbxml.com/)."
 *    Alternately, this acknowledgment may appear in the software
 *    itself, if and wherever such third-party acknowledgments normally
 *    appear.
 *
 * 4. The names "dbXML" and "The dbXML Group" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    info@dbxml.com.
 *
 * 5. Products derived from this software may not be called "dbXML",
 *    nor may "dbXML" appear in their name, without prior written
 *    permission of The dbXML Group.
 *
 * THIS SOFTWARE IS PROVIDED ``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 THE DBXML GROUP 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.
 *
 * $Id: AttrImpl.java,v 1.9 2001/08/03 06:34:24 tom Exp $
 */

import org.w3c.dom.*;

/**
 * AttrImpl
 */

public final class AttrImpl extends ContainerNodeImpl implements Attr {
   private boolean specified = true;
   private short symbolID = -1;

   public AttrImpl() {
   }
   
   public AttrImpl(NodeImpl parentNode, byte[] data, int pos, int len) {
      super(parentNode, data, pos, len);
   }

   public AttrImpl(NodeImpl parentNode, String nodeName, String nsURI, short symbolID, String nodeValue) {
      super(parentNode, false);
      this.nodeName = nodeName;
      this.nsURI = nsURI;
      this.symbolID = symbolID;
      this.nodeValue = nodeValue;
      childNodes.clear();
      TextImpl text = new TextImpl(this, false);
      text.nodeValue = nodeValue;
      childNodes.add(text);
   }
   
   public AttrImpl(NodeImpl parentNode, boolean dirty) {
      super(parentNode, dirty);
   }

   public AttrImpl(NodeImpl parentNode, String nodeName) {
      super(parentNode, true);
      this.nodeName = nodeName;
   }

   public short getNodeType() {
      return Node.ATTRIBUTE_NODE;
   }

   /**
    * Returns the name of this attribute.
    */
   public String getName() {
      return getNodeName();
   }

   public void setSymbolID(short symbolID) {
      this.symbolID = symbolID;
   }

   public short getSymbolID() {
      return symbolID;
   }

   public void setNodeValue(String nodeValue) throws DOMException {
      checkLoaded();
      checkReadOnly();
      childNodes.clear();
      TextImpl text = new TextImpl(this, false);
      text.nodeValue = nodeValue;
      childNodes.add(text);
      setDirty();
   }

   public void setValue(String value) {
      setNodeValue(value);
   }

   protected boolean isNodeTypeValid(short type) {
      return type == Node.TEXT_NODE ||
             type == Node.ENTITY_REFERENCE_NODE;
   }

   protected void checkLoaded() {
      if ( loaded )
         return;
      else
         loaded = true;
   }

   /**
    * If this attribute was explicitly given a value in the original document,
    * this is <code>true</code>; otherwise, it is <code>false</code>. Note
    * that the implementation is in charge of this attribute, not the user. If
    * the user changes the value of the attribute (even if it ends up having
    * the same value as the default value) then the <code>specified</code>
    * flag is automatically flipped to <code>true</code>.  To re-specify the
    * attribute as the default value from the DTD, the user must delete the
    * attribute. The implementation will then make a new attribute available
    * with <code>specified</code> set to <code>false</code> and the default
    * value (if one exists).
    * <br>In summary: If the attribute has an assigned value in the document
    * then  <code>specified</code> is <code>true</code>, and the value is the
    * assigned value. If the attribute has no assigned value in the document
    * and has  a default value in the DTD, then <code>specified</code> is
    * <code>false</code>,  and the value is the default value in the DTD. If
    * the attribute has no assigned value in the document and has  a value of
    * #IMPLIED in the DTD, then the  attribute does not appear  in the
    * structure model of the document.
    */
   public boolean getSpecified() {
      checkLoaded();
      return specified;
   }

   public void setSpecified(boolean specified) {
      checkLoaded();
      this.specified = specified;
   }

   /**
    * On retrieval, the value of the attribute is returned as a string.
    * Character and general entity references are replaced with their values.
    * <br>On setting, this creates a <code>Text</code> node with the unparsed
    * contents of the string.
    */
   public String getValue() {
      return getNodeValue();
   }

   public String getNodeValue() {
      StringBuffer sb = new StringBuffer();
      int size = childNodes.getLength();
      for ( int i = 0; i < size; i++ )
         sb.append(childNodes.item(i).getNodeValue());
      return sb.toString();
   }

   public Element getOwnerElement() {
      if ( parentNode.getNodeType() == Node.ELEMENT_NODE )
         return (Element)parentNode;
      else
         return null;
   }
}
