Package org.xulfaces.annotation.processor

Source Code of org.xulfaces.annotation.processor.VelocityTemplateEngine

/*
*   xulfaces : bring XUL power to Java
*  
*  Copyright (C) 2005  Olivier SCHMITT
*  This library is free software; you can redistribute it and/or
*  modify it under the terms of the GNU Lesser General Public
*  License as published by the Free Software Foundation; either
*  version 2.1 of the License, or (at your option) any later version.
*
*  This library 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
*  Lesser General Public License for more details.
*
*  You should have received a copy of the GNU Lesser General Public
*  License along with this library; if not, write to the Free Software
*  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/


package org.xulfaces.annotation.processor;

import java.io.Writer;
import java.util.Enumeration;
import java.util.ResourceBundle;

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



/**
*
* @author kito31
* @version $Id: VelocityTemplateEngine.java,v 1.3 2005/09/17 17:21:22 kito31 Exp $
*/
public class VelocityTemplateEngine  {

    public final String DEFAULT_CONFIG_RESOURCE = "org/xulfaces/annotation/processor/velocity";
    public final String DEFAULT_ENCODING = "ISO-8859-1";
    public final String DEFAULT_TEMPLATE_ROOT = "org/xulfaces/annotation/processor/template";
   
    private VelocityContext velocityContext;
   
   
    private String encoding;
    private String configurationFilename;
    private String templateRoot;
   
    public VelocityTemplateEngine(){}
   
    /**
     * @inheritDoc
     */
    public void renderTemplate(Writer writer, String templateFilename) throws Exception{
       
        String realname =  getTemplateDirectory() + templateFilename;
       
        if (Velocity.resourceExists(realname)) {
            Velocity.mergeTemplate(realname,DEFAULT_ENCODING,velocityContext, writer);
        } else {
            throw new Exception("Template not found : " + realname);
        }
    }

    /**
     * @inheritDoc
     */
    public void configure() throws Exception {

        ResourceBundle b = null;
        // Initialisation des propri�t� de Velocity
        if(this.configurationFilename == null){
            b = ResourceBundle.getBundle(DEFAULT_CONFIG_RESOURCE);
        }
        else {
            b = ResourceBundle.getBundle(getConfigurationFilename());
        }
       
        if(getTemplateDirectory() == null){
            setTemplateDirectory(DEFAULT_TEMPLATE_ROOT);
        }
        
        Enumeration eKeys = b.getKeys();
        while (eKeys.hasMoreElements()) {
            Object keyObj = eKeys.nextElement();
            if (keyObj instanceof String) {
                String key = (String) keyObj;
                Velocity.setProperty(key, b.getString(key));
            }
        }      
        Velocity.init();
    }

    /**
     * @inheritDoc
     */
    public void setContextValue(String key, Object value) {
        velocityContext.put(key,value);       
    }

    /**
     * @inheritDoc
     */
    public void prepareRender() {
       this.velocityContext = new VelocityContext();       
    }

   
    /**
     * @inheritDoc
     */
    public String getConfigurationFilename() {
        return configurationFilename;
    }

    /**
     * @inheritDoc
     */
    public void setConfigurationFilename(String configurationFilename) {
        this.configurationFilename = configurationFilename;
    }

    /**
     * @inheritDoc
     */
    public String getEncoding() {
        return encoding;
    }
   
    /**
     * @inheritDoc
     */
    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }

    /**
     * @return Returns the templateRoot.
     */
    public String getTemplateDirectory() {
        return templateRoot;
    }
    /**
     * @param templateRoot The templateRoot to set.
     */
    public void setTemplateDirectory(String templateRoot) {
        if(templateRoot != null){
            if(!templateRoot.endsWith("/")){
                templateRoot = templateRoot + "/";
            }
        }
        this.templateRoot = templateRoot;
    }

    /**
     * @inheritDoc
     */   
    public String getDefaultFileExtension() {
        return "vm";
    }   
}    
TOP

Related Classes of org.xulfaces.annotation.processor.VelocityTemplateEngine

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.