tmf: Clean up tmf.core.trace package
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / parsers / custom / CustomTxtTrace.java
index 4f3ddcc67eefee222d7dbf7871a46c0426cbd66a..513fa43ad6a0bd4cca037fda0f0e471e329ed79b 100644 (file)
@@ -1,11 +1,11 @@
 /*******************************************************************************
- * Copyright (c) 2010 Ericsson
- * 
+ * Copyright (c) 2010, 2013 Ericsson
+ *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
  * accompanies this distribution, and is available at
  * http://www.eclipse.org/legal/epl-v10.html
- * 
+ *
  * Contributors:
  *   Patrick Tasse - Initial API and implementation
  *******************************************************************************/
@@ -22,48 +22,78 @@ import java.util.regex.Matcher;
 
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
 import org.eclipse.linuxtools.internal.tmf.ui.Activator;
 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTraceDefinition.InputLine;
-import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
+import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
 import org.eclipse.linuxtools.tmf.core.io.BufferedRandomAccessFile;
+import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
 import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser;
-import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
 import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
-import org.eclipse.linuxtools.tmf.core.trace.TmfLocation;
 import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
+import org.eclipse.linuxtools.tmf.core.trace.indexer.ITmfTraceIndexer;
+import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation;
+import org.eclipse.linuxtools.tmf.core.trace.location.TmfLongLocation;
 
-public class CustomTxtTrace extends TmfTrace<CustomTxtEvent> implements ITmfEventParser<CustomTxtEvent> {
+/**
+ * Base class for custom plain text traces.
+ *
+ * @author Patrick Tassé
+ */
+public class CustomTxtTrace extends TmfTrace implements ITmfEventParser {
 
-    private static final TmfLocation<Long> NULL_LOCATION = new TmfLocation<Long>((Long) null);
+    private static final TmfLongLocation NULL_LOCATION = new TmfLongLocation((Long) null);
     private static final int DEFAULT_CACHE_SIZE = 100;
 
     private final CustomTxtTraceDefinition fDefinition;
     private final CustomTxtEventType fEventType;
     private BufferedRandomAccessFile fFile;
 
+    /**
+     * Basic constructor.
+     *
+     * @param definition
+     *            Text trace definition
+     */
     public CustomTxtTrace(final CustomTxtTraceDefinition definition) {
         fDefinition = definition;
         fEventType = new CustomTxtEventType(fDefinition);
         setCacheSize(DEFAULT_CACHE_SIZE);
     }
 
-    public CustomTxtTrace(final IResource resource, final CustomTxtTraceDefinition definition, final String path, final int cacheSize) throws TmfTraceException {
+    /**
+     * Full constructor.
+     *
+     * @param resource
+     *            Trace's resource.
+     * @param definition
+     *            Text trace definition
+     * @param path
+     *            Path to the trace file
+     * @param cacheSize
+     *            Cache size to use
+     * @throws TmfTraceException
+     *             If we couldn't open the trace at 'path'
+     */
+    public CustomTxtTrace(final IResource resource,
+            final CustomTxtTraceDefinition definition, final String path,
+            final int cacheSize) throws TmfTraceException {
         this(definition);
         setCacheSize((cacheSize > 0) ? cacheSize : DEFAULT_CACHE_SIZE);
         initTrace(resource, path, CustomTxtEvent.class);
     }
 
     @Override
-    public void initTrace(final IResource resource, final String path, final Class<CustomTxtEvent> eventType) throws TmfTraceException {
+    public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> eventType) throws TmfTraceException {
         super.initTrace(resource, path, eventType);
         try {
             fFile = new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
         } catch (IOException e) {
             throw new TmfTraceException(e.getMessage(), e);
         }
-        indexTrace(false);
     }
 
     @Override
@@ -80,7 +110,12 @@ public class CustomTxtTrace extends TmfTrace<CustomTxtEvent> implements ITmfEven
     }
 
     @Override
-    public synchronized TmfContext seekEvent(final ITmfLocation<?> location) {
+    public ITmfTraceIndexer getIndexer() {
+        return super.getIndexer();
+    }
+
+    @Override
+    public synchronized TmfContext seekEvent(final ITmfLocation location) {
         final CustomTxtTraceContext context = new CustomTxtTraceContext(NULL_LOCATION, ITmfContext.UNKNOWN_RANK);
         if (NULL_LOCATION.equals(location) || fFile == null) {
             return context;
@@ -88,8 +123,8 @@ public class CustomTxtTrace extends TmfTrace<CustomTxtEvent> implements ITmfEven
         try {
             if (location == null) {
                 fFile.seek(0);
-            } else if (location.getLocation() instanceof Long) {
-                fFile.seek((Long) location.getLocation());
+            } else if (location.getLocationInfo() instanceof Long) {
+                fFile.seek((Long) location.getLocationInfo());
             }
             String line;
             long rawPos = fFile.getFilePointer();
@@ -97,7 +132,7 @@ public class CustomTxtTrace extends TmfTrace<CustomTxtEvent> implements ITmfEven
                 for (final InputLine input : getFirstLines()) {
                     final Matcher matcher = input.getPattern().matcher(line);
                     if (matcher.find()) {
-                        context.setLocation(new TmfLocation<Long>(rawPos));
+                        context.setLocation(new TmfLongLocation(rawPos));
                         context.firstLineMatcher = matcher;
                         context.firstLine = line;
                         context.nextLineLocation = fFile.getFilePointer();
@@ -124,7 +159,7 @@ public class CustomTxtTrace extends TmfTrace<CustomTxtEvent> implements ITmfEven
             return new CustomTxtTraceContext(NULL_LOCATION, ITmfContext.UNKNOWN_RANK);
         }
         try {
-            long pos = (long) (ratio * fFile.length());
+            long pos = Math.round(ratio * fFile.length());
             while (pos > 0) {
                 fFile.seek(pos - 1);
                 if (fFile.read() == '\n') {
@@ -132,7 +167,7 @@ public class CustomTxtTrace extends TmfTrace<CustomTxtEvent> implements ITmfEven
                 }
                 pos--;
             }
-            final ITmfLocation<?> location = new TmfLocation<Long>(pos);
+            final ITmfLocation location = new TmfLongLocation(pos);
             final TmfContext context = seekEvent(location);
             context.setRank(ITmfContext.UNKNOWN_RANK);
             return context;
@@ -143,13 +178,13 @@ public class CustomTxtTrace extends TmfTrace<CustomTxtEvent> implements ITmfEven
     }
 
     @Override
-    public synchronized double getLocationRatio(final ITmfLocation<?> location) {
+    public synchronized double getLocationRatio(final ITmfLocation location) {
         if (fFile == null) {
             return 0;
         }
         try {
-            if (location.getLocation() instanceof Long) {
-                return (double) ((Long) location.getLocation()) / fFile.length();
+            if (location.getLocationInfo() instanceof Long) {
+                return (double) ((Long) location.getLocationInfo()) / fFile.length();
             }
         } catch (final IOException e) {
             Activator.getDefault().logError("Error seeking event. File: " + getPath(), e); //$NON-NLS-1$
@@ -158,7 +193,7 @@ public class CustomTxtTrace extends TmfTrace<CustomTxtEvent> implements ITmfEven
     }
 
     @Override
-    public ITmfLocation<?> getCurrentLocation() {
+    public ITmfLocation getCurrentLocation() {
         // TODO Auto-generated method stub
         return null;
     }
@@ -171,7 +206,7 @@ public class CustomTxtTrace extends TmfTrace<CustomTxtEvent> implements ITmfEven
 
     @Override
     public synchronized CustomTxtEvent getNext(final ITmfContext context) {
-        final ITmfContext savedContext = context.clone();
+        final ITmfContext savedContext = new TmfContext(context.getLocation(), context.getRank());
         final CustomTxtEvent event = parse(context);
         if (event != null) {
             updateAttributes(savedContext, event.getTimestamp());
@@ -189,7 +224,7 @@ public class CustomTxtTrace extends TmfTrace<CustomTxtEvent> implements ITmfEven
         }
 
         final CustomTxtTraceContext context = (CustomTxtTraceContext) tmfContext;
-        if (!(context.getLocation().getLocation() instanceof Long) || NULL_LOCATION.equals(context.getLocation())) {
+        if (context.getLocation() == null || !(context.getLocation().getLocationInfo() instanceof Long) || NULL_LOCATION.equals(context.getLocation())) {
             return null;
         }
 
@@ -214,7 +249,7 @@ public class CustomTxtTrace extends TmfTrace<CustomTxtEvent> implements ITmfEven
                     for (final InputLine input : getFirstLines()) {
                         final Matcher matcher = input.getPattern().matcher(line);
                         if (matcher.find()) {
-                            context.setLocation(new TmfLocation<Long>(rawPos));
+                            context.setLocation(new TmfLongLocation(rawPos));
                             context.firstLineMatcher = matcher;
                             context.firstLine = line;
                             context.nextLineLocation = fFile.getFilePointer();
@@ -229,7 +264,7 @@ public class CustomTxtTrace extends TmfTrace<CustomTxtEvent> implements ITmfEven
                             for (final InputLine input : getFirstLines()) {
                                 final Matcher matcher = input.getPattern().matcher(line);
                                 if (matcher.find()) {
-                                    context.setLocation(new TmfLocation<Long>(rawPos));
+                                    context.setLocation(new TmfLongLocation(rawPos));
                                     context.firstLineMatcher = matcher;
                                     context.firstLine = line;
                                     context.nextLineLocation = fFile.getFilePointer();
@@ -280,7 +315,7 @@ public class CustomTxtTrace extends TmfTrace<CustomTxtEvent> implements ITmfEven
                             }
                         }
                     }
-                    if (! processed) {
+                    if (!processed && currentInput != null) {
                         final Matcher matcher = currentInput.getPattern().matcher(line);
                         if (matcher.find()) {
                             event.processGroups(currentInput, matcher);
@@ -323,10 +358,20 @@ public class CustomTxtTrace extends TmfTrace<CustomTxtEvent> implements ITmfEven
         return event;
     }
 
+    /**
+     * @return The first few lines of the text file
+     */
     public List<InputLine> getFirstLines() {
         return fDefinition.inputs;
     }
 
+    /**
+     * Parse the first line of the trace (to recognize the type).
+     *
+     * @param context
+     *            Trace context
+     * @return The first event
+     */
     public CustomTxtEvent parseFirstLine(final CustomTxtTraceContext context) {
         final CustomTxtEvent event = new CustomTxtEvent(fDefinition, this, TmfTimestamp.ZERO, "", fEventType, ""); //$NON-NLS-1$ //$NON-NLS-2$
         event.processGroups(context.inputLine, context.firstLineMatcher);
@@ -334,15 +379,20 @@ public class CustomTxtTrace extends TmfTrace<CustomTxtEvent> implements ITmfEven
         return event;
     }
 
+    /**
+     * Get the trace definition.
+     *
+     * @return The trace definition
+     */
     public CustomTraceDefinition getDefinition() {
         return fDefinition;
     }
 
-    /* (non-Javadoc)
-     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
-     */
     @Override
-    public boolean validate(IProject project, String path) {
-        return fileExists(path);
+    public IStatus validate(IProject project, String path) {
+        if( fileExists(path)) {
+            return Status.OK_STATUS;
+        }
+        return new Status(IStatus.ERROR, Activator.PLUGIN_ID ,Messages.CustomTrace_FileNotFound + ": " + path); //$NON-NLS-1$
     }
 }
This page took 0.027645 seconds and 5 git commands to generate.