Swapna
Swapna

Reputation: 907

Template processing using Java

We have an email template which needs to be processed using Java. We have to replace the variables in the template with actual values. We were able to achieve this using pattern matching , ie; by searching the template for particular patters and replace them with actual values. Now we need have conditions in the XML file.For example

$if($subject!=null)
 sample subject
$endif

We need to check for this condition also. subject is a variable whose value needs to be repalced. We are not allowed to use Velocity template processor.

Please suggest the best ways of implementing this.Is it good to have to ways of parsing it, like applying values for variables in the first parse and then checking the logic in the second parse. It will be of great help if anyone can provide their valuable suggestions.

Upvotes: 0

Views: 885

Answers (2)

Eran Harel
Eran Harel

Reputation: 2365

I never used it personally, but StringTempate (http://www.stringtemplate.org/) is a fair replacement for Velocity. Some actually say it is better.

Upvotes: 1

Bruno
Bruno

Reputation: 122729

If you can't use Velocity, you might be interested in other template processors. I've used FreeMarker successfully in a few projects.

If you general template is XML-based, you could use an XSLT transform with parameters to express this template. You're likely to get more support out of the box for this.

After this, if you really want to do it the hard way, you could build your own template processor using lexers and parsers such as JFlex and CUP. This being said, in your example, you're using the $ notation for both keywords ($if, $endif) and variables ($subject). While there's nothing wrong with that in principle, it would probably be easier to come up with a different notation for the two types to make the lexer easier (fewer exceptions/reserved words). Better delimiters to distinguish actual content from template instructions would help.

Upvotes: 0

Related Questions