This POC is based on the Java generator described by
Giuseppe Naccarato
in his articles "Writing a Code Generator in Java" and "Implement an IOM-Based Code Generator ", published on
Java Pro.
Purpose of this POC is to introduce a simple Java generator
that translates an XML model into Java classes, that
can then be edited by hand and later reused across subsequent
generator runs. The generator preserves the contents
of editable sections and the previous version of
each Java class derived from the XML model.
It is thus possible to change the input model, the templates, and the
editable sections independently of one another.
For a discussion on editable section, refer to thread
"Preserving hand-edits across generation cycles: editable sections" on
cgn-talk (you will have
to register to access the thread).
The input model for the generator is provided by the same XML file
used as input model in the aforementioned articles, augmented with
an additional method getDiscount for class Order.
The architecture of this POC generator is shown below.
Figure 2 - XML to Java generator architecture
All output files are generated from the input model file listed in the
following table.
Table 31 - Input Design Files
File name
|
Source code
|
model.xml
|
View
|
Table 32 - Java Generator: Generated Files
File name
|
Source code
|
Order.java
|
View
|
Customer.java
|
View
|
Table 33 - Java Generator: Sample Generator Internal Code
Description
|
File name
|
Source code
|
Class generation template
|
xml_jgen/gen_class
|
View
|
Property declaration template
|
xml_jgen/declare
|
View
|
Property implementation template
|
xml_jgen/implement
|
View
|
Mapping of XML types to Java types
|
xml_jgen/java_type
|
View
|
XML model parser
|
xml_jgen/xml_parse/tag_node.in
|
View
|
Main generator script
|
main.tefigel
|
View
|
[Previous chapter]
[Next chapter]
[Back to top]
|