About Templates Tags Driver Download

Templator drivers

'Driver' is a Java class that defines any extra variables for the template. This class then invokes the Templator which produces the output.

Example 'driver'

Contents of examples/Test.java

import java.util.*;
import java.io.*;

import org.tito.templator.*;
import org.tito.templator.events.*;


public class Test implements TemplatorListener {

    public static void main(String arg[]) {
        new Test().run(arg);
    }

    private void run(String arg[]) {

        System.setProperty("templator.destDir", "generated");

        try {
            Templator templ=new Templator();
            templ.addListener(this);
            Hashtable vars=new Hashtable();
            vars.put("name", "Test");
            vars.put("comment", "Hello World!");
            templ.setVariables(vars);
            templ.translate(new File(arg[0]), new File(arg[1]));

        } catch (TemplatorException te) {
            if (te.getException()!=null)
                te.getException().printStackTrace();
            else
                te.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void templatorFileCreated(FileCreationEvent ev) {
        System.out.println("TEMPL: File Created "+ev.getFilename());
    }
    public void templatorProcessingStarted(StartProcessingEvent ev) {
        System.out.println("TEMPL: Processing "+ev.getClassname());
    }

    public void templatorCompileStarted(StartCompileEvent ev) {
        System.out.println("TEMPL: Compiling "+ev.getFilename());
    }
}

Understanding 'drivers'

First, a driver should specify Templator's working directory.

       System.setProperty("templator.destDir", "generated");

This insures that no generated templates will be accidentally overwritten.

'Driver' adds extra variables to Templator. These variables are used during actual generation of output.

        Hashtable vars=new Hashtable();
        vars.put("name", "Test");
        vars.put("comment", "Hello World!");
        templ.setVariables(vars);

'Driver' should then invoke the Templator object

        templ.translate(templatefile, outputfile);