<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>Trendics Blog &#187; java</title>
	<atom:link href="http://blog.trendics.com/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.trendics.com</link>
	<description></description>
	<pubDate>Mon, 11 Aug 2008 14:59:25 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
	<language>en</language>
			<item>
		<title>Simple Templating System for Java</title>
		<link>http://blog.trendics.com/development/simple-templating-system-for-java/</link>
		<comments>http://blog.trendics.com/development/simple-templating-system-for-java/#comments</comments>
		<pubDate>Fri, 04 Jul 2008 14:59:51 +0000</pubDate>
		<dc:creator>kent</dc:creator>
		
		<category><![CDATA[development]]></category>

		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://blog.trendics.com/development/simple-templating-system-for-java/</guid>
		<description><![CDATA[A templating system allows generating the text for HTML, emails, SQL, etc. without hardcoding the text in Java. This greatly improves the managability of your Java code and makes it much easier to make changes. While the templating system presented below is simple to implement, it still supports variable substitutions, condition logic, loops, etc. This [...]]]></description>
			<content:encoded><![CDATA[<p>A templating system allows generating the text for HTML, emails, SQL, etc. without hardcoding the text in Java. This greatly improves the managability of your Java code and makes it much easier to make changes. While the templating system presented below is simple to implement, it still supports variable substitutions, condition logic, loops, etc. This templating system requires <strong><a href="http://commons.apache.org/jelly/">Jelly</a></strong> and is implemented in the provided <strong><a href="http://blog.trendics.com/wp-content/uploads/2008/07/jellyscript.java" title="JellyScript Wrapper Class">JellyScript Wrapper Class</a></strong>.</p>
<p>Let&#8217;s say you have a welcome email that should look something like this&#8230;</p>
<blockquote>
<pre>Hello, Kent, thanks for creating a Trendics account at
www.trendics.com.  Your new user name and password is...

   User: kent13600
   Password: my.secret

Be sure to checkout the instant website speed
check at http://tools.trendics.com/sc.

- Trendics Support Team</pre>
</blockquote>
<p>Instead of hardcoding this text within a Java class, you can use the provided <strong><a href="http://blog.trendics.com/wp-content/uploads/2008/07/jellyscript.java" title="JellyScript Wrapper Class">JellyScript Wrapper Class</a></strong> to define a template that looks like this&#8230;</p>
<blockquote>
<pre>Hello&lt;jelly:if test='${!empty FirstName}'&gt;,
${FirstName}&lt;/jelly:if&gt;, thanks for creating a
Trendics account at www.trendics.com.  Your new
user name and password is...

   User: ${UserName}
   Password: ${Password}

Be sure to checkout the instant website speed
check at http://tools.trendics.com/sc.

- Trendics Support Team</pre>
</blockquote>
<p>The <em>LastName</em> and <em>Password</em> variables are simply substituted at the appropriate locations.  The &lt;jelly:if test=&#8217;${!empty FirstName}&#8217;&gt;, ${FirstName}&lt;/jelly:if&gt; logic is only adding a comma and the <em>FirstName</em> if the <em>FirstName</em> is not blank.</p>
<p>As another example, you can use this templating system to generate SQL&#8230;</p>
<blockquote>
<pre>SELECT *
FROM Employee
WHERE State='${State}'
    &lt;jelly:if test='${!empty City}'&gt;
        AND City='${City}'
    &lt;/jelly:if&gt;</pre>
</blockquote>
<p>In the SQL template above, a <em>State</em> filter parameter is required and a <em>City</em> filter parameter may optionally be included.</p>
<p>The examples above did only simple variable substitution and a bit of conditional logic; however, Jelly supports a whole range of tags supporting various types of logic constructs including loops, creating Java objects, calling methods on Java objects, etc. (see <a href="http://commons.apache.org/jelly/tags.html">here</a>).</p>
<p>Here is an example of the Java code necessary to execute a template&#8230;</p>
<blockquote>
<pre> public static void main(String[] args) {
    if (logger.isLoggable(Level.FINE)) logger.fine("main()");

    try {
        // Create a template
        final StringBuffer sb = new StringBuffer();
        sb.append("Hello&lt;jelly:if test='${!empty FirstName}'&gt;");
        sb.append(", ${FirstName}&lt;/jelly:if&gt;, ");
        sb.append("thanks for creating a Trendics account ");
        sb.append("at www.trendics.com.  ");
        sb.append("Your new user name and password is...\n");
        sb.append("\n");

        sb.append("   User: ${UserName}\n");
        sb.append("   Password: ${Password}\n");
        sb.append("\n");
        sb.append("Be sure to checkout the instant website ");
        sb.append("speed check at http://tools.trendics.com/sc.\n");
        sb.append("\n");
        sb.append("- Trendics Support Team\n");

        // Create the JellyScript instance
        final JellyScript jellyScript = new JellyScript(sb.toString());

        // Create the variables needed by the template
        final Map&lt;String, Object&gt; variables = new HashMap&lt;String, Object&gt;();
        variables.put("FirstName", "Kent");
        variables.put("UserName", "kent13600");
        variables.put("Password", "my.secret");

        // Execute the script to merge the variables with the template
        final String result = jellyScript.execute(variables);
        System.out.println(result);
    }
    catch (Exception e) {
        logger.log(Level.SEVERE, "main()", e);
    }
}</pre>
</blockquote>
<p>Note that defining the template within Java code sort of defeats a key benefit of templating &#8212; you would normally define your templates within an XML configuration file or store the templates in their own files.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.trendics.com/development/simple-templating-system-for-java/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
