I try to write blogs whenever I get spare time

Just to let you know what I am trying to learn and what I am doing now.

Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

The idea of auto generation of boiler-plate codes of Java EE (part 2)

After defining your XML structure and writing the DTD file, now write your actual XML file that will contain your required info about the entity (like its name, attribute list etc). The XML file can be something like this:

After finishing this initial task, then you have to look for a XML API that would help you to parse and manipulate these XML data. There are many choices like SAX, JAX etc.
I will describe the use of a Borland tool that uses SAX (Simple API for XML). I am telling about this tool because I am familiar with it. You can use any other tool that serves this purpose.
The Borland tool that I am telling about, is provided with JBuilder. Now let us see, how can we combine all these to produce our desired generator.
1. Open JBuilder and choose "New" from the File menu.
2. Choose a databinding project from the XML options. And give the desired name and other info about the project.
3. Then when asked for the DTD file, choose the DTD file we've created before and show the root element of the XML.
4. When you finish the project creation, you will see Some classes attached with your project. The Borland XML tool creates a class for each XML tag it finds in the DTD file. The inter-relationship between classes are defined according to the relations defined in the DTD file. The getter, setter methods for each class and their children.
5. Now you are ready to make your run. You can make a java class named Generator in which you will write the code to generate your desired codes using the XML classes. It will use simple File I/O to write the generated classes.
6. All you need now is to call a unmarshal method of the root class. For example, if we call the method like this: entities.unmarshal("data.xml");
We'll get a tree structure from which, we can get the children list by calling like: entities.getentity().getentityList();
7. And then we are free to use this data anywhere in our code generator class.

The idea of auto generation of boiler-plate codes of Java EE (part 1)

The Java EE is a great framework for building large scale and distributed enterprise softwares. A java enterprise software has many boiler-plate codes that need not to be coded by yourself. They can be generated by smart IDE's like Netbeans just with your few mouse-clicks. But, the code Netbeans generates is kind of a mere skeleton code of those classes. If you need customization in all those netbeans generated classes, it is better to write a generator that will generate customized classes according to your requirement. It's a very simple thing to do.

First, let us assume we need to generate the following classes for a single entity:
1. a entity bean,
2. a session bean,
3. a detail file of the entity bean (that will hold the form level abstraction of the entity bean),
4. a converter class that would convert the detail class to the entity class and vice versa.
5. and a form that would represnt the attributes of the entity bean (in the front end).

For all these, at first you need to write a data source in which you would store the name of the entity, it's attribute list, properties of the attributes etc. An XML is a feasible solution here to write these data. So, write an XML file by defining significant tags in it. Then, write the DTD (Data Type Definintion) file for this XML, in which the type of your tags would be defined. If you are not very familiar with these validation stuffs of XML, you can check these DTD tutorial.