/*
* 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 595817 2007-11-16 20:49:03Z 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;
/**
* NamespaceMap is just a HashMap extension that provides some useful
* Namespace related functionality.
*
* @version $Revision: 595817 $, $Date: 2007-11-16 15:49:03 -0500 (Fri, 16 Nov 2007) $
*/
public final class NamespaceMap extends HashMap {
private Element elem;
public NamespaceMap() {
}
public NamespaceMap(Map namespaces) {
for (Iterator i = namespaces.entrySet().iterator(); i.hasNext(); ) {
Map.Entry e = (Map.Entry) i.next();
setNamespace((String) e.getKey(), (String) e.getValue());
}
}
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;
}
}