fix TreeMap NPE in XML namespace context

This commit is contained in:
Jörg Prante 2018-01-30 09:26:40 +01:00
parent 6f932191b6
commit c2e4bb8646
2 changed files with 18 additions and 11 deletions

View file

@ -27,14 +27,17 @@ public class XmlNamespaceContext implements NamespaceContext {
private static final XmlNamespaceContext DEFAULT_CONTEXT = newDefaultInstance(); private static final XmlNamespaceContext DEFAULT_CONTEXT = newDefaultInstance();
// sort namespace by length in descending order, useful for compacting prefix // sort namespace by length in descending order, useful for compacting prefix
protected final SortedMap<String, String> namespaces = new TreeMap<>(); private final SortedMap<String, String> namespaces;
private final SortedMap<String, Set<String>> prefixes = new TreeMap<>(); private final SortedMap<String, Set<String>> prefixes;
protected XmlNamespaceContext() { protected XmlNamespaceContext() {
this.namespaces = new TreeMap<>();
this.prefixes = new TreeMap<>();
} }
protected XmlNamespaceContext(ResourceBundle bundle) { protected XmlNamespaceContext(ResourceBundle bundle) {
this();
Enumeration<String> en = bundle.getKeys(); Enumeration<String> en = bundle.getKeys();
while (en.hasMoreElements()) { while (en.hasMoreElements()) {
String prefix = en.nextElement(); String prefix = en.nextElement();
@ -79,6 +82,8 @@ public class XmlNamespaceContext implements NamespaceContext {
} }
public void addNamespace(String prefix, String namespace) { public void addNamespace(String prefix, String namespace) {
if (prefix != null && namespace != null) {
synchronized (namespaces) {
namespaces.put(prefix, namespace); namespaces.put(prefix, namespace);
if (prefixes.containsKey(namespace)) { if (prefixes.containsKey(namespace)) {
prefixes.get(namespace).add(prefix); prefixes.get(namespace).add(prefix);
@ -88,6 +93,8 @@ public class XmlNamespaceContext implements NamespaceContext {
prefixes.put(namespace, set); prefixes.put(namespace, set);
} }
} }
}
}
public SortedMap<String, String> getNamespaces() { public SortedMap<String, String> getNamespaces() {
return namespaces; return namespaces;
@ -98,7 +105,7 @@ public class XmlNamespaceContext implements NamespaceContext {
if (prefix == null) { if (prefix == null) {
return null; return null;
} }
return namespaces.containsKey(prefix) ? namespaces.get(prefix) : null; return namespaces.getOrDefault(prefix, null);
} }
@Override @Override

View file

@ -1,6 +1,6 @@
group = org.xbib group = org.xbib
name = content name = content
version = 1.2.2 version = 1.2.3
jackson.version = 2.8.11 jackson.version = 2.8.11
xbib-net.version = 1.0.0 xbib-net.version = 1.0.0