I'd like to simplify my log file parser and replace complex regular expressions with a templates engine. The idea is to reverse the process of a template engine and give as an input the template description and a valid result file. The multi-line log files look like:
*** ID: X821 ***
- type: B5
- time-stamp: 20160202T01:11:01.2991
* Device: XKK-255141
All of them have the same structure and can be described in a pseudo template language:
*** ID: {{string}} ***
- type: {{string}}
- time-stamp: {{date}}
* Device: XKK-{{integer}}
Is there a template engine which parses a log file, looks-up the structure in the template file and returns content in a HashMap/List/Object?
Note: I'm aware that I could write a simple DSL in ANTLR. But the idea here is to simplify the parsing and accept that only basic multi-line log files without recursions are supported.
I am not aware of an existing template engine that does this (they usually work the other way around, filling templates with data).
Why not use something like this:
class ReverseTemplateEngine {
ArrayList<String> prefixes = new ArrayList();
ArrayList<String> suffixes = new ArrayList();
public ReverseTemplateEngine(String... templates) {
for (String s: templates) {
int cut = s.indexOf("$");
suffixes.add(s.substring(0, cut));
prefixes.add(s.substring(cut + 1);
}
}
public List<String> parse(BufferedReader r) {
ArrayList<String> result = new ArrayList<>();
while (true) {
String line = reader.readLine();
for (int i = 0; i < prefixes.length; i++) {
if (line.startsWith(prefixes.get(i))
&& line.endsWith(suffixes.get(i)) {
result.add(line.substring(prefixes.get(i).length(),
line.length() - suffixes.get(i).length()));
break;
}
}
}
return list;
}
}
Usage:
ReverseTemplateEngine rte = new ReverseTemplateEngine(
"*** ID: $ ***",
"- type: $",
"- time-stamp: $",
"* Device: XKK-$");
List<String> result = rte.parse(new BufferedReader(
new FileReader("yourfile.txt")));