import org.antlr.runtime.ANTLRReaderStream;
import org.antlr.runtime.CommonTokenStream;
+import org.antlr.runtime.MismatchedTokenException;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.tree.CommonTree;
-import org.eclipse.linuxtools.ctf.core.event.metadata.exceptions.ParseException;
import org.eclipse.linuxtools.ctf.parser.CTFLexer;
import org.eclipse.linuxtools.ctf.parser.CTFParser;
import org.eclipse.linuxtools.ctf.parser.CTFParser.parse_return;
import org.eclipse.linuxtools.internal.ctf.core.event.metadata.IOStructGen;
+import org.eclipse.linuxtools.internal.ctf.core.event.metadata.exceptions.ParseException;
/**
- * <b><u>Metadata</u></b>
- * <p>
- * Represents a metadata file
+ * The CTF trace metadata file
+ *
+ * @version 1.0
+ * @author Matthew Khouzam
+ * @author Simon Marchi
*/
public class Metadata {
/**
* Name of the metadata file in the trace directory
*/
- final String METADATA_FILENAME = "metadata"; //$NON-NLS-1$
+ final static String METADATA_FILENAME = "metadata"; //$NON-NLS-1$
/**
* Size of the metadata packet header, in bytes, computed by hand.
*/
- final int METADATA_PACKET_HEADER_SIZE = 37;
+ final static int METADATA_PACKET_HEADER_SIZE = 37;
// ------------------------------------------------------------------------
// Attributes
* Parse the metadata file.
*
* @throws CTFReaderException
+ * If there was a problem parsing the metadata
*/
public void parse() throws CTFReaderException {
- /* Open the file and get the FileChannel */
- FileChannel metadataFileChannel;
- try {
- metadataFileChannel = new FileInputStream(metadataFile).getChannel();
- } catch (FileNotFoundException e) {
- throw new CTFReaderException("Cannot find metadata file!"); //$NON-NLS-1$
- }
+ CTFReaderException tempException = null;
+
+ FileInputStream fis = null;
+ FileChannel metadataFileChannel = null;
/*
* Reader. It will contain a StringReader if we are using packet-based
*/
Reader metadataTextInput = null;
- /* Check if metadata is packet-based */
- if (isPacketBased(metadataFileChannel)) {
- /* Create StringBuffer to receive metadata text */
- StringBuffer metadataText = new StringBuffer();
-
- /*
- * Read metadata packet one by one, appending the text to the
- * StringBuffer
- */
- MetadataPacketHeader packetHeader = readMetadataPacket(
- metadataFileChannel, metadataText);
- while (packetHeader != null) {
- packetHeader = readMetadataPacket(metadataFileChannel,
- metadataText);
- }
+ try {
+ fis = new FileInputStream(metadataFile);
+ metadataFileChannel = fis.getChannel();
+
+ /* Check if metadata is packet-based */
+ if (isPacketBased(metadataFileChannel)) {
+ /* Create StringBuffer to receive metadata text */
+ StringBuffer metadataText = new StringBuffer();
- /* Wrap the metadata string with a StringReader */
- metadataTextInput = new StringReader(metadataText.toString());
- } else {
- /* Wrap the metadata file with a FileReader */
- try {
- metadataTextInput = new FileReader(metadataFile);
- } catch (FileNotFoundException e) {
/*
- * We've already checked for this earlier. Why does StringReader
- * not throw this too??
+ * Read metadata packet one by one, appending the text to the
+ * StringBuffer
*/
- throw new CTFReaderException(e);
+ MetadataPacketHeader packetHeader = readMetadataPacket(
+ metadataFileChannel, metadataText);
+ while (packetHeader != null) {
+ packetHeader = readMetadataPacket(metadataFileChannel,
+ metadataText);
+ }
+
+ /* Wrap the metadata string with a StringReader */
+ metadataTextInput = new StringReader(metadataText.toString());
+ } else {
+ /* Wrap the metadata file with a FileReader */
+ metadataTextInput = new FileReader(metadataFile);
}
- }
- /* Create an ANTLR reader */
- ANTLRReaderStream antlrStream;
- try {
- antlrStream = new ANTLRReaderStream(metadataTextInput);
+ CommonTree tree = createAST(metadataTextInput);
+
+ /* Generate IO structures (declarations) */
+ IOStructGen gen = new IOStructGen(tree, trace);
+ gen.generate();
+
+ } catch (FileNotFoundException e) {
+ tempException = new CTFReaderException("Cannot find metadata file!"); //$NON-NLS-1$
} catch (IOException e) {
/* This would indicate a problem with the ANTLR library... */
- throw new CTFReaderException(e);
+ tempException = new CTFReaderException(e);
+ } catch (ParseException e) {
+ tempException = new CTFReaderException(e);
+ } catch (MismatchedTokenException e) {
+ tempException = new CTFReaderException(e);
+ } catch (RecognitionException e) {
+ tempException = new CTFReaderException(e);
+ }
+
+ /* Ghetto resource management. Java 7 will deliver us from this... */
+ if (metadataTextInput != null) {
+ try {
+ metadataTextInput.close();
+ } catch (IOException e) {
+ // Do nothing
+ }
+ }
+ if (metadataFileChannel != null) {
+ try {
+ metadataFileChannel.close();
+ } catch (IOException e) {
+ // Do nothing
+ }
+ }
+ if (fis != null) {
+ try {
+ fis.close();
+ } catch (IOException e) {
+ // Do nothing
+ }
+ }
+
+ if (tempException != null) {
+ throw tempException;
}
+ }
+
+ private static CommonTree createAST(Reader metadataTextInput) throws IOException,
+ RecognitionException {
+ /* Create an ANTLR reader */
+ ANTLRReaderStream antlrStream;
+ antlrStream = new ANTLRReaderStream(metadataTextInput);
/* Parse the metadata text and get the AST */
CTFLexer ctfLexer = new CTFLexer(antlrStream);
CommonTokenStream tokens = new CommonTokenStream(ctfLexer);
CTFParser ctfParser = new CTFParser(tokens, false);
parse_return ret;
- try {
- ret = ctfParser.parse();
- } catch (RecognitionException e) {
- /*
- * We don't want to expose this ANTLR-specific exception type to the
- * outside..
- */
- throw new CTFReaderException(e);
- }
- CommonTree tree = (CommonTree) ret.getTree();
- /* Generate IO structures (declarations) */
- IOStructGen gen = new IOStructGen(tree, trace);
- try {
- gen.generate();
- } catch (ParseException e) {
- throw new CTFReaderException(e);
- }
+ ret = ctfParser.parse();
+
+ CommonTree tree = (CommonTree) ret.getTree();
+ return tree;
}
/**
try {
metadataFileChannel.read(magicByteBuffer, 0);
} catch (IOException e) {
- throw new CTFReaderException(
- "Unable to read metadata file channel."); //$NON-NLS-1$
+ throw new CTFReaderException("Unable to read metadata file channel."); //$NON-NLS-1$
}
/* Get the first int from the file */
try {
metadataFileChannel.read(payloadByteBuffer);
} catch (IOException e) {
- throw new CTFReaderException(
- "Error reading metadata packet payload."); //$NON-NLS-1$
+ throw new CTFReaderException("Error reading metadata packet payload."); //$NON-NLS-1$
}
payloadByteBuffer.rewind();