/
CsvUtil.java
122 lines (106 loc) · 3.17 KB
/
CsvUtil.java
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
/*
* Copyright (C) 2009 Emweb bvba, Leuven, Belgium.
*
* See the LICENSE file for terms of use.
*/
package eu.webtoolkit.jwt.examples.charts.csv;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import eu.webtoolkit.jwt.Orientation;
import eu.webtoolkit.jwt.WAbstractItemModel;
import eu.webtoolkit.jwt.WContainerWidget;
import eu.webtoolkit.jwt.WStandardItemModel;
import eu.webtoolkit.jwt.WString;
import eu.webtoolkit.jwt.WText;
public class CsvUtil {
public static void readFromCsv(BufferedReader reader, WAbstractItemModel model) {
try {
String line = null;
int row = 0;
while ((line = reader.readLine()) != null) {
List<String> fields = splitHandleQuotes(line);
if (row != 0) {
model.insertRow(model.getRowCount());
}
for (int col = 0; col < fields.size(); col++) {
if (col >= model.getColumnCount())
model.insertColumns(model.getColumnCount(), col + 1 - model.getColumnCount());
String value = fields.get(col);
if (row == 0)
model.setHeaderData(col, Orientation.Horizontal, value);
else {
int dataRow = row - 1;
if (dataRow >= model.getRowCount())
model.insertRows(model.getRowCount(), dataRow + 1 - model.getRowCount());
try {
Double d = Double.valueOf(value);
model.setData(dataRow, col, d);
} catch (NumberFormatException e) {
model.setData(dataRow, col, value);
}
}
}
row++;
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
private static List<String> splitHandleQuotes(String s) {
ArrayList<String> results = new ArrayList<String>();
char delimiter = ',';
char quoteChar = '"';
char escapeChar = '\\';
StringBuffer current = new StringBuffer("");
boolean inQuotation = false;
boolean escaping = false;
for (int i = 0; i < s.length(); ++i) {
if (escaping) {
if (s.charAt(i) == quoteChar)
current.append(quoteChar);
else {
current.append(escapeChar);
current.append(quoteChar);
}
escaping = false;
} else {
if (s.charAt(i) == quoteChar) {
inQuotation = !inQuotation;
} else
if (s.charAt(i) == escapeChar) {
escaping = true;
} else
if (!inQuotation)
if (s.charAt(i) == delimiter) {
results.add(new String(current));
current = new StringBuffer("");
} else
current.append(s.charAt(i));
else
current.append(s.charAt(i));
}
}
results.add(new String(current));
return results;
}
public static WAbstractItemModel readCsvFile(String fname, WContainerWidget parent) {
WStandardItemModel model = new WStandardItemModel(0, 0, parent);
InputStream is = model.getClass().getResourceAsStream("/eu/webtoolkit/jwt/examples/charts/data/"+fname);
if (is!=null) {
readFromCsv(new BufferedReader(new InputStreamReader(is)), model);
return model;
} else {
WString error = WString.tr("error-missing-data");
error.arg(fname);
new WText(error, parent);
return null;
}
}
}