package com.fp.common.messages; import java.io.StringWriter; import java.util.Locale; import java.util.Properties; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.runtime.RuntimeConstants; import org.apache.velocity.tools.generic.DateTool; import org.apache.velocity.tools.generic.MathTool; import org.apache.velocity.tools.generic.NumberTool; import com.fp.common.logger.APPLogger; public class MessageUtil { private String pattern; private final VelocityEngine ve; VelocityContext context = new VelocityContext(); public MessageUtil(String pLanguage) throws Exception { Velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, this); Velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.SimpleLog4JLogSystem"); this.ve = new VelocityEngine(); this.ve.init(); this.init(pLanguage); } public MessageUtil(String pLanguage, Properties p) throws Exception { this.ve = new VelocityEngine(); this.ve.init(p); this.init(pLanguage); } private void init(String pLanguage) { this.context.put("date", new DateTool()); this.context.put("math", new MathTool()); this.context.put("number", new NumberTool()); try { //this.context.put("builder", Class.forName("com.fp.builder.templates.MessageTemplates").newInstance()); } catch (Exception e) { APPLogger.getLogger().warn(e, e); } try { this.context.put("general", Class.forName("com.fp.base.persistence.util.helper.GeneralDescriptions").newInstance()); } catch (Exception e) { APPLogger.getLogger().warn(e, e); } this.context.put("locate", new Locale(pLanguage.toLowerCase())); } public MessageUtil(String pLanguage, String pPattern) throws Exception { this(pLanguage); this.pattern = pPattern; } public void setPattern(String pPattern) { this.pattern = pPattern; } public void setValue(String pName, Object pValue) { this.context.put(pName, pValue); } public String getMessage() throws Exception { StringWriter writer = new StringWriter(); try { this.ve.evaluate(this.context, writer, "", this.pattern); } finally { writer.close(); } return writer.toString(); } }