[groovy-user] Special character handling ('$') using SimpleTemplate

1,685 views
Skip to first unread message

Mark Johnson

unread,
Feb 7, 2008, 10:18:28 AM2/7/08
to us...@groovy.codehaus.org
I need to generate some emails with a '$' character for financial data in the email.  The '$' is initially escaped, but by the time it goes through the SimpleTemplate engine the escape is gone and everything blows up.  An code example is shown below:
===========================================================
import groovy.text.Template
import groovy.text.SimpleTemplateEngine

def dollar = "\$"
def origString = '''${aVar}: Early Bird  Registration: USD \\$800/person good thru March 17th, after 900'''
def otherString = '''${aVar}: Early Bird  Registration: USD \$800/person good thru March 17th, after 900'''
println origString
println "-----------------------------"
def aVar = "Test Data"
//def binding = [aVar:'This is a test']


def binding = [aVar:"This is a test", dollar:dollar]
def engine = new SimpleTemplateEngine()
def template = engine.createTemplate(origString).make(binding)
def body = template.toString()

println body

template = engine.createTemplate(otherString).make(binding)
body = template.toString()
println "Second body = ${body}"
==================================================
The output from this block of code is:
${aVar}: Early Bird  Registration: USD \$800/person good thru March 17th, after 900
-----------------------------
This is a test: Early Bird  Registration: USD $800/person good thru March 17th, after 900
Exception in thread "main" org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, Script2.groovy: 2: illegal string body character after dollar sign;
   solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" @ line 2, column 52.
1 error

    at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:296)
    at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:143)
    at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:113)
    at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:125)
    at org.codehaus.groovy.control.SourceUnit.addError(SourceUnit.java:342)
    at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:80)
    at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:239)
    at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:143)
    at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:772)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:438)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:271)
    at groovy.lang.GroovyShell.parseClass(GroovyShell.java:488)
    at groovy.lang.GroovyShell.parse(GroovyShell.java:500)
    at groovy.lang.GroovyShell.parse(GroovyShell.java:480)
    at groovy.lang.GroovyShell.parse(GroovyShell.java:519)
    at groovy.text.SimpleTemplateEngine.createTemplate(SimpleTemplateEngine.java:74)


As the example above shows, if I have two escape characters then the SimpleTemplate handles the '$' okay.  The problem is my data comes to me with just a single '\$'.  I have tried several approaches to find the '$' and add an additional '\' character to make the code work.  My attempted solution has been to use a line like: newString = origString.replaceAll("\$","\\\$") but it has not worked.  Any suggestions will be greatly appreciated.

thanks
Mark Johnson

Jürgen Hermann

unread,
Feb 7, 2008, 11:56:06 AM2/7/08
to us...@groovy.codehaus.org
> From: Mark Johnson [mailto:markfj...@gmail.com]
> def origString = '''${aVar}: Early Bird Registration: USD
> \\$800/person good thru March 17th, after 900'''

You DO realize that the Groovy parser itself remove an escape level here? Which then leaves one \ for the template engine. So no surprise this (and only this) works.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Mark Johnson

unread,
Feb 7, 2008, 12:10:41 PM2/7/08
to us...@groovy.codehaus.org
I understand, but the real question is how can one add the \ back in just before it hits the template?  My efforts with somestring.replaceAll("\$","\\\$") have to date been unsuccessful, so any pointers for how to introduce an escape character before the '$' character will be appreciated.

thanks

mark

tugwilson

unread,
Feb 7, 2008, 12:20:50 PM2/7/08
to us...@groovy.codehaus.org


Mark Johnson-24 wrote:
>
> I understand, but the real question is how can one add the \ back in just
> before it hits the template? My efforts with
> somestring.replaceAll("\$","\\\$")
> have to date been unsuccessful, so any pointers for how to introduce an
> escape character before the '$' character will be appreciated.
>


I think this is a bug. looking at the code for SimpleTemplateEngine there's
special case code for ${} but not for standalone $. This code is very old.
At the time it was written the only way of inserting values into GStrings
was £{} the $identifier form was added later.

Could you raise a JIRA issue please?

People tend not to use Templates much these days (that's no a reason why
they should be broke, of course!). GStrings are very powerful and with
Closures can do pretty much whatever Templates can do.

If you give me some idea of what you are trying to achieve I'll see if I can
help you do it with GStrings.

John Wilson

--
View this message in context: http://www.nabble.com/Special-character-handling-%28%27%24%27%29-using-SimpleTemplate-tp15336061p15338681.html
Sent from the groovy - user mailing list archive at Nabble.com.

Paul King

unread,
Feb 8, 2008, 11:43:00 PM2/8/08
to us...@groovy.codehaus.org

Hi Mark, as you say, SimpleTemplateEngine is doing the right thing
here it is just a case of working out how to give it what it needs.
When you say "The problem is my data comes to me with just a single '\$'."
what do you actually mean? Does it come from a data file?

Paul.

Mark Johnson wrote:
> I understand, but the real question is how can one add the \ back in
> just before it hits the template? My efforts with
> somestring.replaceAll("\$","\\\$") have to date been unsuccessful, so
> any pointers for how to introduce an escape character before the '$'
> character will be appreciated.
>
> thanks
>
> mark
>
> On Feb 7, 2008 11:56 AM, Jürgen Hermann <Juergen...@webde.de

Reply all
Reply to author
Forward
0 new messages