How do I identify the level of a field in copybook using JRecord in Java?

风流意气都作罢 提交于 2019-12-02 10:27:09

Why do you need to get the Field Level ???. To convert a file to ascii you do not need the Field Level.


Utility Conversion Program

To Convert a Cobol File to ascii you can use one of the utility programs:

  • Cobol2Csv sub-project - converts a Cobol data file to a Csv file
  • Cobol2Xml sub-project - converts a Cobol data file to a Xml file
  • Cobol2Json json utiltiy

Java processing of a Cobol file

If you want to do some processing on the File, you can use the Generate function of the RecordEditor to generate sample JRecord code from the Cobol copybook.

Code generated with standard template

If you use the standard template, the RecordEditor will generate code like:

    AbstractLine line;
    int lineNum = 0;

    try {
        ICobolIOBuilder iob = JRecordInterface1.COBOL
                                   .newIOBuilder(copybookName)
                                       .setFont("cp037")
                                       .setFileOrganization(Constants.IO_FIXED_LENGTH)
                                       .setSplitCopybook(CopybookLoader.SPLIT_NONE)
                                   ;  


        FieldNamesDtar020.RecordDtar020 rDtar020 = FieldNamesDtar020.RECORD_DTAR020;
        AbstractLineReader reader = iob.newReader(dataFile);
        while ((line = reader.read()) != null) {
            lineNum += 1;
            System.out.println(
                          line.getFieldValue(rDtar020.keycodeNo).asString()
                  + " " + line.getFieldValue(rDtar020.storeNo).asString()
                  + " " + line.getFieldValue(rDtar020.date).asString()
                  + " " + line.getFieldValue(rDtar020.deptNo).asString()
                  + " " + line.getFieldValue(rDtar020.qtySold).asString()
                  + " " + line.getFieldValue(rDtar020.salePrice).asString()
               );
        }

        reader.close();
    } catch (Exception e) {
        System.out.println("~~> " + lineNum + " " + e);
        System.out.println();

        e.printStackTrace();
    }

Code generated with lineWrapper template

  AbstractLine line;
    int lineNum = 0;

    try {
        ICobolIOBuilder iob = JRecordInterface1.COBOL
                                   .newIOBuilder(copybookName)
                                       .setFont("cp037")
                                       .setFileOrganization(Constants.IO_FIXED_LENGTH)
                                       .setSplitCopybook(CopybookLoader.SPLIT_NONE)
                                   ;  


       LineDtar020JR lineDtar020JR = new LineDtar020JR();


       AbstractLineReader reader = iob.newReader(dataFile);
       while ((line = reader.read()) != null) {
           lineNum += 1;
           lineDtar020JR.setLine(line);
           System.out.println(
                          lineDtar020JR.getKeycodeNo() 
                  + " " + lineDtar020JR.getStoreNo() 
                  + " " + lineDtar020JR.getDate() 
                  + " " + lineDtar020JR.getDeptNo() 
                  + " " + lineDtar020JR.getQtySold() 
                  + " " + lineDtar020JR.getSalePrice() 
               );
        }

        reader.close();
    } catch (Exception e) {
        System.out.println("~~> " + lineNum + " " + e);
        System.out.println();

        e.printStackTrace();
    }

Generic Cobol processing

If you want to do more generic processing you can use a fieldIterator:

FieldIterator fieldIterator = line.getFieldIterator("Record-Name");

JRecord Examples

In the latest release of JRecord 0.81.4 there are examples in the Source/JRecord_IO_Builder_Examples/src directory


Tree processing

If you need to access level numbers with JRecord, use CobolSchemaReader.newCobolSchemaReader(...) interface.

Also you could look at the code for Cobol2Xml sub-project. It does tree processing by extending CobolSchemaReader

Have a read of http://www.catb.org/~esr/faqs/smart-questions.html or https://www.mikeash.com/getting_answers.html about asking questions

But any way:

Using the Code Generation

  • Enter the Cobol Copybook, Sample cobol file (if you hava one). You will probably be able to use the Split Copybook On Redefines option

  • On the Records panel, enter DA147-EXTRA-TYPE in the Record Type field

  • Press the Generate Code button. On the next screen you can select the template. The Standard template is a good starting point

  • Press the Generate code button, The program should generate some sample code like:

        ICobolIOBuilder iob = JRecordInterface1.COBOL
                                   .newIOBuilder(copybookName)
                                       .setFileOrganization(Constants.IO_BIN_TEXT)
                                       .setSplitCopybook(CopybookLoader.SPLIT_REDEFINE)
                                   ;  
    
    
        FieldNamesAmspodownloadRedef1.RecordPoHeaderRecord rPoHeaderRecord = FieldNamesAmspodownloadRedef1.RECORD_PO_HEADER_RECORD;
        FieldNamesAmspodownloadRedef1.RecordProductRecord rProductRecord = FieldNamesAmspodownloadRedef1.RECORD_PRODUCT_RECORD;
        FieldNamesAmspodownloadRedef1.RecordLocationRecord rLocationRecord = FieldNamesAmspodownloadRedef1.RECORD_LOCATION_RECORD;
        AbstractLineReader reader = iob.newReader(dataFile);
        while ((line = reader.read()) != null) {
            lineNum += 1;
            if (
               "H1".equals(line.getFieldValue(rPoHeaderRecord.recordType).asString())
            )  {
    
               System.out.println(
                          line.getFieldValue(rPoHeaderRecord.recordType).asString()
                  + " " + line.getFieldValue(rPoHeaderRecord.sequenceNumber).asString()
                  + " " + line.getFieldValue(rPoHeaderRecord.vendor).asString()
                  + " " + line.getFieldValue(rPoHeaderRecord.po).asString()
                    ....
                  + " " + line.getFieldValue(rPoHeaderRecord.cancelByDate).asString()
                  + " " + line.getFieldValue(rPoHeaderRecord.ediType).asString()
               );
            }
            if (
               "D1".equals(line.getFieldValue(rProductRecord.recordType).asString())
            )  {
    
               System.out.println(
                          line.getFieldValue(rProductRecord.recordType).asString()
                  + " " + line.getFieldValue(rProductRecord.packQty).asString()
                  + " " + line.getFieldValue(rProductRecord.packCost).asString()
                  + " " + line.getFieldValue(rProductRecord.apn).asString()
                  + " " + 
                      .....
                  + " " + line.getFieldValue(rProductRecord.productName).asString()
               );
            }
            if (
               "S1".equals(line.getFieldValue(rLocationRecord.recordType).asString())
            )  {
    
               System.out.println(
                          line.getFieldValue(rLocationRecord.recordType).asString()
                  + " " + line.getFieldValue(rLocationRecord.dcNumbe.get(0)).asString()
                  + " " + line.getFieldValue(rLocationRecord.packQuantit.get(0)).asString()
               );
            }
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!