Assert.isNotNull(log, "log"); //$NON-NLS-1$
Assert.isNotNull(input, "input"); //$NON-NLS-1$
Assert.isNotNull(collector, "collector"); //$NON-NLS-1$
Assert.isTrue(isConfigured(), "Dialect should be configured by now"); //$NON-NLS-1$
try {
LogEntry currentEntry = null;
IHasEncoding enc = (IHasEncoding) log.getAdapter(IHasEncoding.class);
IHasLocale loc = (IHasLocale) log.getAdapter(IHasLocale.class);
if (loc != null) {
// Apply the locale
getPatternTranslator().applyLocale(loc.getLocale(), rules);
}
IHasTimeZone tz = (IHasTimeZone) log.getAdapter(IHasTimeZone.class);
if (tz != null) {
// Apply the timezone
getPatternTranslator().applyTimeZone(tz.getTimeZone(), rules);
}
LineIterator iter = IOUtils.lineIterator(input, enc.getEncoding());
int minLinesPerEntry = getPatternTranslator().getMinLinesPerEntry();
int lineNo = 0;
int moreLinesToCome = 0;
try {
String line = null;
while (iter.hasNext()) {
lineNo++;
if (minLinesPerEntry == 1) {
// Simple case
line = iter.nextLine();
} else {
String s = iter.nextLine();
if (moreLinesToCome == 0) {
Matcher m = getInternalPatternFirstLine().matcher(s);
if (m.find()) {
// First line
line = s;
moreLinesToCome = minLinesPerEntry - 1;
continue;
} else {
// Some crazy stuff
line = s;
}
} else if (iter.hasNext() && (moreLinesToCome > 1)) {
// Some middle line
line += IOUtils.LINE_SEPARATOR + s;
moreLinesToCome--;
continue;
} else {
// Last line
line += IOUtils.LINE_SEPARATOR + s;
if (!iter.hasNext()) {
line += IOUtils.LINE_SEPARATOR;
}
moreLinesToCome = 0;
}
}
// Error handling
List<IStatus> statuses = null;
boolean fatal = false; // determines whether to interrupt parsing
Matcher m = getInternalPatternFull().matcher(line);
if (m.find()) {
// The next line matches, so flush the previous entry and continue
if (currentEntry != null) {
collector.collect(currentEntry);
currentEntry = null;
}
currentEntry = new LogEntry();
for (int i = 0; i < m.groupCount(); i++) {
try {
getPatternTranslator().extractField(currentEntry, getRules().get(i),
m.group(i + 1));
} catch (CoreException e) {
// Mark for interruption
fatal = fatal || e.getStatus().matches(IStatus.ERROR);
// Messages will be displayed later
if (statuses == null) {
statuses = new ArrayList<IStatus>();
}
if (e.getStatus().isMultiStatus()) {
Collections.addAll(statuses, e.getStatus().getChildren());
} else {
statuses.add(e.getStatus());
}
}
}
// We encountered errors or warnings
if (statuses != null && !statuses.isEmpty()) {
currentEntry = null; // Stop propagation
IStatus status = new MultiStatus(PatternDialectPlugin.PLUGIN_ID,
0, statuses.toArray(new IStatus[statuses.size()]),
NLS.bind(Messages.APatternDialect_error_failedToParseLine, lineNo), null);
if (fatal) {
// Interrupt parsing in case of error
throw new CoreException(status);
} else {
collector.addMessage(status);
}
}
} else if (currentEntry != null) {
// Append to message
String msg = currentEntry.get(getFieldProvider().getMessageField());
currentEntry.put(getFieldProvider().getMessageField(), msg + IOUtils.LINE_SEPARATOR + line);
}
if (collector.isCanceled()) {
// Cancel parsing
break;