(C) Copyright Vova Grechka 2004. All
Rights Reserved |
|||
Introduction Why use all those complicated MDAs, XMIs, EMFs, UMLs and other buzz stuff if You only need to generate some code based on Your model defined in simple XML file? I was tired trying to find just right code generation framework for my everyday needs. Some existing frameworks want me to spend hours drawing stupid UML diagrams, some are strictly specialized and don't allow me to define new generation patterns, some introduce new syntaxes and languages, etc, etc, etc... So, I finally did it myself. |
|||
Get it Download lates QuiT version here. Sources are included. You'll need Eclipse 3 M6 or higher to use this plugin (it was developed using M7 version but should run on M6 too). Send comments and wishes to into_box@mail.ru. Visit SourceForge project page. |
|||
How to define a generation pattern Let's create new pattern which will generate JavaBeans. First, there is pattern repository root. You specify it in Window->Preferences->QuiT->Repository. I recommend to add Your repository root into Eclipse workspace as simple project, so You'll be able to edit Your patterns while working on other projects. (1) Create new folder under repository root and name it bean. (2) Create new Java project for which You'll generate beans. I assume source folder is src (of course it could be anything other but in this case You'll have to slightly modify code of this sample). Create folder quit under Java project and place beans.xml file there, this file is XML model of Your beans. (3) Now it's time to develop structure of our XML model and edit beans.xml file according to this structure. Suppose we want to be able to define several packages each containing several beans. Each bean is described by it's name and collection of properties (property has a name and a type). So, our XML could be something like this:
Notice that root element has attribute _quit which is set to "bean". This is only requirement of QuiT framework on Your model files: _quit attribute of root element should specify pattern which will be used for code generation based on the model. On the contrary dir attribute is not required by framework, this attribute will be used later in our pattern to tell generator where to put generated files. (4) Create control script. Control script defines file generation process. It is called control.bsh and has to be placed in pattern folder (i.e. QuitRepository/bean/control.bsh in our case). If You don't know what BeanShell is go to www.beanshell.org and read tutorial. BeanShell is nice Java scripting language in which You'll define Your control scripts and scripts in template files. I have chosen BeanShell cause if You know Java (and You do, don't You?) You don't have to learn anything new, BeanShell's syntax is actually Java with some handy simplifications. Another thing You should know here is small subset of JDOM API (www.jdom.org). JDOM is convenient Java wrapper around DOM and You'll use it to access XML model from scripts. OK, let's look at our control.bsh source:
As You can see QuiT exposes runtime object called rt to the script. This runtime object has some methods to retrieve XML model information and do some other things. I'll go through the script and describe it's functionality.
(5) Create template file. Our project will have only one template file though it can contain any number of templates (appropriate templates to merge are defined in control script). Create file named bean.quit inside bean folder. Here is it's source:
Template is quite simple but I'll comment it anyway.
(6) Finally it's time to make it work all together. Simply choose QuiT->Generate from context menu which appears when right clicking on beans.xml file. That's all. Watch magic appearing of new generated files in Your project: That was really easy, wasn't it? And consider full power of Java which You can use in Your control scripts and templates :-). |