Use of Regex in Notification Templates for Converting IssueTracker Ids to links

I've setup a comment transformation pattern in main-config.xml to transform Issue Tracker Ids to hyperlinks. This seems to work fine and hence I wanted to use the same in the email notification templates.

I modified the common.ftl to include the following:
<i>${description?trim?replace('(\d+)','\1','ri')}</i>

There seems to be a problem with the template parsing and this is the error that is thrown:
[2011-05-26 16:53:47,688]  DEBUG - .notification.FreeMarkerHelper - Error parsing imported template email/common.ftl
Caused by: freemarker.core.ParseException: Token manager error: freemarker.core.TokenMgrError: Lexical error at line 38, column 49.  Encountered: "d" (100), after : "\'(\\" in email/common.ftl

When I parse the FTL using FMPP(
fmpp.sourceforge.net), I get the following error:
The cause of aborting was:
freemarker.template.TemplateModelException: The regular expression classes are
not available.

The BuiltIn.java source of freemarker confirms the above:

            static class replaceBI extends BuiltIn {
                TemplateModel _getAsTemplateModel(Environment env)
                        throws TemplateException {
                    TemplateModel model = target.getAsTemplateModel(env);
                    if (model instanceof  TemplateScalarModel) {
                        return new BIMethod(((TemplateScalarModel) model)
                                .getAsString());
                    }
                    throw invalidTypeException(model, target, env, "string");
                }

                private class BIMethod implements  TemplateMethodModel {
                    private String s;

                    private BIMethod(String s) {
                        this .s = s;
                    }

                    public Object exec(List args) throws TemplateModelException {
                        int numArgs = args.size();
                        if (numArgs < 2 || numArgs > 3) {
                            throw new TemplateModelException(
                                    "?replace(...) needs 2 or 3 arguments.");
                        }
                        String first = (String) args.get(0);
                        String second = (String) args.get(1);
                        String flags = numArgs > 2 ? (String) args.get(2) : "";
                        boolean caseInsensitive = flags.indexOf('i') >= 0;
                        boolean firstOnly = flags.indexOf('f') >= 0;
                        if (flags.indexOf('r') >= 0) {
                            throw new TemplateModelException(
                                    "The regular expression classes are not available.");
                        }
                        return new SimpleScalar(StringUtil.replace(s, first,
                                second, caseInsensitive, firstOnly));
                    }
                }
            }


I am not sure if I am looking at the right place.

The following links from FreeMarker manual confirms that I would be able to use regular expressions:
http://freemarker.sourceforge.net/docs/ref_builtins_string.html#ref_builtin_replace
http://freemarker.sourceforge.net/docs/ref_builtins_string.html#ref_builtin_string_flags

Is it possible to use regular expressions in the FTL files?

Thanks,
Balaji

8 comments
Comment actions Permalink

Hi Balaji,

Can this problem be with the escaping? Did you try "\\d"?


---
Maxim

0
Comment actions Permalink

Yes I did try escaping it. But it did not work.

I used FMPP to run this simple example from the freemarker manual:

<#assign s = 'foo bAr baar'>
${s?replace("ba*", "XY", 'r')}

This didnt seem to work either and threw the exact same error.

Thanks,
Balaji

0
Comment actions Permalink

That is strange indeed. We didn't try regexps in the templates, but accoring to the doc they are supported.
I'll take a look into that.
In the mean time do you consider to write a plugin for TeamCity doing all this?


---
Maxim

0
Comment actions Permalink

I did consider a plugin. Since I am not a Java programmer, I didnt spend much time looking at it though. Initial thoughts, the documentation to extend the notification plugin can be better.

I'll sure look into it. But if we can get the regexps working that would be really cool and easy.

Thanks again for your help.

Cheers,
Balaji

0
Comment actions Permalink

Hi Maxim,

I looked into the sample plugin using the template processor. Is this what I should be using to manipulate the contents of the notification message? Will you be able to shed some light on it? Sample code would be greatly appreciated.

Thanks,
Balaji

0
Comment actions Permalink

Hi Balaji,

> Is this what I should be using to manipulate the contents of the notification message?
Yes, it is.

> Will you be able to shed some light on it? Sample code would be greatly appreciated.
Please search for SampleTemplateProcessor file, it is a sample.
You don't have to register it, just implement the interface and add the reference in Spring XML. The method fillModel is called after the default model is created, and you can add/overwrite the values in it.


---
Maxim

0
Comment actions Permalink

I was able to convert the issue tracker id's to links just by using FreeMarker. I just had to create a macro. The code simply looks for [] and converts anything in between to links. It's crude but works for me.

Common.ftl:
------------------
<#macro incident_url desc>
  <#local start_index=0 />
  <#local maxLoop = 25 />
  <#list 1..maxLoop as i>
    <#local start=desc?index_of("[", start_index) />
    <#if (start>-1)>
        <#local start=start+1 />
        <#local end=desc?index_of("]", start) />
        <#local id=desc?substring(start,end) />
        <#local url="<a href='https://issuetracker.mydomain.com?id=" + id + "'>" + id + "</a>" />
        <#local desc = desc?replace(id, url, "i") />
        <#local start_index=start />
    <#else>
        <#break>
    </#if>
  </#list>
  ${desc?trim}
</#macro>

And the existing code to display description is modified as below:
<i><@incident_url desc=description /></i>

I am trying to migrate from CruiseControl.Net and to be honest I find TeamCity fascinating.It would be great if you can support regular expressions within freemarker. This would help avoid creating plugins for trivial stuff.

Thanks again for your help.

Cheers,
Balaji

0
Comment actions Permalink

> I was able to convert the issue tracker id's to links just by using FreeMarker. I just had to create a macro. The code simply looks for [] and converts anything in between to links. It's crude but works for me.
Ah, ok, you decided to go this way.

> I am trying to migrate from CruiseControl.Net and to be honest I find TeamCity fascinating.
Thanks! =)

> It would be great if you can support regular expressions within freemarker.
We're checking it. FreeMarker claims the feature is supported, we'll investigate why it isn't working in this particular situation.


---
Maxim

0

Please sign in to leave a comment.