11/*
22 AUTH | hwding
3- DATE | Sep 05 2017
3+ DATE | Sep 10 2017
44 DESC | text stamp remover for PDF files
5566 GITH | github.com/hwding
@@ -18,15 +18,16 @@ class TextStampRecognizer {
1818 private static boolean recognizeWithFont (
1919 @ NotNull String [] keywords ,
2020 @ NotNull byte [] inputText ,
21- @ NotNull Set <PDFont > pdFonts ) {
22- String bs = generateByteString (inputText );
21+ @ NotNull Set <PDFont > pdFonts ,
22+ @ NotNull boolean useStrict ) {
23+ String encodedInput = generateByteString (inputText );
2324 for (PDFont f : pdFonts ) {
2425 if (f == null ) continue ;
2526 for (String k : keywords ) {
2627 try {
27- byte [] encodedKeywords = f .encode (k );
28- if ( bs . contains ( generateByteString (encodedKeywords )))
29- return true ;
28+ byte [] encodedKeywordBytes = f .encode (k );
29+ final String encodedKeyword = generateByteString (encodedKeywordBytes );
30+ if ( checkDuplicate ( encodedInput , encodedKeyword , useStrict )) return true ;
3031 } catch (IOException | IllegalArgumentException ignored ) {
3132 }
3233 }
@@ -36,26 +37,38 @@ private static boolean recognizeWithFont(
3637
3738 private static boolean recognizePlain (
3839 @ NotNull String [] keywords ,
39- @ NotNull byte [] inputText
40+ @ NotNull byte [] inputText ,
41+ @ NotNull boolean useStrict
4042 ) {
41- for (String k : keywords ) {
42- if (new String (inputText ).contains (k )) return true ;
43+ for (String k : keywords )
44+ if (checkDuplicate (new String (inputText ), k , useStrict )) return true ;
45+ return false ;
46+ }
47+
48+ private static boolean checkDuplicate (
49+ @ NotNull String input ,
50+ @ NotNull String keyword ,
51+ @ NotNull boolean useStrict ) {
52+ if (useStrict ) {
53+ if (input .equals (keyword )) return true ;
54+ } else {
55+ if (input .contains (keyword )) return true ;
4356 }
4457 return false ;
4558 }
4659
4760 static boolean recognize (@ NotNull String [] keywords ,
4861 @ NotNull byte [] inputText ,
49- @ NotNull Set <PDFont > pdFonts ) {
50- return recognizePlain (keywords , inputText ) ||
51- recognizeWithFont (keywords , inputText , pdFonts );
62+ @ NotNull Set <PDFont > pdFonts ,
63+ @ NotNull boolean useStrict ) {
64+ return recognizePlain (keywords , inputText , useStrict ) ||
65+ recognizeWithFont (keywords , inputText , pdFonts , useStrict );
5266 }
5367
5468 private static String generateByteString (@ NotNull byte [] bytes ) {
5569 StringBuilder stringBuilder = new StringBuilder ();
56- for (byte b : bytes ) {
70+ for (byte b : bytes )
5771 stringBuilder .append (Byte .toString (b ));
58- }
5972 return stringBuilder .toString ();
6073 }
6174}
0 commit comments