tmf/lttng: Update 2014 copyrights
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / internal / ctf / core / event / metadata / IOStructGen.java
index 0ae4aaf5e713631408a94eb03d2a2db8922c9c01..8242a2d3497dd6522181c1a5bff1915d45f2fb86 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others
+ * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others
  *
  * All rights reserved. This program and the accompanying materials are made
  * available under the terms of the Eclipse Public License v1.0 which
@@ -61,13 +61,19 @@ public class IOStructGen {
     /**
      * The trace
      */
-    private final CTFTrace trace;
-    private final CommonTree tree;
+    private final CTFTrace fTrace;
+    private CommonTree fTree;
 
     /**
      * The current declaration scope.
      */
-    private DeclarationScope scope = null;
+    private DeclarationScope fScope = null;
+
+    /**
+     * Data helpers needed for streaming
+     */
+
+    private boolean fHasBeenParsed = false;
 
     // ------------------------------------------------------------------------
     // Constructor
@@ -82,8 +88,9 @@ public class IOStructGen {
      *            the trace containing the places to put all the read metadata
      */
     public IOStructGen(CommonTree tree, CTFTrace trace) {
-        this.trace = trace;
-        this.tree = tree;
+        fTrace = trace;
+        fTree = tree;
+
     }
 
     /**
@@ -93,13 +100,35 @@ public class IOStructGen {
      *             If there was a problem parsing the metadata
      */
     public void generate() throws ParseException {
-        parseRoot(tree);
+        parseRoot(fTree);
+    }
+
+    /**
+     * Parse a partial tree and populate the trace defined in the constructor.
+     * Does not check for a "trace" block as there is only one in the trace and
+     * thus
+     *
+     * @throws ParseException
+     *             If there was a problem parsing the metadata
+     */
+    public void generateFragment() throws ParseException {
+        parseIncompleteRoot(fTree);
     }
 
     // ------------------------------------------------------------------------
     // Operations
     // ------------------------------------------------------------------------
 
+    /**
+     * Sets a new tree to parse
+     *
+     * @param newTree
+     *            the new tree to parse
+     */
+    public void setTree(CommonTree newTree) {
+        fTree = newTree;
+    }
+
     /**
      * Parse the root node.
      *
@@ -126,12 +155,13 @@ public class IOStructGen {
         }
 
         CommonTree traceNode = null;
-        List<CommonTree> streams = new ArrayList<CommonTree>();
-        List<CommonTree> events = new ArrayList<CommonTree>();
-        List<CommonTree> declarations = new ArrayList<CommonTree>();
-        List<CommonTree> environments = new ArrayList<CommonTree>();
-        List<CommonTree> clocks = new ArrayList<CommonTree>();
-        List<CommonTree> callsites = new ArrayList<CommonTree>();
+        List<CommonTree> streams = new ArrayList<>();
+        List<CommonTree> events = new ArrayList<>();
+        List<CommonTree> declarations = new ArrayList<>();
+        List<CommonTree> environments = new ArrayList<>();
+        List<CommonTree> clocks = new ArrayList<>();
+        List<CommonTree> callsites = new ArrayList<>();
+
         /* Create a new declaration scope with no parent. */
         pushScope();
 
@@ -221,7 +251,7 @@ public class IOStructGen {
                 }
             } else {
                 /* Add an empty stream that will have a null id */
-                trace.addStream(new Stream(trace));
+                fTrace.addStream(new Stream(fTrace));
             }
 
             if (DEBUG) {
@@ -244,6 +274,73 @@ public class IOStructGen {
             e.printStackTrace();
         }
         popScope();
+        fHasBeenParsed = true;
+    }
+
+    private void parseIncompleteRoot(CommonTree root) throws ParseException {
+        List<CommonTree> children = root.getChildren();
+
+        if (fHasBeenParsed == false) {
+            throw new ParseException("You need to run generate first"); //$NON-NLS-1$
+        }
+        List<CommonTree> streams = new ArrayList<>();
+        List<CommonTree> events = new ArrayList<>();
+        List<CommonTree> declarations = new ArrayList<>();
+        List<CommonTree> environments = new ArrayList<>();
+        List<CommonTree> clocks = new ArrayList<>();
+        List<CommonTree> callsites = new ArrayList<>();
+        /* Create a new declaration scope with no parent. */
+        pushScope();
+
+        for (CommonTree child : children) {
+            final int type = child.getType();
+            switch (type) {
+            case CTFParser.DECLARATION:
+                declarations.add(child);
+                break;
+            case CTFParser.TRACE:
+                throw new ParseException("Trace block defined here, please use generate and not generateFragment to parse this fragment"); //$NON-NLS-1$
+            case CTFParser.STREAM:
+                streams.add(child);
+                break;
+            case CTFParser.EVENT:
+                events.add(child);
+                break;
+            case CTFParser.CLOCK:
+                clocks.add(child);
+                break;
+            case CTFParser.ENV:
+                environments.add(child);
+                break;
+            case CTFParser.CALLSITE:
+                callsites.add(child);
+                break;
+            default:
+                childTypeError(child);
+            }
+        }
+        for (CommonTree decl : declarations) {
+            parseRootDeclaration(decl);
+        }
+
+        for (CommonTree environment : environments) {
+            parseEnvironment(environment);
+        }
+        for (CommonTree clock : clocks) {
+            parseClock(clock);
+        }
+        for (CommonTree callsite : callsites) {
+            parseCallsite(callsite);
+        }
+
+        for (CommonTree stream : streams) {
+            parseStream(stream);
+        }
+
+        for (CommonTree event : events) {
+            parseEvent(event);
+        }
+        popScope();
     }
 
     private void parseCallsite(CommonTree callsite) {
@@ -277,7 +374,7 @@ public class IOStructGen {
                 lineNumber = Long.parseLong(child.getChild(1).getChild(0).getChild(0).getText());
             }
         }
-        trace.addCallsite(name, funcName, ip, fileName, lineNumber);
+        fTrace.addCallsite(name, funcName, ip, fileName, lineNumber);
     }
 
     private void parseEnvironment(CommonTree environment) {
@@ -287,7 +384,7 @@ public class IOStructGen {
             String right;
             left = child.getChild(0).getChild(0).getChild(0).getText();
             right = child.getChild(1).getChild(0).getChild(0).getText();
-            trace.addEnvironmentVar(left, right);
+            fTrace.addEnvironmentVar(left, right);
         }
     }
 
@@ -325,7 +422,7 @@ public class IOStructGen {
 
         }
         String nameValue = ctfClock.getName();
-        trace.addClock(nameValue, ctfClock);
+        fTrace.addClock(nameValue, ctfClock);
     }
 
     private void parseTrace(CommonTree traceNode) throws ParseException {
@@ -359,7 +456,7 @@ public class IOStructGen {
          * If trace byte order was not specified and not using packet based
          * metadata
          */
-        if (trace.getByteOrder() == null) {
+        if (fTrace.getByteOrder() == null) {
             throw new ParseException("Trace byte order not set"); //$NON-NLS-1$
         }
 
@@ -383,17 +480,17 @@ public class IOStructGen {
         String left = concatenateUnaryStrings(leftStrings);
 
         if (left.equals(MetadataStrings.MAJOR)) {
-            if (trace.majorIsSet()) {
+            if (fTrace.majorIsSet()) {
                 throw new ParseException("major is already set"); //$NON-NLS-1$
             }
 
-            trace.setMajor(getMajorOrMinor(rightNode));
+            fTrace.setMajor(getMajorOrMinor(rightNode));
         } else if (left.equals(MetadataStrings.MINOR)) {
-            if (trace.minorIsSet()) {
+            if (fTrace.minorIsSet()) {
                 throw new ParseException("minor is already set"); //$NON-NLS-1$
             }
 
-            trace.setMinor(getMajorOrMinor(rightNode));
+            fTrace.setMinor(getMajorOrMinor(rightNode));
         } else if (left.equals(MetadataStrings.UUID_STRING)) {
             UUID uuid = getUUID(rightNode);
 
@@ -401,13 +498,13 @@ public class IOStructGen {
              * If uuid was already set by a metadata packet, compare it to see
              * if it matches
              */
-            if (trace.uuidIsSet()) {
-                if (trace.getUUID().compareTo(uuid) != 0) {
+            if (fTrace.uuidIsSet()) {
+                if (fTrace.getUUID().compareTo(uuid) != 0) {
                     throw new ParseException("UUID mismatch. Packet says " //$NON-NLS-1$
-                            + trace.getUUID() + " but metadata says " + uuid); //$NON-NLS-1$
+                            + fTrace.getUUID() + " but metadata says " + uuid); //$NON-NLS-1$
                 }
             } else {
-                trace.setUUID(uuid);
+                fTrace.setUUID(uuid);
             }
 
         } else if (left.equals(MetadataStrings.BYTE_ORDER)) {
@@ -417,16 +514,16 @@ public class IOStructGen {
              * If byte order was already set by a metadata packet, compare it to
              * see if it matches
              */
-            if (trace.getByteOrder() != null) {
-                if (trace.getByteOrder() != byteOrder) {
+            if (fTrace.getByteOrder() != null) {
+                if (fTrace.getByteOrder() != byteOrder) {
                     throw new ParseException(
                             "Endianness mismatch. Magic number says " //$NON-NLS-1$
-                                    + trace.getByteOrder()
+                                    + fTrace.getByteOrder()
                                     + " but metadata says " + byteOrder); //$NON-NLS-1$
                 }
             } else {
-                trace.setByteOrder(byteOrder);
-                final DeclarationScope parentScope = scope.getParentScope();
+                fTrace.setByteOrder(byteOrder);
+                final DeclarationScope parentScope = fScope.getParentScope();
 
                 for (String type : parentScope.getTypeNames()) {
                     IDeclaration d = parentScope.lookupType(type);
@@ -438,7 +535,7 @@ public class IOStructGen {
                 }
             }
         } else if (left.equals(MetadataStrings.PACKET_HEADER)) {
-            if (trace.packetHeaderIsSet()) {
+            if (fTrace.packetHeaderIsSet()) {
                 throw new ParseException("packet.header already defined"); //$NON-NLS-1$
             }
 
@@ -455,7 +552,7 @@ public class IOStructGen {
                 throw new ParseException("packet.header expects a struct"); //$NON-NLS-1$
             }
 
-            trace.setPacketHeader((StructDeclaration) packetHeaderDecl);
+            fTrace.setPacketHeader((StructDeclaration) packetHeaderDecl);
         } else {
             Activator.log(IStatus.WARNING, Messages.IOStructGen_UnknownTraceAttributeWarning + " " + left); //$NON-NLS-1$
         }
@@ -523,7 +620,7 @@ public class IOStructGen {
 
     private void parseStream(CommonTree streamNode) throws ParseException {
 
-        Stream stream = new Stream(trace);
+        Stream stream = new Stream(fTrace);
 
         List<CommonTree> children = streamNode.getChildren();
         if (children == null) {
@@ -551,13 +648,13 @@ public class IOStructGen {
         }
 
         if (stream.isIdSet()) {
-            if (!trace.packetHeaderIsSet()
-                    || !trace.getPacketHeader().hasField(MetadataStrings.STREAM_ID)) {
+            if (!fTrace.packetHeaderIsSet()
+                    || !fTrace.getPacketHeader().hasField(MetadataStrings.STREAM_ID)) {
                 throw new ParseException("Stream has an ID, but there is no stream_id field in packet header."); //$NON-NLS-1$
             }
         }
 
-        trace.addStream(stream);
+        fTrace.addStream(stream);
 
         popScope();
     }
@@ -686,7 +783,7 @@ public class IOStructGen {
          * stream
          */
         if (!event.streamIsSet()) {
-            if (trace.nbStreams() > 1) {
+            if (fTrace.nbStreams() > 1) {
                 throw new ParseException("Event without stream_id with more than one stream"); //$NON-NLS-1$
             }
 
@@ -696,7 +793,7 @@ public class IOStructGen {
              * could be possible to just get the only existing stream, whatever
              * is its id.
              */
-            Stream stream = trace.getStream(null);
+            Stream stream = fTrace.getStream(null);
 
             if (stream != null) {
                 event.setStream(stream);
@@ -752,7 +849,7 @@ public class IOStructGen {
 
             long streamId = getStreamID(rightNode);
 
-            Stream stream = trace.getStream(streamId);
+            Stream stream = fTrace.getStream(streamId);
 
             if (stream == null) {
                 throw new ParseException("Stream " + streamId + " not found"); //$NON-NLS-1$ //$NON-NLS-2$
@@ -965,7 +1062,7 @@ public class IOStructGen {
         CommonTree typeSpecifierList = null;
         CommonTree typeDeclaratorList = null;
         CommonTree typeDeclarator = null;
-        List<CommonTree> pointers = new LinkedList<CommonTree>();
+        List<CommonTree> pointers = new LinkedList<>();
 
         for (CommonTree child : children) {
             switch (child.getType()) {
@@ -1069,8 +1166,8 @@ public class IOStructGen {
 
         IDeclaration declaration = null;
         List<CommonTree> children = null;
-        List<CommonTree> pointers = new LinkedList<CommonTree>();
-        List<CommonTree> lengths = new LinkedList<CommonTree>();
+        List<CommonTree> pointers = new LinkedList<>();
+        List<CommonTree> lengths = new LinkedList<>();
         CommonTree identifier = null;
 
         /* Separate the tokens by type */
@@ -1240,7 +1337,7 @@ public class IOStructGen {
 
         /* The return value */
         FloatDeclaration floatDeclaration = null;
-        ByteOrder byteOrder = trace.getByteOrder();
+        ByteOrder byteOrder = fTrace.getByteOrder();
         long alignment = 0;
         int exponent = 8;
         int mantissa = 24;
@@ -1354,7 +1451,7 @@ public class IOStructGen {
         /* The return value */
         IntegerDeclaration integerDeclaration = null;
         boolean signed = false;
-        ByteOrder byteOrder = trace.getByteOrder();
+        ByteOrder byteOrder = fTrace.getByteOrder();
         long size = 0;
         long alignment = 0;
         int base = 10;
@@ -2040,7 +2137,7 @@ public class IOStructGen {
                 throw new ParseException("Variant tag must be an enum: " + variantTag); //$NON-NLS-1$
             }
             EnumDeclaration tagDecl = (EnumDeclaration) decl;
-            Set<String> intersection = new HashSet<String>(tagDecl.getLabels());
+            Set<String> intersection = new HashSet<>(tagDecl.getLabels());
             intersection.retainAll(variantDeclaration.getFields().keySet());
             if (intersection.isEmpty()) {
                 throw new ParseException("Variant contains no values of the tag, impossible to use: " + variantName); //$NON-NLS-1$
@@ -2321,7 +2418,8 @@ public class IOStructGen {
      * @param unaryInteger
      *            An unary integer node.
      * @return The integer value.
-     * @throws ParseException on an invalid integer format ("bob" for example)
+     * @throws ParseException
+     *             on an invalid integer format ("bob" for example)
      */
     private static long parseUnaryInteger(CommonTree unaryInteger) throws ParseException {
 
@@ -2455,7 +2553,7 @@ public class IOStructGen {
                     || strval.equals(MetadataStrings.NETWORK)) {
                 return ByteOrder.BIG_ENDIAN;
             } else if (strval.equals(MetadataStrings.NATIVE)) {
-                return trace.getByteOrder();
+                return fTrace.getByteOrder();
             } else {
                 throw new ParseException("Invalid value for byte order"); //$NON-NLS-1$
             }
@@ -2725,14 +2823,14 @@ public class IOStructGen {
      * Adds a new declaration scope on the top of the scope stack.
      */
     private void pushScope() {
-        scope = new DeclarationScope(scope);
+        fScope = new DeclarationScope(fScope);
     }
 
     /**
      * Removes the top declaration scope from the scope stack.
      */
     private void popScope() {
-        scope = scope.getParentScope();
+        fScope = fScope.getParentScope();
     }
 
     /**
@@ -2741,7 +2839,7 @@ public class IOStructGen {
      * @return The current declaration scope.
      */
     private DeclarationScope getCurrentScope() {
-        return scope;
+        return fScope;
     }
 
 }
This page took 0.034743 seconds and 5 git commands to generate.