[Tmf][Ctf] Add descriptive fail to import messages.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / trace / Metadata.java
index 0648c1194bdd6a617dbdf81dd9e50a7459d03201..27ca681d63be828d4c3e67c81b0d9a99f2ee895a 100644 (file)
@@ -27,6 +27,7 @@ import java.util.UUID;
 
 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.parser.CTFLexer;
@@ -36,9 +37,11 @@ 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 {
 
@@ -117,6 +120,7 @@ public class Metadata {
      * Parse the metadata file.
      *
      * @throws CTFReaderException
+     *             If there was a problem parsing the metadata
      */
     public void parse() throws CTFReaderException {
         CTFReaderException tempException = null;
@@ -158,19 +162,7 @@ public class Metadata {
                 metadataTextInput = new FileReader(metadataFile);
             }
 
-            /* 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;
-
-            ret = ctfParser.parse();
-
-            CommonTree tree = (CommonTree) ret.getTree();
+            CommonTree tree = createAST(metadataTextInput);
 
             /* Generate IO structures (declarations) */
             IOStructGen gen = new IOStructGen(tree, trace);
@@ -183,11 +175,9 @@ public class Metadata {
             tempException = new CTFReaderException(e);
         } catch (ParseException e) {
             tempException = new CTFReaderException(e);
+        } catch (MismatchedTokenException e) {
+            tempException = new CTFReaderException(e);
         } catch (RecognitionException e) {
-            /*
-             * We don't want to expose this ANTLR-specific exception type to the
-             * outside..
-             */
             tempException = new CTFReaderException(e);
         }
 
@@ -219,6 +209,24 @@ public class Metadata {
         }
     }
 
+    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;
+
+        ret = ctfParser.parse();
+
+        CommonTree tree = (CommonTree) ret.getTree();
+        return tree;
+    }
+
     /**
      * Determines whether the metadata file is packet-based by looking at the
      * TSDL magic number. If it is packet-based, it also gives information about
@@ -241,8 +249,7 @@ public class Metadata {
         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 */
@@ -344,8 +351,7 @@ public class Metadata {
         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();
 
This page took 0.035896 seconds and 5 git commands to generate.