Re: Need to validate ftl before saving in DB
Daniel Dekany <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Monday, February 15, 2010, 10:21:47 AM, rishabh9 wrote:
>
> Hi,
>
> I use the below code to validate a ftl string I get from the UI, before
> saving in the DB. If an exception is thrown, I know there is a validation
> error and I report it accordingly to the UI.
>
> ================= BEGIN CODE ======================
>
> Configuration config = new Configuration();
> config.setStrictSyntaxMode(true);
> config.setTagSyntax(Configuration.AUTO_DETECT_TAG_SYNTAX);
>
> StringTemplateLoader stringLoader = new StringTemplateLoader();
> stringLoader.putTemplate(name, content);
> config.setTemplateLoader(stringLoader);
>
> try {
> config.getTemplate(name);
> } catch (IOException e) {
> LOG.error("The template's body is malformed.");
> throw new CustomException("The template body is malformed.", e);
> }
>
> =================== END CODE =====================
>
> My problem is that, the above code seems to work partly. It does not
> validate the directives.
> For example, even if I miss out on closing a directive, the
> "config.getTemplate(name)" does not throw an exception -
> <#list animals as being>
> ${being.name}
You mean, if the </#list> is missing, getTemplate doesn't throw
exception? Double-check that... it meant to throw exception in that
case:
freemarker.core.ParseException: Unexpected end of file reached.
Unclosed list directive.
> An exception is thrown, only when a variable, like - "${being.name}" is
> typed erroneously; something like - "${being"
>
> How can I validate the the directives?
The *syntax* of directives (and of everything) is always checked by
getTemplate. In general, all Template objects correspond to a
syntactically correct template, since they store the template as
abstract syntax tree (not as text), so they are technically unable to
store syntactically wrong templates.
Note that typos in variable *names* (including user-defined directive
names, the <@...> stuff) are not checked by getTemplate; they only
cause error when the template is evaluated (means: executed, generate
the output) with Template.process(...) or Environment.process(...).
Also, bad paths is #include/#import are not validated until the
template is evaluated, since unlike in C, those are runtime
instructions in FreeMarker.
> Also, Is this the correct way to completely validate a template?
Yes, that does the most comprehensive validation available, apart from
actually evaluating the template, as I pointed out earlier. Note that
the Template object returned by getTemplate will be cached and so it
will consume RAM even if you don't want to get it again in the
foreseeable future. If that's a problem, you can always purge the
cache:
http://freemarker.org/docs/api/freemarker/template/Configuration.html#clearTemplateCache%28%29
> I am using freemarker version 2.3.13
>
> Regards,
> Rishabh
--
Best regards,
Daniel Dekany
------------------------------------------------------------------------------
SOLARIS 10 is the OS for Data Centers - provides features such as DTrace,
Predictive Self Healing and Award Winning ZFS. Get Solaris 10 NOW
http://p.sf.net/sfu/solaris-dev2dev