com.qatickets.service.VelocityTemplatesService.java Source code

Java tutorial

Introduction

Here is the source code for com.qatickets.service.VelocityTemplatesService.java

Source

/*
The MIT License (MIT)
    
Copyright (c) 2015 QAXML Pty Ltd
    
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
    
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
    
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package com.qatickets.service;

import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.context.Context;
import org.springframework.stereotype.Service;

import com.qatickets.common.StringHelper;

@Service
public class VelocityTemplatesService {

    private static final Log log = LogFactory.getLog(VelocityTemplatesService.class);

    static {

        try {

            final Properties prop = new Properties();
            prop.load(VelocityTemplatesService.class.getResourceAsStream("/velocity.properties"));

            Velocity.init(prop);

            log.debug("Velocity Engine started");
        } catch (Exception e) {
            log.error(e);
        }

    }

    private Context convert(Map<String, Object> params) {
        Context ctx = new VelocityContext();
        for (String key : params.keySet()) {
            ctx.put(key, params.get(key));
        }
        return ctx;
    }

    public String processTemplate(String template, Map<String, Object> ctx) {

        try {

            final Writer w = new StringWriter();

            Velocity.evaluate(this.convert(ctx), w, template, new StringReader(template));

            return w.toString();

        } catch (Exception ex) {
            log.error("Error processing template file: " + template, ex);
        }

        return StringHelper.EMPTY;
    }

    public String processFileTemplate(String filePath, Map<String, Object> ctx) {

        try {

            final Template mainTemplate = Velocity.getTemplate(filePath);

            final StringWriter writer = new StringWriter();

            mainTemplate.merge(this.convert(ctx), writer);

            if (log.isDebugEnabled()) {

                log.debug("Template file processed: " + filePath);
            }

            return writer.toString();

        } catch (Exception ex) {
            log.error("Error processing template file: " + filePath, ex);
        }

        return StringHelper.EMPTY;
    }

}