From c2e4bb864684fa951f19250282348168d4c30fee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=CC=88rg=20Prante?= Date: Tue, 30 Jan 2018 09:26:40 +0100 Subject: [PATCH] fix TreeMap NPE in XML namespace context --- .../content/resource/XmlNamespaceContext.java | 27 ++++++++++++------- gradle.properties | 2 +- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/content-resource/src/main/java/org/xbib/content/resource/XmlNamespaceContext.java b/content-resource/src/main/java/org/xbib/content/resource/XmlNamespaceContext.java index a647fa6..5f0b1a6 100644 --- a/content-resource/src/main/java/org/xbib/content/resource/XmlNamespaceContext.java +++ b/content-resource/src/main/java/org/xbib/content/resource/XmlNamespaceContext.java @@ -27,14 +27,17 @@ public class XmlNamespaceContext implements NamespaceContext { private static final XmlNamespaceContext DEFAULT_CONTEXT = newDefaultInstance(); // sort namespace by length in descending order, useful for compacting prefix - protected final SortedMap namespaces = new TreeMap<>(); + private final SortedMap namespaces; - private final SortedMap> prefixes = new TreeMap<>(); + private final SortedMap> prefixes; protected XmlNamespaceContext() { + this.namespaces = new TreeMap<>(); + this.prefixes = new TreeMap<>(); } protected XmlNamespaceContext(ResourceBundle bundle) { + this(); Enumeration en = bundle.getKeys(); while (en.hasMoreElements()) { String prefix = en.nextElement(); @@ -79,13 +82,17 @@ public class XmlNamespaceContext implements NamespaceContext { } public void addNamespace(String prefix, String namespace) { - namespaces.put(prefix, namespace); - if (prefixes.containsKey(namespace)) { - prefixes.get(namespace).add(prefix); - } else { - Set set = new HashSet<>(); - set.add(prefix); - prefixes.put(namespace, set); + if (prefix != null && namespace != null) { + synchronized (namespaces) { + namespaces.put(prefix, namespace); + if (prefixes.containsKey(namespace)) { + prefixes.get(namespace).add(prefix); + } else { + Set set = new HashSet<>(); + set.add(prefix); + prefixes.put(namespace, set); + } + } } } @@ -98,7 +105,7 @@ public class XmlNamespaceContext implements NamespaceContext { if (prefix == null) { return null; } - return namespaces.containsKey(prefix) ? namespaces.get(prefix) : null; + return namespaces.getOrDefault(prefix, null); } @Override diff --git a/gradle.properties b/gradle.properties index dc55aa0..afd7a7e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ group = org.xbib name = content -version = 1.2.2 +version = 1.2.3 jackson.version = 2.8.11 xbib-net.version = 1.0.0