View Javadoc

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