All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.xml.XMLAttribute Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.xml;

import org.w3c.dom.*;

import com.hfg.util.StringUtil;

//------------------------------------------------------------------------------
/**
  
  @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------


public class XMLAttribute implements Node, Cloneable, Comparable
{

   //###########################################################################
   // PRIVATE FIELDS
   //###########################################################################

   private String       mLocalName;
   private String       mValue;
   private XMLNamespace mNamespace;
   private XMLTag       mElement;   // Reference to the element to which this attribute belongs.

   //###########################################################################
   // CONSTRUCTORS
   //###########################################################################

   //---------------------------------------------------------------------------
   public XMLAttribute(String inName, Object inValue)
   {
      setName(inName);
      setValue(inValue);
   }

   //---------------------------------------------------------------------------
   public XMLAttribute(String inName, Object inValue, XMLNamespace inNamespace)
   {
      this(inName, inValue);
      mNamespace = inNamespace;
   }

   //---------------------------------------------------------------------------
   public XMLAttribute(XMLName inName, Object inValue)
   {
      setName(inName.getLocalName());
      mNamespace = inName.getNamespace();
      setValue(inValue);
   }



   //###########################################################################
   // PUBLIC METHODS
   //###########################################################################


   //---------------------------------------------------------------------------
   @Override
   public XMLAttribute clone()
   {
      XMLAttribute obj = null;
      try 
      {
         obj = (XMLAttribute) super.clone(); 
      }
      catch (CloneNotSupportedException e)
      {
         throw new RuntimeException(e.toString());
      }
      
      return obj;
   }
   
   //---------------------------------------------------------------------------
   /**
    XMLAttribute equality and comparison is based on the name.
    */
   public int compareTo(XMLAttribute inAttr2)
   {
      if (null == inAttr2)
      {
         return 1;
      }
       
      return getName().compareTo(inAttr2.getName());
   }


   //---------------------------------------------------------------------------
   public String getName()
   {
      return mLocalName;
   }
	
   //--------------------------------------------------------------------------
   /**
     Sets the attribute's local name
    */
   public void setName(String inName)
   {
      if (null == inName)
      {
         throw new InvalidXMLNameException("XML attribute names cannot be set to null!");
      }

      // Is there a namespace prefix on the name?
      int index = inName.indexOf(":");
      if (index > 0)
      {
         setNamespace(XMLNamespace.getNamespace(inName.substring(0, index), null));
         inName = inName.substring(index + 1);
      }

      // Check the name for XML validity.
      XMLUtil.checkXMLNameValidity(inName);
        
      mLocalName = inName;
   }


   //---------------------------------------------------------------------------
   public String getQualifiedName()
   {
      return (mNamespace != null && StringUtil.isSet(mNamespace.getPrefix()) ? mNamespace.getPrefix() + ":" : "") + mLocalName;
   }

   //---------------------------------------------------------------------------
   public String getValue()
   {
      return mValue;
   }

   //---------------------------------------------------------------------------
   public void setValue(Object inValue)
   {
      mValue = null;
      if (inValue != null)
      {
         mValue = inValue.toString();
      }
   }

   //---------------------------------------------------------------------------
   public String getUnscapedValue()
   {
      return XMLUtil.unescapeAttributeValue(mValue);
   }

   //---------------------------------------------------------------------------
   public String getEscapedValue()
   {
      return XMLUtil.escapeAttributeValue(mValue);
   }

   //---------------------------------------------------------------------------
   public XMLNamespace getNamespace()
   {
      // Dynamically look up the chain for the first namespace instance. XXXXXXXXXXXXX
      return mNamespace;
   }

   //---------------------------------------------------------------------------
   public void setNamespace(XMLNamespace inNamespace)
   {
      // Dynamically look up the chain for the first namespace instance. XXXXXXXXXXXXX
      mNamespace = inNamespace;
   }

   // Methods for the Node interface.

   //---------------------------------------------------------------------------
   /**
    Returns the attribute name.
    */
   public String getNodeName()
   {
      return mLocalName;
   }

   //---------------------------------------------------------------------------
   public String getNodeValue() throws DOMException
   {
      return getValue();
   }

   //---------------------------------------------------------------------------
   public void setNodeValue(String inValue) throws DOMException
   {
      setValue(inValue);
   }

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

   //---------------------------------------------------------------------------
   public Node getParentNode()
   {
      return null;
   }

   //---------------------------------------------------------------------------
   public NodeList getChildNodes()
   {
      return null;  //TODO
   }

   //---------------------------------------------------------------------------
   public Node getFirstChild()
   {
      return null;
   }

   //---------------------------------------------------------------------------
   public Node getLastChild()
   {
      return null;
   }

   //---------------------------------------------------------------------------
   public Node getPreviousSibling()
   {
      return null;  //TODO
   }

   //---------------------------------------------------------------------------
   public Node getNextSibling()
   {
      return null;  //TODO
   }

   //---------------------------------------------------------------------------
   public NamedNodeMap getAttributes()
   {
      return null;
   }

   //---------------------------------------------------------------------------
   public Document getOwnerDocument()
   {
      return null;  //TODO
   }

   //---------------------------------------------------------------------------
   public Node insertBefore(Node node, Node node1) throws DOMException
   {
      throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
                             "insertBefore() cannot be used on an attribute node!");
   }

   //---------------------------------------------------------------------------
   public Node replaceChild(Node node, Node node1) throws DOMException
   {
      throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
                             "replaceChild() cannot be used on an attribute node!");
   }

   //---------------------------------------------------------------------------
   public Node removeChild(Node node) throws DOMException
   {
      throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
                             "removeChild() cannot be used on an attribute node!");
   }

   //---------------------------------------------------------------------------
   public Node appendChild(Node node) throws DOMException
   {
      throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
                             "appendChild() cannot be used on an attribute node!");
   }

   //---------------------------------------------------------------------------
   public boolean hasChildNodes()
   {
      return false;
   }

   //---------------------------------------------------------------------------
   public Node cloneNode(boolean b)
   {
      return clone();
   }

   //---------------------------------------------------------------------------
   public void normalize()
   {

   }

   //---------------------------------------------------------------------------
   public boolean isSupported(String inFeature, String inVersion)
   {
      return false;
   }

   //---------------------------------------------------------------------------
   /**
     Convenience method to get the namespace URI
    */
   public String getNamespaceURI()
   {
       String uri = null;

       XMLNamespace namespace = getNamespace();
       if (namespace != null)
       {
          uri = namespace.getURI();
       }

       return uri;
   }

   //---------------------------------------------------------------------------
   /**
    The namespace prefix of this attribute, or null if it is unspecified.
    */
   public String getPrefix()
   {
       String prefix = null;

       XMLNamespace namespace = getNamespace();
       if (namespace != null)
       {
          prefix = namespace.getPrefix();
       }

       return prefix;
   }

   //---------------------------------------------------------------------------
   /**
    The namespace prefix of this attribute, or null if it is unspecified.
    */
   public void setPrefix(String inValue) throws DOMException
   {
      XMLNamespace namespace = (inValue != null ? XMLNamespace.getNamespaceViaPrefix(inValue) : null);
      setNamespace(namespace);
   }

   //---------------------------------------------------------------------------
   /**
    Returns the local part of the qualified name of this node.
    */
   public String getLocalName()
   {
      return mLocalName;
   }

   //---------------------------------------------------------------------------
   public boolean hasAttributes()
   {
      return false;
   }

   //---------------------------------------------------------------------------
   public String getBaseURI()
   {
      return null;  // TODO
   }

   //---------------------------------------------------------------------------
   public short compareDocumentPosition(Node node) throws DOMException
   {
      return 0; // TODO
   }

   //---------------------------------------------------------------------------
   public String getTextContent() throws DOMException
   {
      return null;
   }

   //---------------------------------------------------------------------------
   public void setTextContent(String string) throws DOMException
   {

   }

   //---------------------------------------------------------------------------
   public boolean isSameNode(Node node)
   {
      return false;
   }

   //---------------------------------------------------------------------------
   public String lookupPrefix(String string)
   {
      return null;
   }

   //---------------------------------------------------------------------------
   public boolean isDefaultNamespace(String string)
   {
      return false;
   }

   //---------------------------------------------------------------------------
   /**
    Look up the namespace URI associated to the given prefix, starting from this node.
    */
   public String lookupNamespaceURI(String inPrefix)
   {
      return null;
   }

   //---------------------------------------------------------------------------
   public boolean isEqualNode(Node node)
   {
      return false;
   }

   //---------------------------------------------------------------------------
   public Object getFeature(String string, String string1)
   {
      return null;
   }

   //---------------------------------------------------------------------------
   public Object setUserData(String string, Object object, UserDataHandler userDataHandler)
   {
      return null;
   }

   //---------------------------------------------------------------------------
   public Object getUserData(String string)
   {
      return null;
   }

   //###########################################################################
   // PROTECTED METHODS
   //###########################################################################

   //---------------------------------------------------------------------------
   protected void setElement(XMLTag inOwner)
   {
      mElement = inOwner;
   }

   //---------------------------------------------------------------------------
   protected XMLTag getElement()
   {
      return mElement;
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy