16
16
package org .culturegraph .mf .formeta .parser ;
17
17
18
18
import org .culturegraph .mf .exceptions .FormatException ;
19
+ import org .culturegraph .mf .util .StringUtil ;
19
20
20
21
/**
21
22
* A parser for the formeta metadata serialisation format.
22
- *
23
+ *
23
24
* @author Christoph Böhme
24
25
*
25
26
*/
@@ -32,73 +33,70 @@ public final class FormetaParser {
32
33
33
34
private static final int BUFFER_SIZE = 1024 * 1024 ;
34
35
35
- private char [] buffer = new char [BUFFER_SIZE ];
36
+ private char [] buffer = new char [BUFFER_SIZE ];
36
37
private final StructureParserContext structureParserContext = new StructureParserContext ();
37
38
38
39
public void setEmitter (final Emitter emitter ) {
39
40
structureParserContext .setEmitter (emitter );
40
41
}
41
-
42
+
42
43
public Emitter getEmitter () {
43
44
return structureParserContext .getEmitter ();
44
45
}
45
46
46
47
public void parse (final String data ) {
47
48
assert structureParserContext .getEmitter () != null : "No emitter set" ;
48
-
49
- // According to http://stackoverflow.com/a/11876086 it is faster to copy
50
- // the string into a char array than to use charAt():
51
- final int recordLen = data .length ();
52
- if (recordLen > buffer .length ) {
53
- buffer = new char [buffer .length * 2 ];
54
- }
55
- data .getChars (0 , recordLen , buffer , 0 );
56
-
49
+
50
+ // According to http://stackoverflow.com/a/11876086 it is faster to copy
51
+ // a string into a char array then to use charAt():
52
+ buffer = StringUtil .copyToBuffer (data , buffer );
53
+ final int bufferLen = data .length ();
54
+
57
55
structureParserContext .reset ();
58
56
StructureParserState state = StructureParserState .ITEM_NAME ;
59
57
int i = 0 ;
60
58
try {
61
- for (; i < recordLen ; ++i ) {
59
+ for (; i < bufferLen ; ++i ) {
62
60
state = state .processChar (buffer [i ], structureParserContext );
63
61
}
64
- } catch (FormatException e ) {
65
- final String errorMsg = "Parsing error at position "
62
+ } catch (final FormatException e ) {
63
+ final String errorMsg = "Parsing error at position "
66
64
+ (i + 1 ) + ": "
67
- + getErrorSnippet (data , i ) + ", "
65
+ + getErrorSnippet (data , i ) + ", "
68
66
+ e .getMessage ();
69
67
throw new FormatException (errorMsg , e );
70
68
}
71
69
try {
72
70
state .endOfInput (structureParserContext );
73
- } catch (FormatException e ) {
71
+ } catch (final FormatException e ) {
74
72
throw new FormatException ("Parsing error: " + e .getMessage (), e );
75
73
}
76
74
}
77
-
75
+
78
76
/**
79
- * Extracts a text snippet from the record for showing the position at
80
- * which an error occurred. The exact position additionally highlighted
77
+ * Extracts a text snippet from the record for showing the position at
78
+ * which an error occurred. The exact position additionally highlighted
81
79
* with {@link POS_MARKER_LEFT} and {@link POS_MARKER_RIGHT}.
82
- *
80
+ *
83
81
* @param record the record currently being parsed
84
82
* @param pos the position at which the error occurred
85
83
* @return a text snippet.
86
84
*/
87
85
private static String getErrorSnippet (final String record , final int pos ) {
88
86
final StringBuilder snippet = new StringBuilder ();
89
-
87
+
90
88
final int start = pos - SNIPPET_SIZE / 2 ;
91
89
if (start < 0 ) {
92
90
snippet .append (record .substring (0 , pos ));
93
91
} else {
94
92
snippet .append (SNIPPET_ELLIPSIS );
95
93
snippet .append (record .substring (start , pos ));
96
94
}
97
-
95
+
98
96
snippet .append (POS_MARKER_LEFT );
99
97
snippet .append (record .charAt (pos ));
100
98
snippet .append (POS_MARKER_RIGHT );
101
-
99
+
102
100
if (pos + 1 < record .length ()) {
103
101
final int end = pos + SNIPPET_SIZE / 2 ;
104
102
if (end > record .length ()) {
@@ -108,7 +106,7 @@ private static String getErrorSnippet(final String record, final int pos) {
108
106
snippet .append (SNIPPET_ELLIPSIS );
109
107
}
110
108
}
111
-
109
+
112
110
return snippet .toString ();
113
111
}
114
112
0 commit comments