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: NamedNodeMapImpl.java,v 1.4 2001/05/23 00:36:15 tom Exp $
 */

import org.w3c.dom.*;
import java.util.*;

/**
 * NamedNodeMapImpl
 */

public final class NamedNodeMapImpl extends NodeListImpl implements NamedNodeMap {
   private HashMap map = new HashMap();

   public NamedNodeMapImpl(NodeImpl owner) {
      super(owner);
   }


   /**
    * Retrieves a node specified by name.
    * @param name Name of a node to retrieve.
    * @return A <code>Node</code> (of any type) with the specified name, or
    *   <code>null</code> if the specified name did not identify any node in
    *   the map.
    */
   public Node getNamedItem(String name) {
      return (Node)map.get(name);
   }

   /**
    * Adds a node using its <code>nodeName</code> attribute.
    * <br>As the <code>nodeName</code> attribute is used to derive the name
    * which the node must be stored under, multiple nodes of certain types
    * (those that have a "special" string value) cannot be stored as the names
    * would clash. This is seen as preferable to allowing nodes to be aliased.
    * @param arg A node to store in a named node map. The node will later be
    *   accessible using the value of the <code>nodeName</code> attribute of
    *   the node. If a node with that name is already present in the map, it
    *   is replaced by the new one.
    * @return If the new <code>Node</code> replaces an existing node with the
    *   same name  the previously existing <code>Node</code> is returned,
    *   otherwise <code>null</code> is returned.
    * @exception DOMException
    *   WRONG_DOCUMENT_ERR: Raised if <code>arg</code> was created from a
    *   different document than the one that created the
    *   <code>NamedNodeMap</code>.
    *   <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this
    *   <code>NamedNodeMap</code> is readonly.
    *   <br>INUSE_ATTRIBUTE_ERR: Raised if <code>arg</code> is an
    *   <code>Attr</code> that is already an attribute of another
    *   <code>Element</code> object. The DOM user must explicitly clone
    *   <code>Attr</code> nodes to re-use them in other elements.
    */
   public Node setNamedItem(Node arg) throws DOMException {
      String name = arg.getNodeName();
      Node node = (Node)map.get(name);
      if ( node != null ) {
         int idx = indexOf(node);
         set(idx, arg);
      }
      else
         add(arg);
      map.put(arg.getNodeName(), arg);
      return node;
   }

   /**
    * Removes a node specified by name. If the removed node is an
    * <code>Attr</code> with a default value it is immediately replaced.
    * @param name The name of a node to remove.
    * @return The node removed from the map or <code>null</code> if no node
    *   with such a name exists.
    * @exception DOMException
    *   NOT_FOUND_ERR: Raised if there is no node named <code>name</code> in
    *   the map.
    */
   public Node removeNamedItem(String name) throws DOMException {
      Node node = (Node)map.get(name);
      map.remove(name);
      remove(node);
      return node;
   }

   public Node getNamedItemNS(String namespaceURI, String localName) {
      Iterator enum = iterator();
      Node node;
      for ( int i = 0; enum.hasNext(); i++ ) {
         node = (Node)enum.next();
         String ns = node.getNamespaceURI();
         String ln = node.getLocalName();
         if ( ns != null
           && ln != null
           && ns.equals(namespaceURI)
           && ln.equals(localName) )
            return node;
      }
      return null;
   }

   public Node setNamedItemNS(Node arg) throws DOMException {
      return setNamedItem(arg);
   }

   public Node removeNamedItemNS(String namespaceURI, String localName) throws DOMException {
      Iterator enum = iterator();
      Node node;
      for ( int i = 0; enum.hasNext(); i++ ) {
         node = (Node)enum.next();
         if ( node.getNamespaceURI().equals(namespaceURI)
           && node.getLocalName().equals(localName) ) {
            return removeNamedItem(node.getNodeName());
         }
      }
      return null;
   }
}

