Hi, Preetam,

Here is my sample code to use Velocity offline:

import java.io.File;
import java.io.OutputStreamWriter;
import java.io.StringWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context;
import org.apache.velocity.runtime.RuntimeConstants;

import util.Util;

public class VelocityParser
{
    private static VelocityEngine ve;

    static
    {
        try
        {
            File templateDir = new File("mytempdir");

            ve = new VelocityEngine();

            // setting up file resource loader
            ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
templateDir.getCanonicalPath());

            // Jian: this is very important, otherwise, there will be out of
memory error when a template
            // with #foreach loop is called and the #foreach loop iterates
through million of times
            ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE,
"true");
            //
ve.setProperty("file.resource.loader.modificationCheckInterval", "5");

            // velocimacro configuration
            ve.setProperty(RuntimeConstants.VM_LIBRARY, "macro/spring.vm");

            // this setting is such that changing inline velocity macro will
take effect
            // WITHOUT restarting the web server!

ve.setProperty(RuntimeConstants.VM_PERM_ALLOW_INLINE_REPLACE_GLOBAL,
"true");

            ve.setProperty(RuntimeConstants.VM_LIBRARY_AUTORELOAD, "true");

            ve.setProperty(RuntimeConstants.COUNTER_INITIAL_VALUE, "0");
            ve.setProperty(RuntimeConstants.PARSER_POOL_SIZE, "1");

            // comment this out if we want to enable logging
            ve.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
"org.apache.velocity.runtime.log.NullLogSystem");

            ve.setProperty(RuntimeConstants.RUNTIME_LOG_ERROR_STACKTRACE,
"false");
            ve.setProperty(RuntimeConstants.RUNTIME_LOG_INFO_STACKTRACE,
"false");
            ve.setProperty(RuntimeConstants.RUNTIME_LOG_WARN_STACKTRACE,
"false");

ve.setProperty(RuntimeConstants.RUNTIME_LOG_REFERENCE_LOG_INVALID, "true");

            ve.setProperty(RuntimeConstants.PARSE_DIRECTIVE_MAXDEPTH, "10");

            // Jian: this is very important, otherwise, there will be out of
memory error when a template
            // with #foreach loop is called and the #foreach loop iterates
through million of times
            ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE,
"true");

            ve.init();
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }

    public static String evaluate(File templateFile, Context ctx) throws
Exception
    {
        return evaluate(Util.readFileAsString(templateFile, "UTF-8"), ctx);
    }

    public static String evaluate(String inputStr, Context ctx) throws
Exception
    {
        StringWriter sw = new StringWriter();
        ve.evaluate(ctx, sw, "velolog", inputStr);
        return sw.toString();
    }
}

It is part of my code as I have something more relating to servlet too. So,
you might want to modify and adapt to your purpose.

Cheers,

Jian
http://www.JiansNet.com



On Sun, Mar 15, 2009 at 6:42 PM, Preetam Palwe <[email protected]> wrote:

> Thanks Byron
>
> Yes I have all UTF-8 encodded templates!
> My problem is I am to passing java.util.Locale with me using this I want to
> load proper velocity template.
> One simple way is as I mentioned earlier:
>
> if (locale == null)
>    {
>        locale = Locale.getDefault();
>    }
>
> String templateFile =  "xyz.vm"+ "." + locale.toString().toLowerCase();
>
> // Use templateFile e.g. xyz.vm.en_us or xyz.vm.de
>
>
> Now I was thinking whether I can use Velocity tools for the same. (Please
> note: I am in offline mode: no servlets, struts etc)
>
>
>
> Thanks
>
>
>
>
> -----Original Message-----
> From: Byron Foster [mailto:[email protected]]
> Sent: Saturday, March 14, 2009 6:39 PM
> To: Velocity Users List
> Subject: Re: Does velocity engine supports I18N ?
>
> Couldn't you always maintain your template files in UTF-8, then always
> merge in UTF-8 and send out the email in UTF-8.  Unless I'm not
> understanding the problem.  I'm not sure what a I18N tool would do for
> you, unless this is more of an issue of date and number formatting.
>
>
> On Mar 14, 2009, at 0:35 , Preetam Palwe wrote:
>
> > Precisely I am trying to use Velocity engine as a offline tool to send
> > emails.
> > I have an option or workaround to keep Locale specific velocity
> > templates
> > like "body.vm.de" , "body.vm.en_us" etc.
> > But I was wondering whether there is an inbuilt support in velocity
> > for
> > I18N?
> >
> > Jian, If you could please share the your parser code which enables
> > Velocity
> > I18N in offline mode to the mailing list, it would be great!
> > I had read about "Multiviews" in velocity tools but looks like I
> > can't use
> > it in offline mode.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>

Reply via email to