Java Code Examples for org.apache.velocity.runtime.RuntimeSingleton#getTemplate()

The following examples show how to use org.apache.velocity.runtime.RuntimeSingleton#getTemplate() . 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 File: Velocity.java    From velocity-engine with 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 2
Source File: StringResourceLoaderTestCase.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
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 3
Source File: DBContextTest.java    From velocity-engine with 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 4
Source File: TemplateTestCase.java    From velocity-engine with 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 5
Source File: Velocity580TestCase.java    From velocity-engine with 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 6
Source File: ResourceLoaderInstanceTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
/**
 * 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 7
Source File: MultipleFileResourcePathTestCase.java    From velocity-engine with Apache License 2.0 4 votes vote down vote up
/**
 * 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 8
Source File: InlineScopeVMTestCase.java    From velocity-engine with 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 9
Source File: ContextSafetyTestCase.java    From velocity-engine with 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 10
Source File: ClasspathResourceTestCase.java    From velocity-engine with 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 11
Source File: IncludeEventHandlingTestCase.java    From velocity-engine with Apache License 2.0 4 votes vote down vote up
/**
 * 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 12
Source File: Velocity.java    From velocity-engine with Apache License 2.0 2 votes vote down vote up
/**
 *  Returns a <code>Template</code> from the Velocity
 *  resource management system.
 *
 * @param name The file name of the desired template.
 * @return     The template.
 * @throws ResourceNotFoundException if template not found
 *          from any available source.
 * @throws ParseErrorException if template cannot be parsed due
 *          to syntax (or other) error.
 */
public static Template getTemplate(String name)
    throws ResourceNotFoundException, ParseErrorException
{
    return RuntimeSingleton.getTemplate( name );
}
 
Example 13
Source File: Velocity.java    From velocity-engine with Apache License 2.0 2 votes vote down vote up
/**
 *  Returns a <code>Template</code> from the Velocity
 *  resource management system.
 *
 * @param name The file name of the desired template.
 * @param encoding The character encoding to use for the template.
 * @return     The template.
 * @throws ResourceNotFoundException if template not found
 *          from any available source.
 * @throws ParseErrorException if template cannot be parsed due
 *          to syntax (or other) error.
 *
 *  @since Velocity v1.1
 */
public static Template getTemplate(String name, String encoding)
    throws ResourceNotFoundException, ParseErrorException
{
    return RuntimeSingleton.getTemplate( name, encoding );
}