Sunday, May 24, 2009

Cobol data + Cobol copybook + Java conversion

Although I'm absolutely not a fan of Cobol, it's still inevitable in the Finance IT development sector.

Lately, we needed a way to let Cobol and Java data work together, and we didn't want to hard code the complete data structure. To make this possible we noticed different non-free applications exist, but the open source project Cb2Xml got our attention.

This project already worked out some Java code to parse a Cobol copybook and convert it into an XML representation. (The copybook can be seen as the interface of the Cobol data). But this project was mend to import and export data from xml to Cobol stream and vice-versa, while we needed some Java objects to work with the received Cobol data input.

So I added some extra code to convert a Cobol data stream (String) into a Java object (using a Hashtable internally). Now, it's possible to provide a String of Cobol data and its interface definition (Cobol copybook file) and return a Java CobolElements object. One can search in this CobolElements object based on the name or xpath. I made a simple example to test, which might make it clear how to use the code.

A simple example of Cobol copybook used as data interface:

 01 ACCOUNT-GROUP                                               .
  02 ACCOUNT                                              .
   03 DFND                                 PIC  9(4)             .
   03 DCTB                                                       .
    04 DUPD                                PIC  9(8)             .
    04 DNUMCTB                                                   .
     05 DCTB-12                            PIC  9(12)            .
     05 DCTB-04                            PIC  9(4)             .
     05 DFMT                               PIC  9(2)             .
    04 DCDRCTB                                                   .
     05 DRGOCDRCTB                         PIC  9(1)             .
     05 DROFCMCCDRCTB                      PIC  9(3)             .
     05 DBRACMCCDRCTB                                            .
      06 DBRACMCCDRCTB                     PIC  9(6)             .
      06 GBRACDRCTB                                               
             REDEFINES DBRACMCCDRCTB                             .
       07 DROFCDRCTB                       PIC  9(3)             .
       07 DBRACDRCTB                       PIC  9(3)             .
    04 DBLK                                PIC  9(2)              
             OCCURS 5                                            .
    04 DBLK2                               PIC  9(3)              
             OCCURS 5                                            .


The data that should match this copybook:



input = "00000000000039300022955600000123703602231122334455111222333444555"; 


Converting the data to a Java CobolElements object:




  • Converting the copybook to an xml representation (this XML Document should be created once for each copybook and can be cached):



Document cb2doc = Cb2Xml.convert(new File(_cobolCopybookFileName), _debug); 



  • Converting the Cobol data stream (input string) to it's matching Java representation:



CobolElements cobolElements = Dat2Java.convertWithDoc(input, cb2doc); 


Retrieving data from the CobolElements object:



CobolElement childElement = cobolElements.retrieveChildElement("GOUTCTT-CSISEQ/GANSCTT-CSISEQ/GCTB/GNUMCTB/NCTB-12"); 
System.out.println(childElement.getData()); 
//returns: 393000229556 
childElement = cobolElements.retrieveChildElement("ACCOUNT-GROUP/ACCOUNT/DCTB/DBLK[3]"); 
System.out.println(childElement.getData()); 
//returns: 44 


Download source and jar (zip 1,78MB) (link updated 16/09/2009)


Update (27/11/2009): After creating the cobol2java, we found another interesting open source project called LegStar. They have a completely worked out solution, while the code we use is quite basic and only usable with simple copy books...

6 comments:

  1. I couldnt download the source file .could you please send me the jar .

    mailtojoy@gmail.com

    ReplyDelete
  2. I updated the link with a new upload of my latest version. Within zip file you can find the jar in the lib\cobolconverter.jar. Keep us informed on any fixes or additions.

    ReplyDelete
  3. the file removed by rapidshare. :(
    can you publish a new link to download?

    ReplyDelete
  4. Dat2Java and Java2Dat missing in the downloaded zip file

    ReplyDelete
  5. I'll try to upload a new version this evening, have to find it back myself somewhere ;)

    ReplyDelete
  6. Dat2Java and Java2Dat missing in the downloaded zip file

    ReplyDelete