SimpleNamespaceContext implements all subtleties of the NamespaceContext contract

Issue: SPR-13713
(cherry picked from commit f8860e2)
This commit is contained in:
Juergen Hoeller
2015-12-21 20:37:44 +01:00
parent 9021f8fd71
commit 935d714333
2 changed files with 219 additions and 128 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,36 +16,38 @@
package org.springframework.util.xml;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
import org.springframework.util.Assert;
/**
* Simple {@code javax.xml.namespace.NamespaceContext} implementation. Follows the standard
* {@code NamespaceContext} contract, and is loadable via a {@code java.util.Map} or
* {@code java.util.Properties} object
* Simple {@code javax.xml.namespace.NamespaceContext} implementation.
* Follows the standard {@code NamespaceContext} contract, and is loadable
* via a {@code java.util.Map} or {@code java.util.Properties} object
*
* @author Arjen Poutsma
* @author Juergen Hoeller
* @since 3.0
*/
public class SimpleNamespaceContext implements NamespaceContext {
private Map<String, String> prefixToNamespaceUri = new HashMap<String, String>();
private final Map<String, String> prefixToNamespaceUri = new HashMap<String, String>();
private Map<String, List<String>> namespaceUriToPrefixes = new HashMap<String, List<String>>();
private final Map<String, Set<String>> namespaceUriToPrefixes = new HashMap<String, Set<String>>();
private String defaultNamespaceUri = "";
@Override
public String getNamespaceURI(String prefix) {
Assert.notNull(prefix, "prefix is null");
Assert.notNull(prefix, "No prefix given");
if (XMLConstants.XML_NS_PREFIX.equals(prefix)) {
return XMLConstants.XML_NS_URI;
}
@@ -53,29 +55,46 @@ public class SimpleNamespaceContext implements NamespaceContext {
return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
}
else if (XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)) {
return defaultNamespaceUri;
return this.defaultNamespaceUri;
}
else if (prefixToNamespaceUri.containsKey(prefix)) {
return prefixToNamespaceUri.get(prefix);
else if (this.prefixToNamespaceUri.containsKey(prefix)) {
return this.prefixToNamespaceUri.get(prefix);
}
return "";
}
@Override
public String getPrefix(String namespaceUri) {
List<?> prefixes = getPrefixesInternal(namespaceUri);
return prefixes.isEmpty() ? null : (String) prefixes.get(0);
Set<String> prefixes = getPrefixesSet(namespaceUri);
return (!prefixes.isEmpty() ? prefixes.iterator().next() : null);
}
@Override
public Iterator<String> getPrefixes(String namespaceUri) {
return getPrefixesInternal(namespaceUri).iterator();
return getPrefixesSet(namespaceUri).iterator();
}
private Set<String> getPrefixesSet(String namespaceUri) {
Assert.notNull(namespaceUri, "No namespaceUri given");
if (this.defaultNamespaceUri.equals(namespaceUri)) {
return Collections.singleton(XMLConstants.DEFAULT_NS_PREFIX);
}
else if (XMLConstants.XML_NS_URI.equals(namespaceUri)) {
return Collections.singleton(XMLConstants.XML_NS_PREFIX);
}
else if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceUri)) {
return Collections.singleton(XMLConstants.XMLNS_ATTRIBUTE);
}
else {
Set<String> prefixes = this.namespaceUriToPrefixes.get(namespaceUri);
return (prefixes != null ? Collections.unmodifiableSet(prefixes) : Collections.<String>emptySet());
}
}
/**
* Sets the bindings for this namespace context. The supplied map must consist of string key value pairs.
*
* @param bindings the bindings
* Set the bindings for this namespace context.
* The supplied map must consist of string key value pairs.
*/
public void setBindings(Map<String, String> bindings) {
for (Map.Entry<String, String> entry : bindings.entrySet()) {
@@ -84,8 +103,7 @@ public class SimpleNamespaceContext implements NamespaceContext {
}
/**
* Binds the given namespace as default namespace.
*
* Bind the given namespace as default namespace.
* @param namespaceUri the namespace uri
*/
public void bindDefaultNamespaceUri(String namespaceUri) {
@@ -93,70 +111,62 @@ public class SimpleNamespaceContext implements NamespaceContext {
}
/**
* Binds the given prefix to the given namespace.
*
* @param prefix the namespace prefix
* Bind the given prefix to the given namespace.
* @param prefix the namespace prefix
* @param namespaceUri the namespace uri
*/
public void bindNamespaceUri(String prefix, String namespaceUri) {
Assert.notNull(prefix, "No prefix given");
Assert.notNull(namespaceUri, "No namespaceUri given");
if (XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)) {
defaultNamespaceUri = namespaceUri;
this.defaultNamespaceUri = namespaceUri;
}
else {
prefixToNamespaceUri.put(prefix, namespaceUri);
getPrefixesInternal(namespaceUri).add(prefix);
}
}
/** Removes all declared prefixes. */
public void clear() {
prefixToNamespaceUri.clear();
}
/**
* Returns all declared prefixes.
*
* @return the declared prefixes
*/
public Iterator<String> getBoundPrefixes() {
return prefixToNamespaceUri.keySet().iterator();
}
private List<String> getPrefixesInternal(String namespaceUri) {
if (defaultNamespaceUri.equals(namespaceUri)) {
return Collections.singletonList(XMLConstants.DEFAULT_NS_PREFIX);
}
else if (XMLConstants.XML_NS_URI.equals(namespaceUri)) {
return Collections.singletonList(XMLConstants.XML_NS_PREFIX);
}
else if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceUri)) {
return Collections.singletonList(XMLConstants.XMLNS_ATTRIBUTE);
}
else {
List<String> list = namespaceUriToPrefixes.get(namespaceUri);
if (list == null) {
list = new ArrayList<String>();
namespaceUriToPrefixes.put(namespaceUri, list);
this.prefixToNamespaceUri.put(prefix, namespaceUri);
Set<String> prefixes = this.namespaceUriToPrefixes.get(namespaceUri);
if (prefixes == null) {
prefixes = new LinkedHashSet<String>();
this.namespaceUriToPrefixes.put(namespaceUri, prefixes);
}
return list;
prefixes.add(prefix);
}
}
/**
* Removes the given prefix from this context.
*
* Remove the given prefix from this context.
* @param prefix the prefix to be removed
*/
public void removeBinding(String prefix) {
if (XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)) {
defaultNamespaceUri = "";
this.defaultNamespaceUri = "";
}
else {
String namespaceUri = prefixToNamespaceUri.remove(prefix);
List<String> prefixes = getPrefixesInternal(namespaceUri);
prefixes.remove(prefix);
else if (prefix != null) {
String namespaceUri = this.prefixToNamespaceUri.remove(prefix);
if (namespaceUri != null) {
Set<String> prefixes = this.namespaceUriToPrefixes.get(namespaceUri);
if (prefixes != null) {
prefixes.remove(prefix);
if (prefixes.isEmpty()) {
this.namespaceUriToPrefixes.remove(namespaceUri);
}
}
}
}
}
/**
* Remove all declared prefixes.
*/
public void clear() {
this.prefixToNamespaceUri.clear();
this.namespaceUriToPrefixes.clear();
}
/**
* Return all declared prefixes.
*/
public Iterator<String> getBoundPrefixes() {
return this.prefixToNamespaceUri.keySet().iterator();
}
}