Java Mailing List Archive

http://www.junlu.com/

Home » FreeMarker-user »

Re: [FreeMarker-user] Detect invalid directives

John Todd

2011-10-20

Replies: Find Java Web Hosting

Author LoginPost Reply
Thanks for the patch. I can pull from the SVN URL and test this in our
app shortly. Do you know when the 2.3.x branch will be released by
chance? Any idea for 2.4?

Thanks,
John T.

On 10/19/11 10:57, Daniel Dekany wrote:
> Wednesday, October 19, 2011, 5:36:33 PM, Denis Bredelet wrote:
>
>> On 19 Oct 2011, at 14:46, Daniel Dekany<ddekany@(protected):
>>
>>> Wednesday, October 19, 2011, 2:11:51 PM, John Todd wrote:
>>>
>>>> On 10/19/11 04:29, Daniel Dekany wrote:
>>>>> Wednesday, October 19, 2011, 5:55:18 AM, John Todd wrote:
>>>>>
>>>>>> Hello,
>>>>>>
>>>>>>  I would like to handle invalid directives however they are just
>>>>>> passing through the templating engine as text, e.g. using the following
>>>>>> FTL "blah.ftl":
>>>>>>
>>>>>> This is [#blah]content[/#blah]. This is [#fu] content.
>>>>> That's not possible, unless you pre-process the templates with a
>>>>> custom TemplateLoader that replaces "[#blah]" with "[[#-- --]#blah]"
>>>>> or like.
>>>>>
>>>>> Out of curiosity, why do you need this?
>>>> We use the templating engine extensively for on-demand personalization
>>>> of content and would like to reject templating requests at the time the
>>>> template is passed to our system if the template contains unknown
>>>> directives and just reject the request.
>>>>
>>>> So, you're saying it's not possible to catch such exceptions or the
>>>> behavior I'm seeing is not possible?
>>> Sorry, I was hasty and misread your question. FreeMarker should throw
>>> exception for invalid # tags always and everywhere. However, now that
>>> I have tried this, it seems it bypasses the # tags until it finds one
>>> that it knows. It's astonishing that I have never ran into this bug
>>> myself, and it's quite concerning that the author didn't have a test
>>> case that catches this. I suppose it slipped in when tag-syntax
>>> detection was added. It can't be straight fixed in 2.3.x as it would
>>> be non-BC, except by adding some new option. The workaround for this
>>> right now is starting all templates with [#ftl]...
>> Is this only for the square bracket syntax?
> No, it affects all tag syntax modes.
>
>> It makes sense to start the template with [#ftl] anyway, instead of
>> relying on auto-detection...
> I think "<#" and "[#" aren't so frequent to warrant that in most
> practical scenarios. Also, if someone explicitly sets
> cfg.setTagSyntax(Configuration.SQUARE_BRACKET_TAG_SYNTAX) it's even
> more redundant. Plus this bug affects the default
> ANGLE_BRACKET_TAG_SYNTAX too.
>
> What I came up with is that I add a new Configuration-level setting,
> "emulate_2_3_parser_bugs", which defaults to true in 2.3.x, and to
> false starting from 2.4.0. So if OP calls
> cfg.setEmulate23ParserBugs(false), it will work as it should. It can
> go into the nightly build pretty quickly, but the next 2.3.x release
> is further away.
>
>> -- Denis.
>>
>>>> thx,
>>>> jt
>>>>
>>>>>> and the code:
>>>>>>
>>>>>> public class App {
>>>>>>
>>>>>>   public static void main(String[] args) throws Exception {
>>>>>>      Configuration cfg = new Configuration();
>>>>>>      cfg.setTagSyntax(Configuration.SQUARE_BRACKET_TAG_SYNTAX);
>>>>>>
>>>>>>      cfg.setDirectoryForTemplateLoading(new
>>>>>> File(System.getProperty("user.dir")));
>>>>>>
>>>>>>      System.out.println("Compiling template: blah.ftl");
>>>>>>      FileReader reader = new
>>>>>> FileReader(System.getProperty("user.dir") + "/blah.ftl");
>>>>>>      // Expect ParseException
>>>>>>      Template template = new Template("blah.ftl", reader, cfg);
>>>>>>
>>>>>>      System.out.println("Getting template from Configuration.");
>>>>>>      // Also expect ParseException
>>>>>>      template = cfg.getTemplate("blah.ftl");
>>>>>>      StringWriter writer = new StringWriter();
>>>>>>      template.process(new HashMap(), writer);
>>>>>>      System.out.println("Output: " + writer.toString());
>>>>>>   }
>>>>>> }
>>>>>>
>>>>>> I get the output:
>>>>>>
>>>>>> This is [#blah]content[/#blah]. This is [#fu] content.
>>>>>>
>>>>>> In each Template reference, I'm expecting a compilation error w/
>>>>>> "Unknown directive" per the FMParser.jj grammar. Any suggestions?
>>>>>>
>>>>>> Thanks,
>>>>>>
>>>>>> John T.
>>>>
>>>> ------------------------------------------------------------------------------


------------------------------------------------------------------------------
The demand for IT networking professionals continues to grow, and the
demand for specialized networking skills is growing even more rapidly.
Take a complimentary Learning@(protected)
about Cisco certifications, training, and career opportunities.
http://p.sf.net/sfu/cisco-dev2dev
_______________________________________________
FreeMarker-user mailing list
FreeMarker-user@(protected)
https://lists.sourceforge.net/lists/listinfo/freemarker-user
©2008 junlu.com - Jax Systems, LLC, U.S.A.