XML technologies / XSLT / Java and XSLT / Further techniques / XSLT as a code generator

(Excerpt from “Java und XSLT” by Eric M. Burke)

For performance, EJB components generally return dependent objects rather than individual variables. These read-only classes hold related variables. This example comes from Enterprise JavaBeans by Richard Monson-Haefel (O’Reilly).

Example: Address.java

public class Address implements java.io.Serializable {
  
  private String street;
  private String city;
  private String state;
  private String zip;
  
  /**
   * Erzeuge eine Instanz eines abhängigen Objekts.
   */
  public Address(String street, String city, String state, String zip) {
    this.street = street;
    this.city = city;
    this.state = state;
    this.zip = zip;
  }
  
  public String getStreet( ) {
    return this.street;
  }
  
  public String getCity( ) {
    return this.city;
  }
  
  public String getState( ) {
    return this.state;
  }
  
  public String getZip( ) {
    return this.zip;
  }
} 

Instead of many methods, an Entity bean can supply one method retrieving an instance of Address. This reduces network and database access and simplifies code. Class Address is simple: a constructor initializes every instance variable, and a set of get methods provides access.

Unlike Address, other dependent objects can have many variables. Typing them is laborious, more a typing exercise than creative programming. XSLT can act as a simple code generator to do this work. AddressDO.xml in the next example provides its input data.

Example: AddressDO.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<dependentObject class="Address">
    <property name="street" type="String" getter="getStreet"/>
    <property name="city" type="String" getter="getCity"/>
    <property name="state" type="String" getter="getState"/>
    <property name="zip" type="String" getter="getZip"/>
</dependentObject>

XML is much shorter than the generated code, increasingly so for larger dependent objects. Element <dependentObject> contains a list of <property> elements defining variable names, types and get-method names. The well-defined XML can be validated using a DTD or schema. An ambitious programmer could build a graphical editor for element <dependentObject>.

An XSLT stylesheet generates the code, using output method text. Pay special attention to whitespace. For HTML or XHTML output, whitespace is unimportant: browsers collapse spaces and line breaks, so stylesheet layout is flexible. A code generator’s formatting matters, making its stylesheet harder to read, XSLT’s main drawback here. This example generates dependent-object code.

Example: dependentObject.xslt

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:variable name="className" select="/dependentObject/@class"/>
    <!--
        ********************************************************************
        ** Erzeuge den Rumpf der Klasse. Die anderen Templates erzeugen
        ** jeweils Teile der Klasse.
        ***************************************************************** -->
    <xsl:template match="/dependentObject">public class <xsl:value-of select="$className"/>
        <xsl:text> implements java.io.Serializable {
  </xsl:text>
        <xsl:apply-templates select="property" mode="generateField"/>
        <xsl:text>
        /** 
         *Erzeuge eine neue Instanz eines abhängigen Objektes.
         */
        public</xsl:text>
        <xsl:value-of select="$className"/>(<xsl:apply-templates select="property" mode="generateConstructorParam"/>
        <xsl:text>) {</xsl:text>
        <xsl:apply-templates select="property" mode="generateInitializers"/>
        }
        <xsl:apply-templates select="property" mode="generateGetter"/>
        }
    </xsl:template>
    <!--
        *****************************************************************
        ** Erzeuge die Deklaration der privaten Instanzvariablen.
        ************************************************************** -->
    <xsl:template match="property" mode="generateField">private <xsl:value-of select="@type"/>
        <xsl:text> </xsl:text>
        <xsl:value-of select="@name"/>;</xsl:template>
    <!--
        *****************************************************************
        ** Erzeuge eine "get"-Methode für jede Eigenschaft.
        ************************************************************** -->
    <xsl:template match="property" mode="generateGetter">public <xsl:value-of select="@type"/>
        <xsl:text> </xsl:text>
        <xsl:value-of select="@getter"/>( ) {return this.<xsl:value-of select="@name"/>;}
    </xsl:template>
    <!--
        *****************************************************************
        ** Erzeuge einen Konstruktor-Parameter.
        ************************************************************** -->
    <xsl:template match="property" mode="generateConstructorParam">
        <xsl:text xml:space="preserve"/>
        <xsl:value-of select="@type"/>
        <xsl:text> </xsl:text>
        <xsl:value-of select="@name"/>
        <xsl:if test="position() != last( )">, </xsl:if>
    </xsl:template>
    <!--
        *****************************************************************
        ** Erzeuge den Code für die Initialisierung im Konstruktor.
        ************************************************************** -->
    <xsl:template match="property" mode="generateInitializers">
        <xsl:text xml:space="preserve">    this.</xsl:text>
        <xsl:value-of select="@name"/>
        <xsl:text> = </xsl:text>
        <xsl:value-of select="@name"/>;
    </xsl:template>
</xsl:stylesheet>

This stylesheet generates code for Address.java. First it sets output method text and creates a class-name variable, avoiding repeated use of <xsl:value-of select="/dependentObject/@class"/> whenever the name is needed.

Code-generating stylesheets often use element <xsl:text> for better whitespace control. Here it creates line breaks at several points, for example:

    <xsl:text> implements java.io.Serializable {
</xsl:text>

The closing tag is on the following line, so the newline reaches the output. Element <xsl:text> also creates individual spaces:

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

XSLT processors ignore whitespace between instructions unless other characters occur. Text private immediately before <xsl:value-of select="@type"/> contains characters followed by a space, so the space after private is preserved. But the space between the two <xsl:value-of> elements disappears unless explicitly preserved using <xsl:text> </xsl:text>.

Correct output formatting is challenging but manageable, usually needing some stylesheet tuning. A formatter is an alternative. Products such as JIndent can clean up Java by breaking long lines and positioning spaces and braces. With such a tool, ignore stylesheet formatting and run JIndent after transformation.

   

 

 

 

back to top