Using JRccord to access COBOL schema

83 Views Asked by At

I'm trying to use JRecord version 0.93 to parse a COBOL copybook to understand the individual fields present in the record. I'm a complete newbie to JRecord and I'm struggling to get what seems like a simple task done.

In the file "test.cbl", I have this:

01 RECORD-DATA.
    05 KEY-FIELD PIC X(8).
    05 DATA-FIELD PIC X(72). 

I hope to get a list of the field names, their data types and lengths.

I found the beginnings of an example (here https://sourceforge.net/p/jrecord/discussion/678634/thread/176dcea6):

ICobolSchemaReader reader = CobolSchemaReader.newCobolSchemaReader("test.cbl");
CobolSchemaDetails schemaDetails = reader.getCobolSchemaDetails();

Unfortunately, while the above compiles cleanly, it doesn't seem to work:

java.lang.ClassCastException: net.sf.JRecord.schema.CobolSchemaReader incompatible with net.sf.JRecord.schema.ICobolSchemaReader
    at net.sf.JRecord.schema.CobolSchemaReader.newCobolSchemaReader(CobolSchemaReader.java:272)
    at TestXform.main(TestJReader.java:56)

Has anyone done this sort of thing before? Any better approach or examples online that I might learn from?

2

There are 2 best solutions below

0
On BEST ANSWER

After trying a few different things, I found an approach that seems to work:

 IIOBuilder iob = JRecordInterface1.COBOL
                                   .newIOBuilder("test.cbl")
                                   .setFileOrganization(Constants.IO_FIXED_LENGTH_RECORDS)
                                   .setFont("cp037")
                                   .setDialect(ICopybookDialects.FMT_MAINFRAME);
 LayoutDetail ld = iob.getLayout();             
 String [] fields = ld.getFieldDescriptions(0, 0);
 for (int i = 0; i < fields.length; i++)
 {
    IFieldDetail fd = ld.getField(0, i);
    System.out.println("Field #" + i + " = " + fd.getGroupName() + fields[i] + 
                       ", Len = " + fd.getLen() + 
                       ", Type = " + fd.getType() + 
                       ", Decimal " + fd.getDecimal());
 }

There's more to do - I notice if I define a field in the copybook as an array, there doesn't seem to be an indicator in the IFieldDetail telling me that. But at least I feel like I'm on the right track now.

2
On

From looking at the code https://github.com/bmTas/JRecord/blob/a4b9d40ca525cc809588cf80fa592809fa2799f3/Source/JRecord_Project/JRecord/src/main/java/net/sf/JRecord/schema/CobolSchemaReader.java#L138-L141 - just use

CobolSchemaReader reader = CobolSchemaReader.newCobolSchemaReader("test.cbl");
CobolSchemaDetails schemaDetails = reader.getCobolSchemaDetails();

which is also used that way in its test programs.

Explanation: the reader only implements implements ISchemaIOBuilder, not the ICobolSchemaReader - for whatever reason; but this object does have a getCobolSchemaDetails() method.

Note: for questions on this library you possibly want to check its issue tracker.