views:

57

answers:

2

Dear All, 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.

A: 

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.

Bruno
Hi Bruno, We are not allowed to use any opensource template processors.
Apps
@Apps, is that home-work or for a real project? Is it a licensing issue? You still have the XSLT option, using JAXP, which comes with J2SE 5 and above (although strictly speaking you might find it's open source in the OpenJDK). Beyond that, JFlex/CUP/Bison are still reasonable options if you need to demonstrate some manual work.
Bruno
It is a real project and we cannot use XSLT because the approach that I'm suppose to follow is approved by the team. So I'm forced to use it
Apps
I'm somewhat confused as to (a) why you can't use any open source (do you realise that even the JDK is open source now?) and (b) what your team approval constraints are considering that XLST is a well-known tool that's there out of the box and that could do what you're after. Re-inventing what already exists and can be obtained for free (even with licences that allow inclusion in commercial products, e.g. Apache Velocity) doesn't sound like a good use of time and money. If the rest of your team doesn't get these arguments, the people who finance your project probably will.
Bruno
A: 

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

Eran Harel
We are not allowed to use any opensource template processors. We need to write our custom processor.
Apps
Sorry to hear that @Apps... That suckz
Eran Harel