use of eu.esdihumboldt.hale.common.core.report.ReportLog in project hale by halestudio.
the class ReportReader method parse.
/**
* Parse a file and creates a {@link ReportSession}.
*
* @param file file to parse
* @param id identifier for {@link ReportSession}
*
* @return the {@link ReportSession}
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
private ReportSession parse(File file, long id) {
// create the new session
ReportSession session = new ReportSession(id);
// get content
StringBuilder sw = new StringBuilder();
BufferedReader reader = null;
String nl = System.getProperty("line.separator");
try {
// try to get a BufferedReader from FileReader
reader = new BufferedReader(new FileReader(file));
String temp;
Report lastReport = null;
String messageType = "";
// read all data
while (reader.ready()) {
temp = reader.readLine();
if (temp == null || temp.isEmpty()) {
continue;
}
// check if the line starts with a marker
if (temp.startsWith("!")) {
// we found a new marker, time to parse previous lines
Report r = rf.parse(sw.toString());
if (!sw.toString().isEmpty() && !messageType.isEmpty()) {
Message m = mf.parse(sw.toString());
if (m != null) {
ReportLog<Message> repLog = (ReportLog<Message>) lastReport;
// add the message to the corresponding report
if (messageType.equals("!ERROR")) {
repLog.error(m);
} else if (messageType.equals("!WARN")) {
repLog.warn(m);
} else if (messageType.equals("!INFO")) {
repLog.info(m);
}
// reset message type
messageType = "";
}
} else if (r != null) {
// new report
lastReport = r;
session.addReport(lastReport);
}
// check if the new line is a marker for messages
if (temp.startsWith("!ERROR")) {
messageType = temp;
temp = "";
} else if (temp.startsWith("!WARN")) {
messageType = temp;
temp = "";
} else if (temp.startsWith("!INFO")) {
messageType = temp;
temp = "";
}
// then flush sw
sw = new StringBuilder();
// and add the new line
sw.append(temp + nl);
} else {
sw.append(temp + nl);
}
}
// close reader
reader.close();
} catch (Exception e) {
_log.error("Error while parsing a log file.", e);
}
return session;
}
Aggregations