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,13 +82,17 @@ public class XmlNamespaceContext implements NamespaceContext {
} }
public void addNamespace(String prefix, String namespace) { public void addNamespace(String prefix, String namespace) {
namespaces.put(prefix, namespace); if (prefix != null && namespace != null) {
if (prefixes.containsKey(namespace)) { synchronized (namespaces) {
prefixes.get(namespace).add(prefix); namespaces.put(prefix, namespace);
} else { if (prefixes.containsKey(namespace)) {
Set<String> set = new HashSet<>(); prefixes.get(namespace).add(prefix);
set.add(prefix); } else {
prefixes.put(namespace, set); Set<String> set = new HashSet<>();
set.add(prefix);
prefixes.put(namespace, set);
}
}
} }
} }
@ -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