📄 configurablenamespacecontext.java
字号:
package com.esri.solutions.jitk.common.metadata;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
/**
* {@code ConfigurableNamespaceContext} is a custom {@link NamespaceContext}
* that is designed to be configured via a JSF configuration file or similar
* mechanism.
* <p>
* This is a fairly minimal implementation of {@code NamespaceContext}.
* {@link #getPrefix(String)} and {@link #getPrefixes(String)} always
* throw an {@link UnsupportedOperationException}. The reason for this
* is because this {@link NamespaceContext} was mainly written to support
* DOM-based XPath parsing, which does not require the aforementioned methods.
* </p>
*/
public class ConfigurableNamespaceContext implements NamespaceContext {
/**
* {@link Map} containing the namespaces.
*/
private Map<String, String> _namespaces = new HashMap<String, String>();
/**
* Default no-arg constructor.
*/
public ConfigurableNamespaceContext() {
_namespaces.put(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI);
}
/*
* (non-Javadoc)
*
* @see javax.xml.namespace.NamespaceContext#getNamespaceURI(java.lang.String)
*/
public String getNamespaceURI(String prefix) {
if (prefix == null) {
throw new NullPointerException("prefix cannot be null");
}
String uri = _namespaces.get(prefix);
if (uri == null) {
uri = "";
}
return uri;
}
/**
* {@inheritDoc}
*
* @see javax.xml.namespace.NamespaceContext#getPrefix(java.lang.String)
* @throws UnsupportedOperationException
* in every case.
*/
public String getPrefix(String uri) {
throw new UnsupportedOperationException();
}
/**
* {@inheritDoc}
*
* @see javax.xml.namespace.NamespaceContext#getPrefixes(java.lang.String)
* @throws UnsupportedOperationException
* in every case.
*/
@SuppressWarnings("unchecked")
public Iterator getPrefixes(String uri) {
throw new UnsupportedOperationException();
}
/**
* Adds the contents of a {@link Map} to the namespace registry.
* <p>
* The key in the {@link Map} parameter must be of type {@link String} and
* must contain the namespace prefix. The value must be a {@link String}
* that contains the namespace URI.
*
* @param namespaces
* {@code Map<String, String>} containing the namespaces. Cannot
* be {@code null}.
*/
public void setRegistry(Map<String, String> namespaces) {
if (namespaces == null) {
throw new NullPointerException("Namespaces cannot be null");
}
_namespaces.putAll(namespaces);
}
/**
* Adds an item to the namespace registry.
* @param prefix The prefix of the namespace that is being added to the registry. Cannot be {@code null}
* @param uri The namespace uri. Cannot be {@code null}.
*/
public void register(String prefix, String uri) {
if (prefix == null) {
throw new NullPointerException("prefix cannot be null");
}
if (uri == null) {
throw new NullPointerException("uri cannot be null");
}
_namespaces.put(prefix, uri);
}
/**
* Removes an item from the namespace registry.
* @param prefix The namespace prefix (i.e. "xml" or "gml")
* of the namespace that should be removed. Cannot be {@code null}.
*/
public void unregister(String prefix) {
if (prefix == null) {
throw new NullPointerException("prefix cannot be null");
}
_namespaces.remove(prefix);
}
/**
* Removes all items from the namespace registry.
*/
public void clear() {
_namespaces.clear();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -