use of org.petitparser.parser.Parser in project MassBank-web by MassBank.
the class RecordParserDefinitionTest method validate.
private <T> T validate(String source, String production) {
Parser parser = recordparser.build(production).end();
Result result = parser.parse(source);
return result.get();
}
use of org.petitparser.parser.Parser in project MassBank-web by MassBank.
the class Validator2 method validate.
public static Record validate(String recordstring, String contributor) {
// test non standard ASCII chars and print warnings
for (int i = 0; i < recordstring.length(); i++) {
if (recordstring.charAt(i) > 0x7F) {
String[] tokens = recordstring.split("\\r?\\n");
System.out.println("Warning: non standard ASCII charactet found. This might be an error. Please check carefully.");
int line = 0, col = 0, offset = 0;
for (String token : tokens) {
offset = offset + token.length() + 1;
if (i < offset) {
col = i - (offset - (token.length() + 1));
System.out.println(tokens[line]);
StringBuilder error_at = new StringBuilder(StringUtils.repeat(" ", tokens[line].length()));
error_at.setCharAt(col, '^');
System.out.println(error_at);
break;
}
line++;
}
}
}
Record record = new Record(contributor);
Parser recordparser = new RecordParser(record);
Result res = recordparser.parse(recordstring);
if (res.isFailure()) {
System.err.println();
System.err.println(res.getMessage());
int position = res.getPosition();
String[] tokens = recordstring.split("\\n");
int line = 0, col = 0, offset = 0;
for (String token : tokens) {
offset = offset + token.length() + 1;
if (position < offset) {
col = position - (offset - (token.length() + 1));
System.err.println(tokens[line]);
StringBuilder error_at = new StringBuilder(StringUtils.repeat(" ", col));
error_at.append('^');
System.err.println(error_at);
// record = new Record();
break;
}
line++;
}
return null;
} else
return record;
}
Aggregations