1 package net.sf.flatpack.examples.csvheaderandtrailer;
2
3
4
5
6
7
8 import java.io.File;
9
10 import net.sf.flatpack.DataSet;
11 import net.sf.flatpack.DefaultParserFactory;
12 import net.sf.flatpack.Parser;
13
14 /**
15 * @author zepernick
16 *
17 * TODO To change the template for this generated type comment go to Window -
18 * Preferences - Java - Code Style - Code Templates
19 */
20 public class CSVHeaderAndTrailer {
21 public static void main(final String[] args) throws Exception {
22 call(getDefaultMapping(), getDefaultDataFile());
23 }
24
25 public static String getDefaultMapping() {
26 return "PEOPLE-DelimitedWithHeaderTrailer.pzmap.xml";
27 }
28
29 public static String getDefaultDataFile() {
30 return "PEOPLE-CommaDelimitedWithQualifier.txt";
31 }
32
33 public static void call(final String mapping, final String data) throws Exception {
34 final File mapFile = new File(mapping);
35 final File dataFile = new File(data);
36
37
38
39 final Parser pzparser = DefaultParserFactory.getInstance().newDelimitedParser(mapFile, dataFile, ',', '\"', true);
40 final DataSet ds = pzparser.parse();
41 while (ds.next()) {
42
43 if (ds.isRecordID("header")) {
44 System.out.println(">>>>>>Found Header Record");
45 System.out.println("COLUMN NAME: RECORDINDICATOR VALUE: " + ds.getString("RECORDINDICATOR"));
46 System.out.println("COLUMN NAME: HEADERDATA VALUE: " + ds.getString("HEADERDATA"));
47 System.out.println("===========================================================================");
48 continue;
49 }
50
51 if (ds.isRecordID("trailer")) {
52 System.out.println(">>>>>>Found Trailer Record");
53 System.out.println("COLUMN NAME: RECORDINDICATOR VALUE: " + ds.getString("RECORDINDICATOR"));
54 System.out.println("COLUMN NAME: TRAILERDATA VALUE: " + ds.getString("TRAILERDATA"));
55 System.out.println("===========================================================================");
56 continue;
57 }
58
59 System.out.println("COLUMN NAME: FIRSTNAME VALUE: " + ds.getString("FIRSTNAME"));
60 System.out.println("COLUMN NAME: LASTNAME VALUE: " + ds.getString("LASTNAME"));
61 System.out.println("COLUMN NAME: ADDRESS VALUE: " + ds.getString("ADDRESS"));
62 System.out.println("COLUMN NAME: CITY VALUE: " + ds.getString("CITY"));
63 System.out.println("COLUMN NAME: STATE VALUE: " + ds.getString("STATE"));
64 System.out.println("COLUMN NAME: ZIP VALUE: " + ds.getString("ZIP"));
65 System.out.println("===========================================================================");
66 }
67
68 if (ds.getErrors() != null && ds.getErrors().size() > 0) {
69 System.out.println("FOUND ERRORS IN FILE");
70 }
71 }
72 }