Generating dynamic output with WebMacro

by Matthew Phillips

I've been thinking about writing an article on WebMacro for a few months now, but I wanted to do something different from the typical web application. I've always hated writing JavaBeans, so when I read Paul Wheaton's request for a bean generator I decided to prototype one with WebMacro. The article is about the prototype, but I've been working on the generator since then.

WebMacro is a templating engine. Simply put, you create a template that combines static output with accessors to java objects. In your Java code you can combine the objects and template to create your output. It's almost too easy. The first thing I need for a JavaBean generator is a template. Since different companies use different style guides, having the template externally defined is a big help.

The template

Figure 1 is the template I decided on for my coding style. The first thing you might notice are the way variables are defined. Anytime WebMacro sees a $ followed by a word, it treats it as a variable. It checks the context, which maps variables to values, for the variable and replaces it in the template with the value from the context. Dot notation can be used to access properties of Java objects, so property.Name is the same as if your java code were calling property.getName().

Figure 1 also shows how loops can be used to access each member of an array. I could have used curly braces instead of #begin and #end to show the beginning and ending of the loop. Since I am outputting Java code, I chose to use the latter so that it would not be confused with the braces you expect to see in the output. Since curly braces can be used for loops, I had to escape them where I wanted the template to write them out.

The bean generation code

Figure 2 shows the beginnings of my code generator. For simplicity, I hard coded the objects that I would use in the template, but these could have come from an external file or database.

WM is the class that manages just about everything WebMacro does in stand alone mode. If you were working on a web application, you could extend WMServlet which would do most of the work for you. After instantiating my manager(webmacro), I can create the context that I will use to hold the information needed to populate my template. The key for each item I place in the context matches a template variable name in Figure 1.

After I finish populating my context, I use my manager to instantiate my template. WebMacro will search the classpath for the template. FastWriter is an output class packaged with WebMacro that is used to write the template to output. In this case, I am writing out to a file called Account.java. I then pass the writer and the context to the template to write it out. If you have the template and WebMacro in your classpath, then you should end up with a file called Appointment.java that you can compile and use as a JavaBean.

The primary use for WebMacro is in web applications. To download WebMacro or to find more information, check out WebMacro.org.