TemplateEvaluator.java

/*
 * Copyright 2007 Roberto Fasciolo
 * 
 * This file is part of vsitegen.
 * 
 * vsitegen is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * vsitegen is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with vsitegen; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
package net.robyf.vsitegen.engine;

import java.io.StringWriter;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Properties;

import net.robyf.vsitegen.model.Navigation;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

public final class TemplateEvaluator {

    private final VelocityEngine velocity;

    private final String projectName;

    private final Navigation navigation;

    public TemplateEvaluator(final String projectName,
            final Navigation navigation,
            final String templatePath) {
        this.projectName = projectName;
        this.navigation = navigation;

        Properties velocityProperties = new Properties();

        // Two resource loaders, one for ant task and one for unit tests.
        velocityProperties.put("resource.loader", "file, class");

        // File resource loader for ant task.
        velocityProperties.put("file.resource.loader.class",
                               "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
        velocityProperties.put("file.resource.loader.path", templatePath);

        // Classpath resource loader for unit tests.
        velocityProperties.put("class.resource.loader.class",
                           "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");

        this.velocity = new VelocityEngine();
        try {
            this.velocity.init(velocityProperties);
        } catch (Exception e) {
            throw new EvaluatorException("Error initializing velocity", e);
        }
    }

    public String evaluate(final String template, final String pageId, final String content) {
        VelocityContext context = new VelocityContext();
        context.put("projectName", this.projectName);
        context.put("navigation", this.navigation);
        context.put("pageId", pageId);
        context.put("content", content);
        context.put("generationTime", DateFormat.getDateTimeInstance(DateFormat.LONG,
                                                                     DateFormat.FULL,
                                                                     Locale.ENGLISH)
                .format(new Date()));

        StringWriter writer = new StringWriter();

        try {
            this.velocity.mergeTemplate(template, "ISO-8859-1", context, writer);
        } catch (Exception e) {
            throw new EvaluatorException("Error merging template", e);
        }

        return writer.toString();
    }

}