1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.diyefi.openlogviewer.decoder;
24
25 import java.io.BufferedReader;
26 import java.io.File;
27 import java.io.FileReader;
28 import java.io.IOException;
29 import java.util.ResourceBundle;
30 import java.util.Scanner;
31
32 import org.diyefi.openlogviewer.OpenLogViewer;
33 import org.diyefi.openlogviewer.genericlog.GenericLog;
34 import org.diyefi.openlogviewer.utils.Utilities;
35 import org.diyefi.openlogviewer.Text;
36
37 public class CSVTypeLog extends AbstractDecoder {
38 private static final int LOAD_FACTOR = 2;
39 private static final String[] DELIMITERS = {"\t", ",", ":", "/", "\\\\"};
40 private final ResourceBundle labels;
41 private int fieldCount = -1;
42
43
44
45
46 public CSVTypeLog(final File f, final ResourceBundle labels) {
47 this.labels = labels;
48 this.setLogFile(f);
49 this.setT(new Thread(this, CSVTypeLog.class.getSimpleName()));
50 this.getT().setPriority(Thread.MAX_PRIORITY);
51 this.getT().start();
52 }
53
54 @Override
55 public final void run() {
56 try {
57 final long startTime = System.currentTimeMillis();
58 decodeLog();
59 OpenLogViewer.getInstance().getEntireGraphingPanel().setGraphSize(this.getDecodedLog().getRecordCount());
60 this.getDecodedLog().setLogStatus(GenericLog.LogState.LOG_LOADED);
61 System.out.println(labels.getString(Text.CSV_LOADED_MSG_PART1)
62 + (this.getDecodedLog().getRecordCount() + 1) + labels.getString(Text.CSV_LOADED_MSG_PART2)
63 + (System.currentTimeMillis() - startTime) + labels.getString(Text.CSV_LOADED_MSG_PART3));
64 } catch (IOException e) {
65 e.printStackTrace();
66 if (this.getDecodedLog() != null) {
67 this.getDecodedLog().setLogStatusMessage(e.getMessage());
68 } else {
69 System.out.println("FIXME!!! CSVTypeLog");
70 }
71 }
72 }
73
74
75
76
77
78
79
80
81
82
83 protected final void decodeLog() throws IOException {
84 final Scanner scan = new Scanner(new BufferedReader(new FileReader(getLogFile())));
85 final String delimiter = scanForDelimiter();
86
87 String[] splitLine;
88 String[] headers = new String[1];
89
90 int finalAndInitialLength = Utilities.countBytes(getLogFile(), (byte) '\n');
91
92 String line = "";
93 boolean headerSet = false;
94 while (scan.hasNextLine() && !headerSet) {
95 line = scan.nextLine();
96 splitLine = line.split(delimiter);
97
98 if (splitLine.length == fieldCount) {
99 headers = splitLine;
100 this.setDecodedLog(new GenericLog(splitLine, finalAndInitialLength, LOAD_FACTOR, labels));
101 headerSet = true;
102 }
103 }
104
105 while (scan.hasNextLine()) {
106 line = scan.nextLine();
107 splitLine = line.split(delimiter);
108 this.getDecodedLog().incrementPosition();
109 if (splitLine.length == fieldCount) {
110 for (int x = 0; x < splitLine.length; x++) {
111 this.getDecodedLog().addValue(headers[x], Double.parseDouble(splitLine[x]));
112 }
113 }
114 }
115 }
116
117
118
119
120
121
122 private String scanForDelimiter() throws IOException {
123 final FileReader reader = new FileReader(getLogFile());
124 final Scanner scan = new Scanner(reader);
125
126 final int magicNumber = 10;
127 String delimiterFind = "";
128 String[] split;
129 int index = -1;
130
131 for (int i = 0; i < magicNumber && scan.hasNext(); i++) {
132 delimiterFind = scan.nextLine();
133 for (int j = 0; j < DELIMITERS.length; j++) {
134 split = delimiterFind.split(DELIMITERS[j]);
135 if (split.length > fieldCount) {
136 fieldCount = split.length;
137 index = j;
138 }
139 }
140 }
141
142 scan.close();
143 reader.close();
144 return DELIMITERS[index];
145 }
146 }