View Javadoc

1   package net.sf.cobol2j.examples;
2   
3   import net.sf.cobol2j.FileFormat;
4   import net.sf.cobol2j.FileFormatException;
5   import net.sf.cobol2j.RecordParseException;
6   import net.sf.cobol2j.RecordSet;
7   
8   import org.apache.commons.logging.Log;
9   import org.apache.commons.logging.LogFactory;
10  
11  import java.io.FileInputStream;
12  import java.io.FileNotFoundException;
13  import java.io.IOException;
14  
15  import java.util.Iterator;
16  import java.util.List;
17  
18  import javax.xml.bind.JAXBContext;
19  import javax.xml.bind.JAXBException;
20  import javax.xml.bind.Unmarshaller;
21  
22  
23  public class Dat2csv {
24      private static Log log = LogFactory.getLog(Dat2csv.class);
25  
26      public static void main(String[] args)
27          throws FileFormatException, JAXBException, FileNotFoundException,
28              IOException, RecordParseException {
29          String xc2jfilename = args[0];
30          JAXBContext context = JAXBContext.newInstance("net.sf.cobol2j");
31          Unmarshaller unmarshaller = context.createUnmarshaller();
32          Object o = unmarshaller.unmarshal(new FileInputStream(xc2jfilename));
33          FileFormat fF = (FileFormat) o;
34          RecordSet rset = new RecordSet(System.in, fF);
35  
36          Iterator fields;
37  
38          while (rset.hasNext()) {
39              try {
40                  fields = rset.next().iterator();
41  
42                  boolean afterfirst = false;
43  
44                  while (fields.hasNext()) {
45                      if (afterfirst) {
46                          System.out.print(',');
47                      }
48  
49                      System.out.print(fields.next().toString());
50                      afterfirst = true;
51                  }
52              } catch (RecordParseException ex) {
53                  displayErrorRecord(ex);
54  
55                  break;
56              }
57  
58              System.out.println();
59          }
60      }
61  
62      private static void displayErrorRecord(RecordParseException e) {
63          StringBuffer b = new StringBuffer();
64          b.append(e.getMessage() + "\n");
65          b.append("Partial record. Fields values until failed field:" + "\n");
66  
67          for (Object o : e.getPartialRecord()) {
68              b.append(o.toString() + "|");
69          }
70  
71          log.error(b);
72      }
73  }