19
19
/**
20
20
* A parser for PICA+ records. Only single records can be parsed as the parser
21
21
* does not recognise end-of-record markers (usually new lines). The initial
22
- * parser state is FIELD_START. A valid state for termination is FIELD_START.
23
- * The parser processes any input, there is no error state.
22
+ * parser state is FIELD_NAME. All states are valid end states. The parser
23
+ * processes any input, there is no error state.
24
24
*
25
25
* The parser ignores spaces in field names. They are not included in the
26
26
* field name.
34
34
*/
35
35
enum PicaParserState {
36
36
37
- FIELD_START {
38
- @ Override
39
- protected PicaParserState parseChar (final char ch , final PicaParserContext ctx ) {
40
- if (ch == PicaConstants .FIELD_DELIMITER || ch == ' ' ) {
41
- return FIELD_START ;
42
- }
43
- return FIELD_NAME .parseChar (ch , ctx );
44
- }
45
- },
46
37
FIELD_NAME {
47
38
@ Override
48
39
protected PicaParserState parseChar (final char ch , final PicaParserContext ctx ) {
49
40
final PicaParserState next ;
50
41
if (ch == PicaConstants .FIELD_DELIMITER ) {
51
42
ctx .emitStartEntity ();
52
43
ctx .emitEndEntity ();
53
- next = FIELD_START ;
44
+ next = FIELD_NAME ;
54
45
} else if (ch == PicaConstants .SUBFIELD_DELIMITER ) {
55
46
ctx .emitStartEntity ();
56
47
next = SUBFIELD_NAME ;
@@ -62,22 +53,33 @@ protected PicaParserState parseChar(final char ch, final PicaParserContext ctx)
62
53
}
63
54
return next ;
64
55
}
56
+
57
+ @ Override
58
+ protected void endOfInput (final PicaParserContext ctx ) {
59
+ ctx .emitStartEntity ();
60
+ ctx .emitEndEntity ();
61
+ }
65
62
},
66
63
SUBFIELD_NAME {
67
64
@ Override
68
65
protected PicaParserState parseChar (final char ch , final PicaParserContext ctx ) {
69
66
final PicaParserState next ;
70
67
if (ch == PicaConstants .FIELD_DELIMITER ) {
71
68
ctx .emitEndEntity ();
72
- next = FIELD_START ;
69
+ next = FIELD_NAME ;
73
70
} else if (ch == PicaConstants .SUBFIELD_DELIMITER ) {
74
- next = SUBFIELD_NAME ;
71
+ next = this ;
75
72
} else {
76
73
ctx .setSubfieldName (ch );
77
74
next = SUBFIELD_VALUE ;
78
75
}
79
76
return next ;
80
77
}
78
+
79
+ @ Override
80
+ protected void endOfInput (final PicaParserContext ctx ) {
81
+ ctx .emitEndEntity ();
82
+ }
81
83
},
82
84
SUBFIELD_VALUE {
83
85
@ Override
@@ -86,7 +88,7 @@ protected PicaParserState parseChar(final char ch, final PicaParserContext ctx)
86
88
if (ch == PicaConstants .FIELD_DELIMITER ) {
87
89
ctx .emitLiteral ();
88
90
ctx .emitEndEntity ();
89
- next = FIELD_START ;
91
+ next = FIELD_NAME ;
90
92
} else if (ch == PicaConstants .SUBFIELD_DELIMITER ) {
91
93
ctx .emitLiteral ();
92
94
next = SUBFIELD_NAME ;
@@ -96,8 +98,16 @@ protected PicaParserState parseChar(final char ch, final PicaParserContext ctx)
96
98
}
97
99
return next ;
98
100
}
101
+
102
+ @ Override
103
+ protected void endOfInput (final PicaParserContext ctx ) {
104
+ ctx .emitLiteral ();
105
+ ctx .emitEndEntity ();
106
+ }
99
107
};
100
108
101
109
protected abstract PicaParserState parseChar (final char ch , final PicaParserContext ctx );
102
110
111
+ protected abstract void endOfInput (final PicaParserContext ctx );
112
+
103
113
}
0 commit comments