package org.eclipse.linuxtools.internal.tmf.ui.parsers.custom;\r
\r
import java.io.ByteArrayInputStream;\r
-import java.io.File;\r
-import java.io.FileNotFoundException;\r
import java.io.IOException;\r
import java.io.RandomAccessFile;\r
\r
\r
import org.eclipse.core.resources.IProject;\r
import org.eclipse.core.resources.IResource;\r
+import org.eclipse.linuxtools.internal.tmf.ui.Activator;\r
import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTraceDefinition.InputAttribute;\r
import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTraceDefinition.InputElement;\r
import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;\r
private final CustomXmlTraceDefinition fDefinition;\r
private final CustomXmlEventType fEventType;\r
private final InputElement fRecordInputElement;\r
+ private BufferedRandomAccessFile fFile;\r
\r
public CustomXmlTrace(final CustomXmlTraceDefinition definition) {\r
fDefinition = definition;\r
fEventType = new CustomXmlEventType(fDefinition);\r
fRecordInputElement = getRecordInputElement(fDefinition.rootInputElement);\r
+ setCacheSize(DEFAULT_CACHE_SIZE);\r
}\r
\r
public CustomXmlTrace(final IResource resource, final CustomXmlTraceDefinition definition, final String path, final int pageSize) throws TmfTraceException {\r
- super(null, CustomXmlEvent.class, path, (pageSize > 0) ? pageSize : DEFAULT_CACHE_SIZE);\r
- fDefinition = definition;\r
- fEventType = new CustomXmlEventType(fDefinition);\r
- fRecordInputElement = getRecordInputElement(fDefinition.rootInputElement);\r
+ this(definition);\r
+ setCacheSize((pageSize > 0) ? pageSize : DEFAULT_CACHE_SIZE);\r
+ initTrace(resource, path, CustomXmlEvent.class);\r
}\r
\r
@Override\r
public void initTrace(final IResource resource, final String path, final Class<CustomXmlEvent> eventType) throws TmfTraceException {\r
super.initTrace(resource, path, eventType);\r
+ try {\r
+ fFile = new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$\r
+ } catch (IOException e) {\r
+ throw new TmfTraceException(e.getMessage(), e);\r
+ }\r
+ indexTrace(false);\r
}\r
\r
@Override\r
- public TmfContext seekEvent(final ITmfLocation<?> location) {\r
+ public synchronized void dispose() {\r
+ super.dispose();\r
+ if (fFile != null) {\r
+ try {\r
+ fFile.close();\r
+ } catch (IOException e) {\r
+ } finally {\r
+ fFile = null;\r
+ }\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public synchronized TmfContext seekEvent(final ITmfLocation<?> location) {\r
final CustomXmlTraceContext context = new CustomXmlTraceContext(NULL_LOCATION, ITmfContext.UNKNOWN_RANK);\r
- if (NULL_LOCATION.equals(location) || !new File(getPath()).isFile())\r
+ if (NULL_LOCATION.equals(location) || fFile == null) {\r
return context;\r
+ }\r
try {\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
+ if (location == null) {\r
+ fFile.seek(0);\r
+ } else if (location.getLocation() instanceof Long) {\r
+ fFile.seek((Long) location.getLocation());\r
}\r
-\r
String line;\r
final String recordElementStart = "<" + fRecordInputElement.elementName; //$NON-NLS-1$\r
- long rawPos = context.raFile.getFilePointer();\r
+ long rawPos = fFile.getFilePointer();\r
\r
- while ((line = context.raFile.getNextLine()) != null) {\r
+ while ((line = fFile.getNextLine()) != null) {\r
final int idx = line.indexOf(recordElementStart);\r
if (idx != -1) {\r
context.setLocation(new TmfLocation<Long>(rawPos + idx));\r
return context;\r
}\r
- rawPos = context.raFile.getFilePointer();\r
+ rawPos = fFile.getFilePointer();\r
}\r
return context;\r
- } catch (final FileNotFoundException e) {\r
- e.printStackTrace();\r
- return context;\r
} catch (final IOException e) {\r
- e.printStackTrace();\r
+ Activator.getDefault().logError("Error seeking event. File: " + getPath(), e); //$NON-NLS-1$\r
return context;\r
}\r
\r
}\r
\r
@Override\r
- public TmfContext seekEvent(final double ratio) {\r
- BufferedRandomAccessFile raFile = null;\r
+ public synchronized TmfContext seekEvent(final double ratio) {\r
+ if (fFile == null) {\r
+ return new CustomTxtTraceContext(NULL_LOCATION, ITmfContext.UNKNOWN_RANK);\r
+ }\r
try {\r
- raFile = new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$\r
- long pos = (long) (ratio * raFile.length());\r
+ long pos = (long) (ratio * fFile.length());\r
while (pos > 0) {\r
- raFile.seek(pos - 1);\r
- if (raFile.read() == '\n') {\r
+ fFile.seek(pos - 1);\r
+ if (fFile.read() == '\n') {\r
break;\r
}\r
pos--;\r
final TmfContext context = seekEvent(location);\r
context.setRank(ITmfContext.UNKNOWN_RANK);\r
return context;\r
- } catch (final FileNotFoundException e) {\r
- e.printStackTrace();\r
- return new CustomXmlTraceContext(NULL_LOCATION, ITmfContext.UNKNOWN_RANK);\r
} catch (final IOException e) {\r
- e.printStackTrace();\r
+ Activator.getDefault().logError("Error seeking event. File: " + getPath(), e); //$NON-NLS-1$\r
return new CustomXmlTraceContext(NULL_LOCATION, ITmfContext.UNKNOWN_RANK);\r
- } finally {\r
- if (raFile != null) {\r
- try {\r
- raFile.close();\r
- } catch (final IOException e) {\r
- }\r
- }\r
}\r
}\r
\r
@Override\r
- public double getLocationRatio(final ITmfLocation<?> location) {\r
- RandomAccessFile raFile = null;\r
+ public synchronized double getLocationRatio(final ITmfLocation<?> location) {\r
+ if (fFile == null) {\r
+ return 0;\r
+ }\r
try {\r
if (location.getLocation() instanceof Long) {\r
- raFile = new RandomAccessFile(getPath(), "r"); //$NON-NLS-1$\r
- return (double) ((Long) location.getLocation()) / raFile.length();\r
+ return (double) ((Long) location.getLocation()) / fFile.length();\r
}\r
- } catch (final FileNotFoundException e) {\r
- e.printStackTrace();\r
} catch (final IOException e) {\r
- e.printStackTrace();\r
- } finally {\r
- if (raFile != null) {\r
- try {\r
- raFile.close();\r
- } catch (final IOException e) {\r
- }\r
- }\r
+ Activator.getDefault().logError("Error getting location ration. File: " + getPath(), e); //$NON-NLS-1$\r
}\r
return 0;\r
}\r
return null;\r
}\r
\r
+ @Override\r
+ public synchronized CustomXmlEvent parseEvent(final ITmfContext tmfContext) {\r
+ ITmfContext context = seekEvent(tmfContext.getLocation());\r
+ return parse(context);\r
+ }\r
+\r
@Override\r
public synchronized CustomXmlEvent getNext(final ITmfContext context) {\r
final ITmfContext savedContext = context.clone();\r
- final CustomXmlEvent event = parseEvent(context);\r
+ final CustomXmlEvent event = parse(context);\r
if (event != null) {\r
updateAttributes(savedContext, event.getTimestamp());\r
context.increaseRank();\r
return event;\r
}\r
\r
- @Override\r
- public CustomXmlEvent parseEvent(final ITmfContext tmfContext) {\r
- if (!(tmfContext instanceof CustomXmlTraceContext))\r
+ private synchronized CustomXmlEvent parse(final ITmfContext tmfContext) {\r
+ if (fFile == null) {\r
return null;\r
+ }\r
+ if (!(tmfContext instanceof CustomXmlTraceContext)) {\r
+ return null;\r
+ }\r
\r
final CustomXmlTraceContext context = (CustomXmlTraceContext) tmfContext;\r
- if (!(context.getLocation().getLocation() instanceof Long) || NULL_LOCATION.equals(context.getLocation()))\r
+ if (!(context.getLocation().getLocation() instanceof Long) || NULL_LOCATION.equals(context.getLocation())) {\r
return null;\r
+ }\r
\r
- synchronized (context.raFile) {\r
- CustomXmlEvent event = null;\r
- try {\r
- if (context.raFile.getFilePointer() != (Long)context.getLocation().getLocation() + 1)\r
- {\r
- context.raFile.seek((Long)context.getLocation().getLocation() + 1); // +1 is for the <\r
- }\r
- final StringBuffer elementBuffer = new StringBuffer("<"); //$NON-NLS-1$\r
- readElement(elementBuffer, context.raFile);\r
- final Element element = parseElementBuffer(elementBuffer);\r
-\r
- event = extractEvent(element, fRecordInputElement);\r
- ((StringBuffer) event.getContent().getValue()).append(elementBuffer);\r
-\r
- String line;\r
- final String recordElementStart = "<" + fRecordInputElement.elementName; //$NON-NLS-1$\r
- long rawPos = context.raFile.getFilePointer();\r
-\r
- while ((line = context.raFile.getNextLine()) != null) {\r
- final int idx = line.indexOf(recordElementStart);\r
- if (idx != -1) {\r
- context.setLocation(new TmfLocation<Long>(rawPos + idx));\r
- return event;\r
- }\r
- rawPos = context.raFile.getFilePointer();\r
+ CustomXmlEvent event = null;\r
+ try {\r
+ if (fFile.getFilePointer() != (Long)context.getLocation().getLocation() + 1)\r
+ {\r
+ fFile.seek((Long)context.getLocation().getLocation() + 1); // +1 is for the <\r
+ }\r
+ final StringBuffer elementBuffer = new StringBuffer("<"); //$NON-NLS-1$\r
+ readElement(elementBuffer, fFile);\r
+ final Element element = parseElementBuffer(elementBuffer);\r
+\r
+ event = extractEvent(element, fRecordInputElement);\r
+ ((StringBuffer) event.getContent().getValue()).append(elementBuffer);\r
+\r
+ String line;\r
+ final String recordElementStart = "<" + fRecordInputElement.elementName; //$NON-NLS-1$\r
+ long rawPos = fFile.getFilePointer();\r
+\r
+ while ((line = fFile.getNextLine()) != null) {\r
+ final int idx = line.indexOf(recordElementStart);\r
+ if (idx != -1) {\r
+ context.setLocation(new TmfLocation<Long>(rawPos + idx));\r
+ return event;\r
}\r
- } catch (final IOException e) {\r
- e.printStackTrace();\r
+ rawPos = fFile.getFilePointer();\r
}\r
- context.setLocation(NULL_LOCATION);\r
- return event;\r
+ } catch (final IOException e) {\r
+ Activator.getDefault().logError("Error parsing event. File: " + getPath(), e); //$NON-NLS-1$\r
+\r
}\r
+ context.setLocation(NULL_LOCATION);\r
+ return event;\r
}\r
\r
private Element parseElementBuffer(final StringBuffer elementBuffer) {\r
final Document doc = db.parse(new ByteArrayInputStream(elementBuffer.toString().getBytes()));\r
return doc.getDocumentElement();\r
} catch (final ParserConfigurationException e) {\r
- e.printStackTrace();\r
+ Activator.getDefault().logError("Error parsing element buffer. File:" + getPath(), e); //$NON-NLS-1$\r
} catch (final SAXException e) {\r
- e.printStackTrace();\r
+ Activator.getDefault().logError("Error parsing element buffer. File:" + getPath(), e); //$NON-NLS-1$\r
} catch (final IOException e) {\r
- e.printStackTrace();\r
+ Activator.getDefault().logError("Error parsing element buffer. File: " + getPath(), e); //$NON-NLS-1$\r
}\r
return null;\r
}\r
break; // found "</"\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
+ } else if (i == '>') {\r
if (buffer.charAt(buffer.length() - 2) == '/') {\r
break; // found "/>"\r
} else if (startTagClosed) {\r
else {\r
startTagClosed = true; // found "<...>"\r
}\r
+ }\r
}\r
return;\r
} catch (final IOException e) {\r
}\r
}\r
\r
- private void readQuote(final StringBuffer buffer, final RandomAccessFile raFile, final char eq) {\r
+ private static void readQuote(final StringBuffer buffer,\r
+ final RandomAccessFile raFile, final char eq) {\r
try {\r
int i;\r
while ((i = raFile.read()) != -1) {\r
}\r
}\r
\r
- private void readComment(final StringBuffer buffer, final RandomAccessFile raFile) {\r
+ private static void readComment(final StringBuffer buffer,\r
+ final RandomAccessFile raFile) {\r
try {\r
int numRead = 0;\r
int i;\r
buffer.append(separator);\r
}\r
final Element element = (Element) node;\r
- if (element.hasChildNodes() == false) {\r
+ if (!element.hasChildNodes()) {\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()); //$NON-NLS-1$\r
parseElement(element, buffer);\r
buffer.append(" ]"); //$NON-NLS-1$\r
}\r
- } else if (node.getNodeType() == Node.TEXT_NODE)\r
+ } else if (node.getNodeType() == Node.TEXT_NODE) {\r
if (node.getNodeValue().trim().length() != 0) {\r
buffer.append(node.getNodeValue().trim());\r
}\r
+ }\r
}\r
return buffer;\r
}\r
\r
public InputElement getRecordInputElement(final InputElement inputElement) {\r
- if (inputElement.logEntry)\r
+ if (inputElement.logEntry) {\r
return inputElement;\r
- else if (inputElement.childElements != null) {\r
+ } else if (inputElement.childElements != null) {\r
for (final InputElement childInputElement : inputElement.childElements) {\r
final InputElement recordInputElement = getRecordInputElement(childInputElement);\r
- if (recordInputElement != null)\r
+ if (recordInputElement != null) {\r
return recordInputElement;\r
+ }\r
}\r
}\r
return null;\r
\r
public CustomXmlEvent extractEvent(final Element element, final InputElement inputElement) {\r
final CustomXmlEvent event = new CustomXmlEvent(fDefinition, this, TmfTimestamp.ZERO, "", fEventType,""); //$NON-NLS-1$ //$NON-NLS-2$\r
- event.setContent(new CustomEventContent(event, "")); //$NON-NLS-1$\r
+ event.setContent(new CustomEventContent(event, new StringBuffer()));\r
parseElement(element, event, inputElement);\r
return event;\r
}\r
for (int i = 0; i < childNodes.getLength(); i++) {\r
final Node node = childNodes.item(i);\r
if (node instanceof Element) {\r
- for (final InputElement child : inputElement.childElements)\r
+ for (final InputElement child : inputElement.childElements) {\r
if (node.getNodeName().equals(child.elementName)) {\r
parseElement((Element) node, event, child);\r
break;\r
}\r
+ }\r
}\r
}\r
}\r