Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Subscription to Java Pro

email article
printer friendly
get the code
more resources

Implement an IOM-Based Code Generator
See how to implement the importer and exporter interfaces of a code generator in this final of two installments
by Giuseppe Naccarato

Posted March 10, 2004

Editor's Note: This is the second of two installments on using Java to write a code generator. This first installment, "Writing a Code Generator in Java" (Java Pro Online, March 3, 2004), defines a code generator, lists its benefits, and begins providing guidelines for designing and implementing a generic code generator. Here the discussion concludes with using importer and exporter interfaces, making manual implementations, and using templates.

Now that we've looked at the Internal Object Model (IOM) for our code generator application (see the first installment of this discussion, "Writing a Code Generator in Java," Java Pro Online, March 3, 2004), let's turn to the importer. The importer has the duty to read the model as input and creates the IOM. The code generator can have different importers for different data sources. In accordance with the MDA paradigm, the importer reads a model—usually exported in XMI—representing UML and created by UML tools, such as Rational Rose or Gentleware Poseidon. Our architecture is more flexible and allows for different kinds of input—for example, proprietary models or even source code.

The importer concept is translated in Java by the Importer interface:

public interface Importer {
  public void start() throws 
    Exception;
  public IOMClass createClass(
    Object data) throws Exception;
  public IOMAttribute 
    createAttribute(Object data) 
    throws Exception;
  public IOMOperation 
    createOperation(Object data) 
    throws Exception;
  public IOMParameter 
    createParameter(Object data) 
    throws Exception;
  public IOMAssociation 
    createAssociation(Object data) 
    throws Exception;
  public IOMRole createRole(
    Object data) throws Exception;
}

The interface contains the start() method that starts the importing process. By implementing the createXxx() methods you can create the IOM structure starting with the data as input. 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.

ADVERTISEMENT

Listing 1 shows a partial implementation of the Importer interface. It is called XMLImporter and works on a simple XML representation of UML that can be seen as a simplified XMI. See Listing 2 for an example of an XML file that is an XML representation of the class diagram shown in Figure 1. The XML structure is straightforward—it recalls UML concepts such as classes, attributes, operations, parameters, associations, and roles.

The XMLImporter class uses a SAX parser. For each relevant element found in the XML as input, it calls the proper method of the Importer interface. For example the element <class> causes the invocation of the createClass() method. That method creates a new IOMClass object, fills the name and stereotype attributes with the value coming from the XML, and pushes the object into a stack for future uses. The other classXxx() methods can be implemented easily in a similar way.

The exporter translates the IOM into a specific output. In Listing 3 you can see the Exporter interface. The architecture is closely bound to the SAX parser. The start() method begins to navigate through the object model and calls the startClass() method each time a IOMClass object is processed, and in general the startXxx() method each time an instance of IOMXxx is processed. The exporter then invokes the endXxx() methods when an instance of IOMXxx is finished to be processed.




Back to top













Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home