Menu

[r27]: / src / com / res / cobol / Main.java  Maximize  Restore  History

Download this file

512 lines (449 with data), 23.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
package com.res.cobol;
/*****************************************************************************
Copyright 2009 Venkat Krishnamurthy
This file is part of RES.
RES is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
RES is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RES. If not, see <http://www.gnu.org/licenses/>.
@author VenkatK mailto: open.cobol.to.java at gmail.com
******************************************************************************/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import com.res.cobol.parser.CobolParser;
import com.res.cobol.parser.CobolParserTokenManager;
import com.res.cobol.parser.ParseException;
import com.res.cobol.parser.RESCharStream;
import com.res.cobol.parser.TokenMgrError;
import com.res.cobol.syntaxtree.CompilationUnit;
import com.res.cobol.visitor.TreeDumper;
import com.res.common.RESConfig;
import com.res.common.RESContext;
import com.res.java.translation.engine.Cobol2Java;
import com.res.java.translation.engine.CobolFillTable;
import com.res.java.translation.engine.CobolRecode;
import com.res.java.translation.engine.VsamIsam2DbGen;
import com.res.java.translation.symbol.SymbolTable;
import com.res.java.util.ClassFile;
public class Main {
private RESConfig config = RESConfig.getInstance();
private static RESContext context = new RESContext();
private File srcF;
private boolean doListDir=false;
private Preprocessor preprocessor = new Preprocessor();
private long startClock;
class CobolFileFilter implements FilenameFilter {
@Override
public boolean accept(File arg0, String arg1) {
if((arg1=arg1.trim()).length()<=4) return false;
arg1=arg1.substring(arg1.length()-4);
if(arg1.equalsIgnoreCase(".COB") ||
arg1.equalsIgnoreCase(".CBL"))
return true;
return false;
}
}
public static void main(String[] args) {
Main instance=new Main();
try {
int i = instance.getOptions(args);
instance.startClock=System.currentTimeMillis();
System.out.println("RES Cobol 2 Java" +RESConfig.getInstance().versionString+"- COPYRIGHT 2009");
if(context.isProcessVerbose()) {
System.out.println("Options: "+context.getOptionsVerbose().toString());
}
if(!instance.doListDir) {
instance.execute(args[i]);
} else {
File dotDir=new File(args[i]);
System.out.println("The Java classes are under the folder: "+new File(".").getAbsolutePath());
System.out.println("Classes from translation of programs will reside in the package: "+RESConfig.getInstance().getProgramPackage());
if(RESConfig.getInstance().getDataPackage()!=null&&RESConfig.getInstance().getDataPackage().length()>0)
System.out.println("Classes from translation of data levels will reside in the package: "+RESConfig.getInstance().getDataPackage());
for(String s:dotDir.list(instance.new CobolFileFilter()))
instance.execute(s);
}
} catch(Exception e) {
System.err.println(args[args.length-1]);
e.printStackTrace();
}
System.out.println("Done in " +Math.round((System.currentTimeMillis()-instance.startClock)/1000)+"s.\r\n");
System.exit(0);
}
public void execute(String fileName) {
File destF = null;
try {
System.err.println(fileName);
RESConfig.getInstance().setInError(false);
context.setSourceFileName(fileName.substring(fileName
.replace('/',File.separatorChar)
.lastIndexOf(File.separatorChar)+1));
srcF=new File(fileName);
if(!srcF.exists()||!srcF.isFile()||!srcF.canRead()){
System.out.println("Invalid Source File: "+fileName);
} else {
String dest="$temporary.file";
if(context.isProcessPreprocessOnly()) {
preprocessor.preprocess(fileName, null);
}
else {
destF = new File(dest);
if(destF.exists()) destF.delete();
preprocessor.preprocess(fileName, dest);
if(!destF.exists()) return;
System.out.println("Parsing Cobol started for: "+srcF.getAbsolutePath()+"");
context.setSourceFile(new BufferedReader(new InputStreamReader(new FileInputStream(destF))));
if(!context.getSourceFile().ready()) throw new ParseException();
if(context.isProcessParseOnly()) {
parse(context);
} else {
CompilationUnit unit=parse(context);
if(RESConfig.getInstance().isInError()) {
System.out.println("Errors encountered. Processing terminated.");
} else
translate(unit);
unit=null;
}
//destF.delete();
destF=null;
}
}
} catch(TokenMgrError pe) {
System.err.println(srcF.getAbsolutePath());
pe.printStackTrace();
} catch(ParseException pe) {
System.err.println(srcF.getAbsolutePath());
pe.printStackTrace();
} catch(IOException eio) {
System.err.println(srcF.getAbsolutePath());
eio.printStackTrace();
}
if(doListDir&&srcF!=null) System.out.println("Done: "+srcF.getAbsolutePath()+"\n");
if(destF!=null) destF.delete();
if(context.getSourceFile()!=null)
try {
context.getSourceFile().close();
} catch (IOException e) {
e.printStackTrace();
}
context.setSourceFile(null);
destF=null;
srcF=null;
CobolParserTokenManager.commentLines.clear();
CobolParserTokenManager.lastToken=null;
SymbolTable.clear();
System.gc();
}
private CobolParser cobolParser=null;
public CompilationUnit parse( RESContext context ) {
CompilationUnit unit ;
try {
context.setCharStream(new RESCharStream(context.getSourceFile()));
cobolParser=new CobolParser(new CobolParserTokenManager(context.getCharStream()));
if(!context.isTraceOn()||context.getTraceLevel()%2==0)
cobolParser.disable_tracing();
unit = cobolParser.CompilationUnit();
if(context.isTraceOn()&&context.getTraceLevel()%2!=0) {
System.out.println("************************************************************************");
TreeDumper dump=new TreeDumper();
dump.visit(unit);
return null;
}
context.getCharStream().Done();
cobolParser=null;
}
catch(ParseException pe) {
pe.printStackTrace();
unit=null;
}
return unit;
}
public void translate(CompilationUnit unit) {
try{
//Create Symbol Table
unit.accept(CobolFillTable.getInstance());
if(RESConfig.getInstance().isInError()) {
System.out.println("Errors encountered. Processing terminated.");
return;
}
System.out.println("Translation to Java started.");
if(!doListDir) {
printPackageHeader();
}
if(context.isTraceOn())
System.out.println("Removing Dead Code...");
//Do Dead-Code Removal and later Restructuring if any
unit.accept(CobolRecode.getInstance());
if(context.isTraceOn()) {
System.out.println("************************************************************************");
SymbolTable.getScope().display();
}
//Do cobol 2 java
if(context.isTraceOn())
System.out.println("Translating Cobol 2 Java...");
unit.accept(Cobol2Java.getInstance());
ClassFile.endProgramScope();
SymbolTable.getScope().endProgram();
if(config.isToGenVSAMISAMDb())
genDB(unit);
}
catch( Exception ex ) {
ex.printStackTrace();
}
}
private static final String DB_GEN_PACKAGE = ".db_gen";
public void genDB(CompilationUnit unit) {
System.out.println("Generation of classes to migrate VSAM/ISAM to DB tables started.");
System.out.println("Migration classes reside in the package: "+RESConfig.getInstance().getProgramPackage()+DB_GEN_PACKAGE);
unit.accept(VsamIsam2DbGen.getInstance());
}
private static void printPackageHeader() {
try {
System.out.println("The java classes are under the folder: "+new File(".").getCanonicalPath());
} catch (IOException e) {
System.out.println("Errors encountered: "+e.getMessage()+". Processing terminated.");
return;
}
System.out.println("Classes from translation of programs reside in the package: "+RESConfig.getInstance().getProgramPackage());
if(RESConfig.getInstance().getDataPackage()!=null&&RESConfig.getInstance().getDataPackage().length()>0)
System.out.println("Classes from translation of data levels reside in the package: "+RESConfig.getInstance().getDataPackage());
}
private int getOptions(String[] args) {
int i=0;
if(i>=args.length) Main.displayProgramUsageAndExit();
if(args[0].equalsIgnoreCase("-dbmigrate")) {
return getDBMigrateOptions(args);
}
// process command line options
for(i = 0; i < args.length; i++ ) {
if( !args[i].startsWith("-") ) break;
if( args[i].substring(1).equalsIgnoreCase("dp") ) {
if(++i>=args.length||args[i].startsWith("-")) break;
config.setDataPackage(args[i]);
context.getOptionsVerbose().append("dataPackageName=").append(config.getDataPackage()).append(';');
}
else
if( args[i].substring(1).equalsIgnoreCase("odir") ) {
if(++i>=args.length||args[i].startsWith("-")) break;
config.setOutputDir(args[i]);
}
else
if( args[i].substring(1).equalsIgnoreCase("config") ) {
if(++i>=args.length||args[i].startsWith("-")) break;
config.setConfigFile(args[i]);
}
else
if( args[i].substring(1).equalsIgnoreCase("pp") ) {
if(++i>=args.length||args[i].startsWith("-")) break;
config.setProgramPackage(args[i]);
context.getOptionsVerbose().append("programPackageName=").append(config.getDataPackage()).append(';');
}
else
if( args[i].substring(1).equalsIgnoreCase("dp0") ) {
config.setLongDataPackageName(false);
context.getOptionsVerbose().append("longDataPackageName=").append("false").append(';');
}
else
if( args[i].substring(1).equalsIgnoreCase("dp1") ) {
config.setLongDataPackageName(true);
context.getOptionsVerbose().append("longDataPackageName=").append("true").append(';');
}
else
if( args[i].substring(1).equalsIgnoreCase("fixed") ) {
preprocessor.setSourceFormat( Preprocessor.FORMAT_FIXED );
context.getOptionsVerbose().append("sourceFormat=").append("fixed").append(';');
}
else
if( args[i].substring(1).equalsIgnoreCase("free") ) {
preprocessor.setSourceFormat( Preprocessor.FORMAT_VARIABLE );
config.setFixedFormat(false);
context.getOptionsVerbose().append("sourceFormat=").append("free").append(';');
}
else
if( args[i].substring(1).equalsIgnoreCase("e") ) {
context.setProcessPreprocessOnly(true);
}
else
if( args[i].substring(1).equalsIgnoreCase("p") ) {
context.setProcessParseOnly(true);
}
else
if( args[i].substring(1).equalsIgnoreCase("v") ) {
context.setProcessVerbose(true);
}
else
if( args[i].substring(1).equalsIgnoreCase("c0") ) {
config.setPrintCobolStatementsAsComments(false);
config.setRetainCobolComments(false);
context.getOptionsVerbose().append("cobolComments=").append("false").append(';');
context.getOptionsVerbose().append("cobolStatementsASComments=").append("false").append(';');
}
else
if( args[i].substring(1).equalsIgnoreCase("c1") ) {
config.setPrintCobolStatementsAsComments(true);
config.setRetainCobolComments(false);
context.getOptionsVerbose().append("cobolComments=").append("false").append(';');
context.getOptionsVerbose().append("cobolStatementsASComments=").append("true").append(';');
}
else
if( args[i].substring(1).equalsIgnoreCase("c2") ) {
config.setPrintCobolStatementsAsComments(false);
config.setRetainCobolComments(true);
context.getOptionsVerbose().append("cobolComments=").append("true").append(';');
context.getOptionsVerbose().append("cobolStatementsASComments=").append("false").append(';');
}
else
if( args[i].substring(1).equalsIgnoreCase("c3") ) {
config.setPrintCobolStatementsAsComments(true);
config.setRetainCobolComments(true);
context.getOptionsVerbose().append("cobolComments=").append("true").append(';');
context.getOptionsVerbose().append("cobolStatementsASComments=").append("true").append(';');
}
else
if( args[i].substring(1).equalsIgnoreCase("overwrite") ) {
config.setOverwriteJavaFiles(true);
context.getOptionsVerbose().append("overwrite=").append("true").append(';');
}
else
if( args[i].substring(1).equalsIgnoreCase("nooverwrite") ) {
config.setOverwriteJavaFiles(false);
context.getOptionsVerbose().append("overwrite=").append("false").append(';');
}
else
if( args[i].substring(1).equalsIgnoreCase("dolistdir") ) {
doListDir=true;
}
else
if( args[i].substring(1).equalsIgnoreCase("opt0")||
args[i].substring(1).equalsIgnoreCase("nojavatype") ) {
config.setOptimizeAlgorithm(0);
context.getOptionsVerbose().append("javaType=").append("none").append(';');
}
else
if( args[i].substring(1).equalsIgnoreCase("opt1") ||
args[i].substring(1).equalsIgnoreCase("javatype") ) {
config.setOptimizeAlgorithm(1);
context.getOptionsVerbose().append("javaType=").append("more").append(';');
}
else
if( args[i].substring(1).equalsIgnoreCase("opt2")||
args[i].substring(1).equalsIgnoreCase("javatype2") ) {
config.setOptimizeAlgorithm(2);
context.getOptionsVerbose().append("javaType=").append("most").append(';');
}
else
if( args[i].substring(1).equalsIgnoreCase("allsym") ) {
config.setAllSymbols(true);
context.getOptionsVerbose().append("allSymbols=").append("true").append(';');
}
else
if( args[i].substring(1).equalsIgnoreCase("inline") ) {
if(++i>=args.length||args[i].startsWith("-")) break;
config.setInlineStatement(Integer.parseInt(args[i]));
}
else
if(args[i].substring(1).equalsIgnoreCase("gendb")) {
config.setToGenVSAMISAMDb(true);
} else
if(args[i].substring(1).equalsIgnoreCase("nogendb")) {
config.setToGenVSAMISAMDb(false);
} else
if(args[i].substring(1).equalsIgnoreCase("trace")){
if(++i>=args.length||args[i].startsWith("-")) {
System.out.println("-trace option must specify PARSER or TRANSLATION OR ALL");
break;
}
if(args[i].equalsIgnoreCase("PARSER"))
context.setTraceLevel(1);
else
if(args[i].equalsIgnoreCase("TRANSLATION"))
context.setTraceLevel(2);
else
context.setTraceLevel(3);
context.setTraceOn(true);
}
else
displayProgramUsageAndExit();
}
if(i>=args.length)
Main.displayProgramUsageAndExit();
return i;
}
//private String migrateVSAMISAMFileName=null;
//private String migrateDataFileName=null;
private int getDBMigrateOptions(String[] args) {
int i=1;
// process command line options
for(i = 0; i < args.length; i++ ) {
if( !args[i].startsWith("-") ) break;
if( args[i].substring(1).equalsIgnoreCase("dp") ) {
if(++i>=args.length||args[i].startsWith("-")) break;
config.setDataPackage(args[i]);
}
else
if( args[i].substring(1).equalsIgnoreCase("file") ) {
if(++i>=args.length||args[i].startsWith("-")) break;
//migrateVSAMISAMFileName=args[i];
}
else
if( args[i].substring(1).equalsIgnoreCase("data") ) {
if(++i>=args.length||args[i].startsWith("-")) break;
//migrateDataFileName=args[i];
}
}
return i;
}
public static RESContext getContext() {
return context;
}
private static void displayProgramUsageAndExit() {
System.out.println("\nUsage: java -jar RES.jar [options] <Source-File-Name>");
System.out.println("\nOptions for COBOL translation may one or more of the following.\n");
System.out.println("\t-odir output-direcory: specifies directory where target packages reside.");
System.out.println("\t-dp package-name: spcifies the package for data classes.");
System.out.println("\t-pp Package-Name: spcifies the package for program classes.");
System.out.println("\n\t-dp0 \t\t: creates packages like \"coboldataclasses\".");
System.out.println("\t-dp1 \t\t: creates packages like \"coboldataclasses.programname\".");
System.out.println("\n\t-fixed \t\t: fixed source format");
System.out.println("\t-free \t\t: free/variable source format");
System.out.println("\n\t-e \t\t: run only preprocesser.");
System.out.println("\t-p \t\t: run only preprocesser and parser");
System.out.println("\n\t-c0 \t\t: target Java files contain no Cobol comments.");
System.out.println("\t-c1 \t\t: print Cobol statements in comments.");
System.out.println("\t-c2 \t\t: retain Cobol comments.");
System.out.println("\t-c3 \t\t: retain Cobol comments and statements in comments.");
System.out.println("\n\t-nojavatype \t ");
System.out.println("\t-opt0 \t\t: force use of CobolBytes stream.");
System.out.println("\n\t-javatype " );
System.out.println("\t-opt1 \t\t: use native Java types whereever possible.");
System.out.println("\n\t-javatype2 " );
System.out.println("\t-opt2 \t\t: use native Java types for all. enables cache.");
System.out.println("\n\t-allsym\t\t: translate all symbols including not REFeferenced or MODified.");
System.out.println("\n\t-inline stmt-count: Inline all paras & sections with no. of statements < count.");
System.out.println("\n\t-gendb\t\t: generate db migration classes for ISAM/VSAM files.");
System.out.println("\t-nogendb\t: suppress generation of db migration classes.");
System.out.println("\n\t-v \t\t: verbose output.");
System.out.println("\nThese options override the defaults in Config.properties file.");
System.out.println("\nOR\n\nUsage: java -jar RES.jar -dbmigrate [options]");
System.out.println("\nOptions for VSAM/ISAM Migration may be one or more of the following.\n");
System.out.println("\t-dp Package-Name: spcifies the package for program classes.");
System.out.println("\t Default package is coboldataclasses.db_gen from translation.");
System.out.println("\n\t-file File-name: specifies the COBOL FD name for VSAM/ISAM file.");
System.out.println("\t Default is all files generated from translation.");
System.out.println("\n\t-data File-name: specifies the sequential file with VSAM/ISAM data.");
System.out.println("\t Default is ASSIGN name of generated file.");
System.out.println("\n\t-readback\t: read back from DB and compare for validation.");
System.out.println("\nThese options use DB configuraion in RunConfig.properties file.");
System.exit(1);
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.