简体   繁体   English

使用Java程序生成源代码(COBOL)

[英]source code(COBOL) generation using a java program

I am going to write a code generator to generate a COBOL program using some input file of records. 我将编写一个代码生成器,以使用记录的某些输入文件来生成COBOL程序。 I am going to implement it as java program. 我将其实现为Java程序。 I think XML/XSL approach would not be appropriate in this case, because the input file is not in XML format. 我认为XML / XSL方法在这种情况下不合适,因为输入文件不是XML格式。

I think a template processor would be helpful, because some parts of it can be generated using the existing source template. 我认为模板处理器会有所帮助,因为它的某些部分可以使用现有的源模板生成。 Which tool should be useful for this. 哪个工具应该对此有用。 What about Apache Velocity ? 那Apache Velocity呢? would that helpful in this regard? 在这方面有帮助吗?

I've used Velocity for source code generation in the past. 过去,我曾使用Velocity生成源代码。 It worked fairly well, but I ended up writing a fair bit of the generator in Java anyway. 它工作得很好,但是我还是还是用Java编写了相当一部分的生成器。

Velocity is good when there is a straight-forward from your data structures to the target code. 当从数据结构到目标代码直接时,速度很好。 The trick is to get the in-memory data structures into the right form before you start generating. 诀窍是在开始生成之前将内存中的数据结构转换为正确的格式。 This may involve making preliminary passes over the data to reorganize things. 这可能涉及对数据进行初步传递以重组事物。

(And I'm not going to criticize COBOL as a target language. There's clearly a pragmatic reason for using it. Nuff said.) (而且我不会批评COBOL作为目标语言。显然有一个务实的理由使用它。Nuff说。)

Other alternatives to Velocity include Freemarker and JET (used in the Eclipse / EMF world). Velocity的其他替代方案包括Freemarker和JET(在Eclipse / EMF世界中使用)。

We used a headless Eclipse to do some of the transformation between the source code and the generated Cobol code. 我们使用无头的Eclipse在源代码和生成的Cobol代码之间进行了一些转换。 We defined the transformation rules in several XML files, and Eclipse processed them and the source code 我们在几个XML文件中定义了转换规则,然后Eclipse处理了它们以及源代码。

It depends somewhat on your source input, but in our transformation process, the Data Division was a lot more difficult than the Procedure Division. 这在某种程度上取决于您的源输入,但是在我们的转换过程中,数据部比过程部要困难得多。 We pretty much had to code each Data Division transformation as a separate Java method. 我们几乎不得不将每个数据分区转换编码为单独的Java方法。 We were able to use a factory model for the Procedure Division. 我们能够为过程司使用工厂模型。 The factory had 8 concrete class implementations, with one used in the majority of the transformations. 该工厂有8种具体的类实现,其中大部分用于转换。

Edited to add examples. 编辑添加示例。

Here's something we insert into Working Storage: 我们在工作存储中插入以下内容:

01 PROGRAM-COMPILE-INFO.                                                 
    05  PGMNAME-COMPILED    PIC X(08)   VALUE 'J1PP2D0'.             
    05  PGMDATE-COMPILED    PIC X(10)   VALUE '2009-08-11'.          
    05  PGMTIME-COMPILED    PIC X(08)   VALUE '08:46:47'.             

Here's a simple Data Division transformation: 这是一个简单的数据分区转换:

$$COPY J1PP2D1

converts to 转换为

 COPY J1PP2D1.

Here's a Procedure Division transformation: 这是过程分部的转换:

SQL-OTHER-ERROR IASN CLOSE

converts to 转换为

IF SQL-DEADLOCK                                              
    MOVE '0329' TO ERROR-STATUS OF SUBSCHEMA-CTRL            
ELSE                                                         
    MOVE '0399' TO ERROR-STATUS OF SUBSCHEMA-CTRL            
END-IF                                                       
MOVE 'IASN'     TO ERROR-RECORD OF SUBSCHEMA-CTRL            
MOVE '000600,CLOSE  ,0056-PROCESS'                           
                TO XI-EHK-STMT-CONTEXT                       
PERFORM XI-SQL-ERROR                                         
GO TO IDMS-STATUS  

The 000600 is an error code number automatically calculated by the precompiler. 000600是由预编译器自动计算的错误代码号。 The 0056-PROCESS is the paragraph name of the paragraph containing the SQL-OTHER-ERROR precompiler statement. 0056-PROCESS是包含SQL-OTHER-ERROR预编译器语句的段落的段落名称。

The Java code to do the Data Division examples is pretty simple. 进行数据划分示例的Java代码非常简单。

The Java code to do the Procedure Division example is a factory method written for the SQL-OTHER-ERROR verb. 执行“过程划分”示例的Java代码是为SQL-OTHER-ERROR动词编写的工厂方法。

Here's one of our XML transformation scripts. 这是我们的XML转换脚本之一。 We have several. 我们有几个。

<?xml version="1.0" encoding="UTF-8"?>
<script>

  <transformation name="DB2Pre">
     <param name="cobol.in" kind="in" type="text-files"/>
     <param name="cobol.out" kind="inout" type="text-files"/>
     <param name="mapsusage-xml-files" kind="inout" type="xml-files"/>

     <call-transformation name="DB2PreInit"/>

     <call-transformation name="DB2PreImpl">
       <with-param name="cobol-src-files" value="$cobol.in"/>
       <with-param name="cobol-out-files" value="$cobol.out"/>
       <with-param name="mapsusage-xml-files" value="$mapsusage-xml-files"/>
       <with-param name="NEED_MAP_MACRO_FLAG" value="$YES"/>
     </call-transformation>

     <call-transformation name="Map2Cobol"/>

     <call-transformation name="GenStrutsConfig">
       <with-param name="cobol-src-files" value="$cobol.in"/>
       <with-param name="mapsusage-xml-files" value="$mapsusage-xml-files"/>
     </call-transformation>

  </transformation>

</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM