|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| package org.proteomecommons.tranche.tasks; |
|
18 |
| |
|
19 |
| import java.io.BufferedReader; |
|
20 |
| import java.io.File; |
|
21 |
| import java.io.FileReader; |
|
22 |
| import java.io.IOException; |
|
23 |
| import java.util.ArrayList; |
|
24 |
| import java.util.HashMap; |
|
25 |
| import java.util.List; |
|
26 |
| import java.util.Map; |
|
27 |
| import org.proteomecommons.tranche.util.BigHash; |
|
28 |
| import org.proteomecommons.tranche.util.ExcelSavvyLineSplitter; |
|
29 |
| import org.proteomecommons.tranche.util.IOUtil; |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| public class CSVReader { |
|
39 |
| |
|
40 |
| private File file; |
|
41 |
| |
|
42 |
| private CSVFile csvObj; |
|
43 |
| |
|
44 |
| private List<String> legend; |
|
45 |
| private List<Map<String,String>> records; |
|
46 |
| |
|
47 |
| private static final String COMMA_TOKEN = "COMMA_TOKEN"; |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
1
| public CSVReader(File file) {
|
|
54 |
1
| this.file = file;
|
|
55 |
1
| this.legend = new ArrayList();
|
|
56 |
1
| this.records = new ArrayList();
|
|
57 |
| } |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
1
| public CSVFile read() throws Exception {
|
|
65 |
| |
|
66 |
1
| BufferedReader in = null;
|
|
67 |
| |
|
68 |
| |
|
69 |
1
| int line = 1;
|
|
70 |
| |
|
71 |
1
| try {
|
|
72 |
| |
|
73 |
1
| in = new BufferedReader(new FileReader(this.file));
|
|
74 |
1
| String str;
|
|
75 |
| |
|
76 |
| |
|
77 |
1
| boolean isLegend = true;
|
|
78 |
| |
|
79 |
| |
|
80 |
?
| while ((str = in.readLine()) != null) {
|
|
81 |
| |
|
82 |
| |
|
83 |
1
| if (!str.trim().equals("")) {
|
|
84 |
| |
|
85 |
1
| String[] keys = this.getEntries(str);
|
|
86 |
| |
|
87 |
1
| for (String key : keys) {
|
|
88 |
3
| this.legend.add(key.trim());
|
|
89 |
| |
|
90 |
| |
|
91 |
3
| try {
|
|
92 |
| |
|
93 |
3
| BigHash.createHashFromString(key);
|
|
94 |
| |
|
95 |
| |
|
96 |
0
| isLegend = false;
|
|
97 |
0
| break;
|
|
98 |
| } |
|
99 |
| |
|
100 |
| catch(Exception e) { } |
|
101 |
| } |
|
102 |
| |
|
103 |
| |
|
104 |
1
| line++;
|
|
105 |
1
| break;
|
|
106 |
| } |
|
107 |
| |
|
108 |
0
| line++;
|
|
109 |
| } |
|
110 |
| |
|
111 |
| |
|
112 |
1
| if (!isLegend) {
|
|
113 |
| |
|
114 |
| |
|
115 |
0
| IOUtil.safeClose(in);
|
|
116 |
0
| in = new BufferedReader(new FileReader(this.file));
|
|
117 |
| |
|
118 |
0
| this.legend.clear();
|
|
119 |
| |
|
120 |
| |
|
121 |
0
| int numEntries = getEntries(str).length;
|
|
122 |
| |
|
123 |
0
| for (int i = 1; i <= numEntries; i++) {
|
|
124 |
0
| this.legend.add("Column #"+i);
|
|
125 |
| } |
|
126 |
| } |
|
127 |
| |
|
128 |
| |
|
129 |
?
| while ((str = in.readLine()) != null) {
|
|
130 |
| |
|
131 |
| |
|
132 |
4
| if (!str.trim().equals("")) {
|
|
133 |
| |
|
134 |
4
| String[] items = this.getEntries(str);
|
|
135 |
| |
|
136 |
4
| if (items.length != this.legend.size()) {
|
|
137 |
0
| throw new IOException("Wrong number of entries on line " + line +": expecting " + this.legend.size() +", found " +items.length);
|
|
138 |
| } |
|
139 |
| |
|
140 |
| |
|
141 |
4
| Map<String,String> record = new HashMap();
|
|
142 |
| |
|
143 |
4
| for (int i = 0; i < items.length; i++) {
|
|
144 |
12
| String key = this.legend.get(i);
|
|
145 |
12
| String value = items[i].trim();
|
|
146 |
| |
|
147 |
12
| record.put(key, value);
|
|
148 |
| } |
|
149 |
| |
|
150 |
4
| this.records.add(record);
|
|
151 |
| } |
|
152 |
| |
|
153 |
4
| line++;
|
|
154 |
| } |
|
155 |
| |
|
156 |
| } |
|
157 |
| |
|
158 |
| finally { |
|
159 |
1
| IOUtil.safeClose(in);
|
|
160 |
| } |
|
161 |
| |
|
162 |
1
| return new CSVFile(this.file, this.legend, this.records);
|
|
163 |
| } |
|
164 |
| |
|
165 |
| |
|
166 |
| |
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
5
| private String[] getEntries(String entry) {
|
|
173 |
5
| return ExcelSavvyLineSplitter.split(entry);
|
|
174 |
| } |
|
175 |
| } |