/Users/lyon/j4p/src/addBk/address/CsvReader.java
|
1 package addBk.address;
2
3 import futils.CsvParser;
4 import futils.Futil;
5 import futils.ReaderUtil;
6
7 import java.io.BufferedReader;
8 import java.io.IOException;
9 import java.util.Vector;
10
11 /**
12 * CSVReaders are bad way to read in data.
13 * A better way is to input a sample instance
14 * of a class that is serializable (like an Address),
15 * then use the fields of the address to formulate a
16 * means of parsing the input data.
17 * Such information like, record delimiter, field delimiter
18 * and line delimiter should be include during the parsing.
19 * But how is this done?
20 * Given an instance of the Address class, we can automatically
21 * convert the Address into xml using an XmlEncoder, see the
22 * toXml method in xml.adbk.Address.
23 * So, we need to know the XML schema BEFORE we read in
24 * the delimited data. Then, based on the XML schema, and
25 * delimiter information, we should be able to read in the
26 * data and produce correct AML (Address Markup Language).
27 * Thus we need:
28 * 1. Record delimiter
29 * 2. Field delimiter
30 * 3. Line delimiter
31 * 4. tag list, in the correct order.
32 */
33
34 public class CsvReader {
35 private BufferedReader br;
36 private Vector v = new Vector();
37
38 public CsvReader(BufferedReader _br) {
39 br = _br;
40 getLines();
41 System.out.println("read " + v.size() + " lines");
42 System.out.println("number of bad records=" + numberOfBadRecords);
43 }
44
45 public CsvReader(BufferedReader _br, char delimiter) {
46 br = _br;
47 getLines(delimiter);
48 System.out.println("read " + v.size() + " lines");
49 System.out.println("number of bad records=" + numberOfBadRecords);
50 }
51
52 private int numberOfBadRecords = 0;
53
54 class Address {
55 String lastName = "";
56 String firstName = "";
57 String address1 = "";
58 String address2 = "";
59 String address3 = "";
60 String address4 = "";
61 String address5 = "";
62 String homePhone = "";
63 String businessPhone = "";
64 String faxPhone = "";
65
66 Address(String s[]) {
67 try {
68 lastName = s[0];
69 firstName = s[1];
70 address1 = s[2];
71 address2 = s[3];
72 address3 = s[4];
73 address4 = s[5];
74 address5 = s[6];
75 homePhone = s[7];
76 businessPhone = s[8];
77 faxPhone = s[9];
78 } catch (ArrayIndexOutOfBoundsException e) {
79 numberOfBadRecords++;
80 }
81 }
82
83 private String outLine(String s) {
84 if (s.equals("")) return s;
85 return "\n" + s;
86 }
87
88 public String toString() {
89 return outLine("---")
90 +
91 outLine(firstName + " " + lastName)
92 +
93 outLine(address1)
94 +
95 outLine(address2)
96 +
97 outLine(address3)
98 +
99 outLine(address4)
100 +
101 outLine(address5)
102 +
103 outLine(homePhone)
104 +
105 outLine(businessPhone)
106 + outLine(faxPhone);
107 }
108
109 }
110
111 /**
112 * Return the address element converted into a string
113 *
114 * @param i
115 * @return
116 */
117 public String getRecord(int i) {
118 return v.elementAt(i).toString();
119 }
120
121 private void processLine(String l) {
122 v.addElement(new Address(new CsvParser(l).getTokens()));
123 }
124
125 private void processLine(String l, char delimiter) {
126 v.addElement(new Address(new CsvParser(l, delimiter).getTokens()));
127 }
128
129 private void getLines() {
130 try {
131 for (String l = br.readLine();
132 l != null; l = br.readLine())
133 processLine(l);
134 } catch (IOException e) {
135 e.printStackTrace();
136 }
137 }
138
139 private void getLines(char delimiter) {
140 try {
141 for (String l = br.readLine();
142 l != null; l = br.readLine())
143 processLine(l, delimiter);
144 } catch (IOException e) {
145 e.printStackTrace();
146 }
147 }
148
149 public static void main(String args[]) {
150 final char delimiter = ';';
151 CsvReader cr = new CsvReader(
152 ReaderUtil.getBufferedReader(
153 Futil.getReadFile("Select a CSV file")),delimiter);
154 for (int i = 0; i < 10; i++)
155 System.out.println(cr.getRecord(i));
156 }
157 }