<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>HardLikeSoftware &#187; StringTemplate</title>
	<atom:link href="http://hardlikesoftware.com/weblog/category/stringtemplate/feed/" rel="self" type="application/rss+xml" />
	<link>http://hardlikesoftware.com/weblog</link>
	<description>The writings of John Snyders, mostly about software.</description>
	<lastBuildDate>Wed, 14 Sep 2011 04:57:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Script injection and StringTemplate</title>
		<link>http://hardlikesoftware.com/weblog/2008/02/15/script-injection-and-stringtemplate/</link>
		<comments>http://hardlikesoftware.com/weblog/2008/02/15/script-injection-and-stringtemplate/#comments</comments>
		<pubDate>Sat, 16 Feb 2008 03:03:01 +0000</pubDate>
		<dc:creator>John Snyders</dc:creator>
				<category><![CDATA[StringTemplate]]></category>
		<category><![CDATA[Web Apps]]></category>
		<category><![CDATA[ajax]]></category>

		<guid isPermaLink="false">http://hardlikesoftware.com/weblog/2008/02/15/script-injection-and-stringtemplate/</guid>
		<description><![CDATA[I just read a very interesting article called Secure String Interpolation in JS that got me thinking about rendering in StringTemplate. I don&#8217;t follow the caja project but it looks interesting. I found the article linked from John Resig&#8217;s blog.
Script injection in web applications, which also goes by the name XSS (Cross Site Scripting), is [...]]]></description>
			<content:encoded><![CDATA[<p>I just read a very interesting article called <a href="http://google-caja.googlecode.com/svn/changes/mikesamuel/string-interpolation-29-Jan-2008/trunk/src/js/com/google/caja/interp/index.html">Secure String Interpolation in JS</a> that got me thinking about rendering in <a href="http://www.antlr.org/wiki/display/ST/Object+rendering">StringTemplate</a>. I don&#8217;t follow the caja project but it looks interesting. I found the article linked from John Resig&#8217;s <a href="http://ejohn.org/blog/javascript-based-injection-attacks/">blog</a>.</p>
<p>Script injection in web applications, which also goes by the name <a href="http://en.wikipedia.org/wiki/Cross-site_scripting">XSS</a> (Cross Site Scripting), is a serious and common security problem. It arises when the application does not appropriately escape data that comes from an untrusted source. The data could contain malicious  JavaScript that would get executed. If developers are burdened with remembering to escape each and every instance of data they will eventually forget or make a mistake and a security hole will result. One main point of the article is that string interpolation should be automatically secure &mdash; it should do the right escaping for you.</p>
<p>Every now and again the issue of how to escape special characters when generating HTML files comes up on the  StringTemplate mailing list. The answer is very straight forward &#8211; write a renderer. My main point, which I&#8217;ll get to eventually, is about the details of the renderer.</p>
<p>The author of the above article distinguishes string interpolation from &#8220;full blown&#8221; templating languages. JSP, PHP, and XSL were given as examples. Three problems with full blown templating languages were given (folks familiar with StringTemplate know there are more):</p>
<ol>
<li>They do nothing to help solve the escaping problem.</li>
<li>They are verbose and add lots of boiler plate.</li>
<li>They don&#8217;t make simple things simple.</li>
</ol>
<p>I think StringTemplate holds up well against these complaints. The renders can do escaping. The simple templates (.st files) have no boiler plate at all and the group files (.stg) add only a little. Lastly, it doesn&#8217;t get much simpler than &#8220;Hello $name$!&#8221;. </p>
<p>The most important issue and the one the article is really about is number one &mdash; escaping.</p>
<p>On an HTML page what characters need to be escaped depends on the context: element content, attribute value, JavaScript, and URIs. StringTemplate rendering assumes a single rendering context. As of StringTemplate version 3.1 the format option can be used to specify a specific rendering (format). The trouble with this is that the template writer must be aware of the different contexts and what format to use in each. An alternative is to come up with a one size fits all escaping renderer. This will end up escaping more than it needs to. There may be some fringe cases where this could cause problems.</p>
<p>The thing I found most interesting about the approach given in the article is that the context scanner keeps track of the current context in the output so that the escaper can do the right escaping according to the context. To put this in StringTemplate terms the output after rendering would be feed into a parser that tracks changes in context (element to attribute value to JavaScript to element content etc.) and informs the renderer(s) of the current context.</p>
<p>You can still use the format option or other custom renderers to specify how the data is turned  into a string (for example a number could be currency or a percentage). This is something that the template author is expected to know. It seems reasonable to me that it would be better if the author didn&#8217;t need to understand the intricate escaping rules of web pages.</p>
<p>A key innovation of StringTemplate is the identification of the Model-View-Controller-Renderer pattern &mdash; that there is a renderer distinct from the view. (Read about MVCR <a href="http://www.cs.usfca.edu/~parrt/papers/mvc.templates.pdf">here</a>.) Now I am seeing  the render as two distinct pieces. There is the format renderer and the escaping renderer. The former is concerned with if negative 23 should be output as (23) or -23. The latter is concerned with if single quote should be output as &#8216; or &amp;apos; The format renderer runs first and passes its output to the escaping renderer.</p>
<p>The reason it makes sense to think of two different renderers is that they each have different masters. The format renderer is directed by the needs of the application, the locale and perhaps presentation directed formatting with the format option. The escaping renderer&#8217;s function is determined by the language of the output: SQL, HTML, XML or just the text of an email message.</p>
<p>Other thoughts and observations:</p>
<p>Is there a need for a JavaScript implementation of StringTemplate? As it becomes more common to move the model, view and controller to the client there could be a role for it. It could also run server side in Rhino. Although the author of the article states that it is not a goal to create a template language I wonder if the extra capabilities of StringTemplate would be welcome.</p>
<p>The method used in the article for implementing auditable exemptions (cases where you know the data is safe and you don&#8217;t want it escaped) is the same as would be done in StringTemplate. Just wrap the data in a type that returns the raw string when rendered.</p>
<p>In the article a simple FSM was used to parse the HTML. Only an excerpt of the code was shown and I didn&#8217;t bother to download it and look deeper. It seems to me that the output HTML needs to be well formed otherwise the parser would be very complicated if it has to deal with the tag soup that browsers deal with.</p>
<p>This is now at least the fourth time I&#8217;ve seen a parser written in JavaScript so I think there could be a need for a JavaScript backend for ANTLR. I would have liked to create a l-system language parser in JavaScript for a recent <a href="http://hardlikesoftware.com/weblog/2008/01/23/l-systems-in-javascript-using-canvas/">project</a> of mine.  Is there already an ANTLR for JavaScript that I don&#8217;t know about?</p>
<p>Does anyone else think that it would be useful/feasible to have a HTML escaping renderer (that does the correct escaping in automatically) as part of the StringTemplate library?</p>
]]></content:encoded>
			<wfw:commentRss>http://hardlikesoftware.com/weblog/2008/02/15/script-injection-and-stringtemplate/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>L-systems in JavaScript using Canvas</title>
		<link>http://hardlikesoftware.com/weblog/2008/01/23/l-systems-in-javascript-using-canvas/</link>
		<comments>http://hardlikesoftware.com/weblog/2008/01/23/l-systems-in-javascript-using-canvas/#comments</comments>
		<pubDate>Wed, 23 Jan 2008 04:50:50 +0000</pubDate>
		<dc:creator>John Snyders</dc:creator>
				<category><![CDATA[StringTemplate]]></category>
		<category><![CDATA[Web Apps]]></category>
		<category><![CDATA[ajax]]></category>

		<guid isPermaLink="false">http://hardlikesoftware.com/weblog/2008/01/23/l-systems-in-javascript-using-canvas/</guid>
		<description><![CDATA[While playing with Scratch I was thinking about what kinds of things would make good Scratch projects. I have had an interest in L-systems for a long time now but never did more than read about them and look at the pretty pictures.  I thought scratch might motivate me to do something in this [...]]]></description>
			<content:encoded><![CDATA[<p>While <a href="http://hardlikesoftware.com/weblog/2007/09/29/scratch/">playing with Scratch</a> I was thinking about what kinds of things would make good Scratch projects. I have had an interest in <a href="http://en.wikipedia.org/wiki/L-system">L-systems</a> for a long time now but never did more than read about them and look at the pretty pictures. <img src='http://hardlikesoftware.com/weblog/wp-content/uploads/2008/01/tree.png' alt='L-system tree' style="float: right; margin: 4px;" /> I thought scratch might motivate me to do something in this area but I quickly realized with Scratch&#8217;s lack of data structures (besides the 2d drawing surface) and argument passing it would take all the fun out of doing an L-system implementation. Sure, people have come up with ways of drawing <a href="http://scratch.mit.edu/tags/fractal">fractals in Scratch</a> but these are fixed algorithms usually at a specific generation. I have not yet seen an L-system implementation in Scratch.</p>
<p>About a month later I went to the <a href="http://hardlikesoftware.com/weblog/2007/10/27/the-ajax-experience/">ajax experience conference</a> and learned about the new <a href="http://developer.mozilla.org/en/docs/Canvas_tutorial">HTML canvas</a> element among other things. I wanted a project to get practice writing JavaScript, more experience with using jQuery, and to try out jQueryUI and canvas. L-systems seemed like a great project for this purpose.</p>
<p>Before I get to my project here is a real quick definition of what an L-System is. A Lindenmayer system (L-system for short), named after biologist Aristid Lindenmeyer, is a rewriting system. It takes a list of symbols and repeatedly generates a new list of symbols by following a set of rules. Each new list of symbols is called a generation. The symbols can be given a graphical representation to produce an image algorithmically. The beauty of L-systems is that with just a few symbols and rules amazingly complex images can be created.  L-systems were developed to model plants but can also draw fractals. More information is available from the links above and from the <a href="http://hardlikesoftware.com/projects/lsystem/doc/index.html">lsystem.js documentation</a>.</p>
<p>My project, <a href="http://hardlikesoftware.com/projects/lsystem/lsystem.html">L-system.html</a>, is a serverless web application for exploring a number of plant and fractal models created with L-systems. You select a model, change its parameters such as angle or distance depending on the model, set the number of generations then press the play button to see it drawn.</p>
<p>Being able to adjust the parameters of a model helps to understand it. When you see a fractal of some reasonably high number of generations it is not always clear what process gives rise to it. Stepping through from the zeroth to first and second generation of a fractal really helps to visualize the rule that makes the fractal what it is.</p>
<p>L-system.html is not very fancy or even complete. The main purpose for me was learning canvas, jQuery, and jQueryUI. It makes use of two reusable JavaScript libraries that I wrote: </p>
<ul>
<li>lsystem.js – a L-system library that supports parametric or non-parametric, deterministic or non-deterministic, context sensitive or context free, and branching or flat models.</li>
<li>turtle.js – A 2d turtle graphics library on top of the HTML canvas API.</li>
</ul>
<p>Both of these are open source with a BSD license if you care to use them. They can be used together or independently. You can <a href="http://hardlikesoftware.com/projects/lsystem/lsystem-html_v0.1.zip">download</a> the whole project. If you make improvements to them or have suggestions I&#8217;d love to hear about it.</p>
<p>The rest of this post covers what I discovered along the way.</p>
<p>First I have to say I&#8217;m impressed with canvas and its capabilities. Its not as full featured as most mature graphics libraries and there are a few features that are not supported on all browsers but it opens up another area to web applications (without having to resort to Flash) that was previously only found in desktop applications. People are doing <a href="http://developer.mozilla.org/en/docs/Category:Canvas_examples">cool things</a> with it already. Support for IE is accomplished with <a href="http://code.google.com/p/explorercanvas/">explorercanvas</a> from Google. I have tested my project on Firefox 2, IE 7, Opera 9.24 and Safari 3beta (on Windows) and it works well except that the save functionality doesn&#8217;t work in Safari or IE. Also I have noticed IE to be slower than the others but have not done any rigorous timing.</p>
<p>Thanks to <a href="http://canvaspaint.org/">Canvas Paint</a> (a canvas implementation of Microsoft Paint) for the technique for saving canvas images.</p>
<p>L-system.html is an example of a serverless web application. Its an application because it is interactive and does something at least a little interesting or useful. Its a web application because it runs in a web browser. Serverless is a somewhat new category of web application. It basically means that none of the application behavior is running on a server. A web server may be serving the HTML and related resources but there is no servlet, or PHP script, etc. A server may be providing raw data or persisting data (like the Amazon S3 service) but this is just data storage or acquisition and often the servers or services are not even owned by the creator of the application. In some cases the application can just be an HTML file on your local file system. L-system.html can run this way. An even cooler example of the kind of interesting things that can be done without a server is <a href="http://www.tiddlywiki.com/">TiddlyWiki</a> &#8211; a serverless wiki.</p>
<p>After working more with <a href="http://jquery.com/">jQuery</a> I&#8217;m convinced that this is the way I want to write JavaScript and to work with the <abbr title="Document Object Model">DOM</abbr>. However I&#8217;m not anywhere near as impressed with jQueryUI. It just doesn&#8217;t seem ready for prime time. I only wanted a few UI elements: a slider, a dialog, a progress control and a color picker. There is no progress control or color picker in jQueryUI although there are plugins for color pickers and maybe a progress control as well. I thought it would be best to stick to the official UI widgets. I struggled to learn the UI widgets and get them to work the way I wanted. I needed to read the source as well as the documentation to figure them out.</p>
<p>The first problem I ran into was with the slider on on IE 6 and 7. I found a bug report that said the problem was fixed in the latest. After downloading the latest from the SVN source repository the problem was indeed fixed. </p>
<p>I wanted to be able to move the slider with the right and left arrow keys and to show that the slider had focus. I know that jQueryUI doesn&#8217;t have full accessibility support yet, but keyboard support seemed like it should work for a control like slider. I noticed some code to turn on keyboard support when webforms (not sure what that is) is present. This helped me figure out how to get it working the way I wanted. You can tab to or click on the slider to give it focus and then use the arrow keys to move the slider.</p>
<p>One thing that bugs me about some ajax apps is that they break the back button.  I really wanted to get back button history working with jQuery in this project. At the ajax experience conference I saw the history plugin demonstrated and it looked so easy. So far my attempts to get it working have failed. The history plugin seems like it would be easy to use with the tab control but i&#8217;m not using the tab control. Then I read that the new tab control in jQueryUI doesn&#8217;t work with the history plugin. There is a <a href="http://www.stilbuero.de/2007/10/23/jquery-ui-tabs-aka-tabs-3/">mention</a> that the history plugin needs to be rewritten but no time frame for when it will be available. Since I&#8217;m not using tabs it may be possible to get the current history plugin working but I haven&#8217;t figured it out yet.</p>
<p>My next problem was with the dialog control. I found that I could end up with multiple copies of the dialog displayed. I decided to try the latest version from SVN like I did with the slider. I did manage to solve my problem by using the new dialogInit method. (There may have been another way.) The trouble is that at the time I got the latest version of dialog the mouse management was rewritten which broke the slider and resizing of dialogs. As of this writing they still are not fixed. There may be some set of source versions that will solve both my slider and dialog problems but I don&#8217;t have time to find it. So at this point the sliders do not move with the mouse (you get a JavaScript error). I&#8217;ll just have to wait for the jQueryUI mouse implementation to get finished.</p>
<p>I noticed that some of the jQueryUI documentation (at least the examples) are in advance of the current 1.0 release. For example, the dialogInit method was shown in one of the dialog examples even thought it is not in the 1.0 release. </p>
<p>There is no progress control in the jQueryUI so I made my own. The next step will be to learn how to turn it into a plugin.</p>
<p>Along the way I found a number of useful tools and practices for writing JavaScript</p>
<ul>
<li>Firebug is a very good tool. I knew that already but with this project I used the profiler for the first time. The profiler is very easy to use and helped me to improve the performance of the L-system algorithm. I also used the console log, which Firebug lite makes work cross browser.</li>
<li>I&#8217;m using JSLint from an ant task and it has found some problems. I described the technique for ant integration <a href="http://dev2dev.bea.com/blog/jsnyders/archive/2007/11/using_jslint_from_ant.html">here</a>.</li>
<li>I used <a href="http://jsdoctoolkit.org/">jsdoc-toolkit</a> to generate the library documentation. This tool is very easy to use and produces nice results. Since the JavaScript is going to get minified anyway I think it is a good idea to keep the documentation in the source.</li>
<li>For testing the 2 library modules I used <a href="http://www.jsunit.net/">jsunit</a>. Actually I only used the assertion functions from the core. This looks like it could be a good tool but I need to learn more about how to use it effectively.</li>
<li>I use just the CSS library from <a href="http://developer.yahoo.com/yui/">YUI</a>.</li>
<li>To build the final html as well as the aggregated and minified resources I use ant, yui compressor, and <a href="http://www.antlr.org/wiki/display/ST/STST+-+StringTemplate+Standalone+Tool">STST</a>. One thing I noticed is that yui compressor catches some JavaScript problems that JSLint did not.</li>
</ul>
<p>After I was just about done with this project I discovered that someone else created a canvas L-system implementation <a href="http://lsysjs.qwert.ch/">LSys/JS</a>. Although it doesn&#8217;t support as many kinds of L-systems it does allow you to enter your own. Also it does not have a reusable library layer.</p>
<p>If you have read this far without trying it out you should take a look at <a href="http://hardlikesoftware.com/projects/lsystem/lsystem.html">L-system.html</a> now. If you want to add your own L-system models you can download the <a href="http://hardlikesoftware.com/projects/lsystem/lsystem-html_v0.1.zip">project</a> and modify the lsystem.html file. If you come up with a cool model leave a comment with a link to it.</p>
]]></content:encoded>
			<wfw:commentRss>http://hardlikesoftware.com/weblog/2008/01/23/l-systems-in-javascript-using-canvas/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Thoughts on StringTemplate part 2</title>
		<link>http://hardlikesoftware.com/weblog/2007/06/25/thoughts-on-stringtemplate-part-2/</link>
		<comments>http://hardlikesoftware.com/weblog/2007/06/25/thoughts-on-stringtemplate-part-2/#comments</comments>
		<pubDate>Mon, 25 Jun 2007 05:02:47 +0000</pubDate>
		<dc:creator>John Snyders</dc:creator>
				<category><![CDATA[StringTemplate]]></category>

		<guid isPermaLink="false">http://hardlikesoftware.com/weblog/2007/06/25/thoughts-on-stringtemplate-part-2/</guid>
		<description><![CDATA[Part one in this series mainly focused on the data model of the StringTemplate language. In this post I&#8217;ll talk about programming-in-the-large with StringTemplate.

Programming-in-the-large is about how to deal with creating and maintaining large programs. Of particular interest are the facilities a language provides for modularization and how the modules interact. These interactions may touch [...]]]></description>
			<content:encoded><![CDATA[<p>Part <a href="http://hardlikesoftware.com/weblog/2007/06/01/thoughts-on-stringtemplate-part-1/">one</a> in this series mainly focused on the data model of the StringTemplate language. In this post I&#8217;ll talk about programming-in-the-large with StringTemplate.<br />
<span id="more-31"></span></p>
<p>Programming-in-the-large is about how to deal with creating and maintaining large programs. Of particular interest are the facilities a language provides for modularization and how the modules interact. These interactions may touch on issues of information hiding and the abstractions available in the language.</p>
<p>StringTemplate allows you to organize your templates in different files. There are two types of template files: the simple template file and the group file. The simple template file is often just called a template although this can be ambiguous because groups contain templates and templates can even be created programmatically.  I use the term simple template file after the XSLT term <a href="http://www.w3.org/TR/xslt20/#stylesheet-structure">simplified stylesheet module</a> which is a stylesheet that starts with a literal result element.</p>
<p>Simple templates are just files with StringTemplate actions between delimiters ($ $ or < >). There is one template per file. They cannot declare arguments, the name of the template is the name of the file (less the extension) and the extension must be .st. Simple templates can invoke (also known as call, reference or include) other simple templates by using a relative path to the other file. For example:</p>
<pre>
/dir1/template1.st:
  This is the caller. $dir2/template2.st()$
/dir2/template2.st:
  This is the callee.
</pre>
<p>Group files allow you to define several templates in a single file. The group file must have a .stg extension. Group files also support inheritance, and interfaces (more on interfaces later).  The templates in a group file can declare arguments. Templates can invoke other templates in the group file or any of its super groups. For example:</p>
<pre>
supergroup.stg:
  group supergroup;
  util(arg1) ::= <<
  This is in the super group. $arg1$.
  >>
subgroup.stg:
  group subgroup : supergroup;
  template1(arg1) ::= <<
  This is in the sub group.
  $util(arg1)$.
  >>
</pre>
<p>Now here is where implementation details get in the way. For simple templates to invoke other simple templates they must be gathered together in a StringTemplateGroup object. This object is given a root directory and all template files must be under that root. This same object when constructed differently represents a group file. Templates in one group cannot call templates in another group unless the groups are in the same inheritance hierarchy. This means that templates in a group cannot call simple templates and vice versa. (Technically it should be possible to programmatically cause simple template groups and group file groups to inherit from one another but it can&#8217;t be done from within the group or simple template file syntax.)</p>
<p>I have been using StringTemplate to generate the HTML for a web application. I started out using simple templates but soon found I wanted the ability to declare arguments on my templates so I switched to the group file format. </p>
<p>The program calls a template that will produce a complete web page. That template relies on other templates to generate parts of the page. These other templates in effect represent controls &mdash; aggregations of HTML such as an input field and its label, an image with a drop shadow, or a table with next and previous links. I use one group file for each page. The controls are reusable so they shouldn&#8217;t go in the group for a specific page. The only solution available is to put them all in a base group and have the page groups inherit from that base group.</p>
<p>There are a number of problems with this. The first is that all the control templates must be put together in a single group (because a group can only inherit from one base group). This group file quickly gets large. Second it forces an is-a relationship where there isn&#8217;t one. If my base group is called “dumping-ground-for-all-controls.stg” and my page group is called “accounts-page.stg” it doesn&#8217;t mean that an accounts-page is a kind of dumping ground for all controls as inheritance would imply. </p>
<p>I should be able to choose my own way of organizing my templates into groups. I would much rather put each control in its own group or possibly group like controls together. My page group would then call templates in any of these group files. </p>
<p>I wouldn&#8217;t use inheritance unless one page or control was truly a specialization of another page or control.</p>
<p>To get a better understanding of the StringTemplate notions of templates and groups it is useful to compare them with the abstractions of other languages.</p>
<p>Conceptually a template is like a function. It takes a collection of named attributes and returns a text string. Simple template files and group files are modules. They are the building blocks of a StringTemplate text generation system.</p>
<p>Languages like C allow you to group your functions together into files (modules) any way you like and functions can call functions in other modules. StringTemplate should allow the same.</p>
<p>On the surface group files may seem like classes. They have some things in common but also important differences. A group is a collection of related templates in the same way a class is a collection of related methods. The thing that relates a class&#8217;s methods is access to and operations on a common set of data. This data is an object or class instance. In StringTemplate there is no mutable state so it makes no sense to speak of class instances or objects. Groups support inheritance like classes do but template polymorphism is a little different from traditional OO languages. In OO languages like Java or C++ the method called depends on the type of the object the call is made on rather than the type of the object reference. In StringTemplate, again, there are no objects so the template called depends on the group used to make the initial template call.  It is a good thing that the StringTemplate group abstraction was not called a class lest someone take the analogy too far.</p>
<p>StringTemplate groups do a good job at providing needed capabilities for programming in the large. Specifically grouping like templates together in a single file for convenience, declaring template arguments, inheritance and template polymorphism, and interfaces. They fall short in assuming that all templates needed by a text generation system belong in a single group hierarchy. </p>
<p>It may seem restrictive for simple templates to limit the file to containing a single template. Imagine C forcing you to have one function per file. One might be tempted to get rid of the simple template and just keep the group file. However there is value in having simple templates in addition to groups. The simple template is useful when the output is mostly static text with just a few holes that need to be filled in with data. The simple template is like a form letter (Dear $name$, You may have already won a million dollars&#8230;). These templates are the easiest for non-programmers to understand because there is no extra syntax beyond the template actions and the template looks very close to what the final result will be. The same argument for the existence of simplified stylesheet modules in XSLT can be made for simple templates.</p>
<p>I said I would come back to group interfaces and this seems like a good time to do it. I think StringTemplate gets interfaces exactly right. An interface provides a contract between the caller of templates (the program) and the templates themselves. The interface  defines what templates the program plans to call and that the group must implement. </p>
<p>Interfaces in StringTemplate work very similar to Java interfaces. The interface specifies a set of template names and their arguments. When a group implements one or more interfaces it is agreeing to implement all the templates from the interfaces with the right number of arguments for each. An error is given if the group doesn&#8217;t implement the templates specified in the interfaces. If the writer of the program changes the interface then the writers of the group files will know that they need to make corresponding changes.</p>
<p>Another difference between groups and simple templates is in their ability to reference other groups or templates in different folders. Simple templates can use relative paths to reference templates in other folders. Groups can not use relative paths to reference super groups, or interfaces. However the implementation allows specifying multiple directories to search for super groups and interfaces but simple templates must be under a single root. I view these as implementation details and not limitations that are inherent in the language.</p>
<p>To better support programming in the large I would like to see StringTemplate meet these requirements:</p>
<ul>
<li>allow multiple groups during template processing</li>
<li>allow mixing simple templates and groups. A simple template can call a template in a group and a template in a group can call a simple template.</li>
<li>support some kind of namespaces for simple template, group and template names</li>
</ul>
<p>Here is a proposal for meeting the above requirements. Groups and simple template files, collectively modules, are kept in a multi-rooted hierarchical store such as a file system. Other stores such as a database or compressed archive could be used as well &mdash; even a mix of them. The StringTemplate engine implementation would provide a way to specify the roots where modules are found. Something along the lines of the Java class path would be one possibility. The implementation could borrow some ideas from the Java ClassLoader. I&#8217;ll call the hierarchical containers of modules folders. They could just as well be called packages or directories.</p>
<p>Example: </p>
<pre>
controls/listview.stg:
    listView(id, caption, data) ::= ...
    listCell(item) ::= ...
    ...
controls/form.stg:
    button(id, value, label, accessKey) ::= ...
    checkbox(id, value, label, accessKey, checked) ::= ...
    ...
pages/users/list.st
pages/users/add.st
</pre>
<p>The above example defines two groups (listview and form) and six templates (listView, listCell, button, checkbox, list, and add) in two folders (controls and pages).</p>
<p>A group file would introduce a kind of virtual sub folder that contains templates just as a normal folder contains simple templates. The fully qualified name of a template is the forward slash separated list of folders, possibly including a trailing group name and finally the template name. For example the template <code>pages/users/add</code> would add a button to the page by referencing <code>$controls/form/button(...)$</code> and template <code>pages/users/list</code> would reference the <code>listView</code> control as <code>$controls/listview/listView(...)$</code>. The <code>listView</code> template would reference the <code>listCell</code> template simply as <code>$listCell(...)$</code>. The general rule is that templates in the same folder or group don&#8217;t need any folder or group path in front of it. If part of a <code>listView</code> control included buttons then the <code>listView</code> template would refer to the button template as <code>$controls/form/button(...)$</code>.</p>
<p>The hierarchy of folders provides a kind of namespace similar to Java packages. Folder, simple template, and group names are in the same namespace. You can&#8217;t have a folder, group, or template with the same name in the same folder. Even though the file system will allow a folder to contain sub folder foo, and files foo.st and foo.stg this would not be allowed. Folders can contain sub folders, groups and templates. I seen no need for groups to contain other groups (as in Java&#8217;s inner classes).</p>
<p>I used the &#8216;/&#8217; character rather than &#8216;.&#8217; to separate folders, groups and templates because &#8216;.&#8217; is already used to specify nested attribute data and because it is already used in simple templates.</p>
<p>The syntax of group files and even simple templates should be extended to support importing folders or groups so that the templates in those groups and folders can be used without having to use fully qualified names. Different languages have different functionality for one module specifying other modules it intends to use. In StringTemplate I think it should be possible to specify a specific template in a group or folder or all of them. It should be possible to provide an alias for a template. </p>
<p>As for the syntax I&#8217;m not sure what to do. It might look something like this: </p>
<ul>
<li>$use controls/listview/*$ to import all the templates from group listview.</li>
<li>$use controls/listview/listView$ to import just the listView template from group listview.</li>
<li>$use controls/listview/listView as list$ to import the listView template but call it as $list(&#8230;)$</li>
</ul>
<p>In a group file the use directive could go at module scope without being enclosed in the action delimiters. At module scope it would apply to all templates in the module. For example:</p>
<pre>
  group foo;
  use bar/template1;
  ...
</pre>
<p>Another important principal in OO languages (and even languages like ADA) is information hiding. Classes can declare their members, data or methods, as public, private or protected. Since template groups have no mutable state there is no data to be hidden. </p>
<p>I think it may be useful to specify templates as being private. Some templates are just a useful way of implementing another template and don&#8217;t need to be called from outside the group. The implementation of one template can be broken down in to a number of sub templates. The details of which are not important to the callers of the main template. The main template is public while the sub templates should be private. </p>
<p>One possibility would be to add keywords like public or private to template definitions in a group file but there is no elegant way to do the same for simple templates. I think it would be fine to use a naming convention such as template names beginning with an underscore can only be used from templates in the same folder or group.</p>
<p>I believe these enhancement will make reusable template libraries possible. Even if there isn&#8217;t much need for template libraries a large project will benefit from being able to organize the templates in a way that makes sense for the problem at hand. You can choose to use simple templates or groups depending on what is best for each template rather than based on rules for what calls are allowed. </p>
]]></content:encoded>
			<wfw:commentRss>http://hardlikesoftware.com/weblog/2007/06/25/thoughts-on-stringtemplate-part-2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Thoughts on StringTemplate part 1</title>
		<link>http://hardlikesoftware.com/weblog/2007/06/01/thoughts-on-stringtemplate-part-1/</link>
		<comments>http://hardlikesoftware.com/weblog/2007/06/01/thoughts-on-stringtemplate-part-1/#comments</comments>
		<pubDate>Fri, 01 Jun 2007 04:08:37 +0000</pubDate>
		<dc:creator>John Snyders</dc:creator>
				<category><![CDATA[StringTemplate]]></category>

		<guid isPermaLink="false">http://hardlikesoftware.com/weblog/2007/06/01/thoughts-on-stringtemplate-part-1/</guid>
		<description><![CDATA[I have been working with StringTemplate for a few months now. I know that isn&#8217;t too long and others have been using it for much longer. But in that time I have thought a lot about it and formed some opinions. (I hope it is clear from what I have already written that I&#8217;m a [...]]]></description>
			<content:encoded><![CDATA[<p>I have been working with StringTemplate for a few months now. I know that isn&#8217;t too long and others have been using it for much longer. But in that time I have thought a lot about it and formed some opinions. (I hope it is clear from what I have already written that I&#8217;m a big fan.) In this and the next few articles I&#8217;ll try to organize my thoughts, hopefully stir up some discussion and possibly impact future versions of the language.<br />
<span id="more-29"></span></p>
<h3>StringTemplate the language</h3>
<p>Presently StringTemplate is described in terms of both its syntax and implementation (API). I think it is important to describe the StringTemplate language independently from its implementation. I see the following benefits to doing this:</p>
<ul>
<li>It allows thinking more clearly about the language without implementation details affecting language design decisions.</li>
<li>It promotes independent interoperable implementations either for additional host languages or alternate implementations for currently supported languages. Implementations could be interpreters or compilers.</li>
</ul>
<p>A consequence of describing the StringTemplate language is that the language should be versioned independent of the implementation.</p>
<p>Implementation details include locating, loading/parsing, and invoking templates, providing data to the templates, output filters and rendering.</p>
<p>For the most part the language specification can state that an implementation will provide a way to do X without having to specify how. For example, it is up to the implementation to provide a method for invoking a template. </p>
<p>Some things will be dependent on the host language for example the character set encodings supported for templates. Even though there is no syntax in SringTemplate for specifying the character set encoding the language specification should state any underlying assumptions or dependencies. </p>
<h3>Data model</h3>
<p>One very important thing to describe independent of the implementation is the data model that StringTemplate operates on. StringTemplate should clearly define its data model and then, separately, the implementation would define how it maps native types onto that data model. Although a template knows nothing about the type of an attribute (with a minor exception for booleans in <code>if</code> expressions) it is very aware of the “shape” of the data. StringTemplate supports data with these shapes: scalars (single value), lists (ordered collection), and maps (unordered keyed values). It also has a special value called null that produces no output, and evaluates as false in <code>if</code> expressions.</p>
<p>Lists can contain any mix of scalars, lists, maps and nulls. A map entry value can be a list, scalar, map or null. The keys of a map must be scalar strings.</p>
<p>The ability to represent the data model literally in StringTemplate is currently inconsistent. </p>
<p>There is a literal syntax for strings, which makes sense since everything gets turned into a string in the end. The grammar includes a definition for integers (INT in <code>action.g</code>) and this should be removed. There is no reason why StringTemplate should know anything about numbers of any kind. I think Terence <a href="http://www.antlr.org:8080/pipermail/stringtemplate-interest/2006-October/000751.html">agreed</a> to this.</p>
<p>List literals can only be defined in the context where an expression is expected and are unnamed but maps can only be defined in group files at the file level and they are named. I don&#8217;t see the harm in allowing literal maps, lists and strings at both the group file level and in the context of an expression. One problem is that literal maps use square brackets as delimiters just like lists. I&#8217;m not sure what the best syntax for literal maps should be. Curly brackets are already used for anonymous templates, square brackets are used for lists, parentheses are used for template argument lists. Perhaps [: could introduce a literal map. At the group level you should be able to name a list, map or string like so:</p>
<pre>
  group example;
  map ::= [: "key1": "value1", default: "none"]
  list ::= [ "1", "2", "3" ]
  string ::= &#8220;something&#8221;
  &#8230;
</pre>
<p>The capabilities for literal data should be similar to the <abbr title="JavaScript Object Notation"><a title="JavaScript Object Notation" class="gloss" href="/weblog/glossary#JSON">JSON</a></abbr><br />
format even though the syntax will likely need to be different. The exception is that numbers and boolean types are not supported.</p>
<p>The program can define a list of lists (for example to represent a matrix) but the literal syntax doesn't preserve the nesting. Even though <code>$table(t=[ [ "a", "b" ], [ "c", "d" ] ])$</code> is allowed it gets flattened. Not sure if this is intentional or a bug but I think that the literal structure should be preserved. If there is a need to flatten a list then it should be made  into a function such as <code>$flatten(list) : ...$</code>. Once there is a literal syntax for maps it should be possible to compose a literal map of lists and a list of maps. </p>
<p>There should be a literal syntax for null so that you can create lists (or maps) with nulls in them and  explicitly pass null to a template.</p>
<h3>Map and object unification</h3>
<p>Maps and objects should be unified. There are a few differences between how StringTemplate deals with objects and maps. Ideally from the StringTemplate data model point of view there should just be one thing; call it a map or call it an object it should behave the same. When you write $A.B$ you have no idea if A is an object and B is a property or A is map and B is a key. In fact the program should be able to change the implementation from an object to a map or the other way around without the template being affected.</p>
<p>The differences are: 1) Maps support the pseudo keys "keys" and "values" and objects do not. 2) Objects have an implied call to <code>toString</code> that maps don't. 3) They have different behavior when a property/key doesn't exist. These differences make the template aware of the underlying implementation type.</p>
<p>I never did like these pseudo keys because they pollute the namespace of map keys. Also there is no reason why you couldn't ask for all the properties of an object. The "values" key is not really needed because $map$ has the same effect as $map.values$. To solve these problems I think values and keys should be functions like <code>first</code> and <code>last</code>. <code>$keys(A)$</code> will return a list of all the keys of a map or the properties of an object.  For a scalar it should probably return an empty list and for a null return null. The <code>values</code> function would be defined similarly except that it would return a list of the values of all the keys or all the properties. </p>
<p>The remaining two inconsistencies (access as a scalar and access a key that isn't present) are trickier. I'm not too sure what the best behaviors are. </p>
<p>There is good reason to support accessing an object as a scalar and providing access to its properties as well. Imagine a date object. When you access it as a scalar it should result in the text representation of its date value. But it may also have useful methods such as month and day. Example: <code>$today$, $today.month$</code>. For a map I'm not sure if <code>$map$</code> should return null (empty string) or a list of all its values as it does now. Either way a fundamental differences remains between maps and objects. Hopefully it isn't too surprising; this extra ability of objects.</p>
<p>It would be nice if you had control over the behavior when a property/key doesn't exist in an object/map. I'm not sure if the program or template should get to control it and if it is the template how it would be specified. The literal map syntax already has a default keyword to specify the value when a key doesn't exist. I think the default should be to give an error if an object property or map key doesn't exist. For either a map or an object there should be a way to specify the default value for a non-existent key/property. The value of the default (as well as the values in the literal map syntax) should support template expressions. The implicit attribute key would be available to the template. This is more powerful than the key keyword currently supported in the literal map syntax. </p>
<p>One way to support adding a default specification for maps/objects would be composing maps from other maps. Suppose <code>[: map1, map2...  default: expr ]</code> was a literal syntax for composing a new map from map1 and map2.  The action <code>$foo(arg=[: myMap, default: "none" ])$</code> would create a new map and add all the key value pairs from myMap to it and also specify that if a key is missing the string "none" is returned. This map is then passed as the value for argument arg to template foo. There may be a better way to specify the non-existent key behavior.</p>
<p>Another possibility might be an option like the null option. It might look like this: <code>$map.optionalkey;notexists="none"$</code></p>
<h3>Template argument shape</h3>
<p>Clearly there is no point in specifying the type of arguments in template definitions when templates have no knowledge of the underlying attribute type. Example: <code>myTemplate(int i, Date d) ::= ...</code> makes no sense. But would it be useful for a template to declare the shape of the argument value (scalar, map, or list)? </p>
<p>This may be similar to what Terence wrote in this <a href="http://www.antlr.org:8080/pipermail/stringtemplate-interest/2007-April/001024.html">post</a>:</p>
<blockquote><p>I have been thinking for quite a while about adding *, +, and ? to the definition of template arguments so that you know whether you should get zero more, one or more, or zero or one values in the attribute.  That way ST can check the cardinality and existence<br />
automatically for you.</p></blockquote>
<p>It seems that this would only apply to lists and would tell something about how many items are expected in the list.</p>
<p>If a template definition declared that its argument must be a scalar and a list or map is passed instead an error would be given. The distinction between a scalar and an object/map is very subtle. The declaration is more about how you intend to use it rather than the actual underlying type. In one case you expect it to have some specific properties and in the other case you don't care if it has properties or not.</p>
<p>The shape declaration could be optional. I'm not sure if this is useful or not. At a minimum it would help document what the template expects. Which leads to the next topic.</p>
<h3>Agreeing on the data</h3>
<p>Every now and then the question of "how can I find out what attributes a template uses" gets asked on the stringtemplate-interest mailing list. From the other vantage point a reasonable question is "what data does the program make available to the template". The way you look at it may come down to who is driving the application design; the template writer or the program writer. If they are the same person it may depend on how they see the problem; either driven by the needs of the output or driven by the program and the data it has. </p>
<p>Either way the important point is that the program and the template have to agree on the data and need some way to communicate what the data looks like. Currently StringTemplate has no way to facilitate this communication or validate that the data meets the agreed upon structure. I'm not saying that it needs to. I'm just wondering if it would be useful and what it might look like. </p>
<p>In my own work so far I have not done anything to write down what the structure of the data is. It has not been a problem for me because the application is small and I am writing both the program and the templates but I know this won't scale. Simply looking at the template arguments is of little help because the data can be deeply nested. Even the above mentioned suggestion to declare the shape of the template arguments doesn't help much because of this deep structure. How do you solve this problem? (Thats not a rhetorical question - I'd like to know what you do.) </p>
<p>In the <abbr title="Extensible Markup Language">XML</abbr> world a schema is used for this purpose. The producer and consumer of some structured data both agree on the schema. The schema describes the structure of the data as well as the types of individual values. Any time between when the data is produced and when it is consumed the data can be validated against the schema. With XML the data is usually serialized in a text document in XML format but it doesn't need to be. The schema language may be <a href="http://relaxng.org/">Relax NG</a> (unless you are unfortunate and have to use XML Schema).  </p>
<p>This makes sense for XML but may be overkill for StringTemplate. A big difference between the two is that the coupling between the producer (the program) and the consumer (the template) is much tighter with StringTemplate. The template and the program are designed and built together and the data is unlikely to be serialized in between. Still I wonder if something like Relax NG could be useful as a way to communicate the data agreement. Validation of the data could be done as part of a unit test. Another potential benefit of a schema would be assisted editing. The editor would know about what attribute and attribute properties are available.</p>
<p>In the Java/OO world the class declarations describe the data. The data structure may be given as a UML diagram. This works for communication. Within the program code Java's type checking can help ensure that the data is valid but there is nothing to enforce, at "build" time, that the template is using the data according to the Java types. Again, I'm not suggesting that StringTemplate needs strong or static type checking - just making a comparison.</p>
<h3>Possible host languages</h3>
<p>Thinking about StringTemplate as an independent language has me wondering what kinds of host languages could it be implemented in. Are there any languages or classes of languages where it wouldn't work well, isn't needed, or would have limitations. </p>
<p>One issue that comes to mind is that the rendering of attributes depends on the type of the attribute. The identification of rendering as a distinct aspect of template processing is a very powerful feature of StringTemplate. The template doesn't need to know about data types or type specific formating because the renderer knows how to format the attribute according to its type. </p>
<p>What would happen in a dynamically typed language, would the renderer know how to format the attribute in a meaningful way? If not then the template would need to rely on the format option.</p>
<p>Well that's more than enough thoughts for now. I hope to get the next batch written up soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://hardlikesoftware.com/weblog/2007/06/01/thoughts-on-stringtemplate-part-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>On Learning StringTemplate</title>
		<link>http://hardlikesoftware.com/weblog/2007/04/26/on-learning-stringtemplate/</link>
		<comments>http://hardlikesoftware.com/weblog/2007/04/26/on-learning-stringtemplate/#comments</comments>
		<pubDate>Thu, 26 Apr 2007 04:38:40 +0000</pubDate>
		<dc:creator>John Snyders</dc:creator>
				<category><![CDATA[StringTemplate]]></category>

		<guid isPermaLink="false">http://hardlikesoftware.com/weblog/2007/04/26/on-learning-stringtemplate/</guid>
		<description><![CDATA[I have been thinking lately about the learnability (the ease with which something can be learned) of StringTemplate. I like StringTemplate and would like to see more people make use of it. To that end I think there are some things that can help make it easier for people to get started with it.
StringTemplate is [...]]]></description>
			<content:encoded><![CDATA[<p>I have been thinking lately about the learnability (the ease with which something can be learned) of <a href="http://www.stringtemplate.org/">StringTemplate</a>. I like StringTemplate and would like to see more people make use of it. To that end I think there are some things that can help make it easier for people to get started with it.</p>
<p>StringTemplate is both a language for describing text generation and a tool (template engine) that implements that language and provides an API to connect the tool to another program.</p>
<p>StringTemplate is not a general purpose language and cannot stand on its own. By design it has no way to perform general computation. It also has no general way to create data and limited ability to specify literal data. It is up to some driving program to produce and or make data available to the StringTemplate template engine. This is not a negative – this is it&#8217;s strength. See <a href="http://www.cs.usfca.edu/~parrt/papers/mvc.templates.pdf">this excellent paper</a> for why. </p>
<p>However, the lack of readily available data makes learning the template language difficult without also learning the StringTemplate API. In order to try StringTemplate out one must learn the template syntax and semantics, and the API and then write a program to produce some data and call the template engine. </p>
<p>In theory one benefit of the separation of presentation from business logic is that a project can be divided between people who work on the business logic (the program) and people who work on the presentation (the templates). The people working on presentation don&#8217;t need to be programmers.</p>
<p>The current documentation for StringTemplate describes both the template syntax and the Java API at the same time. In some cases it is confusing to learn two things at once and more importantly it excludes non-programmers from learning StringTemplate.</p>
<p>In contrast, trying out XSLT is is easy (even if mastering XSLT is hard) because all you need is a text editor to create both your input documents and your style sheet and an XSLT program to process the style sheet with the input documents.  Likewise trying out HTML is easy, all you need is a text editor and a browser. Interpreted languages like Python that have an interactive mode also make it easy to jump right in and get the feel of things. (I&#8217;m not saying that StringTemplate is like XSLT, HTML or Python, I&#8217;m just comparing the ease with which one can dive in and try the technology out.) </p>
<p>So what StringTemplate needs is a literal representation for data that can be used as input  and a stand alone program to read the data and templates to produce the output text.</p>
<p>I choose <abbr title="JavaScript Object Notation"><a title="JavaScript Object Notation" class="gloss" href="/weblog/glossary#JSON">JSON</a></abbr> as the literal data format. The reasons are:</p>
<ul>
<li>Its a <a title="RFC4627" href="http://www.ietf.org/rfc/rfc4627.txt?number=4627">standard</a></li>
<li>It is becoming increasingly well known</li>
<li>It is easy to type in and not overly verbose</li>
<li>I had <a href="http://hardlikesoftware.com/weblog/2006/12/12/using-json-with-stringtemplate/">already made changes</a> to get a Java JSON library to play nice with StringTemplate.</li>
<li>It supports all the data shapes that StringTemplate works with.</li>
</ul>
<p>I created a command line tool called StringTemplate Standalone Tool (STST). It takes as input the name of a file containing data in JSON format, the name of a template and optionally the name of a group as well as various options.</p>
<h2>Look ma, no programming.</h2>
<p>Here is a simple example of how it is used.</p>
<p>Create a JSON file called data.js with this content:<br />
<code>{ "audience": "World" }</code></p>
<p>Create a StringTemplate file called first.st with this content:<br />
<code>Hello $audience$!</code></p>
<p>From a command prompt in the same folder as the above two files type<br />
<code>&gt; stst first data.js</code><br />
The output is<br />
<code>Hello World!</code></p>
<p>It also works with group files and can use the angle bracket lexer. For command usage type: stst -h</p>
<p>The next thing StringTemplate needs is a tutorial that focuses on the template syntax independent of the API. STST enables this kind of tutorial. Separate documentation can focus primarily on the API. </p>
<p>Download STST <a href="http://hardlikesoftware.com/weblog/stst/">here</a></p>
<p>This tool can have other uses as well. Here are some ideas:</p>
<ul>
<li>It is a quick way to try out template syntax without having to write or modify a program. This can benefit seasoned template authors as well as beginners. </li>
<li>It could be part of a test framework for StringTemplate itself or for a set of templates in an application. The templates can easily be processed with known data and the expected results asserted.</li>
<li>It enables development of the templates before or in parallel with code by creating a JSON data file that represents what the code will produce.</li>
<li>It could be used as part of shell scripts where some existing program outputs JSON data and STST is used to generate text from it. The data could come from a web service for example.</li>
</ul>
<p>What could you use it for?</p>
<p>Future possible directions for STST</p>
<ul>
<li>Integrate the functionality into an IDE for StringTemplate</li>
<li>Expose the functionality in an interactive servlet (web app).</li>
<li>Support other data formats like CSV, YAML, or XML</li>
<li>More support for renderers</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://hardlikesoftware.com/weblog/2007/04/26/on-learning-stringtemplate/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>I18n With StringTemplate</title>
		<link>http://hardlikesoftware.com/weblog/2007/01/15/i18n-with-stringtemplate/</link>
		<comments>http://hardlikesoftware.com/weblog/2007/01/15/i18n-with-stringtemplate/#comments</comments>
		<pubDate>Mon, 15 Jan 2007 18:01:56 +0000</pubDate>
		<dc:creator>John Snyders</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[StringTemplate]]></category>

		<guid isPermaLink="false">http://hardlikesoftware.com/weblog/2007/01/15/i18n-with-stringtemplate/</guid>
		<description><![CDATA[Developers accustomed to internationalizing Java [web] applications are very familiar with ResourceBundle, MessageFormat and property files. The articles Localization and Internationalization with StringTemplate and Web Application Internationalization and Localization in Action give a good introduction to how StringTemplate can be used to do i18n but they don’t explain how to get the full set of [...]]]></description>
			<content:encoded><![CDATA[<p>Developers accustomed to internationalizing Java [web] applications are very familiar with ResourceBundle, MessageFormat and property files. The articles <a href="http://www.stringtemplate.org/article/i18n/index.html" shape="rect">Localization and Internationalization with StringTemplate</a> and <a title="PDF document" href="http://www.cs.usfca.edu/~parrt/papers/i18n.pdf" shape="rect">Web Application Internationalization and Localization in Action</a> give a good introduction to how StringTemplate can be used to do <abbr title="Internationalization"><a href="/weblog/glossary#i18n" title="Internationalization" class="gloss">i18n</a></abbr> but they don’t explain how to get the full set of benefits we derive from ResourceBundle, and MessageFormat. In this article I describe how StringTemplate can leverage ResourceBundle and remove the need for MessageFormat in the presentation layer.</p>
<p>	<span id="more-17"></span></p>
<p>The article <a href="http://www.stringtemplate.org/article/i18n/index.html" shape="rect">Localization and Internationalization with StringTemplate</a> shows an example using the java.util.Properties class. This is sufficient for showing the benefits of using StringTemplate but there are some nice things that ResourceBundle does for us that the Properties class doesn’t. ResourceBundle will automatically locate the property files on the classpath and form a hierarchy of properties from most specific to most general.</p>
<p>If you have these property files:</p>
<pre xml:space="preserve"><code>Messages.properties
Messages_en.properties
Messages_en_US.properties</code></pre>
<p>then</p>
<pre xml:space="preserve"><code>ResourceBundle bundle =
    ResourceBundle.getBundle("Messages", new Locale("en", "US");</code></pre>
<p>will load them from the class path and chain them together so that if a string is missing from Messages_en_US.properties it will attempt to find it in Messages_en.properties and if it is still not found it will look in Messages.properties. This is useful so that the country file (_en_US) only needs to contain country specific translations.</p>
<p>Note: You may have other ways to get the locale such as request.getLocale(); in a web application.</p>
<p>The above code is much simpler than loading Properties objects and chaining them together yourself. The problem with ResourceBundle is that it is not readily usable by StringTemplate. Wrapping ResourceBundle in a class that implements the Map interface can solve this. I’ll show this class later but here is how it would be used with StringTemplate:</p>
<pre xml:space="preserve"><code>StringTemplate st = templates.getInstanceOf("templates/Test");
st.setAttribute("bundle", new STResourceBundleMapWrapper(bundle));</code></pre>
<p>In templates/Test you can use bundle like so:</p>
<pre xml:space="preserve"><code>…&lt;h1&gt;$bundle.Title$&lt;/h1&gt;…</code></pre>
<p>Where the Messages* files contain:</p>
<pre xml:space="preserve"><code>Title=This is the Test Title</code></pre>
<p>With the benefits of ResourceBundle and property files now available to StringTemplate its time to look at  MessageFormat.</p>
<p>MessageFormat is very important for <abbr title="Internationalization"><a href="/weblog/glossary#i18n" title="Internationalization" class="gloss">i18n</a></abbr> because it allows translators to move substitutable text values around in a phrase or sentence to accommodate the structural differences between languages. MessageFormat has a few capabilities in its pattern syntax. Most of the time it is used in its simplest form for example:</p>
<pre xml:space="preserve"><code>MessageFormat.format(
	"On {0} server {1} crashed because {2}"…).</code></pre>
<p>In the <a title="PDF document" href="http://www.cs.usfca.edu/~parrt/papers/i18n.pdf" shape="rect">Web Application Internationalization and Localization in Action</a> article there is a short example showing how StringTemplate can do what the MessageFormat {} patterns can. The example it gives is:</p>
<pre xml:space="preserve"><code>"title=Welcome to $username$’s test page"</code></pre>
<p>At first glance it would seem that you can simply replace { and } with $ and $ in your properties files and you are done. The article then goes on to explain that this only works if the format string (Welcome to $username$’s test page) is a StringTemplate object and not a String. This can be done without having to create a named template and add it to a StringTemplateGroup. StringTemplate is smart enough to recursively process an attribute value when it&#8217;s type is StringTemplate.</p>
<p>There is, however, one other big difference between how MessageFormat is used and the StringTemplate example above. The title message has knowledge of another template attribute named &#8220;username&#8221;. In some cases this may be appropriate but generally:</p>
<ol>
<li>the person doing the localization should not have any knowledge of the set of attribute names (the attribute names form the contract between the model and the view) and
		</li>
<li>a single formatted message may need to be used in multiple contexts where different attributes are substituted.
		</li>
</ol>
<p>This problem is easy to fix by simply changing $username$ to $p0$ and then passing in the value for p0 in the template. For example you could do the following to define a formatted message:</p>
<pre xml:space="preserve"><code>StringTemplate st = templates.getInstanceOf("sometemplate");
StringTemplate message =
    new StringTemplate("On $p0$ server $p1$ crashed because $p2$);
HashMap&lt;String, Object&gt; bundle = new HashMap&lt;String, Object&gt;();
bundle.put("message", message);
st.setAttribute("bundle", bundle)
...</code></pre>
<p>and then use the message from some template like so:</p>
<pre xml:space="preserve"><code>
Error: $date, serverName, errorReason:{p0, p1, p2 | $bundle.message$}$
	</code></pre>
<p>where date, serverName, and errorReason are other attributes available to the template.</p>
<p>The trouble is that the message should come from the message property file; it should not be hard coded. This is where the STResourceBundleMapWrapper comes in handy again. The get method can simply look at the resource bundle value and see if it has a $ in it. If it does then it must be a template so return the string wrapped in a StringTemplate, otherwise simply return the value as is.</p>
<p>Now this changes the rules for the translator a little. They need to be trained on the new syntax $pn$ rather than {n}. Also $ characters anywhere else in the property file will need to be escaped as &#8220;\\$&#8221;. Thankfully the complex single quote rules of MessageFormat can be forgotten.</p>
<p>What about the other capabilities of the message format patterns? A pattern can include format type and format style. For example {0,number, percent}. I think that the StringTemplate rendering functionality is better than what MessageFormat provides so type and style are not needed. For the most part StringTemplate rendering means that the translator has one less thing to be concerned about because the rendering is gong to do the right thing automatically.</p>
<p>That leaves format type choice. I don’t think this is used much but I believe StringTemplate can handle it in a similar way. The example from the SDK javadoc is:</p>
<pre xml:space="preserve"><code>There {0,choice,0#are no files|1#is one file|1&lt;are {0,number,integer} files}.</code></pre>
<p>To do this with StringTemplate create a properties file that contains:</p>
<pre xml:space="preserve"><code>DiskInfo0=There are no files.
DiskInfo1=There is one file.
DiskInfo2=There are $p0$ files.</code></pre>
<p>Make it available to the template as described above through an attribute called bundle. The controller code sets attributes as follows according to how many files there are:</p>
<pre xml:space="preserve"><code>int numFiles = 500; // TODO get a real value
String numFilesMessage = null;
if (numFiles == 0)
    numFilesMessage = "DiskInfo0";
else if (numFiles == 1)
    numFilesMessage = "DiskInfo1";
else
    numFilesMessage = "DiskInfo2";
st.setAttribute("numFiles", numFiles);
st.setAttribute("numFilesMessage", numFilesMessage);</code></pre>
<p>The template would look like this:</p>
<pre xml:space="preserve"><code>$numFiles:{p0 | $bundle.(numFilesMessage)$ }$</code></pre>
<p>As promised, here is a sketch of the STResourceBundleMapWrapper class. This defines the constructor that takes the ResourceBundle to wrap and property methods for the same.</p>
<pre xml:space="preserve"><code>
public class STResourceBundleMapWrapper implements Map
{
    private ResourceBundle m_wrappedBundle = null;

    public STResourceBundleMapWrapper(ResourceBundle bundle)
    {
        m_wrappedBundle = bundle;
    }
    public ResourceBundle getWrappedResourceBundle()
    {
        return m_wrappedBundle;
    }
    public void setWrappedResourceBundle(ResourceBundle bundle)
    {
       m_wrappedBundle = bundle;
    }
    …</code></pre>
<p>All the optional methods (clear, put, putAll, remove) throw an exception as is shown for clear.</p>
<pre xml:space="preserve"><code>
    public void clear()
    {
        throw new UnsupportedOperationException();
    }
    …</code></pre>
<p>Because it is not intended for the map to be iterated over a trivial implementation for most of the methods will suffice. For example:</p>
<pre xml:space="preserve"><code>public boolean isEmpty()
    {
       //not intended to iterate over
       return false;
    }
    public Set keySet()
    {
       //not intended to iterate over
       return null;
    }
    …
	</code></pre>
<p>Actually StringTemplate does use keySet when the &#8220;keys&#8221; pseudo property is referenced but it shouldn&#8217;t be needed for a resource bundle attribute. ResourceBundle does have a getKeys method but it returns an enumeration which would have to be turned into a Set. I didn&#8217;t bother.</p>
<p>The interesting stuff is in these two methods.</p>
<pre xml:space="preserve"><code>
    public boolean containsKey(Object key)
    {
        if (key != null &amp;&amp; key instanceof String)
        {
            try
            {
                m_wrappedBundle.getObject((String)key);
            }
            catch (MissingResourceException mre)
            {
                return false;
            }
            return true;
        }
        return false;
    }

    public Object get(Object key)
    {
        if (key != null &amp;&amp; key instanceof String)
        {
            try
            {
                Object o = m_wrappedBundle.getObject((String)key);
                if (o instanceof String)
                {
                    // check for a potential template
                    String s = (String)o;
                    if (s.contains("$"))
                    {
                        // TODO cache this?
                        return new StringTemplate(s);
                    }
                }
                return o;
            }
            catch (MissingResourceException mre)
            {
                // fall through
            }
        }
        return null;
    }</code></pre>
<p>If the key is not found in the ResourceBundle StringTemplate will render nothing because it first checks if the map contains the key and only looks up the value if it does. If you wanted to you could lie in the containsKey implementation and say all keys are present so that in the get method you can return the key itself if it is missing. This can make it obvious when a typo is made in key names.</p>
<p>Once I created STResourceBundleMapWrapper and figured out the StringTemplate syntax for passing the arguments to $pn$ attributes, internationalizing my web app was much easier.</p>
]]></content:encoded>
			<wfw:commentRss>http://hardlikesoftware.com/weblog/2007/01/15/i18n-with-stringtemplate/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

