@@ -49,8 +49,9 @@ This file is part of the iText (R) project.
4949import java .util .Arrays ;
5050import java .util .Collection ;
5151import java .util .Collections ;
52- import java .util .Map ;
53- import java .util .concurrent .ConcurrentHashMap ;
52+ import java .util .Comparator ;
53+ import java .util .SortedMap ;
54+ import java .util .TreeMap ;
5455
5556/**
5657 * The class that retrieves context of its invocation.
@@ -62,7 +63,7 @@ public class ContextManager {
6263 private static final long SECURITY_ERROR_LOGGING_INTERVAL = 60000 ;
6364 private volatile long securityErrorLastLogged = 0 ;
6465
65- private final Map <String , IContext > contextMappings = new ConcurrentHashMap <>();
66+ private final SortedMap <String , IContext > contextMappings = new TreeMap <>(new LengthComparator () );
6667
6768 private ContextManager () {
6869 registerGenericContext (Arrays .asList (
@@ -75,10 +76,17 @@ private ContextManager() {
7576 NamespaceConstant .CORE_FORMS ,
7677 NamespaceConstant .CORE_SXP ,
7778 NamespaceConstant .CORE_SVG ), Collections .singletonList (NamespaceConstant .ITEXT ));
78- registerGenericContext (Collections .singletonList (NamespaceConstant .PDF_DEBUG ), Collections .singletonList (NamespaceConstant .PDF_DEBUG ));
79- registerGenericContext (Collections .singletonList (NamespaceConstant .PDF_HTML ), Collections .singletonList (NamespaceConstant .PDF_HTML ));
80- registerGenericContext (Collections .singletonList (NamespaceConstant .PDF_INVOICE ), Collections .singletonList (NamespaceConstant .PDF_INVOICE ));
81- registerGenericContext (Collections .singletonList (NamespaceConstant .PDF_SWEEP ), Collections .singletonList (NamespaceConstant .PDF_SWEEP ));
79+ registerGenericContext (Collections .singletonList (NamespaceConstant .PDF_DEBUG ),
80+ Collections .singletonList (NamespaceConstant .PDF_DEBUG ));
81+ registerGenericContext (Collections .singletonList (NamespaceConstant .PDF_HTML ),
82+ Collections .singletonList (NamespaceConstant .PDF_HTML ));
83+ registerGenericContext (Collections .singletonList (NamespaceConstant .PDF_INVOICE ),
84+ Collections .singletonList (NamespaceConstant .PDF_INVOICE ));
85+ registerGenericContext (Collections .singletonList (NamespaceConstant .PDF_SWEEP ),
86+ Collections .singletonList (NamespaceConstant .PDF_SWEEP ));
87+ registerGenericContext (Collections .singletonList (NamespaceConstant .PDF_OCR_TESSERACT4 ),
88+ Collections .singletonList (NamespaceConstant .PDF_OCR_TESSERACT4 ));
89+ registerGenericContext (Collections .singletonList (NamespaceConstant .PDF_OCR ), Collections .<String >emptyList ());
8290 }
8391
8492 /**
@@ -112,8 +120,11 @@ public IContext getContext(String className) {
112120 return getNamespaceMapping (getRecognisedNamespace (className ));
113121 }
114122
115- private String getRecognisedNamespace (String className ) {
123+ String getRecognisedNamespace (String className ) {
116124 if (className != null ) {
125+ // If both "a" and "a.b" namespaces are registered,
126+ // iText should consider the context of "a.b" for an "a.b" event,
127+ // that's why the contexts are sorted by the length of the namespace
117128 for (String namespace : contextMappings .keySet ()) {
118129 //Conversion to lowercase is done to be compatible with possible changes in case of packages/namespaces
119130 if (className .toLowerCase ().startsWith (namespace )) {
@@ -138,8 +149,20 @@ private void registerGenericContext(Collection<String> namespaces, Collection<St
138149 registerContext (namespace .toLowerCase (), context );
139150 }
140151 }
141-
152+
142153 private void registerContext (String namespace , IContext context ) {
143154 contextMappings .put (namespace , context );
144155 }
156+
157+ private static class LengthComparator implements Comparator <String > {
158+ @ Override
159+ public int compare (String o1 , String o2 ) {
160+ int lengthComparison = -Integer .compare (o1 .length (), o2 .length ());
161+ if (0 != lengthComparison ) {
162+ return lengthComparison ;
163+ } else {
164+ return o1 .compareTo (o2 );
165+ }
166+ }
167+ }
145168}
0 commit comments