1 package net.sf.pzfilereader.examples.delimiteddynamiccolumnswitherrors;
2
3
4
5
6
7
8 import java.io.File;
9 import java.util.Iterator;
10
11 import net.sf.pzfilereader.DataError;
12 import net.sf.pzfilereader.DataSet;
13 import net.sf.pzfilereader.DefaultPZParserFactory;
14 import net.sf.pzfilereader.DataSet;
15 import net.sf.pzfilereader.PZParser;
16 import net.sf.pzfilereader.ordering.OrderBy;
17 import net.sf.pzfilereader.ordering.OrderColumn;
18
19 /**
20 * @author zepernick
21 *
22 * TODO To change the template for this generated type comment go to Window -
23 * Preferences - Java - Code Style - Code Templates
24 */
25 public class DelimitedWithPZMapErrors {
26 public static void main(final String[] args) throws Exception {
27 String mapping = getDefaultMapping();
28 String data = getDefaultDataFile();
29 call(mapping, data);
30
31 }
32
33 public static String getDefaultDataFile() {
34 return "PEOPLE-CommaDelimitedWithQualifierAndHeaderTrailerRecError.txt";
35 }
36
37 public static String getDefaultMapping() {
38 return "PEOPLE-DelimitedWithHeaderTrailer.pzmap.xml";
39 }
40
41 public static void call(String mapping, String data) throws Exception {
42
43
44
45 final PZParser pzparser = DefaultPZParserFactory.getInstance().newDelimitedParser(new File(mapping), new File(data),
46 ',', '"', true);
47 final DataSet ds = pzparser.parse();
48
49 final OrderBy orderby = new OrderBy();
50 orderby.addOrderColumn(new OrderColumn("CITY", false));
51 orderby.addOrderColumn(new OrderColumn("LASTNAME", true));
52 ds.orderRows(orderby);
53
54 final String[] colNames = ds.getColumns();
55
56 while (ds.next()) {
57
58 if (ds.isRecordID("header")) {
59 System.out.println(">>>>found header");
60 System.out.println("COLUMN NAME: INDICATOR VALUE: " + ds.getString("RECORDINDICATOR"));
61 System.out.println("COLUMN NAME: HEADERDATA VALUE: " + ds.getString("HEADERDATA"));
62 System.out.println("===========================================================================");
63 continue;
64 }
65
66 if (ds.isRecordID("trailer")) {
67 System.out.println(">>>>found trailer");
68 System.out.println("COLUMN NAME: INDICATOR VALUE: " + ds.getString("RECORDINDICATOR"));
69 System.out.println("COLUMN NAME: TRAILERDATA VALUE: " + ds.getString("TRAILERDATA"));
70 System.out.println("===========================================================================");
71 continue;
72 }
73
74
75 for (int i = 0; i < colNames.length; i++) {
76 System.out.println("COLUMN NAME: " + colNames[i] + " VALUE: " + ds.getString(colNames[i]));
77 }
78
79 System.out.println("===========================================================================");
80 }
81
82 System.out.println(">>>>>>ERRORS!!!");
83 final Iterator errors = ds.getErrors().iterator();
84 while (errors.hasNext()) {
85 final DataError dataError = (DataError) errors.next();
86
87 System.out.println("ERROR: " + dataError.getErrorDesc() + " LINE NUMBER: " + dataError.getLineNo());
88 }
89
90 }
91 }