Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts

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...

Thursday, February 19, 2009

Easy PrettyPrint XML files


Xml files are much more readable, when they are 'Pretty Printed', using correct indentation.
I made a little tool which will provide a new context menu when right-clicking on an xml file: 'Pretty Print'. When clicking on it, all xml indentation will be set correctly and the original file will be overwritten with a pretty printed version of it.

Just run this setup to install this little add-on, tested in Windows XP. One can check what's inside the setup with WinRar, since you should never trust an exe coming from the net.

Update 16/02/2012: dropbox link to setup file.