package com.dbxml.xml;

/*
 * dbXML - Native XML Database
 * Copyright (C) 1999-2004  The dbXML Group, L.L.C.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: NamespaceMap.java,v 1.3 2004/02/12 00:17:58 bradford Exp $
 */

import com.dbxml.xml.dom.DOMHelper;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * NamespaceMap is just a HashMap extension that provides some useful
 * Namespace related functionality.
 */

public final class NamespaceMap extends HashMap {
   private static final String XMLNS = "xmlns";
   private static final String XMLNSC = XMLNS + ':';
   private static final int XMLNS_LEN = XMLNS.length();

   private Element elem;

   public NamespaceMap() {
   }

   public NamespaceMap(Map map) {
      if ( map != null )
         putAll(map);
   }

   public static boolean isNamespaceDeclaration(String name) {
      if ( name.startsWith(XMLNS) )
         return name.length() == XMLNS_LEN || name.charAt(XMLNS_LEN) == ':';
      else
         return false;
   }

   public static boolean isNamespaceDeclaration(Attr attr) {
      return isNamespaceDeclaration(attr.getName());
   }

   public Element getContextNode() {
      if ( elem == null ) {
         Document d = DOMHelper.newDocument();
         elem = d.createElement("nsmap");
         d.appendChild(elem);
         Iterator i = entrySet().iterator();
         while ( i.hasNext() ) {
            Map.Entry entry = (Map.Entry)i.next();
            String pfx = (String)entry.getKey();
            String uri = (String)entry.getValue();
            if ( pfx.equals("") )
               elem.setAttribute(XMLNS, uri);
            else
               elem.setAttribute(XMLNSC + pfx, uri);
         }
      }
      return elem;
   }

   public void clearNamespaces() {
      clear();
      elem = null;
   }

   public String getDefaultNamespaceURI() {
      return (String)get("");
   }

   public String getNamespaceURI(String prefix) {
      return (String)get(prefix);
   }

   public void setDefaultNamespace(String uri) {
      put("", uri);
      elem = null;
   }

   public void setNamespace(String prefix, String uri) {
      put(prefix, uri);
      elem = null;
   }

   public void removeDefaultNamespace() {
      remove("");
      elem = null;
   }

   public void removeNamespace(String prefix) {
      remove(prefix);
      elem = null;
   }

   public void includeNamespaces(Map nsMap, boolean override) {
      Iterator newEntries = nsMap.entrySet().iterator();
      while ( newEntries.hasNext() ) {
         Map.Entry entry = (Map.Entry)newEntries.next();
         if ( !override && super.containsKey(entry.getKey()) )
            continue;
         super.put(entry.getKey(), entry.getValue());
      }
      elem = null;
   }
}
