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

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

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

import java.util.*;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.DOMException;

import com.hfg.util.collection.CollectionUtil;


public class XMLAttributeMap implements NamedNodeMap, Set
{

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

   // Stored internally as a SortedMap so that attributes will be output consistently
   // (alphabetically).
   private TreeMap mMap = new TreeMap();


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


   // Methods for interface NamedNodeMap

   //---------------------------------------------------------------------------
   public Node getNamedItem(String inName)
   {
      return mMap.get(inName);
   }

   //---------------------------------------------------------------------------
   public Node setNamedItem(Node inNode) throws DOMException
   {
      Node result = null;
      if (inNode != null)
      {
         if (inNode.getNodeType() != Node.ATTRIBUTE_NODE)
         {
            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
                                   "The added node must be an attribute!");
         }

         XMLAttribute attr = null;
         if (inNode instanceof XMLAttribute)
         {
            attr = (XMLAttribute) inNode;
         }
         else
         {
            attr = new XMLAttribute(inNode.getNodeName(), inNode.getNodeValue());
            // TODO: attitional things like namespace that need to be passed?
         }

         result = mMap.put(attr.getName(), attr);
      }

      return result;
   }

   //---------------------------------------------------------------------------
   public Node removeNamedItem(String inName) throws DOMException
   {
      return mMap.remove(inName);
   }

   //---------------------------------------------------------------------------
   public Node item(int i)
   {
      Node node = null;
      int index = 0;
      for (XMLAttribute attr : mMap.values())
      {
         if (index == i)
         {
            node = attr;
            break;
         }
         index++;
      }

      return node;
   }

   //---------------------------------------------------------------------------
   public int getLength()
   {
      return mMap.size();
   }

   //---------------------------------------------------------------------------
   public Node getNamedItemNS(String inNamespaceURI, String inLocalName) throws DOMException
   {
      return null;  //To change body of implemented methods use File | Settings | File Templates.
   }

   //---------------------------------------------------------------------------
   public Node setNamedItemNS(Node node) throws DOMException
   {
      return null;  //To change body of implemented methods use File | Settings | File Templates.
   }

   //---------------------------------------------------------------------------
   public Node removeNamedItemNS(String inNamespaceURI, String inLocalName) throws DOMException
   {
      return null;  //To change body of implemented methods use File | Settings | File Templates.
   }


   // Methods for interface Set

   //---------------------------------------------------------------------------
   public int size()
   {
      return mMap.size();
   }

   //---------------------------------------------------------------------------
   public boolean isEmpty()
   {
      return mMap.isEmpty();
   }

   //---------------------------------------------------------------------------
   public boolean contains(Object inObject)
   {
      return mMap.containsValue(inObject);
   }

   //---------------------------------------------------------------------------
   public Iterator iterator()
   {
      return mMap.values().iterator();
   }

   //---------------------------------------------------------------------------
   public Object[] toArray()
   {
      return mMap.values().toArray();
   }

   //---------------------------------------------------------------------------
   public T[] toArray(T[] ts)
   {
      return mMap.values().toArray(ts);
   }

   //---------------------------------------------------------------------------
   public boolean add(XMLAttribute inXMLAttribute)
   {
      boolean result = false;
      if (inXMLAttribute != null)
      {
         result = mMap.containsKey(inXMLAttribute.getName());
         mMap.put(inXMLAttribute.getName(), inXMLAttribute);
      }

      return result;
   }

   //---------------------------------------------------------------------------
   public boolean remove(Object inObject)
   {
      boolean result = false;
      if (mMap.containsValue(inObject))
      {
         XMLAttribute attr = (XMLAttribute) inObject;
         mMap.remove(attr.getName());
         result = true;
      }

      return result;
   }

   //---------------------------------------------------------------------------
   public boolean containsAll(Collection inObjects)
   {
      boolean result = true;
      if (CollectionUtil.hasValues(inObjects))
      {
         for (Object obj : inObjects)
         {
            // TODO: Note that this is comparing Object signatures. Should we compare attribute names?
            if (!mMap.containsValue(obj))
            {
               result = false;
            }
         }
      }

      return result;
   }

   //---------------------------------------------------------------------------
   public boolean addAll(Collection inXMLAttributes)
   {
      boolean changed = false;
      if (CollectionUtil.hasValues(inXMLAttributes))
      {
         for (XMLAttribute attr : inXMLAttributes)
         {
            if (!mMap.containsKey(attr.getName()))
            {
               changed = true;
            }

            mMap.put(attr.getName(), attr);
         }
      }

      return changed;
   }

   //---------------------------------------------------------------------------
   public boolean retainAll(Collection inObjects)
   {
      boolean changed = false;
      if (CollectionUtil.hasValues(inObjects))
      {
         for (XMLAttribute attr : mMap.values())
         {
            if (!inObjects.contains(attr))
            {
               mMap.remove(attr.getName());
               changed = true;
            }
         }
      }

      return changed;
   }

   //---------------------------------------------------------------------------
   public boolean removeAll(Collection inObjects)
   {
      boolean changed = false;
      if (CollectionUtil.hasValues(inObjects))
      {
         for (Object obj : inObjects)
         {
            if (mMap.containsValue(obj))
            {
               mMap.remove(((XMLAttribute)obj).getName());
               changed = true;
            }
         }
      }

      return changed;
   }

   //---------------------------------------------------------------------------
   public void clear()
   {
      mMap.clear();
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy