From 5289b080a1959bfd359b4dd02be404b46cc90384 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=CC=88rg=20Prante?= Date: Tue, 30 Jan 2018 16:13:06 +0100 Subject: [PATCH] fix TreeMap NPE in XML namespace context --- .../content/resource/IRINamespaceContext.java | 52 +++++++++++-------- .../content/resource/XmlNamespaceContext.java | 5 +- gradle.properties | 2 +- 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/content-resource/src/main/java/org/xbib/content/resource/IRINamespaceContext.java b/content-resource/src/main/java/org/xbib/content/resource/IRINamespaceContext.java index e8e2306..b0a67e5 100644 --- a/content-resource/src/main/java/org/xbib/content/resource/IRINamespaceContext.java +++ b/content-resource/src/main/java/org/xbib/content/resource/IRINamespaceContext.java @@ -1,7 +1,6 @@ package org.xbib.content.resource; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Locale; import java.util.Map; @@ -22,11 +21,15 @@ public final class IRINamespaceContext extends XmlNamespaceContext { */ private static final String DEFAULT_RESOURCE = XmlNamespaceContext.class.getPackage().getName().replace('.', '/') + '/' + "namespace"; + private static IRINamespaceContext instance; + private static final IRINamespaceContext DEFAULT_CONTEXT = newInstance(DEFAULT_RESOURCE); + private List sortedNamespacesByPrefixLength; private IRINamespaceContext() { + super(); } private IRINamespaceContext(ResourceBundle bundle) { @@ -60,26 +63,30 @@ public final class IRINamespaceContext extends XmlNamespaceContext { @Override public void addNamespace(String prefix, String namespace) { super.addNamespace(prefix, namespace); - sortedNamespacesByPrefixLength = new ArrayList<>(getNamespaces().values()); - // sort from longest to shortest prefix for successful matching - Collections.sort(sortedNamespacesByPrefixLength, (s1, s2) -> { - Integer l1 = s1.length(); - Integer l2 = s2.length(); - return l2.compareTo(l1); - }); + synchronized (lock) { + sortedNamespacesByPrefixLength = new ArrayList<>(getNamespaces().values()); + // sort from longest to shortest prefix for successful matching + sortedNamespacesByPrefixLength.sort((s1, s2) -> { + Integer l1 = s1.length(); + Integer l2 = s2.length(); + return l2.compareTo(l1); + }); + } } public IRINamespaceContext add(Map map) { for (Map.Entry e : map.entrySet()) { super.addNamespace(e.getKey(), e.getValue()); } - sortedNamespacesByPrefixLength = new ArrayList<>(getNamespaces().values()); - // sort from longest to shortest prefix for successful matching - Collections.sort(sortedNamespacesByPrefixLength, (s1, s2) -> { - Integer l1 = s1.length(); - Integer l2 = s2.length(); - return l2.compareTo(l1); - }); + synchronized (lock) { + sortedNamespacesByPrefixLength = new ArrayList<>(getNamespaces().values()); + // sort from longest to shortest prefix for successful matching + sortedNamespacesByPrefixLength.sort((s1, s2) -> { + Integer l1 = s1.length(); + Integer l2 = s2.length(); + return l2.compareTo(l1); + }); + } return this; } @@ -100,12 +107,15 @@ public final class IRINamespaceContext extends XmlNamespaceContext { return null; } // drop fragment (useful for resource counters in fragments) - final String s = dropfragment ? new IRI(uri.getScheme(), uri.getSchemeSpecificPart(), null).toString() : uri.toString(); - // search from longest to shortest namespace prefix - if (sortedNamespacesByPrefixLength != null) { - for (String ns : sortedNamespacesByPrefixLength) { - if (s.startsWith(ns)) { - return getPrefix(ns) + ':' + s.substring(ns.length()); + String s = dropfragment ? new IRI(uri.getScheme(), uri.getSchemeSpecificPart(), null).toString() : uri.toString(); + synchronized (lock) { + // search from longest to shortest namespace prefix + if (sortedNamespacesByPrefixLength != null) { + for (String ns : sortedNamespacesByPrefixLength) { + if (s.startsWith(ns)) { + s = getPrefix(ns) + ':' + s.substring(ns.length()); + break; + } } } } 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 5f0b1a6..3ef27f8 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 @@ -31,9 +31,12 @@ public class XmlNamespaceContext implements NamespaceContext { private final SortedMap> prefixes; + protected final Object lock; + protected XmlNamespaceContext() { this.namespaces = new TreeMap<>(); this.prefixes = new TreeMap<>(); + this.lock = new Object(); } protected XmlNamespaceContext(ResourceBundle bundle) { @@ -83,7 +86,7 @@ public class XmlNamespaceContext implements NamespaceContext { public void addNamespace(String prefix, String namespace) { if (prefix != null && namespace != null) { - synchronized (namespaces) { + synchronized (lock) { namespaces.put(prefix, namespace); if (prefixes.containsKey(namespace)) { prefixes.get(namespace).add(prefix); diff --git a/gradle.properties b/gradle.properties index afd7a7e..b9f2211 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ group = org.xbib name = content -version = 1.2.3 +version = 1.2.4 jackson.version = 2.8.11 xbib-net.version = 1.0.0