1 package net.sf.cobol2j.examples; 2 3 import net.sf.cobol2j.FieldFormat; 4 import net.sf.cobol2j.FieldsGroup; 5 import net.sf.cobol2j.FieldsList; 6 import net.sf.cobol2j.FileFormat; 7 import net.sf.cobol2j.FileFormatException; 8 import net.sf.cobol2j.RecordFormat; 9 import net.sf.cobol2j.RecordParseException; 10 import net.sf.cobol2j.RecordSet; 11 import net.sf.cobol2j.RecordsMap; 12 13 import org.apache.commons.lang.mutable.MutableInt; 14 15 import org.apache.poi.hssf.usermodel.HSSFRow; 16 import org.apache.poi.hssf.usermodel.HSSFSheet; 17 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 18 19 import java.io.FileInputStream; 20 import java.io.FileNotFoundException; 21 import java.io.FileOutputStream; 22 import java.io.IOException; 23 24 import java.math.BigDecimal; 25 import java.math.BigInteger; 26 27 import java.util.Iterator; 28 import java.util.List; 29 import java.util.Map; 30 31 import javax.xml.bind.JAXBContext; 32 import javax.xml.bind.JAXBException; 33 import javax.xml.bind.Unmarshaller; 34 35 36 public class Dat2xls { 37 public static void main(String[] args) 38 throws FileNotFoundException, IOException, FileFormatException, 39 JAXBException, RecordParseException { 40 String xc2jfilename = args[0]; 41 JAXBContext context = JAXBContext.newInstance("net.sf.cobol2j"); 42 Unmarshaller unmarshaller = context.createUnmarshaller(); 43 Object o = unmarshaller.unmarshal(new FileInputStream(xc2jfilename)); 44 FileFormat fF = (FileFormat) o; 45 RecordSet rset = new RecordSet(System.in, fF); 46 int rowNr = 0; 47 HSSFWorkbook wb = new HSSFWorkbook(); 48 HSSFSheet sheet = wb.createSheet("COBOL2J - dat2xls"); 49 HSSFRow titlerow; 50 HSSFRow datarow; 51 Map recdefs = new RecordsMap(fF); 52 53 while (rset.hasNext()) { 54 List fields = rset.next(); 55 RecordFormat rF = (RecordFormat) recdefs.get("0"); 56 57 if (recdefs.size() > 1) { 58 String first1 = fields.get(0).toString(); 59 rF = (RecordFormat) recdefs.get(first1); 60 } 61 62 MutableInt cellNr = new MutableInt(0); 63 titlerow = sheet.createRow((short) rowNr); 64 datarow = sheet.createRow((short) rowNr + 1); 65 66 Iterator data = fields.listIterator(); 67 createFields(data, rF, titlerow, datarow, cellNr); 68 rowNr = rowNr + 2; 69 } 70 71 wb.write(System.out); 72 } 73 74 private static void createFields(Iterator data, FieldsList deflist, 75 HSSFRow titlerow, HSSFRow datarow, MutableInt cellNr) { 76 Iterator def = deflist.getFieldFormatOrFieldsGroup().listIterator(); 77 78 while (def.hasNext()) { 79 Object u = def.next(); 80 81 if (u instanceof FieldFormat) { 82 int occurs = 1; 83 FieldFormat ff = (FieldFormat) u; 84 String dependingon = ff.getDependingOn(); 85 86 if (dependingon.length() > 0) { 87 int c = cellNr.intValue() - 1; 88 89 while (c >= 0) { 90 if (titlerow.getCell((short) c).getStringCellValue() == dependingon) { 91 occurs = Integer.parseInt(datarow.getCell((short) c) 92 .getStringCellValue()); 93 94 break; 95 } 96 97 c--; 98 } 99 } else { 100 occurs = ff.getOccurs().intValue(); 101 } 102 103 while (occurs-- > 0) { 104 titlerow.createCell(cellNr.shortValue()) 105 .setCellValue(ff.getName()); 106 107 Object o = data.next(); 108 109 if (o instanceof BigDecimal) { 110 datarow.createCell(cellNr.shortValue()) 111 .setCellValue(((BigDecimal) o).doubleValue()); 112 } else if (o instanceof BigInteger) { 113 datarow.createCell(cellNr.shortValue()) 114 .setCellValue(((BigInteger) o).intValue()); 115 } else if (o instanceof Float) { 116 datarow.createCell(cellNr.shortValue()) 117 .setCellValue(((Float) o).intValue()); 118 } else if (o instanceof Double) { 119 datarow.createCell(cellNr.shortValue()) 120 .setCellValue(((Double) o).intValue()); 121 } else if (o instanceof String) { 122 datarow.createCell(cellNr.shortValue()) 123 .setCellValue(o.toString()); 124 } 125 126 cellNr.increment(); 127 } 128 } else if (u instanceof FieldsGroup) { 129 int occurs = 1; 130 FieldsGroup fG = (FieldsGroup) u; 131 String dependingon = fG.getDependingOn(); 132 133 if (dependingon.length() > 0) { 134 int c = cellNr.intValue() - 1; 135 136 while (c >= 0) { 137 if (titlerow.getCell((short) c).getStringCellValue() == dependingon) { 138 occurs = Integer.parseInt(datarow.getCell((short) c) 139 .getStringCellValue()); 140 141 break; 142 } 143 144 c--; 145 } 146 } else { 147 occurs = fG.getOccurs().intValue(); 148 } 149 150 while (occurs-- > 0) { 151 createFields(data, fG, titlerow, datarow, cellNr); 152 } 153 } 154 } 155 } 156 }