Skip to content

Commit d4a4e3c

Browse files
committed
Register pdfOcr events on kernel level. Treat inner namespaces in ContextManager correctly
Now iText could return a context for a.b event even if a.b context is registered DEVSIX-4170
1 parent 2cf618d commit d4a4e3c

File tree

3 files changed

+111
-9
lines changed

3 files changed

+111
-9
lines changed

kernel/src/main/java/com/itextpdf/kernel/counter/ContextManager.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ This file is part of the iText (R) project.
4949
import java.util.Arrays;
5050
import java.util.Collection;
5151
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;
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
}

kernel/src/main/java/com/itextpdf/kernel/counter/NamespaceConstant.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,6 @@ public class NamespaceConstant {
6363
public static final String PDF_HTML = ITEXT + ".html2pdf";
6464
public static final String PDF_INVOICE = ITEXT + ".zugferd";
6565
public static final String PDF_SWEEP = ITEXT + ".pdfcleanup";
66+
public static final String PDF_OCR = ITEXT + ".pdfocr";
67+
public static final String PDF_OCR_TESSERACT4 = PDF_OCR + ".tesseract4";
6668
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2020 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License version 3
8+
as published by the Free Software Foundation with the addition of the
9+
following permission added to Section 15 as permitted in Section 7(a):
10+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
11+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
12+
OF THIRD PARTY RIGHTS
13+
14+
This program is distributed in the hope that it will be useful, but
15+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
or FITNESS FOR A PARTICULAR PURPOSE.
17+
See the GNU Affero General Public License for more details.
18+
You should have received a copy of the GNU Affero General Public License
19+
along with this program; if not, see http://www.gnu.org/licenses or write to
20+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
22+
http://itextpdf.com/terms-of-use/
23+
24+
The interactive user interfaces in modified source and object code versions
25+
of this program must display Appropriate Legal Notices, as required under
26+
Section 5 of the GNU Affero General Public License.
27+
28+
In accordance with Section 7(b) of the GNU Affero General Public License,
29+
a covered work must retain the producer line in every PDF that is created
30+
or manipulated using iText.
31+
32+
You can be released from the requirements of the license by purchasing
33+
a commercial license. Buying such a license is mandatory as soon as you
34+
develop commercial activities involving the iText software without
35+
disclosing the source code of your own applications.
36+
These activities include: offering paid services to customers as an ASP,
37+
serving PDFs on the fly in a web application, shipping iText with a closed
38+
source product.
39+
40+
For more information, please contact iText Software Corp. at this
41+
42+
*/
43+
package com.itextpdf.kernel.counter;
44+
45+
import com.itextpdf.test.ExtendedITextTest;
46+
import com.itextpdf.test.annotations.type.UnitTest;
47+
48+
import org.junit.Assert;
49+
import org.junit.Test;
50+
import org.junit.experimental.categories.Category;
51+
52+
@Category(UnitTest.class)
53+
public class ContextManagerTest extends ExtendedITextTest {
54+
55+
@Test
56+
public void getRecognisedNamespaceForSpecificNamespaceTest() {
57+
String outerNamespaces = NamespaceConstant.PDF_OCR.toLowerCase();
58+
String innerNamespaces = NamespaceConstant.PDF_OCR_TESSERACT4.toLowerCase();
59+
60+
// Since both NamespaceConstant.PDF_OCR and NamespaceConstant.PDF_OCR_TESSERACT4 are registered
61+
// and the latter one begins with the former, we should check that correct namespaces are
62+
// recognized for each of them
63+
Assert.assertTrue(innerNamespaces.startsWith(outerNamespaces));
64+
Assert.assertEquals(outerNamespaces,
65+
ContextManager.getInstance().getRecognisedNamespace(outerNamespaces));
66+
Assert.assertEquals(innerNamespaces,
67+
ContextManager.getInstance().getRecognisedNamespace(innerNamespaces));
68+
}
69+
70+
@Test
71+
public void notRegisteredNamespaceTest() {
72+
String notRegisteredNamespace = "com.hello.world";
73+
74+
Assert.assertEquals(null,
75+
ContextManager.getInstance().getRecognisedNamespace(notRegisteredNamespace));
76+
}
77+
}

0 commit comments

Comments
 (0)