Sample File OrderList taken from IBM "https://www.ibm.com/support/knowledgecenter/SSMKHH_9.0.0/com.ibm.etools.mft.samples.cobol.doc/doc/building.htm" is showing garbage value

32 views
Skip to first unread message

Manalisha Kalita

unread,
Jul 16, 2016, 2:01:15 PM7/16/16
to legstar-user
Binary file Location :OrderList.bin

COpyBook Location : OrderList_CopyBook


Code to read the Binary Host file is : 

byte[] hostData = main.readSampleData(inputDataFile);

     // converting to JSON
String packageName = "com.zaloni.cobol.generated." + copyBookName.toLowerCase();
String subFolder = packageName.replace(".", "/") + "/";
try {
for (Object gen_class : main.gen_classes.toArray()) {
String java_source = (String) gen_class + ".java";
System.out.println("For java class... " + java_source + " with package " + packageName + " in "
+ outputDir.getAbsolutePath());

// compiling the class
File sourceFile = new File(outputDir.getAbsolutePath(), subFolder + java_source);
// Compile source file.
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
System.out.println("Compile class from:" + sourceFile.getPath());
compiler.run(null, null, null, sourceFile.getPath());

// Load and instantiate compiled class.
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { outputDir.toURI().toURL() });
Class<?> cls = Class.forName(packageName + "." + gen_class, true, classLoader);
CobolComplexType instance = (CobolComplexType) cls.newInstance();
System.out.println("Instanciated:" + instance);

// convert to JSON
main.convert(hostData, instance);
       // ****************************************//
// Methods used for conversion on data file//
// ________________________________________//
public String convert(byte[] hostData, CobolComplexType cobolComplexType) {

// Characteristics of the mainframe data (such as code page)
CobolContext cobolContext = new EbcdicCobolContext();

// COBOL structure model (generated by legstar-base-generator)
// CobolComplexType cobolComplexType = new CobolCustomerData();
    ChoiceStrategy ChoiceStrategy = new ChoiceStrategy();

// Create a reusable, thread safe, converter.
Cob2HashMapConverter converter = new Cob2HashMapConverter.Builder().cobolContext(cobolContext)
.cobolComplexType(cobolComplexType).customChoiceStrategy(ChoiceStrategy).build();

// Convert the mainframe data to java map

               
FromHostResult<Map<String, Object>> result = converter.convert(hostData);
                  System.out.println("result" + result.getBytesProcessed());

// Print out the result hash map
System.out.println("Hash map: " + result.getValue());
String jsonString = null;
try {
jsonString = java_map_to_json(result.getValue());
System.out.println("JSON string:" + jsonString);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}

return jsonString;
}
// Reading 

private byte[] readSampleData(String filePath) {
System.out.println("data file path:" + filePath);
File sampleFile = new File(filePath);
byte[] buffer = new byte[(int) sampleFile.length()];
FileInputStream fis = null;
try {
fis = new FileInputStream(sampleFile);
readFill(fis, buffer);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
fis.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return buffer;
}
//reading
private int readFill(InputStream stream, byte[] buffer) throws IOException {
int r = buffer.length;
//System.out.println("buffer length:" + r);
while (r > 0) {
int p = buffer.length - r;
int c = stream.read(buffer, p, r);
      System.out.println("hello reading... " + c);
if (c == -1) {
break;
}
r -= c;
}
return buffer.length - r;

}



OUTPUT Contain the garbage value:
{
    "startpoint": "/",
    "invoicecount": 5,
    "itemcount": 10,
    "inventory": {
        "item": [
            {
                "itemtype": "‘",
                "itemcode": "//",
                "description": "////////////////////€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€",
                "price": 9999,
                "category": "////////////////////€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€"
            },
            {
                "itemtype": "\u0016",
                "itemcode": "ÂÂ",
                "description": "////////////////////€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€",
                "price": 9999,
                "category": "////////////////////€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€"
            },
            {
                "itemtype": "‘",
                "itemcode": "ÄÄ",
                "description": "////////////////////€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€",
                "price": 9999,
                "category": "////////////////////€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€"
            },
            {
                "itemtype": "\u0016",
                "itemcode": "ÀÀ",
                "description": "////////////////////€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€",
                "price": 9999,
                "category": "////////////////////€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€"
            },
            {
                "itemtype": "‘",
                "itemcode": "ÁÁ",
                "description": "////////////////////€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€",
                "price": 9999,
                "category": "////////////////////€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€"
            },
            {
                "itemtype": "\u0016",
                "itemcode": "ÃÃ",
                "description": "////////////////////€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€",
                "price": 9999,
                "category": "////////////////////€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€"
            },
            {
                "itemtype": "‘",
                "itemcode": "ÅÅ",
                "description": "////////////////////€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€",
                "price": 9999,
                "category": "////////////////////€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€"
            },
            {
                "itemtype": "\u0016",
                "itemcode": "ÇÇ",
                "description": "////////////////////€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€",
                "price": 9999,
                "category": "////////////////////€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€"
            },
            {
                "itemtype": "‘",
                "itemcode": "ÑÑ",
                "description": "////////////////////€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€",
                "price": 9999,
                "category": "////////////////////€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€"
            },
            {
                "itemtype": "\u0016",
                "itemcode": "¦¦",
                "description": "////////////////////€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€",
                "price": 9999,
                "category": "////////////////////€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€"
            }
        ]
    },
    "salelist": {
        "invoice": [
            {
                "invoiceheader": {
                    "customerinitial": [
                        "/",
                        "/"
                    ],
                    "surname": "////////////////////€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€"
                },
                "items": {
                    "item": [
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "ÂÂ",
                            "quantity": 1
                        },
                        {
                            "itemcode": "ÄÄ",
                            "quantity": 1
                        },
                        {
                            "itemcode": "ÀÀ",
                            "quantity": 1
                        },
                        {
                            "itemcode": "ÁÁ",
                            "quantity": 1
                        },
                        {
                            "itemcode": "ÃÃ",
                            "quantity": 1
                        },
                        {
                            "itemcode": "ÅÅ",
                            "quantity": 1
                        },
                        {
                            "itemcode": "ÇÇ",
                            "quantity": 1
                        },
                        {
                            "itemcode": "ÑÑ",
                            "quantity": 1
                        },
                        {
                            "itemcode": "¦¦",
                            "quantity": 1
                        }
                    ]
                }
            },
            {
                "invoiceheader": {
                    "customerinitial": [
                        "/",
                        "/"
                    ],
                    "surname": "////////////////////€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€"
                },
                "items": {
                    "item": [
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        }
                    ]
                }
            },
            {
                "invoiceheader": {
                    "customerinitial": [
                        "/",
                        "/"
                    ],
                    "surname": "////////////////////€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€"
                },
                "items": {
                    "item": [
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        }
                    ]
                }
            },
            {
                "invoiceheader": {
                    "customerinitial": [
                        "/",
                        "/"
                    ],
                    "surname": "////////////////////€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€"
                },
                "items": {
                    "item": [
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        }
                    ]
                }
            },
            {
                "invoiceheader": {
                    "customerinitial": [
                        "/",
                        "/"
                    ],
                    "surname": "////////////////////€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€"
                },
                "items": {
                    "item": [
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        },
                        {
                            "itemcode": "//",
                            "quantity": 1
                        }
                    ]
                }
            }
        ]
    },
    "pointofsaletype": "‘",
    "pointofsale1Choice": {
        "pointofsale1": {
            "tillnumber": 999999999,
            "filler26": "////////////////////€€€€€€€€€€€€€€€€€€€€€€€€€€€"
        }
    }
}


ANYONE HELP WILL BE APPRECIABLE...



THANKS


Manalisha Kalita

unread,
Jul 18, 2016, 5:38:29 AM7/18/16
to legstar-user
I got the solution..

Context I applied was wrong :

CobolContext cobolContext = new EbcdicCobolContext();

Since file was encoded with ASCII, about which we were not aware earlier..

Correct context should be :  

     CobolContext cobolContext = new AsciiCobolContext("UTF-8");
Reply all
Reply to author
Forward
0 new messages