November 25, 2014
Hot Topics:

Code Generation With XSL

  • December 30, 2002
  • By Jeff Ryan
  • Send Email »
  • More Articles »

The root template builds the shell of the Java program we are generating:

  • We begin by creating the package statement for the program. The <xsl:value-of> element is used to "pull" the appropriate data from the meta data document, in this case the value of the Package node, into the static code fragments.
  • Next, templates are applied for all of the Import elements.
  • Then, the class declaration pulls the JavaBean/@name and SuperClass/@name from the metadata document. The constructor for our Java program is built using the same technique.

The template for outputting import statements is very straightforward. This template is invoked once for every Import node.


<xsl:template match="Import">
  import <xsl:value-of select="."/>;
</xsl:template>

Generating Instance Variables, Getters and Setters

The Property meta data is going to be used to generate three different portions of our Java program:

  • The instance variables
  • The accessor, or getter, methods
  • The mutator, or setter, methods

Because we want to process the Property nodes three times, producing different output each time, the mode attribute is used when templates are applied to distinguish which templates get invoked.

We're going to need a utility routine for converting lower case property names to mixed case. For example, the city property will have a corresponding getCity() and setCity() method. So, we'll build a template to do this conversion for us and call it in the appropriate place.


<xsl:template name="initCap">
  <xsl:param name="x"/>
  <xsl:value-of select="translate(substring($x,1,1)
                 ,'abcdefghijklmnopqrstuvwxyz'
                 ,'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
  <xsl:value-of select="substring($x,2)"/>
</xsl:template>

The initCap template takes in a variable x as a parameter. The first character of x is extracted with substring($x,1,1). The translate function is used to convert lower case to upper case for this character. This letter is then output via <xsl:value-of>. Next, the remaining characters of the word are extracted with substring($x,2) and output with <xsl:value-of>.

Now, let's look at the templates matching on Property nodes beginning with the instanceVariable template.


<xsl:template match="Property" mode="instanceVariable">
    private <xsl:value-of select="@type"/><xsl:text>
        </xsl:text><xsl:value-of select="@name"/>;
</xsl:template>

When templates are applied to Property nodes in instanceVariable mode, the instance variables are generated for our Java program in the format:

    private String city;

Now, let's look at the accessor template:


<xsl:template match="Property" mode="accessor">
    public <xsl:value-of select="@type"/>
        get<xsl:call-template name="initCap">
        <xsl:with-param name="x" select="@name"/>
        </xsl:call-template>()
    {
      return <xsl:value-of select="@name"/>;
    }
</xsl:template>

When templates are applied to Property nodes in accessor mode, the getter methods are generated for our Java program in the format:


    public String getCity()
    {
      return city;
    }

Notice that the initCap template is passed the name attribute of the Property element in order to output it in mixed case.

Finally, let's look at the mutator template:


<xsl:template match="Property" mode="mutator">
    public void set<xsl:call-template name="initCap">
        <xsl:with-param name="x" select="@name"/>
      </xsl:call-template>(<xsl:value-of select="@type"/>
          the<xsl:call-template name="initCap">
        <xsl:with-param name="x" select="@name"/>
      </xsl:call-template>)
    {
      <xsl:value-of select="@name"/> = the<xsl:call-template
          name="initCap">
        <xsl:with-param name="x" select="@name"/>
      </xsl:call-template>;
    }
</xsl:template>

When templates are applied to Property nodes in mutator mode, the setter methods are generated as follows:


    public void setCity(String theCity)
    {
      city = theCity;
    }

Once again, the initCap template is used to generate mixed case. The formatting of these templates is not the most readable. However, the goal here is to make the generated program readable.





Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel