1 package org.catacomb.dataview.formats;
2
3 import java.io.BufferedReader;
4 import java.util.ArrayList;
5 import java.util.StringTokenizer;
6
7 import org.catacomb.report.E;
8
9
10
11
12 public class TableDataReader implements DataReader {
13
14 TableDataHandler handler;
15
16 int ncol;
17 String[] columnNames;
18 ArrayList<double[]> data;
19
20 public TableDataReader(TableDataHandler tdh) {
21 handler = tdh;
22 data = new ArrayList<double[]>();
23 }
24
25
26 public boolean canRead(String line) {
27 boolean ret = false;
28 if (line.startsWith("tableColumnNames") || line.startsWith("tableRow")) {
29 ret = true;
30 }
31 return ret;
32 }
33
34
35 public void readBlock(String line, BufferedReader br) {
36
37 if (line.startsWith("tableColumnNames")) {
38 StringTokenizer st = new StringTokenizer(line, " ");
39 st.nextToken();
40 st.nextToken();
41 ncol = Integer.parseInt(st.nextToken());
42
43 columnNames = new String[ncol];
44 int nread = 0;
45 while (nread < ncol) {
46
47
48 try {
49 String sline = br.readLine();
50
51
52 StringTokenizer xst = new StringTokenizer(sline);
53 while (xst.hasMoreTokens() && nread < ncol) {
54 columnNames[nread] = xst.nextToken();
55 nread += 1;
56 }
57 } catch (Exception ex) {
58 E.error("cant read line needed in table data reader " + ex);
59 }
60
61 }
62
63
64 } else if (line.startsWith("tableRow")) {
65 StringTokenizer st = new StringTokenizer(line, " ");
66 st.nextToken();
67 st.nextToken();
68 int ndat = Integer.parseInt(st.nextToken());
69
70 double[] dat = new double[ndat];
71 int nread = 0;
72 try {
73 while (nread < ncol) {
74 String sline = br.readLine();
75 StringTokenizer xst = new StringTokenizer(sline);
76 while (xst.hasMoreTokens() && nread < ncol) {
77 dat[nread] = Double.parseDouble(xst.nextToken());
78 nread += 1;
79 }
80 }
81 } catch (Exception ex) {
82 E.error("read error " + ex);
83 }
84 if (dat.length != ncol) {
85 E.error("wrong length data in table reader: need " + ncol + " but got " + dat.length);
86 } else {
87 data.add(dat);
88 }
89
90
91 } else {
92 E.error("cant read " + line);
93 }
94
95 }
96
97
98
99
100 public void fix() {
101 handler.setColumnNames(columnNames);
102
103 double[][] db = new double[ncol][data.size()];
104 for (int i = 0; i < data.size(); i++) {
105 double[] dat = data.get(i);
106 for (int j = 0; j < ncol; j++) {
107 db[j][i] = dat[j];
108 }
109 }
110 handler.setData(db);
111 }
112
113
114
115
116
117 }