XSLT as a Code Generator

For performance reasons, EJB components typically return dependent objects rather than many individual fields. These are implemented as read-only classes that encapsulate a group of related fields. Borrowing an example from Enterprise JavaBeans by Richard Monson-Haefel (O’Reilly), Example 8-13 shows a typical dependent object.

Example 8-13. Address.java

public class Address implements java.io.Serializable {

    private String street;
    private String city;
    private String state;
    private String zip;

    /**
     * Construct a new dependent object instance.
     */
    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;
    }
}

Now, rather than containing numerous fine-grained methods, an entity bean can provide a single method to retrieve an instance of Address. This reduces load on the network and database and makes the code somewhat easier to understand. As you can see, the Address class is very straightforward. It has a constructor that initializes all fields and a series of get methods.

Although Address is small, some dependent objects may have dozens of fields. These are tedious to write at best, resulting in a typing exercise rather than programming creativity. XSLT can help by acting as a simple ...

Get Java and XSLT now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.