/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* CVS $Id: NamespaceMap.java,v 1.10 2004/02/24 03:20:27 vgritsenko Exp $
*/
package org.apache.xindice.xml;
import org.apache.xindice.xml.dom.DocumentImpl;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Enumeration;
import java.util.Hashtable;
/**
* NamespaceMap is just a HashMap extension that provides some useful
* Namespace related functionality.
*
* @version CVS $Revision: 1.10 $, $Date: 2004/02/24 03:20:27 $
*/
public final class NamespaceMap extends HashMap {
private Element elem = null;
public NamespaceMap() {
}
public NamespaceMap(Hashtable namespaces) {
for (Enumeration keys = namespaces.keys(); keys.hasMoreElements(); ) {
final String key = (String) keys.nextElement();
setNamespace(key, (String) namespaces.get(key));
}
}
public Node getContextNode() {
if (elem == null) {
Document d = new DocumentImpl();
elem = d.createElement("nsmap");
d.appendChild(elem);
Iterator i = keySet().iterator();
while (i.hasNext()) {
String pfx = (String) i.next();
if (pfx.equals("")) {
elem.setAttribute("xmlns", (String) get(pfx));
} else {
elem.setAttribute("xmlns:" + pfx, (String) get(pfx));
}
}
}
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;
}
}