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