1313
1414import java .io .FileNotFoundException ;
1515import java .io .IOException ;
16+ import java .util .Hashtable ;
1617
1718import coderarjob .kpdfsync .lib .clipparser .ParserResult .SupportedFields ;
19+ import coderarjob .kpdfsync .lib .clipparser .ParserResult .AnnotationType ;
20+ import coderarjob .kpdfsync .lib .clipparser .ParserResult .PageNumberType ;
1821
1922public class KindleParserV1 extends AbstractParser
2023{
21- public enum ParsingStages
22- {
23- TITLE ("Title" ),
24- FILE_OFFSET ("Annotation block offset in file" ),
25- ANNOTATION_TYPE ("Annotation Type" ),
26- PAGE_OR_LOCATION_NUMBER ("Page or location number" ),
27- PAGE_NUMBER_TYPE ("Page number type" ),
28- TEXT ("Text" ),
29- END_OF_BLOCK ("End of Block" );
30-
31- private final String _name ;
32- public String getName () { return _name ; }
33- private ParsingStages (String name ) { _name = name ; }
34- }
24+ private final int ANNOTATION_TYPE_WORD_POS = 2 ;
25+ private final int PAGE_NUMBER_TYPE_WORD_POS = 4 ;
26+ private final int PAGE_NUMBER_OR_LOCATION_WORD_POS = 5 ;
27+ private final String TERMINATION_LINE_PATTERN = "==========" ;
3528
3629 public KindleParserV1 (String fileName ) throws FileNotFoundException , IOException
3730 {
@@ -50,17 +43,48 @@ public String toString()
5043 }
5144
5245 /* Implementing abstract methods from AbstractParser*/
53- protected AbstractKindleParserConstants getKindleParserConstants ()
46+ protected boolean isTerminationLine (String linestr )
47+ {
48+ return linestr .toLowerCase ().equals (TERMINATION_LINE_PATTERN );
49+ }
50+
51+ /*protected AbstractKindleParserConstants getKindleParserConstants ()
5452 {
5553 AbstractKindleParserConstants constants = new AbstractKindleParserConstants () {
56- public int getAnnotationLineTypePosition () { return 2 ; }
57- public int getAnnotationLinePageNumberTypePosition () { return 4 ; }
58- public int getAnnotationLinePageOrLocationNumberPosition () { return 5 ; }
59- public String getTeminationLinePattern () { return "==========" ; }
54+ public ParserResultFieldsFilter<AnnotationType> getAnnotationTypeFilter(ParserResult res)
55+ {
56+ Hashtable<String, AnnotationType> ht = new Hashtable<>();
57+ ht.put("highlight", AnnotationType.HIGHLIGHT);
58+ ht.put("note", AnnotationType.NOTE);
59+ ht.put("bookmark", AnnotationType.BOOKMARK);
60+
61+ return new ParserResultFieldsFilter<> (2, ht);
62+ }
63+
64+ public ParserResultFieldsFilter<PageNumberType> getPageNumberTypeFilter(ParserResult res)
65+ {
66+ Hashtable<String, PageNumberType> ht = new Hashtable<>();
67+ ht.put("page", PageNumberType.PAGE_NUMBER);
68+ ht.put("location", PageNumberType.LOCATION_NUMBER);
69+
70+ return new ParserResultFieldsFilter<> (4, ht);
71+ }
72+
73+ public ParserResultFieldsFilter<Object> getPageOrLocationNumberFilter(ParserResult res)
74+ {
75+ return new ParserResultFieldsFilter<> (5, null);
76+ }
77+
78+ public ParserResultFieldsFilter<Boolean> getTerminationLineFilter()
79+ {
80+ Hashtable<String, Boolean> ht = new Hashtable<>();
81+ ht.put ("==========", true);
82+ return new ParserResultFieldsFilter<> (0, ht);
83+ }
6084 };
6185
6286 return constants;
63- }
87+ }*/
6488
6589 public String getParserVersion ()
6690 {
@@ -88,7 +112,9 @@ protected boolean parseLine(int lineIndex, ParserResult result)
88112 break ;
89113 case 1 :
90114 this .readLineWithProperEncoding ();
91- parseAnnotationLine (result );
115+ parseAnnotationType (result );
116+ parsePageNumberType (result );
117+ parsePageOrLocationNumber (result );
92118 break ;
93119 case 2 :
94120 this .readLineWithProperEncoding ();
@@ -115,11 +141,11 @@ protected void parseTitleLine (ParserResult result) throws IOException, ParserEx
115141 /* Read current line. Cannot be EOF.*/
116142 String linestr = this .lastLineRead ();
117143 if (linestr == null )
118- throw genParserException (ParsingStages .TITLE .getName ());
144+ throw genParserException (SupportedFields .TITLE .getName ());
119145
120146 boolean isValid = (linestr .length () > 0 );
121147 if (isValid == false )
122- throw genParserException (ParsingStages .TITLE .getName ());
148+ throw genParserException (SupportedFields .TITLE .getName ());
123149
124150 result .setFieldValue (SupportedFields .TITLE , linestr .trim ());
125151 result .setFieldValue (SupportedFields .FILE_OFFSET , String .valueOf (this .lastFilePointer ()));
@@ -128,57 +154,72 @@ protected void parseTitleLine (ParserResult result) throws IOException, ParserEx
128154 /**
129155 * Validates Book Annotation type line and adds to ParserResult.
130156 */
131- protected void parseAnnotationLine (ParserResult result ) throws IOException , ParserException
157+ protected void parseAnnotationType (ParserResult result ) throws IOException , ParserException
132158 {
133159 /* Read current line. Cannot be EOF.*/
134160 String linestr = this .lastLineRead ();
135161 if (linestr == null )
136- throw genParserException (ParsingStages .ANNOTATION_TYPE .getName ());
137-
138- boolean isValid = false ;
139- String value = "" ;
162+ throw genParserException (SupportedFields .ANNOTATION_TYPE .getName ());
140163
141164 /* Annotation Type */
142- value = trySplitString (linestr , " " , mConstants .getAnnotationLineTypePosition ());
143- isValid = (value != null )
144- && (value .toLowerCase ().equals ("highlight" )
145- || value .toLowerCase ().equals ("note" )
146- || value .toLowerCase ().equals ("bookmark" ));
165+ Hashtable <String , AnnotationType > ht = new Hashtable <>();
166+ ht .put ("highlight" , AnnotationType .HIGHLIGHT );
167+ ht .put ("note" , AnnotationType .NOTE );
168+ ht .put ("bookmark" , AnnotationType .BOOKMARK );
147169
148- if (isValid == false )
149- throw genParserException (ParsingStages .ANNOTATION_TYPE .getName ());
170+ String value = trySplitString (linestr , " " , ANNOTATION_TYPE_WORD_POS ).toLowerCase ();
171+
172+ AnnotationType annotationType = ht .getOrDefault (value ,AnnotationType .UNKNOWN );
173+ if (annotationType == AnnotationType .UNKNOWN )
174+ throw genParserException (SupportedFields .ANNOTATION_TYPE .getName ());
150175
151- result .setFieldValue (SupportedFields .ANNOTATION_TYPE , value );
152- String annotationType = value ;
176+ result .setFieldValue (SupportedFields .ANNOTATION_TYPE , annotationType .getName ());
177+ }
178+
179+ protected void parsePageNumberType (ParserResult result ) throws IOException , ParserException
180+ {
181+ String linestr = this .lastLineRead ();
153182
154183 /* Page Number Type */
155- value = trySplitString (linestr , " " , mConstants .getAnnotationLinePageNumberTypePosition ());
156- isValid = (value != null )
157- && (value .toLowerCase ().equals ("page" )
158- || value .toLowerCase ().equals ("location" ));
184+ Hashtable <String , PageNumberType > ht = new Hashtable <>();
185+ ht .put ("page" , PageNumberType .PAGE_NUMBER );
186+ ht .put ("location" , PageNumberType .LOCATION_NUMBER );
159187
160- if (isValid == false )
161- throw genParserException (ParsingStages .PAGE_NUMBER_TYPE .getName ());
188+ String value = trySplitString (linestr , " " , PAGE_NUMBER_TYPE_WORD_POS ).toLowerCase ();
162189
163- result .setFieldValue (SupportedFields .PAGE_NUMBER_TYPE , value );
190+ PageNumberType pageNumberType = ht .getOrDefault (value ,PageNumberType .UNKNOWN );
191+ if (pageNumberType == PageNumberType .UNKNOWN )
192+ throw genParserException (SupportedFields .PAGE_NUMBER_TYPE .getName ());
193+
194+ result .setFieldValue (SupportedFields .PAGE_NUMBER_TYPE , pageNumberType .getName ());
195+ }
196+
197+ protected void parsePageOrLocationNumber (ParserResult result )
198+ throws IOException , ParserException
199+ {
200+ boolean isValid = false ;
201+ String value = "" ;
202+
203+ String linestr = this .lastLineRead ();
204+ AnnotationType annotationType = result .annotationType ();
164205
165206 /* Page or Location Number */
166- value = trySplitString (linestr , " " , mConstants . getAnnotationLinePageOrLocationNumberPosition () );
207+ value = trySplitString (linestr , " " , PAGE_NUMBER_OR_LOCATION_WORD_POS );
167208 isValid = (value != null );
168209 if (isValid == false )
169- throw genParserException (ParsingStages .PAGE_OR_LOCATION_NUMBER .getName ());
210+ throw genParserException (SupportedFields .PAGE_OR_LOCATION_NUMBER .getName ());
170211
171- if (annotationType . toLowerCase (). equals ( "bookmark" ) == false )
212+ if (annotationType == AnnotationType . NOTE || annotationType == AnnotationType . HIGHLIGHT )
172213 {
173214 value = trySplitString (value , "-" , 0 );
174215 isValid = (value != null );
175216 if (isValid == false )
176- throw genParserException (ParsingStages .PAGE_OR_LOCATION_NUMBER .getName ());
217+ throw genParserException (SupportedFields .PAGE_OR_LOCATION_NUMBER .getName ());
177218 }
178219
179220 isValid = tryParseUnsigendInt (value );
180221 if (isValid == false )
181- throw genParserException (ParsingStages .PAGE_OR_LOCATION_NUMBER .getName ());
222+ throw genParserException (SupportedFields .PAGE_OR_LOCATION_NUMBER .getName ());
182223
183224 result .setFieldValue (SupportedFields .PAGE_OR_LOCATION_NUMBER , value );
184225 }
@@ -191,15 +232,15 @@ protected void parseTextLine (ParserResult result) throws IOException, ParserExc
191232 /* Read current line. Cannot be EOF.*/
192233 String linestr = this .lastLineRead ();
193234 if (linestr == null )
194- throw genParserException (ParsingStages .TEXT .getName ());
235+ throw genParserException (SupportedFields .TEXT .getName ());
195236
196237 boolean isValid = false ;
197- String annotationType = result .getFieldValue ( SupportedFields . ANNOTATION_TYPE ). toLowerCase ();
238+ AnnotationType annotationType = result .annotationType ();
198239
199240 /* There should be a blank line */
200241 isValid = (linestr .length () == 0 );
201242 if (isValid == false )
202- throw genParserException (ParsingStages .TEXT .getName ());
243+ throw genParserException (SupportedFields .TEXT .getName ());
203244
204245 /* Read the actual text, in the following lines */
205246 StringBuilder sb = new StringBuilder ();
@@ -208,11 +249,11 @@ protected void parseTextLine (ParserResult result) throws IOException, ParserExc
208249 sb .append (linestr + "\n " );
209250
210251 /* NOTE: This line can also be blank, in case of a bookmark annotation type.*/
211- if (annotationType . equals ( "bookmark" ) == false )
252+ if (annotationType == AnnotationType . NOTE || annotationType == AnnotationType . HIGHLIGHT )
212253 {
213254 isValid = (linestr .length () > 0 );
214255 if (isValid == false )
215- throw genParserException (ParsingStages .TEXT .getName ());
256+ throw genParserException (SupportedFields .TEXT .getName ());
216257 }
217258 }
218259
@@ -233,12 +274,12 @@ protected void parseTerminationLine (ParserResult result) throws IOException, Pa
233274 /* Read current line. Cannot be EOF.*/
234275 String linestr = this .lastLineRead ();
235276 if (linestr == null )
236- throw genParserException (ParsingStages . END_OF_BLOCK . getName () );
277+ throw genParserException ("Termination Line" );
237278
238279 /* Check for termination line. */
239- boolean isValid = isTerminationLine ( linestr );
280+ boolean isValid = linestr . equals ( TERMINATION_LINE_PATTERN );
240281 if (isValid == false )
241- throw genParserException (ParsingStages . END_OF_BLOCK . getName () );
282+ throw genParserException ("Termination Line" );
242283 }
243284
244285 protected String trySplitString (String s , String p , int index )
0 commit comments