package org.eclipse.tracecompass.tmf.core.parsers.custom;
+import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
+
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
import org.eclipse.tracecompass.tmf.core.io.BufferedRandomAccessFile;
import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTraceDefinition.InputLine;
+import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
+import org.eclipse.tracecompass.tmf.core.signal.TmfTraceRangeUpdatedSignal;
import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
import org.eclipse.tracecompass.tmf.core.trace.TmfContext;
import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
+import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfPersistentlyIndexable;
import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;
@Override
public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> eventType) throws TmfTraceException {
super.initTrace(resource, path, eventType);
+ initFile();
+ }
+
+ private void initFile() throws TmfTraceException {
+ closeFile();
try {
fFile = new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
} catch (IOException e) {
@Override
public synchronized void dispose() {
super.dispose();
+ closeFile();
+ }
+
+ private void closeFile() {
if (fFile != null) {
try {
fFile.close();
final ITmfContext savedContext = new TmfContext(context.getLocation(), context.getRank());
final CustomTxtEvent event = parse(context);
if (event != null) {
- updateAttributes(savedContext, event.getTimestamp());
+ updateAttributes(savedContext, event);
context.increaseRank();
}
return event;
}
}
} else {
- if (countMap.get(currentInput) >= currentInput.getMinCount()) {
+ if (checkNotNull(countMap.get(currentInput)) >= currentInput.getMinCount()) {
final List<InputLine> nextInputs = currentInput.getNextInputs(countMap);
if (nextInputs.size() == 0 || nextInputs.get(nextInputs.size() - 1).getMinCount() == 0) {
for (final InputLine input : getFirstLines()) {
if (countMap.get(currentInput) == null) {
countMap.put(currentInput, 1);
} else {
- countMap.put(currentInput, countMap.get(currentInput) + 1);
+ countMap.put(currentInput, checkNotNull(countMap.get(currentInput)) + 1);
}
Iterator<InputLine> iter = countMap.keySet().iterator();
while (iter.hasNext()) {
if (currentInput.childrenInputs != null && currentInput.childrenInputs.size() > 0) {
currentInput = currentInput.childrenInputs.get(0);
countMap.put(currentInput, 0);
- } else if (countMap.get(currentInput) >= currentInput.getMaxCount()) {
+ } else if (checkNotNull(countMap.get(currentInput)) >= currentInput.getMaxCount()) {
if (currentInput.getNextInputs(countMap).size() > 0) {
currentInput = currentInput.getNextInputs(countMap).get(0);
if (countMap.get(currentInput) == null) {
final Matcher matcher = currentInput.getPattern().matcher(line);
if (matcher.matches()) {
event.processGroups(currentInput, matcher);
- countMap.put(currentInput, countMap.get(currentInput) + 1);
+ countMap.put(currentInput, checkNotNull(countMap.get(currentInput)) + 1);
if (currentInput.childrenInputs != null && currentInput.childrenInputs.size() > 0) {
currentInput = currentInput.childrenInputs.get(0);
countMap.put(currentInput, 0);
- } else if (countMap.get(currentInput) >= currentInput.getMaxCount()) {
+ } else if (checkNotNull(countMap.get(currentInput)) >= currentInput.getMaxCount()) {
if (currentInput.getNextInputs(countMap).size() > 0) {
currentInput = currentInput.getNextInputs(countMap).get(0);
if (countMap.get(currentInput) == null) {
}
}
}
- ((StringBuffer) event.getContent().getValue()).append("\n").append(line); //$NON-NLS-1$
+ ((StringBuffer) event.getContentValue()).append("\n").append(line); //$NON-NLS-1$
}
}
rawPos = fFile.getFilePointer();
return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CustomTrace_FileNotFound + ": " + path); //$NON-NLS-1$
}
int confidence = 0;
+ try {
+ if (!TmfTraceUtils.isText(file)) {
+ return new TraceValidationStatus(confidence, Activator.PLUGIN_ID);
+ }
+ } catch (IOException e) {
+ Activator.logError("Error validating file: " + path, e); //$NON-NLS-1$
+ return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "IOException validating file: " + path, e); //$NON-NLS-1$
+ }
try (BufferedRandomAccessFile rafile = new BufferedRandomAccessFile(path, "r")) { //$NON-NLS-1$
int lineCount = 0;
double matches = 0.0;
String line = rafile.getNextLine();
+
while ((line != null) && (lineCount++ < MAX_LINES)) {
for (InputLine inputLine : fDefinition.inputs) {
Matcher matcher = inputLine.getPattern().matcher(line);
}
return traceTypeId;
}
+
+ @TmfSignalHandler
+ @Override
+ public void traceRangeUpdated(TmfTraceRangeUpdatedSignal signal) {
+ if (signal.getTrace() == this) {
+ try {
+ synchronized (this) {
+ // Reset the file handle in case it has reached the end of the
+ // file already. Otherwise, it will not be able to read new data
+ // pass the previous end.
+ initFile();
+ }
+ } catch (TmfTraceException e) {
+ Activator.logError(e.getLocalizedMessage(), e);
+ }
+ }
+ super.traceRangeUpdated(signal);
+ }
}