Tuesday, April 15, 2008

Code Generator Part V: Importer Layer

As was mentioned in the previous post that a typical code generator includes an IOM, Importer and an Exporter, this post will try to draw a picture of the design model of the Importer part of a code generator. This layer is responsible for importing the object model that has to be generated in code to the memory and draw a IOM model in the memory. The input can be XML, database schema or any other customized format provided the importer has to be designed in order to understand and interpret the input format and convert it to the IOM. There is one more layer included in the anatomy of the code generator - the Exporter. I will try explain it in my next post.

The Importer Layer

The importer has the responsibility of reading the model as input and creating the IOM. The code generator can have different importers for different input data sources (XML, CSV, Database schema etc.).In accordance with MDA paradigm, the importer for code generation process reads an input model — representing UML and created by UML tools, such as Rational Rose. Currently we are creating the XML model manually. Presently the importer has been designed using a generic interface (Importer) as the parent. Any class that imports a model should have the Importer interface as its parent. In our case we have an importer class (XMLOOImporter) that imports XML model containing the specification for the classes whose source files has to be generated. The class named XMLOOImporter implements another interface called OOSpecImporter which already implements the Importer interface (Fig 6). The reason behind introducing the OOSpecImporter interface is to encapsulate the activities involved in importing an object-oriented model in a single interface.This way the Importer interface can be further implemented by other importers for importing models defined in different formats other than XML (e.g. XMI, CSV, XML schema, Database schema etc.). Below are the descriptions for the classes that belong to the importer layer. Please refer to Fig 6 for the classes involved in the Importer layer.




Fig 6


An Importer (as shown in fig 6) is designed as a generic interface. The interface contains the start() method that starts the importing process. The main entry point of the code generator i.e. the Generator class invokes the importer using the start() method of this interface. To use the code generator for generating source codes in a specific object-oriented language (e.g. Java, C++, C# etc.) new importer classes can be introduced as the children to the same Importer interface. New importer classes can be written for different formats (e.g. XML, Database schema, DTD, XML schema, XMI etc.) of the input model to the code generator as well.



OOSpecImporter is a child interface of Importer interface. The purpose of this interface is to encapsulate the operations required to import object-oriented class specifications. Currently the XMLOOImporter class implements this interface to import object-oriented class specifications defined in the XML format. In future new importers can be introduced as children to this interface for importing different input models other than XML, e.g. database schema, XML schema, CSV format etc. Each of the createXxx() (i.e. createClass(), createAttribute() etc.) methods in this interface takes “data” of type java.lang.Object as the input. By implementing the createXxx() methods the IOM structure can be created starting with the “data” as input. The createXxx() methods returns the corresponding types i.e. createClass() returns IOMClass, createAttribute() returns IOMAttribute and so on. The implementation of the Importer interface depends on the model as input, so different models can have very different implementations. Nevertheless, the common target is creating the IOM structure.



XMLOOImporter class is inherited from the OOSpecImporter interface and org.sax.helpers.DefaultHandler class (to handle SAX parser events). This class works on a XML representation of UML. The XML structure is straightforward—it recalls UML concepts such as classes, attributes, operations, parameters, associations, and roles. The XMLOOImporter class uses a SAX parser. For each relevant element found in the XML as input, it calls the proper method of the OOSpecImporter interface. For example the element causes the invocation of the createClass() method. The method creates a new IOMClass object, populates the name, stereotype, visibility, documentation, packageName etc. attributes with the value coming from the XML, and pushes the object into a stack for future uses. The other createXxx() methods (createAttribute(), createOperation(), createParameter() etc.) are implemented in a similar way.