Hi,

In my code I use a small utility class to do something like
  org.apache.velocity.app.Velocity.evaluate()
but without parsing the template String every time I want to
render the template.

This class - appended to this message - is a simplification
from my code, was not tested and was made for a older
version of Velocity.

So, the questions are:
 - Is someone interested on this little thing?
 - Am I using the right classes, i.e.: should I really use
     org.apache.velocity.runtime.RuntimeInstance
   or should I use something else?

If you are interested, I will fix it according to the experts
remarks, test it and post it.


Have fun,
Paulo Gaspar


http://www.krankikom.de
http://www.ruhronline.de


import java.io.Writer;
import java.io.StringReader;
import java.io.Reader;
import java.io.StringWriter;

import org.apache.velocity.context.Context;
import org.apache.velocity.context.InternalContextAdapterImpl;
import org.apache.velocity.runtime.RuntimeInstance;
import org.apache.velocity.runtime.parser.node.SimpleNode;
import org.apache.velocity.runtime.parser.ParseException;


class StaticTemplate
{   private RuntimeInstance  m_runtimeInstance;
    private SimpleNode       m_nodeTree = null;
    private String           m_logTag;

    protected StaticTemplate(RuntimeInstance i_runtimeInstance, String
i_logTag, Reader i_reader)
    throws ParseException
    {   super();

        m_logTag          = i_logTag;
        m_runtimeInstance = i_runtimeInstance;
        m_nodeTree        = m_runtimeInstance.parse(i_reader, m_logTag);
    }

    protected StaticTemplate( RuntimeInstance i_runtimeInstance,
                              String i_logTag, String i_templateText
                            )
    throws ParseException
    {   this(i_runtimeInstance, i_logTag, new StringReader(i_templateText));
    }

    private void render(Writer o_writer, Context i_ctx)
    throws Exception
    {   InternalContextAdapterImpl ica = new
InternalContextAdapterImpl(i_ctx);
        ica.pushCurrentTemplateName(m_logTag);
        m_nodeTree.init(ica, m_runtimeInstance);

        try
        {   m_nodeTree.render(ica, o_writer);
        }
        finally
        {   ica.popCurrentTemplateName();
        }
    }

    public String render(Context i_ctx)
    throws Exception
    {   Writer wrt = new StringWriter();
        render(wrt, i_ctx);
        return wrt.toString();
    }
}


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to