@@ -49,8 +49,9 @@ This file is part of the iText (R) project.
49
49
import java .util .Arrays ;
50
50
import java .util .Collection ;
51
51
import 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 ;
54
55
55
56
/**
56
57
* The class that retrieves context of its invocation.
@@ -62,7 +63,7 @@ public class ContextManager {
62
63
private static final long SECURITY_ERROR_LOGGING_INTERVAL = 60000 ;
63
64
private volatile long securityErrorLastLogged = 0 ;
64
65
65
- private final Map <String , IContext > contextMappings = new ConcurrentHashMap <>();
66
+ private final SortedMap <String , IContext > contextMappings = new TreeMap <>(new LengthComparator () );
66
67
67
68
private ContextManager () {
68
69
registerGenericContext (Arrays .asList (
@@ -75,10 +76,17 @@ private ContextManager() {
75
76
NamespaceConstant .CORE_FORMS ,
76
77
NamespaceConstant .CORE_SXP ,
77
78
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 ());
82
90
}
83
91
84
92
/**
@@ -112,8 +120,11 @@ public IContext getContext(String className) {
112
120
return getNamespaceMapping (getRecognisedNamespace (className ));
113
121
}
114
122
115
- private String getRecognisedNamespace (String className ) {
123
+ String getRecognisedNamespace (String className ) {
116
124
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
117
128
for (String namespace : contextMappings .keySet ()) {
118
129
//Conversion to lowercase is done to be compatible with possible changes in case of packages/namespaces
119
130
if (className .toLowerCase ().startsWith (namespace )) {
@@ -138,8 +149,20 @@ private void registerGenericContext(Collection<String> namespaces, Collection<St
138
149
registerContext (namespace .toLowerCase (), context );
139
150
}
140
151
}
141
-
152
+
142
153
private void registerContext (String namespace , IContext context ) {
143
154
contextMappings .put (namespace , context );
144
155
}
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
+ }
145
168
}
0 commit comments