com.squeakysand.commons.xml.SimpleNamespaceContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of squeakysand-commons Show documentation
Show all versions of squeakysand-commons Show documentation
Classes, interfaces and enums that assist with everyday Java development tasks.
The newest version!
/*
* Copyright 2010-2012 Craig S. Dickson (http://craigsdickson.com)
*
* 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.
*/
package com.squeakysand.commons.xml;
import com.squeakysand.commons.lang.ToStringHelper;
import com.squeakysand.commons.util.CollectionUtils;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Implementation of the {@link NamespaceContext} interface that takes care of the basic namespace mappings (xml, xmlns)
* and allows developers to just bind any additional namespaces as necessary.
*
* @author Craig S. Dickson
*/
public class SimpleNamespaceContext implements NamespaceContext {
private static final Logger LOG = LoggerFactory.getLogger(SimpleNamespaceContext.class);
private Map mappings;
/**
* Constructs a {@link NamespaceContext} that can handle the basic XML namespaces.
*/
public SimpleNamespaceContext() {
mappings = new HashMap();
mappings.put(XMLConstants.DEFAULT_NS_PREFIX, XMLConstants.NULL_NS_URI);
mappings.put(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI);
mappings.put(XMLConstants.XMLNS_ATTRIBUTE, XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
if (LOG.isDebugEnabled()) {
LOG.debug("mappings = {}", ToStringHelper.toString(mappings));
}
}
/**
* Constructs a {@link NamespaceContext} that can handle the basic XML namespaces and also binds a URI to
* the default prefix.
*
* @param defaultNamespaceURI the URI to use, cannot be null.
*/
public SimpleNamespaceContext(String defaultNamespaceURI) {
this();
if (defaultNamespaceURI == null) {
throw new IllegalArgumentException("defaultNamespaceURI cannot be null");
} else {
mappings.put(XMLConstants.DEFAULT_NS_PREFIX, defaultNamespaceURI);
}
if (LOG.isDebugEnabled()) {
LOG.debug("mappings = {}", ToStringHelper.toString(mappings));
}
}
/**
* Adds an additional namespace mapping to the context.
*
* @param prefix the prefix to use, cannot be null.
* @param namespaceURI the URI to use, cannot be null.
*/
public void bind(String prefix, String namespaceURI) {
if (prefix == null) {
throw new IllegalArgumentException("prefix cannot be null");
} else if (namespaceURI == null) {
throw new IllegalArgumentException("namespaceURI cannot be null");
} else {
mappings.put(prefix, namespaceURI);
}
if (LOG.isDebugEnabled()) {
LOG.debug("mappings = {}", ToStringHelper.toString(mappings));
}
}
/**
* Binds a URI to the default prefix.
*
* @param namespaceURI the URI to use, cannot be null.
*/
public void bindDefaultNamespace(String namespaceURI) {
if (namespaceURI == null) {
throw new IllegalArgumentException("namespaceURI cannot be null");
} else {
mappings.put(XMLConstants.DEFAULT_NS_PREFIX, namespaceURI);
}
if (LOG.isDebugEnabled()) {
LOG.debug("mappings = {}", ToStringHelper.toString(mappings));
}
}
@Override
public String getNamespaceURI(String prefix) {
if (prefix == null) {
throw new IllegalArgumentException("prefix cannot be null");
}
String result = XMLConstants.NULL_NS_URI;
if (mappings.containsKey(prefix)) {
result = mappings.get(prefix);
}
LOG.debug("returning namespaceURL '{}' for prefix '{}'", result, prefix);
return result;
}
@Override
public String getPrefix(String namespaceURI) {
if (namespaceURI == null) {
throw new IllegalArgumentException("namespaceURI cannot be null");
}
String result = CollectionUtils.getKeyByValue(mappings, namespaceURI);
LOG.debug("returning prefix '{}' for namespaceURI '{}'", result, namespaceURI);
return result;
}
@Override
public Iterator getPrefixes(String string) {
return mappings.keySet().iterator();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy