All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.template.velocity.util.VelocityTemplateTool Maven / Gradle / Ivy

The newest version!
package org.zodiac.template.velocity.util;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.exception.VelocityException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zodiac.commons.util.PropertiesUtil;
import org.zodiac.sdk.toolkit.constants.CharsetConstants;
import org.zodiac.sdk.toolkit.constants.StringPool;
import org.zodiac.sdk.toolkit.io.WriterOutputStream;
import org.zodiac.sdk.toolkit.resource.Resource;
import org.zodiac.sdk.toolkit.util.ExceptionUtil;
import org.zodiac.sdk.toolkit.util.lang.ObjUtil;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;
import org.zodiac.template.base.TemplateContext;
import org.zodiac.template.base.TemplateException;
import org.zodiac.template.velocity.VelocityTemplateEngine;
import org.zodiac.template.velocity.constants.VelocityTemplateConstants;
import org.zodiac.template.velocity.impl.TemplateContextAdapter;
import org.zodiac.template.velocity.platform.PlatformVelocityContext;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicBoolean;

public class VelocityTemplateTool {

    protected Logger log = LoggerFactory.getLogger(getClass());

    private AtomicBoolean engineInitialized = new AtomicBoolean(false);
    private VelocityEngine velocityEngine;

    public VelocityTemplateTool() {
        this(StringPool.EMPTY);
    }

    public VelocityTemplateTool(String config) {
        init(config);
    }

    public VelocityTemplateTool(Resource config) {
        init(config);
    }

    public VelocityEngine getVelocityEngine() {
        return velocityEngine;
    }

    /*================================= Default Velocity =================================*/

    /**
     * Find tamplate.
     * 
     * @param templateName template name
     * @return {@link Template}
     */
    public Template getTemplate(String templateName) {
        return getTemplate(this.velocityEngine, templateName);
    }

    public Template getTemplate(String templateName, Charset charset) {
        return getTemplate(this.velocityEngine, templateName, charset);
    }

    public String evaluate(Context context, String templateName, String template) {
        StringWriter out = new StringWriter();
        try {
            this.velocityEngine.evaluate(context, out, templateName, template);
        } catch (ParseErrorException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return template;
        } catch (MethodInvocationException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return template;
        } catch (ResourceNotFoundException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return template;
        } catch (Exception e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return template;
        }
        return out.toString();
    }

    public void evaluate(Context context, Writer out, String templateName, String template) {
        try {
            this.velocityEngine.evaluate(context, out, templateName, template);
        } catch (ParseErrorException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (MethodInvocationException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (ResourceNotFoundException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (Exception e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        }
    }

    public String evaluate(Context context, String templateName, InputStream inputStream) {
        StringWriter out = new StringWriter();
        try {
            this.velocityEngine.evaluate(context, out, templateName, new InputStreamReader(inputStream));
        } catch (ParseErrorException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        } catch (MethodInvocationException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        } catch (ResourceNotFoundException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        } catch (Exception e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        }
        return out.toString();
    }

    public void evaluate(Context context, Writer out, String templateName, InputStream inputStream) {
        try {
            this.velocityEngine.evaluate(context, out, templateName, new InputStreamReader(inputStream));
        } catch (ParseErrorException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (MethodInvocationException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (ResourceNotFoundException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (Exception e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        }
    }

    public String evaluate(Context context, String templateName, Reader reader) {
        StringWriter out = new StringWriter();
        try {
            this.velocityEngine.evaluate(context, out, templateName, reader);
        } catch (ParseErrorException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        } catch (MethodInvocationException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        } catch (ResourceNotFoundException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        } catch (Exception e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        }
        return out.toString();
    }

    public void evaluate(Context context, Writer out, String templateName, Reader reader) {
        try {
            this.velocityEngine.evaluate(context, out, templateName, reader);
        } catch (ParseErrorException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (MethodInvocationException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (ResourceNotFoundException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (Exception e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        }
    }

    public String evaluate(Map context, String templateName, String template) {
        StringWriter out = new StringWriter();
        try {
            this.velocityEngine.evaluate(buildVelocityContext(context), out, templateName, template);
        } catch (ParseErrorException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return template;
        } catch (MethodInvocationException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return template;
        } catch (ResourceNotFoundException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return template;
        } catch (Exception e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return template;
        }
        return out.toString();
    }

    public void evaluate(Map context, Writer out, String templateName, String template) {
        try {
            this.velocityEngine.evaluate(buildVelocityContext(context), out, templateName, template);
        } catch (ParseErrorException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (MethodInvocationException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (ResourceNotFoundException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (Exception e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        }
    }

    public String evaluate(Map context, String templateName, InputStream inputStream) {
        StringWriter out = new StringWriter();
        try {
            this.velocityEngine.evaluate(buildVelocityContext(context), out, templateName, new InputStreamReader(inputStream));
        } catch (ParseErrorException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        } catch (MethodInvocationException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        } catch (ResourceNotFoundException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        } catch (Exception e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        }
        return out.toString();
    }

    public void evaluate(Map context, Writer out, String templateName, InputStream inputStream) {
        try {
            this.velocityEngine.evaluate(buildVelocityContext(context), out, templateName, new InputStreamReader(inputStream));
        } catch (ParseErrorException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (MethodInvocationException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (ResourceNotFoundException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (Exception e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        }
    }

    public String evaluate(Map context, String templateName, Reader template) {
        StringWriter out = new StringWriter();
        try {
            this.velocityEngine.evaluate(buildVelocityContext(context), out, templateName, template);
        } catch (ParseErrorException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        } catch (MethodInvocationException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        } catch (ResourceNotFoundException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        } catch (Exception e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        }
        return out.toString();
    }

    public void evaluate(Map context, Writer out, String templateName, Reader template) {
        try {
            this.velocityEngine.evaluate(buildVelocityContext(context), out, templateName, template);
        } catch (ParseErrorException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (MethodInvocationException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (ResourceNotFoundException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (Exception e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        }
    }

    /*================================= VelocityEngine =================================*/

    public Template getTemplate(VelocityEngine velocityEngine, String templateName) {
        return getTemplate(velocityEngine, templateName, null);
    }

    public Template getTemplate(VelocityEngine velocityEngine, String templateName, Charset charset) {
        if (null == velocityEngine || StrUtil.isEmpty(templateName)) {
            return null;
        }

        Template template = null;
        try {
            /*
            if (!templateName.startsWith("templates/")) {
                templateName = "templates/" + templateName;
            }
            */
            
            Charset cs = ObjUtil.defaultIfNull(charset, CharsetConstants.UTF_8);
            template = velocityEngine.getTemplate(templateName, cs.name());
        } catch (ResourceNotFoundException rnfe) {
            log.error("Cannot find template {}, caused by {}.", templateName, ExceptionUtil.stackTrace(rnfe));
        } catch (ParseErrorException pee) {
            log.error("Syntax error in template {}, caused by {}.", templateName, ExceptionUtil.stackTrace(pee));
        } catch (Exception ex) {
            log.error("Error in template {}, caused by {}.", templateName, ExceptionUtil.stackTrace(ex));
        }
        return template;
    }

    /**
     * Evaluate tamplate.
     * 
     * @param velocityEngine velocity template engine
     * @param context context
     * @param templateName template name
     * @param template template
     * @return content
     */
    public String evaluate(VelocityEngine velocityEngine, Context context, String templateName, String template) {
        StringWriter out = new StringWriter();
        try {
            velocityEngine.evaluate(context, out, templateName, template);
        } catch (ParseErrorException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return template;
        } catch (MethodInvocationException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return template;
        } catch (ResourceNotFoundException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return template;
        } catch (Exception e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return template;
        }
        return out.toString();
    }

    public void evaluate(VelocityEngine velocityEngine, Context context, Writer out, String templateName, String template) {
        try {
            velocityEngine.evaluate(context, out, templateName, template);
        } catch (ParseErrorException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (MethodInvocationException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (ResourceNotFoundException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (Exception e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        }
    }

    public String evaluate(VelocityEngine velocityEngine, Context context, String templateName, InputStream inputStream) {
        StringWriter out = new StringWriter();
        try {
            velocityEngine.evaluate(context, out, templateName, new InputStreamReader(inputStream));
        } catch (ParseErrorException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        } catch (MethodInvocationException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        } catch (ResourceNotFoundException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        } catch (Exception e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        }
        return out.toString();
    }

    public void evaluate(VelocityEngine velocityEngine, Context context, Writer out, String templateName, InputStream inputStream) {
        try {
            velocityEngine.evaluate(context, out, templateName, new InputStreamReader(inputStream));
        } catch (ParseErrorException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (MethodInvocationException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (ResourceNotFoundException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (Exception e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        }
    }

    public String evaluate(VelocityEngine velocityEngine, Context context, String templateName, Reader reader) {
        StringWriter out = new StringWriter();
        try {
            velocityEngine.evaluate(context, out, templateName, reader);
        } catch (ParseErrorException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        } catch (MethodInvocationException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        } catch (ResourceNotFoundException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        } catch (Exception e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        }
        return out.toString();
    }

    public void evaluate(VelocityEngine velocityEngine, Context context, Writer out, String templateName, Reader reader) {
        try {
            velocityEngine.evaluate(context, out, templateName, reader);
        } catch (ParseErrorException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (MethodInvocationException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));

        } catch (ResourceNotFoundException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (Exception e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        }
    }

    public String evaluate(VelocityEngine velocityEngine, Map context, String templateName, String template) {
        StringWriter out = new StringWriter();
        try {
            velocityEngine.evaluate(buildVelocityContext(context), out, templateName, template);
        } catch (ParseErrorException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return template;
        } catch (MethodInvocationException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return template;
        } catch (ResourceNotFoundException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return template;
        } catch (Exception e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return template;
        }
        return out.toString();
    }

    public void evaluate(VelocityEngine velocityEngine, Map context, Writer out, String templateName, String template) {
        try {
            velocityEngine.evaluate(buildVelocityContext(context), out, templateName, template);
        } catch (ParseErrorException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (MethodInvocationException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (ResourceNotFoundException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (Exception e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        }
    }

    public String evaluate(VelocityEngine velocityEngine, Map context, String templateName, InputStream inputStream) {
        StringWriter out = new StringWriter();
        try {
            velocityEngine.evaluate(buildVelocityContext(context), out, templateName, new InputStreamReader(inputStream));
        } catch (ParseErrorException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        } catch (MethodInvocationException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        } catch (ResourceNotFoundException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        } catch (Exception e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        }
        return out.toString();
    }

    public void evaluate(VelocityEngine velocityEngine, Map context, Writer out, String templateName, InputStream inputStream) {
        try {
            velocityEngine.evaluate(buildVelocityContext(context), out, templateName, new InputStreamReader(inputStream));
        } catch (ParseErrorException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (MethodInvocationException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (ResourceNotFoundException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (Exception e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        }
    }

    public String evaluate(VelocityEngine velocityEngine, Map context, String templateName, Reader template) {
        StringWriter out = new StringWriter();
        try {
            velocityEngine.evaluate(buildVelocityContext(context), out, templateName, template);
        } catch (ParseErrorException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        } catch (MethodInvocationException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        } catch (ResourceNotFoundException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        } catch (Exception e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
            return null;
        }
        return out.toString();
    }

    public void evaluate(VelocityEngine velocityEngine, Map context, Writer out, String templateName, Reader template) {
        try {
            velocityEngine.evaluate(buildVelocityContext(context), out, templateName, template);
        } catch (ParseErrorException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (MethodInvocationException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (ResourceNotFoundException e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        } catch (Exception e) {
            log.error("Evaluate template {} fiald, caused by {}.", templateName, ExceptionUtil.stackTrace(e));
        }
    }

    public void mergeTemplate(VelocityEngine velocityEngine, String templateLocation, Map model,
        Writer writer) throws VelocityException {
        mergeTemplate(velocityEngine, templateLocation, null, model, writer);
    }

    public String mergeTemplateToString(VelocityEngine velocityEngine, String templateLocation,
        Map model) throws VelocityException {
        return mergeTemplateToString(velocityEngine, templateLocation, null, model);
    }

    public String mergeTemplateToString(VelocityEngine velocityEngine, String templateLocation, String encoding,
        Map model) throws VelocityException {
        StringWriter result = new StringWriter();
        mergeTemplate(velocityEngine, templateLocation, encoding, model, result);
        return result.toString();
    }

    public void mergeTemplate(VelocityEngine velocityEngine, String templateLocation, String encoding,
        Map model, Writer writer) throws VelocityException {
        String _encoding = StrUtil.trimTo(encoding, VelocityTemplateConstants.DEFAULT_ENCODING_NAME);
        Context context = buildVelocityContext(model);
        velocityEngine.mergeTemplate(templateLocation, _encoding, context, writer);
    }

    public String mergeTemplateToString(VelocityEngine velocityEngine, String templateLocation,
        Context context) throws VelocityException {
        StringWriter result = new StringWriter();
        mergeTemplate(velocityEngine, templateLocation, null, context, result);
        return result.toString();
    }

    public String mergeTemplateToString(VelocityEngine velocityEngine, String templateLocation, String encoding,
        Context context) throws VelocityException {
        String _encoding = StrUtil.trimTo(encoding, VelocityTemplateConstants.DEFAULT_ENCODING_NAME);
        StringWriter result = new StringWriter();
        velocityEngine.mergeTemplate(templateLocation, _encoding, context, result);
        return result.toString();
    }

    public void mergeTemplate(VelocityEngine velocityEngine, String templateLocation,
        Context context, Writer writer) throws VelocityException {
        mergeTemplate(velocityEngine, templateLocation, null, context, writer);
    }

    public void mergeTemplate(VelocityEngine velocityEngine, String templateLocation, String encoding,
        Context context, Writer writer) throws VelocityException {
        String _encoding = StrUtil.trimTo(encoding, VelocityTemplateConstants.DEFAULT_ENCODING_NAME);
        velocityEngine.mergeTemplate(templateLocation, _encoding, context, writer);
    }

    /*================================= VelocityTemplateEngine =================================*/

    /**
     * Merge tamplate.
     * @param velocityTemplateEngine velocity template engine
     * @param templateLocation tamplate path
     * @param model tamplate model
     * @return content
     * @throws VelocityException VelocityException
     * @throws TemplateException TemplateException
     * @throws IOException IOException
     */
    public String mergeTemplateToString(VelocityTemplateEngine velocityTemplateEngine, String templateLocation,
        Map model) throws VelocityException, TemplateException, IOException {
        return mergeTemplateToString(velocityTemplateEngine, templateLocation, buildVelocityContext(model));
    }

    public String mergeTemplateToString(VelocityTemplateEngine velocityTemplateEngine, String templateLocation,
        Context context) throws VelocityException, TemplateException, IOException {
        return mergeTemplateToString(velocityTemplateEngine, templateLocation, null, context);
    }

    public String mergeTemplateToString(VelocityTemplateEngine velocityTemplateEngine, String templateLocation,
        String encoding, Map model) throws VelocityException, TemplateException, IOException {
        return mergeTemplateToString(velocityTemplateEngine, templateLocation, encoding, buildVelocityContext(model));
    }

    public String mergeTemplateToString(VelocityTemplateEngine velocityTemplateEngine, String templateLocation,
        String encoding, Context context) throws VelocityException, TemplateException, IOException {
        String _encoding = StrUtil.trimTo(encoding, VelocityTemplateConstants.DEFAULT_ENCODING_NAME);
        StringWriter result = new StringWriter();
        mergeTemplate(velocityTemplateEngine, templateLocation, _encoding, context, result);
        return result.toString();
    }

    public void mergeTemplate(VelocityTemplateEngine velocityTemplateEngine, String templateLocation,
        Map model, Writer writer) throws VelocityException, TemplateException, IOException {
        mergeTemplate(velocityTemplateEngine, templateLocation, buildVelocityContext(model), writer);
    }

    public void mergeTemplate(VelocityTemplateEngine velocityTemplateEngine, String templateLocation,
        Context context, Writer writer) throws VelocityException, TemplateException, IOException {
        mergeTemplate(velocityTemplateEngine, templateLocation, null, context, writer);
    }

    public void mergeTemplate(VelocityTemplateEngine velocityTemplateEngine, String templateLocation,
        String encoding, Map model, Writer writer)
        throws VelocityException, TemplateException, IOException {
        mergeTemplate(velocityTemplateEngine, templateLocation, encoding, buildVelocityContext(model), writer);
    }

    public void mergeTemplate(VelocityTemplateEngine velocityTemplateEngine, String templateLocation,
        String encoding, Context context, Writer writer)
        throws VelocityException, TemplateException, IOException {
        String _encoding = StrUtil.trimTo(encoding, VelocityTemplateConstants.DEFAULT_ENCODING_NAME);
        mergeTemplate(velocityTemplateEngine, templateLocation, _encoding, context,
            new WriterOutputStream(writer, _encoding));
    }

    public void mergeTemplate(VelocityTemplateEngine velocityTemplateEngine, String templateLocation,
        Map model, OutputStream ostream) throws VelocityException, TemplateException, IOException {
        mergeTemplate(velocityTemplateEngine, templateLocation, buildVelocityContext(model), ostream);
    }

    public void mergeTemplate(VelocityTemplateEngine velocityTemplateEngine, String templateLocation,
        Context context, OutputStream ostream)
        throws VelocityException, TemplateException, IOException {
        mergeTemplate(velocityTemplateEngine, templateLocation, null, context, ostream);
    }

    public void mergeTemplate(VelocityTemplateEngine velocityTemplateEngine, String templateLocation,
        String encoding, Map model, OutputStream ostream)
        throws VelocityException, TemplateException, IOException {
        mergeTemplate(velocityTemplateEngine, templateLocation, encoding, buildVelocityContext(model), ostream);
    }

    public void mergeTemplate(VelocityTemplateEngine velocityTemplateEngine, String templateLocation,
        String encoding, Context context, OutputStream ostream)
        throws VelocityException, TemplateException, IOException {
        String _encoding = StrUtil.trimTo(encoding, VelocityTemplateConstants.DEFAULT_ENCODING_NAME);
        velocityTemplateEngine.mergeTemplate(templateLocation, context, ostream, _encoding, _encoding);
    }

    /*================================= Tool methods =================================*/

    /**
     * Build velocity context.
     * 
     * @param contextMap context parameters
     * @return context
     */
    public VelocityContext buildVelocityContext(final Map contextMap) {
        //VelocityContext velocityContext = null != contextMap ? new VelocityContext(contextMap) : new VelocityContext();
        VelocityContext velocityContext = null != contextMap ? new PlatformVelocityContext(contextMap) : new PlatformVelocityContext();
        return velocityContext;
    }

    public VelocityContext buildVelocityContext(final TemplateContext templateContext) {
        VelocityContext velocityContext = templateContext != null ? new TemplateContextAdapter(templateContext) : null;
        return velocityContext;
    }

    public VelocityTemplateTool init(String classpathConfig) {
        initVelocityEngine(StrUtil.trimTo(classpathConfig, VelocityTemplateConstants.DEFAULT_CONFIG_PATH));
        return this;
    }

    public VelocityTemplateTool init(Resource configResource) {
        initVelocityEngine(configResource);
        return this;
    }

    private void initVelocityEngine(String classpathConfig) {
        if (engineInitialized.compareAndSet(false, true)) {
            try {
                Properties pros = null != classpathConfig ? PropertiesUtil.getProperties(classpathConfig, VelocityTemplateTool.class)
                    : new Properties();
                velocityEngine = new VelocityEngine();
                velocityEngine.init(pros);
            } catch (RuntimeException e) {
                throw new VelocityException(String.format("Init VelocityEngine from %s failed, caused by %s .",
                    classpathConfig, ExceptionUtil.stackTrace(e)));
            } catch (Exception e) {
                throw new VelocityException(String.format("Init VelocityEngine from %s failed, caused by %s .",
                    classpathConfig, ExceptionUtil.stackTrace(e)));
            }
        }
    }

    private void initVelocityEngine(Resource configResource) {
        if (engineInitialized.compareAndSet(false, true)) {
            try {
                Properties pros = new Properties();
                if (null != configResource) {
                    pros.load(configResource.getInputStream());
                }
                velocityEngine = new VelocityEngine();
                velocityEngine.init(pros);
            } catch (RuntimeException e) {
                throw new VelocityException(String.format("Init VelocityEngine from %s failed, caused by %s .",
                    configResource, ExceptionUtil.stackTrace(e)));
            } catch (Exception e) {
                throw new VelocityException(String.format("Init VelocityEngine from %s failed, caused by %s .",
                    configResource, ExceptionUtil.stackTrace(e)));
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy