Tags is a Java module that makes it very easy to perform tag substitution on an input text file. Tags are of the form ${value}. I consider this project to be a very simple version of the Apache Velocity project. Tags doesn't have the functionality of Velocity, but it's simpler to use and fits my needs when Velocity is overkill. For instance, I've used Tags in the past to generate HTML files from data in a database. Here's an example. Suppose you want to generate a form letter of the form: Dear Mr. ${last.name},
The first thing to do is implement the interface GetTagValue. Its only method is: String getTagValue(final String tag)This method's tag parameter is the tag found in the input file (inside the ${ and }), and returns the string to use instead. Here's one example implementation of this interface: public String getTagValue(final String tag) All that's left to do is construct a ParseFileTags object using this constructor: public ParseFileTags(final String strFilename,The first argument is the input file name, and the second argument is the name of the class implementing the GetTagValue interface. That's it. Here's the complete example: public final class Tags implements GetTagValue Running the above example code on the input file given at the top of this page causes the following to be printed out: Dear Mr. Smith, The code can be downloaded here. It is released under the MIT license. |