com.jamesmurty.utils.NamespaceContextImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-xmlbuilder Show documentation
Show all versions of java-xmlbuilder Show documentation
XML Builder is a utility that creates simple XML documents using relatively sparse Java code
The newest version!
package com.jamesmurty.utils;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.xml.namespace.NamespaceContext;
import org.w3c.dom.Element;
/**
* Mappings between prefix strings and namespace URI strings, as required to
* perform XPath queries on namespaced XML documents.
*
* @author jmurty
*/
public class NamespaceContextImpl implements NamespaceContext {
protected Element element = null;
protected Map prefixToNsUriMap = new HashMap();
protected Map> nsUriToPrefixesMap = new HashMap>();
/**
* Create an empty namespace context.
*/
public NamespaceContextImpl() {
}
/**
* Create a namespace context that will lookup namespace
* information in the given element.
*
* @param element
* Element in which to look up namespace information.
*/
public NamespaceContextImpl(Element element) {
this.element = element;
}
/**
* Add a custom mapping from prefix to a namespace. This mapping will
* override any mappings present in this class's XML Element (if provided).
*
* @param prefix
* the namespace's prefix. Use an empty string for the
* default prefix.
* @param namespaceURI
* the namespace URI to map.
*/
public void addNamespace(String prefix, String namespaceURI) {
this.prefixToNsUriMap.put(prefix, namespaceURI);
if (this.nsUriToPrefixesMap.get(namespaceURI) == null) {
this.nsUriToPrefixesMap.put(namespaceURI, new HashSet());
}
this.nsUriToPrefixesMap.get(namespaceURI).add(prefix);
}
public String getNamespaceURI(String prefix) {
String namespaceURI = this.prefixToNsUriMap.get(prefix);
if (namespaceURI == null && this.element != null) {
// Need null to find default namespace, not an empty string
if (prefix != null && prefix.length() == 0) {
prefix = null;
}
namespaceURI = this.element.lookupNamespaceURI(prefix);
}
return namespaceURI;
}
public String getPrefix(String namespaceURI) {
Set prefixes = this.nsUriToPrefixesMap.get(namespaceURI);
if (prefixes != null && prefixes.size() > 0) {
return prefixes.iterator().next();
}
if (this.element != null) {
return this.element.lookupPrefix(namespaceURI);
}
return null;
}
// Not implemented
@SuppressWarnings({ "rawtypes" })
public Iterator getPrefixes(String namespaceURI) {
return Collections.EMPTY_LIST.iterator();
}
}