/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*
* $Id: NamespaceMap.java 511426 2007-02-25 03:25:02Z vgritsenko $
*/
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 $Revision: 511426 $, $Date: 2007-02-24 22:25:02 -0500 (Sat, 24 Feb 2007) $
*/
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 = 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("xmlns:" + 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;
}
}