View Javadoc

1   package net.sf.flatpack.examples.delimiteddynamiccolumns;
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 DelimitedWithPZMap {
25      public static void main(final String[] args) throws Exception {
26  
27          final String mapping = getDefaultMapping();
28          final 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(final String mapping, final String data) throws Exception {
42          // delimited by a comma
43          // text qualified by double quotes
44          // ignore first record
45          OrderBy orderby = null;
46          final Parser pzparser = DefaultParserFactory.getInstance().newDelimitedParser(new File(mapping), new File(data), ',', '"', true);
47          final DataSet ds = pzparser.parse();
48  
49          // re order the data set by last name
50          orderby = new OrderBy();
51          orderby.addOrderColumn(new OrderColumn("CITY", false));
52          orderby.addOrderColumn(new OrderColumn("LASTNAME", true));
53          // ds.orderRows(orderby);
54  
55          final String[] colNames = ds.getColumns();
56  
57          while (ds.next()) {
58  
59              if (ds.isRecordID("header")) {
60                  System.out.println(">>>>found header");
61                  System.out.println("COLUMN NAME: INDICATOR VALUE: " + ds.getString("RECORDINDICATOR"));
62                  System.out.println("COLUMN NAME: HEADERDATA VALUE: " + ds.getString("HEADERDATA"));
63                  System.out.println("===========================================================================");
64                  continue;
65              }
66  
67              if (ds.isRecordID("trailer")) {
68                  System.out.println(">>>>found trailer");
69                  System.out.println("COLUMN NAME: INDICATOR VALUE: " + ds.getString("RECORDINDICATOR"));
70                  System.out.println("COLUMN NAME: TRAILERDATA VALUE: " + ds.getString("TRAILERDATA"));
71                  System.out.println("===========================================================================");
72                  continue;
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          if (ds.getErrors() != null && ds.getErrors().size() > 0) {
83              System.out.println("<<<<FOUND ERRORS IN FILE>>>>");
84              final Iterator pzerrors = ds.getErrors().iterator();
85              while (pzerrors.hasNext()) {
86                  final DataError error = (DataError) pzerrors.next();
87                  System.out.println("Error Msg: " + error.getErrorDesc() + " line no: " + error.getLineNo());
88              }
89  
90          }
91  
92      }
93  }