Skip to main content
summaryrefslogtreecommitdiffstats
blob: 78562973893e36126d96d8993f3c349b10f23da2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*******************************************************************************
 * Copyright (c) 2010 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.ote.ui.message.watch.recording.xform;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.logging.Level;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.action.Action;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.ui.plugin.util.AWorkbench;
import org.eclipse.osee.ote.ui.message.internal.Activator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;

/**
 * @author Andrew M. Finkbeiner
 */
public class CsvTransform extends Action {
   @Override
   public void run() {

      FileDialogSelectionGetter getter = new FileDialogSelectionGetter();

      AWorkbench.getDisplay().syncExec(getter);
      if (getter.getPath() != null) {
         File file = new File(getter.getPath());
         File csvFile = new File(getter.getPath() + "_transformed.csv");
         if (!file.exists()) {
            OseeLog.log(Activator.class, Level.SEVERE,
               String.format("[%s] does not exist.  Exiting the csv transformer.", file.toString()));
         }

         try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            LinkedHashSet<String> columns = new LinkedHashSet<String>();

            String line;
            while ((line = br.readLine()) != null) {
               String[] items = line.split(",");
               if (items.length <= 3) {
                  continue;
               }
               String msg = items[1];
               msg += ".";
               for (int i = 3; i < items.length; i += 2) {
                  columns.add(msg + items[i]);
               }
            }
            br.close();

            String[] columnsArray = columns.toArray(new String[columns.size()]);

            BufferedWriter bw = new BufferedWriter(new FileWriter(csvFile));
            bw.write("time,");
            if (columnsArray.length >= 254) {
               OseeLog.log(CsvTransform.class, Level.SEVERE, String.format(
                  "%d columns have been lost in the CsvTransform because there is a limitation of 256 rows in excel.",
                  (columnsArray.length - 254)));
            }
            for (int i = 0; i < columnsArray.length && i < 254; i++) {
               bw.write(columnsArray[i]);
               bw.write(",");
            }
            bw.write("\n");

            br = new BufferedReader(new FileReader(file));
            Map<String, String> values = new HashMap<String, String>();
            while ((line = br.readLine()) != null) {
               values.clear();
               String[] items = line.split(",");
               if (items.length <= 3) {
                  continue;
               }
               String time = items[0];
               String msg = items[1];
               msg += ".";
               for (int i = 3; i < items.length; i += 2) {
                  String value = null;
                  if (i + 1 >= items.length) {
                     value = "null";
                  } else {
                     value = items[i + 1];
                  }
                  values.put(msg + items[i], value);
               }
               bw.write(time);
               bw.write(",");
               for (int i = 0; i < columnsArray.length && i < 254; i++) {
                  String value = values.get(columnsArray[i]);
                  if (value != null) {
                     bw.write(value);
                  }
                  bw.write(",");
               }
               bw.write("\n");
            }
            br.close();
            bw.flush();
            bw.close();

            OseeLog.log(Activator.class, Level.INFO, String.format("Wrote [%s].", csvFile.toString()));
         } catch (Throwable ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
         }
      } else {
         OseeLog.log(Activator.class, Level.INFO, "No file was selected for translating.");
      }
   }

   private static final class FileDialogSelectionGetter implements Runnable {

      String path;

      @Override
      public void run() {
         FileDialog fd = new FileDialog(AWorkbench.getActiveShell(), SWT.OPEN);
         fd.setFilterPath(ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString());
         // fd.setFilterExtensions(new String[]{"*.rec"});
         if (fd.open() != null) {
            path = fd.getFilterPath() + File.separator + fd.getFileName();
            // path = fd.getFileName();
         } else {
            path = null;
         }
      }

      public String getPath() {
         return path;
      }
   }
}

Back to the top