Skip to content

Commit 62ff1ed

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 Autoported commit. Original commit hash: [4c1cf0b3a] Manual files: kernel/src/main/java/com/itextpdf/kernel/counter/NamespaceConstant.java
1 parent 9f11349 commit 62ff1ed

File tree

4 files changed

+96
-5
lines changed

4 files changed

+96
-5
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
using System;
44+
using iText.Test;
45+
46+
namespace iText.Kernel.Counter {
47+
public class ContextManagerTest : ExtendedITextTest {
48+
[NUnit.Framework.Test]
49+
public virtual void GetRecognisedNamespaceForSpecificNamespaceTest() {
50+
String outerNamespaces = NamespaceConstant.PDF_OCR.ToLowerInvariant();
51+
String innerNamespaces = NamespaceConstant.PDF_OCR_TESSERACT4.ToLowerInvariant();
52+
// Since both NamespaceConstant.PDF_OCR and NamespaceConstant.PDF_OCR_TESSERACT4 are registered
53+
// and the latter one begins with the former, we should check that correct namespaces are
54+
// recognized for each of them
55+
NUnit.Framework.Assert.IsTrue(innerNamespaces.StartsWith(outerNamespaces));
56+
NUnit.Framework.Assert.AreEqual(outerNamespaces, ContextManager.GetInstance().GetRecognisedNamespace(outerNamespaces
57+
));
58+
NUnit.Framework.Assert.AreEqual(innerNamespaces, ContextManager.GetInstance().GetRecognisedNamespace(innerNamespaces
59+
));
60+
}
61+
62+
[NUnit.Framework.Test]
63+
public virtual void NotRegisteredNamespaceTest() {
64+
String notRegisteredNamespace = "com.hello.world";
65+
NUnit.Framework.Assert.AreEqual(null, ContextManager.GetInstance().GetRecognisedNamespace(notRegisteredNamespace
66+
));
67+
}
68+
}
69+
}

itext/itext.kernel/itext/kernel/counter/ContextManager.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ source product.
4242
4343
*/
4444
using System;
45-
using System.Collections.Concurrent;
4645
using System.Collections.Generic;
4746
using iText.IO.Util;
4847
using iText.Kernel.Counter.Context;
@@ -53,8 +52,8 @@ public class ContextManager {
5352
private static readonly iText.Kernel.Counter.ContextManager instance = new iText.Kernel.Counter.ContextManager
5453
();
5554

56-
private readonly IDictionary<String, IContext> contextMappings = new ConcurrentDictionary<String, IContext
57-
>();
55+
private readonly SortedDictionary<String, IContext> contextMappings = new SortedDictionary<String, IContext
56+
>(new ContextManager.LengthComparator());
5857

5958
private ContextManager() {
6059
RegisterGenericContext(JavaUtil.ArraysAsList(NamespaceConstant.CORE_IO, NamespaceConstant.CORE_KERNEL, NamespaceConstant
@@ -69,6 +68,10 @@ private ContextManager() {
6968
.SingletonList(NamespaceConstant.PDF_INVOICE));
7069
RegisterGenericContext(JavaCollectionsUtil.SingletonList(NamespaceConstant.PDF_SWEEP), JavaCollectionsUtil
7170
.SingletonList(NamespaceConstant.PDF_SWEEP));
71+
RegisterGenericContext(JavaCollectionsUtil.SingletonList(NamespaceConstant.PDF_OCR_TESSERACT4), JavaCollectionsUtil
72+
.SingletonList(NamespaceConstant.PDF_OCR_TESSERACT4));
73+
RegisterGenericContext(JavaCollectionsUtil.SingletonList(NamespaceConstant.PDF_OCR), JavaCollectionsUtil.EmptyList
74+
<String>());
7275
}
7376

7477
/// <summary>Gets the singelton instance of this class</summary>
@@ -115,8 +118,11 @@ public virtual IContext GetContext(String className) {
115118
return GetNamespaceMapping(GetRecognisedNamespace(className));
116119
}
117120

118-
private String GetRecognisedNamespace(String className) {
121+
internal virtual String GetRecognisedNamespace(String className) {
119122
if (className != null) {
123+
// If both "a" and "a.b" namespaces are registered,
124+
// iText should consider the context of "a.b" for an "a.b" event,
125+
// that's why the contexts are sorted by the length of the namespace
120126
foreach (String @namespace in contextMappings.Keys) {
121127
//Conversion to lowercase is done to be compatible with possible changes in case of packages/namespaces
122128
if (className.ToLowerInvariant().StartsWith(@namespace)) {
@@ -145,5 +151,17 @@ private void RegisterGenericContext(ICollection<String> namespaces, ICollection<
145151
private void RegisterContext(String @namespace, IContext context) {
146152
contextMappings.Put(@namespace, context);
147153
}
154+
155+
private class LengthComparator : IComparer<String> {
156+
public virtual int Compare(String o1, String o2) {
157+
int lengthComparison = -JavaUtil.IntegerCompare(o1.Length, o2.Length);
158+
if (0 != lengthComparison) {
159+
return lengthComparison;
160+
}
161+
else {
162+
return string.CompareOrdinal(o1, o2);
163+
}
164+
}
165+
}
148166
}
149167
}

itext/itext.kernel/itext/kernel/counter/NamespaceConstant.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,9 @@ public class NamespaceConstant {
7474
public const String PDF_INVOICE = ITEXT + ".Zugferd";
7575

7676
public const String PDF_SWEEP = ITEXT + ".PdfCleanup";
77+
78+
public const String PDF_OCR = ITEXT + ".Pdfocr";
79+
80+
public const String PDF_OCR_TESSERACT4 = PDF_OCR + ".Tesseract4";
7781
}
7882
}

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2cf618d256e38f483918157a47a9184c98729f4e
1+
d4a4e3c555d69e0f913dd7cb49c77305a0b2fe0f

0 commit comments

Comments
 (0)