View Javadoc

1   package net.sf.flatpack.examples.fixedlengthheaderandtrailer;
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  
16  /**
17   * @author zepernick
18   * 
19   * TODO To change the template for this generated type comment go to Window -
20   * Preferences - Java - Code Style - Code Templates
21   */
22  public class FixedLengthHeaderAndTrailer {
23      public static void main(final String[] args) throws Exception {
24          final String mapping = getDefaultMapping();
25          final String data = getDefaultDataFile();
26          call(mapping, data);
27  
28      }
29  
30      public static String getDefaultDataFile() {
31          return "PEOPLE-FixedLengthWithHeaderTrailer.txt";
32      }
33  
34      public static String getDefaultMapping() {
35          return "PEOPLE-FixedLengthWithHeaderTrailer.pzmap.xml";
36      }
37  
38      public static void call(final String mapping, final String data) throws Exception {
39          Iterator errors = null;
40          DataError dataError = null;
41          final Parser pzparser = DefaultParserFactory.getInstance().newFixedLengthParser(new File(mapping), new File(data));
42          final DataSet ds = pzparser.parse();
43  
44          while (ds.next()) {
45  
46              if (ds.isRecordID("header")) {
47                  System.out.println(">>>>found header");
48                  System.out.println("COLUMN NAME: INDICATOR VALUE: " + ds.getString("INDICATOR"));
49                  System.out.println("COLUMN NAME: HEADERDATA VALUE: " + ds.getString("HEADERDATA"));
50                  System.out.println("===========================================================================");
51                  continue;
52              }
53  
54              if (ds.isRecordID("trailer")) {
55                  System.out.println(">>>>found trailer");
56                  System.out.println("COLUMN NAME: INDICATOR VALUE: " + ds.getString("INDICATOR"));
57                  System.out.println("COLUMN NAME: TRAILERDATA VALUE: " + ds.getString("TRAILERDATA"));
58                  System.out.println("===========================================================================");
59                  continue;
60              }
61  
62              System.out.println("COLUMN NAME: FIRSTNAME VALUE: " + ds.getString("FIRSTNAME"));
63              System.out.println("COLUMN NAME: LASTNAME VALUE: " + ds.getString("LASTNAME"));
64              System.out.println("COLUMN NAME: ADDRESS VALUE: " + ds.getString("ADDRESS"));
65              System.out.println("COLUMN NAME: CITY VALUE: " + ds.getString("CITY"));
66              System.out.println("COLUMN NAME: STATE VALUE: " + ds.getString("STATE"));
67              System.out.println("COLUMN NAME: ZIP VALUE: " + ds.getString("ZIP"));
68              System.out.println("===========================================================================");
69  
70          }
71  
72          if (ds.getErrors() != null && ds.getErrors().size() > 0) {
73              errors = ds.getErrors().iterator();
74  
75              while (errors.hasNext()) {
76                  dataError = (DataError) errors.next();
77  
78                  System.out.println("ERROR: " + dataError.getErrorDesc() + " LINE NUMBER: " + dataError.getLineNo());
79              }
80          }
81  
82      }
83  }