Java Code Examples for org.apache.velocity.runtime.RuntimeSingleton

The following examples show how to use org.apache.velocity.runtime.RuntimeSingleton. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: sunbird-lms-service   Source File: OTPService.java    License: MIT License 6 votes vote down vote up
public static String getSmsBody(String templateFile, Map<String, String> smsTemplate) {
  try {
    String sms = getOTPSMSTemplate(templateFile);
    RuntimeServices rs = RuntimeSingleton.getRuntimeServices();
    SimpleNode sn = rs.parse(sms, "Sms Information");
    Template t = new Template();
    t.setRuntimeServices(rs);
    t.setData(sn);
    t.initDocument();
    VelocityContext context = new VelocityContext();
    context.put(JsonKey.OTP, smsTemplate.get(JsonKey.OTP));
    context.put(
        JsonKey.OTP_EXPIRATION_IN_MINUTES, smsTemplate.get(JsonKey.OTP_EXPIRATION_IN_MINUTES));
    context.put(
        JsonKey.INSTALLATION_NAME,
        ProjectUtil.getConfigValue(JsonKey.SUNBIRD_INSTALLATION_DISPLAY_NAME));

    StringWriter writer = new StringWriter();
    t.merge(context, writer);
    return writer.toString();
  } catch (Exception ex) {
    ProjectLogger.log(
        "OTPService:getSmsBody: Exception occurred with error message = " + ex.getMessage(), ex);
  }
  return "";
}
 
Example 2
Source Project: pippo   Source File: VelocityTemplateEngine.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void renderString(String templateContent, Map<String, Object> model, Writer writer) {
    // create the velocity context
    VelocityContext context = createVelocityContext(model);

    // merge the template
    try {
        RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
        StringReader reader = new StringReader(templateContent);
        SimpleNode node = runtimeServices.parse(reader, "StringTemplate");
        Template template = new Template();
        template.setRuntimeServices(runtimeServices);
        template.setData(node);
        template.initDocument();
        template.merge(context, writer);
    } catch (Exception e) {
        throw new PippoRuntimeException(e);
    }
}
 
Example 3
Source Project: velocity-engine   Source File: Velocity.java    License: Apache License 2.0 6 votes vote down vote up
/**
 *  Merges a template and puts the rendered stream into the writer
 *
 *  @param templateName name of template to be used in merge
 *  @param encoding encoding used in template
 *  @param context  filled context to be used in merge
 *  @param  writer  writer to write template into
 *
 *  @return true if successful, false otherwise.  Errors
 *           logged to velocity log
 *
 * @throws ParseErrorException The template could not be parsed.
 * @throws MethodInvocationException A method on a context object could not be invoked.
 * @throws ResourceNotFoundException A referenced resource could not be loaded.
 *
 * @since Velocity v1.1
 */
public static boolean mergeTemplate( String templateName, String encoding,
                                  Context context, Writer writer )
    throws ResourceNotFoundException, ParseErrorException, MethodInvocationException
{
    Template template = RuntimeSingleton.getTemplate(templateName, encoding);

    if ( template == null )
    {
        String msg = "Velocity.mergeTemplate() was unable to load template '"
                       + templateName + "'";
        getLog().error(msg);
        throw new ResourceNotFoundException(msg);
    }
    else
    {
        template.merge(context, writer);
        return true;
    }
}
 
Example 4
public void testAlternateStaticRepo() throws Exception
{
    VelocityEngine engine = newStringEngine("alternate.repo", true);
    // should be null be for init
    StringResourceRepository repo = getRepo("alternate.repo", null);
    assertNull(repo);
    engine.init();
    // and not null after init
    repo = getRepo("alternate.repo", null);
    assertNotNull(repo);
    repo.putStringResource("foo", "This is NOT $foo");

    // get and merge template with the same name from both runtimes with the same context
    String engineOut = render(engine.getTemplate("foo"));
    String singletonOut = render(RuntimeSingleton.getTemplate("foo"));

    // make sure they're NOT equal
    assertFalse(engineOut.equals(singletonOut));
}
 
Example 5
public void  testSimpleTemplate()
        throws Exception
{
    StringResourceLoader.getRepository().putStringResource("simpletemplate.vm", "This is a test for ${foo}");

    Template template = RuntimeSingleton.getTemplate(getFileName(null, "simpletemplate", TMPL_FILE_EXT));

    FileOutputStream fos =
        new FileOutputStream (
            getFileName(RESULTS_DIR, "simpletemplate", RESULT_FILE_EXT));

    Writer writer = new BufferedWriter(new OutputStreamWriter(fos));

    VelocityContext context = new VelocityContext();
    context.put("foo", "a foo object");

    template.merge(context, writer);
    writer.flush();
    writer.close();

    if (!isMatch(RESULTS_DIR, COMPARE_DIR, "simpletemplate",
                    RESULT_FILE_EXT, CMP_FILE_EXT))
    {
        fail("Output incorrect.");
    }
}
 
Example 6
Source Project: AuTe-Framework   Source File: VelocityTransformer.java    License: Apache License 2.0 5 votes vote down vote up
private String render(final String stringTemplate) throws ParseException {
    RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
    runtimeServices.setProperty("userdirective", GroovyDirective.class.getName());
    StringReader reader = new StringReader(stringTemplate);
    SimpleNode node = runtimeServices.parse(reader, "Template name");
    Template template = new Template();
    template.setEncoding("UTF-8");
    template.setRuntimeServices(runtimeServices);
    template.setData(node);
    template.initDocument();

    StringWriter writer = new StringWriter();
    template.merge(velocityContext, writer);
    return String.valueOf(writer.getBuffer());
}
 
Example 7
private String getRenderedBodyFromFile(final ResponseDefinition response) throws ParseException {
    RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
    StringReader reader = new StringReader(response.getBody());
    SimpleNode node = runtimeServices.parse(reader, "Template name");
    Template template = new Template();
    template.setEncoding("UTF-8");
    template.setRuntimeServices(runtimeServices);
    template.setData(node);
    template.initDocument();

    StringWriter writer = new StringWriter();
    template.merge(context, writer);
    return String.valueOf(writer.getBuffer());
}
 
Example 8
Source Project: iotplatform   Source File: VelocityUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static Template create(String source, String templateName) throws ParseException {
  RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
  StringReader reader = new StringReader(source);
  SimpleNode node = runtimeServices.parse(reader, templateName);
  Template template = new Template();
  template.setRuntimeServices(runtimeServices);
  template.setData(node);
  template.initDocument();
  return template;
}
 
Example 9
Source Project: RetroFacebook   Source File: TemplateVarsTest.java    License: Apache License 2.0 5 votes vote down vote up
static SimpleNode parsedTemplateForString(String string) {
  try {
    Reader reader = new StringReader(string);
    return RuntimeSingleton.parse(reader, string);
  } catch (ParseException e) {
    throw new AssertionError(e);
  }
}
 
Example 10
Source Project: RetroFacebook   Source File: TemplateVarsTest.java    License: Apache License 2.0 5 votes vote down vote up
static SimpleNode parsedTemplateForString(String string) {
  try {
    Reader reader = new StringReader(string);
    return RuntimeSingleton.parse(reader, string);
  } catch (ParseException e) {
    throw new AssertionError(e);
  }
}
 
Example 11
Source Project: mybaties   Source File: VelocitySqlSource.java    License: Apache License 2.0 5 votes vote down vote up
public VelocitySqlSource(Configuration configuration, String scriptText) {
  this.configuration = configuration;
  try {
    RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
    StringReader reader = new StringReader(scriptText);
    SimpleNode node = runtimeServices.parse(reader, "Template name");
    script = new Template();
    script.setRuntimeServices(runtimeServices);
    script.setData(node);
    script.initDocument();
  } catch (Exception ex) {
    throw new BuilderException("Error parsing velocity script", ex);
  }
}
 
Example 12
Source Project: SimpleWeibo   Source File: TemplateVarsTest.java    License: Apache License 2.0 5 votes vote down vote up
static SimpleNode parsedTemplateForString(String string) {
  try {
    Reader reader = new StringReader(string);
    return RuntimeSingleton.parse(reader, string);
  } catch (ParseException e) {
    throw new AssertionError(e);
  }
}
 
Example 13
Source Project: mybatis   Source File: VelocitySqlSource.java    License: Apache License 2.0 5 votes vote down vote up
public VelocitySqlSource(Configuration configuration, String scriptText) {
  this.configuration = configuration;
  try {
    RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
    StringReader reader = new StringReader(scriptText);
    SimpleNode node = runtimeServices.parse(reader, "Template name");
    script = new Template();
    script.setRuntimeServices(runtimeServices);
    script.setData(node);
    script.initDocument();
  } catch (Exception ex) {
    throw new BuilderException("Error parsing velocity script", ex);
  }
}
 
Example 14
Source Project: velocity-engine   Source File: DBContextTest.java    License: Apache License 2.0 5 votes vote down vote up
public DBContextTest(String templateFile)
{
    try
    {
        RuntimeSingleton.init( new Properties() );

        Template template = RuntimeSingleton.getTemplate(templateFile);

        DBContext dbc = new DBContext();

        Hashtable h = new Hashtable();
        h.put("Bar", "this is from a hashtable!");

        dbc.put( "string", "Hello!");
        dbc.put( "hashtable", h );

        Writer writer = new BufferedWriter(new OutputStreamWriter(System.out));

        template.merge(dbc, writer);

        writer.flush();
        writer.close();
    }
    catch( Exception e )
    {
        RuntimeSingleton.getLog().error("Something funny happened", e);
    }
}
 
Example 15
public void testSharedRepo() throws Exception
{
    // this engine's string resource loader should share a repository
    // with the singleton's string resource loader
    VelocityEngine engine = newStringEngine(null, true);

    // get and merge the same template from both runtimes with the same context
    String engineOut = render(engine.getTemplate("foo"));
    String singletonOut = render(RuntimeSingleton.getTemplate("foo"));

    // make sure they're equal
    assertEquals(engineOut, singletonOut);
}
 
Example 16
Source Project: velocity-engine   Source File: TemplateNodeView.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Default constructor: sets up the Velocity
 * Runtime, creates the visitor for traversing
 * the node structure and then produces the
 * visual representation by the visitation.
 */
public TemplateNodeView(String templateFile)
{
    try
    {
        RuntimeSingleton.init("velocity.properties");

        InputStreamReader isr = new InputStreamReader(
                                   new FileInputStream(templateFile),
                                   RuntimeSingleton.getString(RuntimeSingleton.INPUT_ENCODING));

        BufferedReader br = new BufferedReader( isr );

        Template tmpl = new Template();
        tmpl.setName(templateFile);
        document = RuntimeSingleton.parse( br, tmpl);

        visitor = new NodeViewMode();
        visitor.setContext(null);
        visitor.setWriter(new PrintWriter(System.out));
        document.jjtAccept(visitor, null);
    }
    catch (Exception e)
    {
        System.out.println(e);
        e.printStackTrace();
    }
}
 
Example 17
Source Project: velocity-engine   Source File: TemplateTestCase.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Runs the test.
 */
public void runTest ()
    throws Exception
{
    Template template = RuntimeSingleton.getTemplate
        (getFileName(null, baseFileName, TMPL_FILE_EXT));

    assureResultsDirectoryExists(RESULT_DIR);

    /* get the file to write to */
    FileOutputStream fos =
        new FileOutputStream (getFileName(
            RESULT_DIR, baseFileName, RESULT_FILE_EXT));

    Writer writer = new BufferedWriter(new OutputStreamWriter(fos));

    /* process the template */
    template.merge( context, writer);

    /* close the file */
    writer.flush();
    writer.close();

    if (!isMatch(RESULT_DIR,COMPARE_DIR,baseFileName,
            RESULT_FILE_EXT,CMP_FILE_EXT))
    {
        fail("Processed template "+getFileName(
            RESULT_DIR, baseFileName, RESULT_FILE_EXT)+" did not match expected output");
    }
}
 
Example 18
Source Project: velocity-engine   Source File: Velocity580TestCase.java    License: Apache License 2.0 5 votes vote down vote up
protected Template executeTest(final String templateName) throws Exception
{
    Template template = RuntimeSingleton.getTemplate(templateName);

    FileOutputStream fos = new FileOutputStream(getFileName(RESULTS_DIR, templateName, RESULT_FILE_EXT));

    Writer writer = new BufferedWriter(new OutputStreamWriter(fos));

    VelocityContext context = new VelocityContext();

    template.merge(context, writer);
    writer.flush();
    writer.close();

    if (!isMatch(RESULTS_DIR, COMPARE_DIR, templateName, RESULT_FILE_EXT, CMP_FILE_EXT))
    {
        // just to be useful, output the output in the fail message
        StringWriter out = new StringWriter();
        template.merge(context, out);

        String compare = getFileContents(COMPARE_DIR, templateName, CMP_FILE_EXT);

        fail("Output incorrect for Template: " + templateName + ": \""+out+"\""+
             "; it did not match: \""+compare+"\"");
    }

    return template;
}
 
Example 19
/**
 * Runs the test.
 */
public void testResourceLoaderInstance ()
        throws Exception
{
    assureResultsDirectoryExists(RESULTS_DIR);

    Template template = RuntimeSingleton.getTemplate(
            getFileName(null, "testfile", TMPL_FILE_EXT));

    FileOutputStream fos =
            new FileOutputStream (
                    getFileName(RESULTS_DIR, "testfile", RESULT_FILE_EXT));

    Writer writer = new BufferedWriter(new OutputStreamWriter(fos));

    /*
     *  put the Vector into the context, and merge both
     */

    VelocityContext context = new VelocityContext();

    template.merge(context, writer);
    writer.flush();
    writer.close();

    if ( !isMatch(RESULTS_DIR, COMPARE_DIR, "testfile",
                    RESULT_FILE_EXT, CMP_FILE_EXT) )
    {
        String result = getFileContents(RESULT_DIR, "testfile", RESULT_FILE_EXT);
        String compare = getFileContents(COMPARE_DIR, "testfile", CMP_FILE_EXT);

        String msg = "Processed template did not match expected output\n"+
            "-----Result-----\n"+ result +
            "----Expected----\n"+ compare +
            "----------------";

        fail(msg);
    }
}
 
Example 20
Source Project: consulo   Source File: VelocityWrapper.java    License: Apache License 2.0 4 votes vote down vote up
static SimpleNode parse(Reader reader, String templateName) throws ParseException {
  Template template = new Template();
  template.setName(templateName);
  return RuntimeSingleton.parse(reader, template);
}
 
Example 21
Source Project: velocity-engine   Source File: Velocity.java    License: Apache License 2.0 4 votes vote down vote up
/**
 *  initialize the Velocity runtime engine, using the default
 *  properties of the Velocity distribution
 */
public static void init()
{
    RuntimeSingleton.init();
}
 
Example 22
/**
 * Runs the test.
 */
public void  testMultipleFileResources ()
        throws Exception
{
    Template template1 = RuntimeSingleton.getTemplate(
        getFileName(null, "path1", TMPL_FILE_EXT));

    Template template2 = RuntimeSingleton.getTemplate(
        getFileName(null, "path2", TMPL_FILE_EXT));

    FileOutputStream fos1 =
        new FileOutputStream (
            getFileName(RESULTS_DIR, "path1", RESULT_FILE_EXT));

    FileOutputStream fos2 =
        new FileOutputStream (
            getFileName(RESULTS_DIR, "path2", RESULT_FILE_EXT));

    Writer writer1 = new BufferedWriter(new OutputStreamWriter(fos1));
    Writer writer2 = new BufferedWriter(new OutputStreamWriter(fos2));

    /*
     *  put the Vector into the context, and merge both
     */

    VelocityContext context = new VelocityContext();

    template1.merge(context, writer1);
    writer1.flush();
    writer1.close();

    template2.merge(context, writer2);
    writer2.flush();
    writer2.close();

    if (!isMatch(RESULTS_DIR, COMPARE_DIR, "path1",
            RESULT_FILE_EXT, CMP_FILE_EXT) ||
        !isMatch(RESULTS_DIR, COMPARE_DIR, "path2",
            RESULT_FILE_EXT, CMP_FILE_EXT))
    {
        fail("Output incorrect.");
    }
}
 
Example 23
Source Project: velocity-engine   Source File: InlineScopeVMTestCase.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Runs the test.
 */
public void testInlineScopeVM ()
        throws Exception
{
    assureResultsDirectoryExists(RESULT_DIR);

    /*
     * Get the template and the output. Do them backwards.
     * vm_test2 uses a local VM and vm_test1 doesn't
     */

    Template template2 = RuntimeSingleton.getTemplate(
        getFileName(null, "vm_test2", TMPL_FILE_EXT));

    Template template1 = RuntimeSingleton.getTemplate(
        getFileName(null, "vm_test1", TMPL_FILE_EXT));

    FileOutputStream fos1 =
        new FileOutputStream (
            getFileName(RESULT_DIR, "vm_test1", RESULT_FILE_EXT));

    FileOutputStream fos2 =
        new FileOutputStream (
            getFileName(RESULT_DIR, "vm_test2", RESULT_FILE_EXT));

    Writer writer1 = new BufferedWriter(new OutputStreamWriter(fos1));
    Writer writer2 = new BufferedWriter(new OutputStreamWriter(fos2));

    /*
     *  put the Vector into the context, and merge both
     */

    VelocityContext context = new VelocityContext();

    template1.merge(context, writer1);
    writer1.flush();
    writer1.close();

    template2.merge(context, writer2);
    writer2.flush();
    writer2.close();

    if (!isMatch(RESULT_DIR,COMPARE_DIR,"vm_test1",
            RESULT_FILE_EXT,CMP_FILE_EXT) ||
        !isMatch(RESULT_DIR,COMPARE_DIR,"vm_test2",
            RESULT_FILE_EXT,CMP_FILE_EXT))
    {
        fail("Output incorrect.");
    }
}
 
Example 24
Source Project: velocity-engine   Source File: ContextSafetyTestCase.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Runs the test.
 */
public void testContextSafety ()
    throws Exception
{
    /*
     *  make a Vector and String array because
     *  they are treated differently in Foreach()
     */
    Vector v = new Vector();

    v.addElement( new String("vector hello 1") );
    v.addElement( new String("vector hello 2") );
    v.addElement( new String("vector hello 3") );

    String strArray[] = new String[3];

    strArray[0] = "array hello 1";
    strArray[1] = "array hello 2";
    strArray[2] = "array hello 3";

    VelocityContext context = new VelocityContext();

    assureResultsDirectoryExists(RESULT_DIR);

    /*
     *  get the template and the output
     */

    Template template = RuntimeSingleton.getTemplate(
        getFileName(null, "context_safety", TMPL_FILE_EXT));

    FileOutputStream fos1 =
        new FileOutputStream (
            getFileName(RESULT_DIR, "context_safety1", RESULT_FILE_EXT));

    FileOutputStream fos2 =
        new FileOutputStream (
            getFileName(RESULT_DIR, "context_safety2", RESULT_FILE_EXT));

    Writer writer1 = new BufferedWriter(new OutputStreamWriter(fos1));
    Writer writer2 = new BufferedWriter(new OutputStreamWriter(fos2));

    /*
     *  put the Vector into the context, and merge
     */

    context.put("vector", v);
    template.merge(context, writer1);
    writer1.flush();
    writer1.close();

    /*
     *  now put the string array into the context, and merge
     */

    context.put("vector", strArray);
    template.merge(context, writer2);
    writer2.flush();
    writer2.close();

    if (!isMatch(RESULT_DIR,COMPARE_DIR,"context_safety1",
            RESULT_FILE_EXT,CMP_FILE_EXT) ||
        !isMatch(RESULT_DIR,COMPARE_DIR,"context_safety2",
            RESULT_FILE_EXT,CMP_FILE_EXT))
    {
        fail("Output incorrect.");
    }
}
 
Example 25
Source Project: velocity-engine   Source File: ClasspathResourceTestCase.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Runs the test.
 */
public void testClasspathResource ()
        throws Exception
{
    /*
     *  lets ensure the results directory exists
     */
    assureResultsDirectoryExists(RESULTS_DIR);

    Template template1 = RuntimeSingleton.getTemplate("/includeevent/test1-cp." + TMPL_FILE_EXT);

    // Uncomment when http://jira.codehaus.org/browse/MPTEST-57 has been resolved
    //            Template template2 = RuntimeSingleton.getTemplate(
    //                getFileName(null, "template/test2", TMPL_FILE_EXT));

    FileOutputStream fos1 =
        new FileOutputStream (
            getFileName(RESULTS_DIR, "test1", RESULT_FILE_EXT));

    // Uncomment when http://jira.codehaus.org/browse/MPTEST-57 has been resolved
    //            FileOutputStream fos2 =
    //                new FileOutputStream (
    //                    getFileName(RESULTS_DIR, "test2", RESULT_FILE_EXT));

    Writer writer1 = new BufferedWriter(new OutputStreamWriter(fos1));
    // Uncomment when http://jira.codehaus.org/browse/MPTEST-57 has been resolved
    //            Writer writer2 = new BufferedWriter(new OutputStreamWriter(fos2));

    /*
     *  put the Vector into the context, and merge both
     */

    VelocityContext context = new VelocityContext();

    template1.merge(context, writer1);
    writer1.flush();
    writer1.close();

    // Uncomment when http://jira.codehaus.org/browse/MPTEST-57 has been resolved
    //            template2.merge(context, writer2);
    //            writer2.flush();
    //            writer2.close();

    if (!isMatch(RESULTS_DIR,COMPARE_DIR,"test1",RESULT_FILE_EXT,CMP_FILE_EXT)
            // Uncomment when http://jira.codehaus.org/browse/MPTEST-57 has been resolved
            //                || !isMatch(RESULTS_DIR,COMPARE_DIR,"test2",RESULT_FILE_EXT,CMP_FILE_EXT)
        )
    {
        fail("Output is incorrect!");
    }
}
 
Example 26
/**
 * Runs the test.
 */
public void testIncludeEventHandling ()
        throws Exception
{
    Template template1 = RuntimeSingleton.getTemplate(
        getFileName(null, "test1", TMPL_FILE_EXT));

    Template template2 = RuntimeSingleton.getTemplate(
        getFileName(null, "subdir/test2", TMPL_FILE_EXT));

    Template template3 = RuntimeSingleton.getTemplate(
        getFileName(null, "test3", TMPL_FILE_EXT));

    FileOutputStream fos1 =
        new FileOutputStream (
            getFileName(RESULTS_DIR, "test1", RESULT_FILE_EXT));

    FileOutputStream fos2 =
        new FileOutputStream (
            getFileName(RESULTS_DIR, "test2", RESULT_FILE_EXT));

    FileOutputStream fos3 =
        new FileOutputStream (
            getFileName(RESULTS_DIR, "test3", RESULT_FILE_EXT));

    Writer writer1 = new BufferedWriter(new OutputStreamWriter(fos1));
    Writer writer2 = new BufferedWriter(new OutputStreamWriter(fos2));
    Writer writer3 = new BufferedWriter(new OutputStreamWriter(fos3));

    /*
     *  lets make a Context and add the event cartridge
     */

    Context context = new VelocityContext();

    /*
     *  Now make an event cartridge, register the
     *  input event handler and attach it to the
     *  Context
     */

    EventCartridge ec = new EventCartridge();
    ec.addEventHandler(this);
    ec.attachToContext( context );


    // BEHAVIOR A: pass through #input and #parse with no change
    EventHandlerBehavior = PASS_THROUGH;

    template1.merge(context, writer1);
    writer1.flush();
    writer1.close();

    // BEHAVIOR B: pass through #input and #parse with using a relative path
    EventHandlerBehavior = RELATIVE_PATH;

    template2.merge(context, writer2);
    writer2.flush();
    writer2.close();

    // BEHAVIOR C: refuse to pass through #input and #parse
    EventHandlerBehavior = BLOCK;

    template3.merge(context, writer3);
    writer3.flush();
    writer3.close();

    if (!isMatch(RESULTS_DIR, COMPARE_DIR, "test1",
            RESULT_FILE_EXT, CMP_FILE_EXT) ||
        !isMatch(RESULTS_DIR, COMPARE_DIR, "test2",
            RESULT_FILE_EXT, CMP_FILE_EXT) ||
        !isMatch(RESULTS_DIR, COMPARE_DIR, "test3",
            RESULT_FILE_EXT, CMP_FILE_EXT)
            )
    {
        fail("Output incorrect.");
    }
}
 
Example 27
Source Project: velocity-engine   Source File: Velocity.java    License: Apache License 2.0 3 votes vote down vote up
/**
 *  renders the input string using the context into the output writer.
 *  To be used when a template is dynamically constructed, or want to use
 *  Velocity as a token replacer.
 *
 *  @param context context to use in rendering input string
 *  @param out  Writer in which to render the output
 *  @param logTag  string to be used as the template name for log
 *                 messages in case of error
 *  @param instring input string containing the VTL to be rendered
 *
 *  @return true if successful, false otherwise.  If false, see
 *             Velocity runtime log
 * @throws ParseErrorException The template could not be parsed.
 * @throws MethodInvocationException A method on a context object could not be invoked.
 * @throws ResourceNotFoundException A referenced resource could not be loaded.
 */
public static  boolean evaluate( Context context,  Writer out,
                                 String logTag, String instring )
    throws ParseErrorException, MethodInvocationException,
        ResourceNotFoundException
{
    return RuntimeSingleton.getRuntimeServices()
        .evaluate(context, out, logTag, instring);
}
 
Example 28
Source Project: velocity-engine   Source File: Velocity.java    License: Apache License 2.0 3 votes vote down vote up
/**
 *  Renders the input reader using the context into the output writer.
 *  To be used when a template is dynamically constructed, or want to
 *  use Velocity as a token replacer.
 *
 *  @param context context to use in rendering input string
 *  @param writer  Writer in which to render the output
 *  @param logTag  string to be used as the template name for log messages
 *                 in case of error
 *  @param reader Reader containing the VTL to be rendered
 *
 *  @return true if successful, false otherwise.  If false, see
 *               Velocity runtime log
 * @throws ParseErrorException The template could not be parsed.
 * @throws MethodInvocationException A method on a context object could not be invoked.
 * @throws ResourceNotFoundException A referenced resource could not be loaded.
 *  @since Velocity v1.1
 */
public static boolean evaluate( Context context, Writer writer,
                                String logTag, Reader reader )
    throws ParseErrorException, MethodInvocationException,
        ResourceNotFoundException
{
    return RuntimeSingleton.getRuntimeServices().evaluate(context, writer,
                                                          logTag, reader);
}
 
Example 29
Source Project: velocity-engine   Source File: Velocity.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Invokes a currently registered Velocimacro with the params provided
 * and places the rendered stream into the writer.
 * <br>
 * Note : currently only accepts args to the VM if they are in the context.
 *
 * @param vmName name of Velocimacro to call
 * @param logTag string to be used for template name in case of error. if null,
 *               the vmName will be used
 * @param params keys for args used to invoke Velocimacro, in java format
 *               rather than VTL (eg  "foo" or "bar" rather than "$foo" or "$bar")
 * @param context Context object containing data/objects used for rendering.
 * @param writer  Writer for output stream
 * @return true if Velocimacro exists and successfully invoked, false otherwise.
 */
public static  boolean invokeVelocimacro( String vmName, String logTag,
                                          String params[], Context context,
                                          Writer writer )
{
    return RuntimeSingleton.getRuntimeServices()
        .invokeVelocimacro(vmName, logTag, params, context, writer);
}
 
Example 30
Source Project: velocity-engine   Source File: Velocity.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Resets the instance, so Velocity can be re-initialized again.
 *
 * @since 2.0.0
 */
public static void reset()
{
    RuntimeSingleton.reset();
}