Skip to content

Commit d67ff53

Browse files
committed
ParserResults now independent of Parser field
[NOT FINAL] * ParserResults now do not interpret raw text from Parser. Parser now provides setFieldValue function values defined in ParserResults not and directly from the Clippings file. * New Parser working for older format of Clippings file.
1 parent 271ca20 commit d67ff53

File tree

6 files changed

+311
-94
lines changed

6 files changed

+311
-94
lines changed

src/coderarjob/kpdfsync/lib/clipparser/AbstractKindleParserConstants.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,28 @@
1010

1111
package coderarjob.kpdfsync.lib.clipparser;
1212

13-
public abstract class AbstractKindleParserConstants
14-
{
13+
import coderarjob.kpdfsync.lib.clipparser.ParserResult.AnnotationType;
14+
import coderarjob.kpdfsync.lib.clipparser.ParserResult.PageNumberType;
15+
import java.util.List;
1516

16-
/** When annotation line split by spaces, this is the index at which annotation type (Note or
17-
* Highlight) can be found.
17+
abstract class AbstractKindleParserConstants
18+
{
19+
/** Annotation type position and patters to identify annotation types.
1820
*/
19-
public abstract int getAnnotationLineTypePosition ();
21+
public abstract
22+
List<ParserResultFieldsFilter<AnnotationType>> getAnnotationTypeFilter(ParserResult res);
2023

21-
/** When annotation line split by spaces, this is the index at which Page number type (Page or
22-
* location) can be found.
24+
/** Page number type position and patters to identify annotation types.
2325
*/
24-
public abstract int getAnnotationLinePageNumberTypePosition ();
26+
public abstract
27+
ParserResultFieldsFilter<PageNumberType> getPageNumberTypeFilter(ParserResult res);
2528

26-
/** When annotation line split by spaces, this is the index at which Page number or location
27-
* number can be found.
29+
/** Page number word position.
30+
* There is no pattern to match here.
2831
*/
29-
public abstract int getAnnotationLinePageOrLocationNumberPosition ();
32+
public abstract
33+
ParserResultFieldsFilter<Object> getPageOrLocationNumberFilter(ParserResult res);
3034

3135
/** Pattern by which the end of a block (i.e Termination line) is recognized. */
32-
public abstract String getTeminationLinePattern ();
36+
public abstract ParserResultFieldsFilter<Boolean> getTerminationLineFilter();
3337
}

src/coderarjob/kpdfsync/lib/clipparser/AbstractParser.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ public abstract class AbstractParser
1616
public abstract String[] getSupportedKindleVersions();
1717

1818
/* Abstract protected methods */
19+
protected abstract boolean isTerminationLine (String linestr);
1920
protected abstract boolean parseLine(int linei, ParserResult res) throws Exception;
20-
protected abstract AbstractKindleParserConstants getKindleParserConstants();
21+
//protected abstract AbstractKindleParserConstants getKindleParserConstants();
2122

2223
/* Protected fields */
2324
protected String mFileName;
@@ -28,7 +29,7 @@ public abstract class AbstractParser
2829
False indicates, no error, or file pointer has
2930
moved to the next block after the previous parsing
3031
error.*/
31-
protected AbstractKindleParserConstants mConstants = null;
32+
//protected AbstractKindleParserConstants mConstants = null;
3233

3334
/* Private fields */
3435
private long mLastFilePointer;
@@ -87,7 +88,7 @@ public AbstractParser (String fileName) throws FileNotFoundException, IOExceptio
8788
this.mLastFilePointer = -1;
8889
this.mParserEvents = null;
8990
this.mIsInvalidState = false;
90-
this.mConstants = getKindleParserConstants();
91+
//this.mConstants = getKindleParserConstants();
9192
}
9293

9394
/**
@@ -190,11 +191,12 @@ protected ParserException genParserException (String field)
190191
/**
191192
* Checks if the specified line is the termination line.
192193
*/
193-
protected boolean isTerminationLine (String linestr)
194+
/*protected boolean isTerminationLine (String linestr)
194195
{
195196
assert (linestr != null);
196-
return linestr.equals(mConstants.getTeminationLinePattern());
197-
}
197+
ParserResultFieldsFilter<Boolean> filter = mConstants.getTerminationLineFilter();
198+
return filter.getOrDefault(linestr, false);
199+
}*/
198200

199201
/**
200202
* Checks if End of File has been reached.

src/coderarjob/kpdfsync/lib/clipparser/KindleParserV1.java

Lines changed: 98 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,18 @@
1313

1414
import java.io.FileNotFoundException;
1515
import java.io.IOException;
16+
import java.util.Hashtable;
1617

1718
import coderarjob.kpdfsync.lib.clipparser.ParserResult.SupportedFields;
19+
import coderarjob.kpdfsync.lib.clipparser.ParserResult.AnnotationType;
20+
import coderarjob.kpdfsync.lib.clipparser.ParserResult.PageNumberType;
1821

1922
public 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

Comments
 (0)