View Javadoc

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