Re-structure LTTng sub-project as per the Linux Tools guidelines
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / parsers / custom / CustomXmlTrace.java
index ca4c593f7a3b5503b9bdd533256f04d6da41eb0b..9f8f2931a982463e299b9dc22a033e11d99fe645 100644 (file)
@@ -22,17 +22,17 @@ import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;\r
 import javax.xml.parsers.ParserConfigurationException;\r
 \r
-import org.eclipse.linuxtools.tmf.event.TmfEvent;\r
-import org.eclipse.linuxtools.tmf.event.TmfEventReference;\r
-import org.eclipse.linuxtools.tmf.event.TmfEventSource;\r
-import org.eclipse.linuxtools.tmf.event.TmfEventType;\r
-import org.eclipse.linuxtools.tmf.event.TmfTimestamp;\r
-import org.eclipse.linuxtools.tmf.trace.ITmfContext;\r
-import org.eclipse.linuxtools.tmf.trace.ITmfLocation;\r
-import org.eclipse.linuxtools.tmf.trace.ITmfTrace;\r
-import org.eclipse.linuxtools.tmf.trace.TmfContext;\r
-import org.eclipse.linuxtools.tmf.trace.TmfLocation;\r
-import org.eclipse.linuxtools.tmf.trace.TmfTrace;\r
+import org.eclipse.linuxtools.tmf.core.event.TmfEvent;\r
+import org.eclipse.linuxtools.tmf.core.event.TmfEventReference;\r
+import org.eclipse.linuxtools.tmf.core.event.TmfEventSource;\r
+import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;\r
+import org.eclipse.linuxtools.tmf.core.io.BufferedRandomAccessFile;\r
+import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;\r
+import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;\r
+import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;\r
+import org.eclipse.linuxtools.tmf.core.trace.TmfContext;\r
+import org.eclipse.linuxtools.tmf.core.trace.TmfLocation;\r
+import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;\r
 import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomXmlTraceDefinition.InputAttribute;\r
 import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomXmlTraceDefinition.InputElement;\r
 import org.w3c.dom.Document;\r
@@ -47,12 +47,22 @@ import org.xml.sax.SAXParseException;
 \r
 public class CustomXmlTrace extends TmfTrace<CustomXmlEvent> {\r
 \r
+    private static final TmfLocation<Long> NULL_LOCATION = new TmfLocation<Long>((Long) null);\r
+    \r
     private CustomXmlTraceDefinition fDefinition;\r
+    private CustomXmlEventType fEventType;\r
     private InputElement fRecordInputElement;\r
     \r
+    public CustomXmlTrace(CustomXmlTraceDefinition definition) {\r
+        fDefinition = definition;\r
+        fEventType = new CustomXmlEventType(fDefinition);\r
+        fRecordInputElement = getRecordInputElement(fDefinition.rootInputElement);\r
+    }\r
+\r
     public CustomXmlTrace(String name, CustomXmlTraceDefinition definition, String path, int cacheSize) throws FileNotFoundException {\r
         super(name, CustomXmlEvent.class, path, cacheSize);\r
         fDefinition = definition;\r
+        fEventType = new CustomXmlEventType(fDefinition);\r
         fRecordInputElement = getRecordInputElement(fDefinition.rootInputElement);\r
     }\r
 \r
@@ -60,21 +70,21 @@ public class CustomXmlTrace extends TmfTrace<CustomXmlEvent> {
     public TmfContext seekLocation(ITmfLocation<?> location) {\r
         //System.out.println(Thread.currentThread().getName() + "::" + getName() + " seekLocation(" + ((location == null || location.getLocation() == null) ? "null" : location) + ")");\r
         //new Throwable().printStackTrace();\r
-        CustomXmlTraceContext context = new CustomXmlTraceContext(new TmfLocation<Long>((Long)null), ITmfContext.INITIAL_RANK);\r
-        if (!new File(getPath()).isFile()) {\r
+        CustomXmlTraceContext context = new CustomXmlTraceContext(NULL_LOCATION, ITmfContext.INITIAL_RANK);\r
+        if (NULL_LOCATION.equals(location) || !new File(getPath()).isFile()) {\r
             return context;\r
         }\r
         try {\r
-            context.raFile = new RandomAccessFile(getPath(), "r");\r
+            context.raFile = new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$\r
             if (location != null && location.getLocation() instanceof Long) {\r
                 context.raFile.seek((Long)location.getLocation());\r
             }\r
             \r
             String line;\r
-            String recordElementStart = "<" + fRecordInputElement.elementName;\r
+            String recordElementStart = "<" + fRecordInputElement.elementName; //$NON-NLS-1$\r
             long rawPos = context.raFile.getFilePointer();\r
             \r
-            while ((line = context.raFile.readLine()) != null) {\r
+            while ((line = context.raFile.getNextLine()) != null) {\r
                 int idx = line.indexOf(recordElementStart); \r
                 if (idx != -1) {\r
                     context.setLocation(new TmfLocation<Long>(rawPos + idx));\r
@@ -93,14 +103,55 @@ public class CustomXmlTrace extends TmfTrace<CustomXmlEvent> {
         \r
     }\r
 \r
-    public ITmfTrace createTraceCopy() {\r
+    @Override\r
+    public TmfContext seekLocation(double ratio) {\r
+        try {\r
+            BufferedRandomAccessFile raFile = new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$\r
+            long pos = (long) (ratio * raFile.length());\r
+            while (pos > 0) {\r
+                raFile.seek(pos - 1);\r
+                if (raFile.read() == '\n') break;\r
+                pos--;\r
+            }\r
+            ITmfLocation<?> location = new TmfLocation<Long>(new Long(pos));\r
+            TmfContext context = seekLocation(location);\r
+            context.setRank(ITmfContext.UNKNOWN_RANK);\r
+            return context;\r
+        } catch (FileNotFoundException e) {\r
+            e.printStackTrace();\r
+            return new CustomXmlTraceContext(NULL_LOCATION, ITmfContext.INITIAL_RANK);\r
+        } catch (IOException e) {\r
+            e.printStackTrace();\r
+            return new CustomXmlTraceContext(NULL_LOCATION, ITmfContext.INITIAL_RANK);\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public double getLocationRatio(ITmfLocation<?> location) {\r
+        try {\r
+            if (location.getLocation() instanceof Long) {\r
+                RandomAccessFile raFile = new RandomAccessFile(getPath(), "r"); //$NON-NLS-1$\r
+                return (double) ((Long) location.getLocation()) / raFile.length();\r
+            }\r
+        } catch (FileNotFoundException e) {\r
+            e.printStackTrace();\r
+        } catch (IOException e) {\r
+            e.printStackTrace();\r
+        }\r
+        return 0;\r
+    }\r
+\r
+    @Override\r
+    @SuppressWarnings({ "rawtypes", "unchecked" })\r
+       public ITmfTrace copy() {\r
         // TODO Auto-generated method stub\r
         return null;\r
     }\r
     \r
     @Override\r
     public ITmfLocation<?> getCurrentLocation() {\r
-        return new TmfLocation<Object>(null);\r
+        // TODO Auto-generated method stub\r
+        return null;\r
     }\r
 \r
     @Override\r
@@ -122,7 +173,7 @@ public class CustomXmlTrace extends TmfTrace<CustomXmlEvent> {
         }\r
         \r
         CustomXmlTraceContext context = (CustomXmlTraceContext) tmfContext;\r
-        if (!(context.getLocation().getLocation() instanceof Long)) {\r
+        if (!(context.getLocation().getLocation() instanceof Long) || NULL_LOCATION.equals(context.getLocation())) {\r
             return null;\r
         }\r
 \r
@@ -132,17 +183,18 @@ public class CustomXmlTrace extends TmfTrace<CustomXmlEvent> {
                 if (context.raFile.getFilePointer() != (Long)context.getLocation().getLocation() + 1) {\r
                     context.raFile.seek((Long)context.getLocation().getLocation() + 1); // +1 is for the <\r
                 }\r
-                StringBuffer elementBuffer = new StringBuffer("<");\r
+                StringBuffer elementBuffer = new StringBuffer("<"); //$NON-NLS-1$\r
                 readElement(elementBuffer, context.raFile);\r
                 Element element = parseElementBuffer(elementBuffer);\r
                 \r
                 event = extractEvent(element, fRecordInputElement);\r
+                ((StringBuffer) event.getContent().getContent()).append(elementBuffer);\r
                 \r
                 String line;\r
-                String recordElementStart = "<" + fRecordInputElement.elementName;\r
+                String recordElementStart = "<" + fRecordInputElement.elementName; //$NON-NLS-1$\r
                 long rawPos = context.raFile.getFilePointer();\r
                 \r
-                while ((line = context.raFile.readLine()) != null) {\r
+                while ((line = context.raFile.getNextLine()) != null) {\r
                     int idx = line.indexOf(recordElementStart); \r
                     if (idx != -1) {\r
                         context.setLocation(new TmfLocation<Long>(rawPos + idx));\r
@@ -153,7 +205,7 @@ public class CustomXmlTrace extends TmfTrace<CustomXmlEvent> {
             } catch (IOException e) {\r
                 e.printStackTrace();\r
             }\r
-            context.setLocation(new TmfLocation<Long>((Long)null));\r
+            context.setLocation(NULL_LOCATION);\r
             return event;\r
         }\r
     }\r
@@ -165,8 +217,9 @@ public class CustomXmlTrace extends TmfTrace<CustomXmlEvent> {
 \r
             // The following allows xml parsing without access to the dtd\r
             EntityResolver resolver = new EntityResolver () {\r
-                public InputSource resolveEntity (String publicId, String systemId) {\r
-                    String empty = "";\r
+                @Override\r
+                               public InputSource resolveEntity (String publicId, String systemId) {\r
+                    String empty = ""; //$NON-NLS-1$\r
                     ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());\r
                     return new InputSource(bais);\r
                 }\r
@@ -175,9 +228,12 @@ public class CustomXmlTrace extends TmfTrace<CustomXmlEvent> {
 \r
             // The following catches xml parsing exceptions\r
             db.setErrorHandler(new ErrorHandler(){\r
-                public void error(SAXParseException saxparseexception) throws SAXException {}\r
-                public void warning(SAXParseException saxparseexception) throws SAXException {}\r
-                public void fatalError(SAXParseException saxparseexception) throws SAXException {\r
+                @Override\r
+                               public void error(SAXParseException saxparseexception) throws SAXException {}\r
+                @Override\r
+                               public void warning(SAXParseException saxparseexception) throws SAXException {}\r
+                @Override\r
+                               public void fatalError(SAXParseException saxparseexception) throws SAXException {\r
                     throw saxparseexception;\r
                 }});\r
             \r
@@ -210,7 +266,7 @@ public class CustomXmlTrace extends TmfTrace<CustomXmlEvent> {
                     readElement(buffer, raFile);\r
                 } else if (c == '/' && numRead == 1) {\r
                     break; // found "</"\r
-                } else if (c == '-' && numRead == 3 && buffer.substring(buffer.length() - 3, buffer.length() - 1).equals("!-")) {\r
+                } else if (c == '-' && numRead == 3 && buffer.substring(buffer.length() - 3, buffer.length() - 1).equals("!-")) { //$NON-NLS-1$\r
                     readComment(buffer, raFile); // found "<!--"\r
                 } else if (i == '>') {\r
                     if (buffer.charAt(buffer.length() - 2) == '/') {\r
@@ -252,7 +308,7 @@ public class CustomXmlTrace extends TmfTrace<CustomXmlEvent> {
                 numRead++;\r
                 char c = (char)i;\r
                 buffer.append(c);\r
-                if (c == '>' && numRead >= 2 && buffer.substring(buffer.length() - 3, buffer.length() - 1).equals("--")) {\r
+                if (c == '>' && numRead >= 2 && buffer.substring(buffer.length() - 3, buffer.length() - 1).equals("--")) { //$NON-NLS-1$\r
                     break; // found "-->"\r
                 }\r
             }\r
@@ -269,7 +325,7 @@ public class CustomXmlTrace extends TmfTrace<CustomXmlEvent> {
             Node node = nodeList.item(i);\r
             if (node.getNodeType() == Node.ELEMENT_NODE) {\r
                 if (separator == null) {\r
-                    separator = " | ";\r
+                    separator = " | "; //$NON-NLS-1$\r
                 } else {\r
                     buffer.append(separator);\r
                 }\r
@@ -277,12 +333,12 @@ public class CustomXmlTrace extends TmfTrace<CustomXmlEvent> {
                 if (element.hasChildNodes() == false) {\r
                     buffer.append(element.getNodeName());\r
                 } else if (element.getChildNodes().getLength() == 1 && element.getFirstChild().getNodeType() == Node.TEXT_NODE) {\r
-                    buffer.append(element.getNodeName() + ":" + element.getFirstChild().getNodeValue().trim());\r
+                    buffer.append(element.getNodeName() + ":" + element.getFirstChild().getNodeValue().trim()); //$NON-NLS-1$\r
                 } else {\r
                     buffer.append(element.getNodeName());\r
-                    buffer.append(" [ ");\r
+                    buffer.append(" [ "); //$NON-NLS-1$\r
                     parseElement(element, buffer);\r
-                    buffer.append(" ]");\r
+                    buffer.append(" ]"); //$NON-NLS-1$\r
                 }\r
             } else if (node.getNodeType() == Node.TEXT_NODE) {\r
                 if (node.getNodeValue().trim().length() != 0) {\r
@@ -308,7 +364,8 @@ public class CustomXmlTrace extends TmfTrace<CustomXmlEvent> {
     }\r
     \r
     public CustomXmlEvent extractEvent(Element element, InputElement inputElement) {\r
-        CustomXmlEvent event = new CustomXmlEvent(fDefinition, TmfTimestamp.Zero, new TmfEventSource(""), new TmfEventType(fDefinition.definitionName, new String[0]), new TmfEventReference(""));\r
+        CustomXmlEvent event = new CustomXmlEvent(fDefinition, this, TmfTimestamp.Zero, new TmfEventSource(""), fEventType, new TmfEventReference("")); //$NON-NLS-1$ //$NON-NLS-2$\r
+        event.setContent(new CustomEventContent(event, new StringBuffer()));\r
         parseElement(element, event, inputElement);\r
         return event;\r
     }\r
This page took 0.037757 seconds and 5 git commands to generate.