View Javadoc

1   package net.sf.pzfilereader.examples.csvheaderandtrailer;
2   
3   /*
4    * Created on Dec 31, 2004
5    *
6    */
7   
8   import java.io.File;
9   
10  import net.sf.pzfilereader.DefaultPZParserFactory;
11  import net.sf.pzfilereader.DataSet;
12  import net.sf.pzfilereader.PZParser;
13  
14  /**
15   * @author zepernick
16   * 
17   * TODO To change the template for this generated type comment go to Window -
18   * Preferences - Java - Code Style - Code Templates
19   */
20  public class CSVHeaderAndTrailer {
21      public static void main(final String[] args) throws Exception {
22          call(getDefaultMapping(), getDefaultDataFile());
23      }
24  
25      public static String getDefaultMapping() {
26          return "PEOPLE-DelimitedWithHeaderTrailer.pzmap.xml";
27      }
28  
29      public static String getDefaultDataFile() {
30          return "PEOPLE-CommaDelimitedWithQualifier.txt";
31      }
32  
33      public static void call(final String mapping, final String data) throws Exception {
34          final File mapFile = new File(mapping);
35          final File dataFile = new File(data);
36          // delimited by a comma
37          // text qualified by double quotes
38          // ignore first record 
39          final PZParser pzparser = DefaultPZParserFactory.getInstance().newDelimitedParser(mapFile, dataFile, 
40                  ',', '\"', true);
41          final DataSet ds = pzparser.parse();
42          while (ds.next()) {
43  
44              if (ds.isRecordID("header")) {
45                  System.out.println(">>>>>>Found Header Record");
46                  System.out.println("COLUMN NAME: RECORDINDICATOR VALUE: " + ds.getString("RECORDINDICATOR"));
47                  System.out.println("COLUMN NAME: HEADERDATA VALUE: " + ds.getString("HEADERDATA"));
48                  System.out.println("===========================================================================");
49                  continue;
50              }
51  
52              if (ds.isRecordID("trailer")) {
53                  System.out.println(">>>>>>Found Trailer Record");
54                  System.out.println("COLUMN NAME: RECORDINDICATOR VALUE: " + ds.getString("RECORDINDICATOR"));
55                  System.out.println("COLUMN NAME: TRAILERDATA VALUE: " + ds.getString("TRAILERDATA"));
56                  System.out.println("===========================================================================");
57                  continue;
58              }
59  
60              System.out.println("COLUMN NAME: FIRSTNAME VALUE: " + ds.getString("FIRSTNAME"));
61              System.out.println("COLUMN NAME: LASTNAME VALUE: " + ds.getString("LASTNAME"));
62              System.out.println("COLUMN NAME: ADDRESS VALUE: " + ds.getString("ADDRESS"));
63              System.out.println("COLUMN NAME: CITY VALUE: " + ds.getString("CITY"));
64              System.out.println("COLUMN NAME: STATE VALUE: " + ds.getString("STATE"));
65              System.out.println("COLUMN NAME: ZIP VALUE: " + ds.getString("ZIP"));
66              System.out.println("===========================================================================");
67          }
68  
69          if (ds.getErrors() != null && ds.getErrors().size() > 0) {
70              System.out.println("FOUND ERRORS IN FILE");
71          }
72      }
73  }