1 25 package org.ofbiz.webapp.ftl; 26 27 import java.io.IOException ; 28 import java.io.Writer ; 29 import java.util.Locale ; 30 import java.util.Map ; 31 import javax.servlet.http.HttpServletRequest ; 32 33 import freemarker.core.Environment; 34 import freemarker.ext.beans.BeanModel; 35 import freemarker.ext.beans.NumberModel; 36 import freemarker.template.SimpleNumber; 37 import freemarker.template.SimpleScalar; 38 import freemarker.template.TemplateModelException; 39 import freemarker.template.TemplateScalarModel; 40 import freemarker.template.TemplateTransformModel; 41 42 import org.ofbiz.base.util.Debug; 43 import org.ofbiz.base.util.UtilFormatOut; 44 import org.ofbiz.base.util.UtilHttp; 45 46 54 public class OfbizCurrencyTransform implements TemplateTransformModel { 55 56 public static final String module = OfbizCurrencyTransform.class.getName(); 57 58 private static String getArg(Map args, String key) { 59 String result = ""; 60 Object o = args.get(key); 61 if (o != null) { 62 if (Debug.verboseOn()) Debug.logVerbose("Arg Object : " + o.getClass().getName(), module); 63 if (o instanceof TemplateScalarModel) { 64 TemplateScalarModel s = (TemplateScalarModel) o; 65 try { 66 result = s.getAsString(); 67 } catch (TemplateModelException e) { 68 Debug.logError(e, "Template Exception", module); 69 } 70 } else { 71 result = o.toString(); 72 } 73 } 74 return result; 75 } 76 77 private static Double getAmount(Map args, String key) { 78 if (args.containsKey(key)) { 79 Object o = args.get(key); 80 if (Debug.verboseOn()) Debug.logVerbose("Amount Object : " + o.getClass().getName(), module); 81 82 if (o == null) { 84 o = new Double (0.00); 85 } 86 87 if (o instanceof NumberModel) { 88 NumberModel s = (NumberModel) o; 89 return new Double ( s.getAsNumber().doubleValue() ); 90 } 91 if (o instanceof SimpleNumber) { 92 SimpleNumber s = (SimpleNumber) o; 93 return new Double ( s.getAsNumber().doubleValue() ); 94 } 95 if (o instanceof SimpleScalar) { 96 SimpleScalar s = (SimpleScalar) o; 97 return new Double ( s.getAsString() ); 98 } 99 return new Double ( o.toString() ); 100 } 101 return new Double (0.00); 102 } 103 104 public Writer getWriter(final Writer out, Map args) { 105 final StringBuffer buf = new StringBuffer (); 106 107 final Double amount = OfbizCurrencyTransform.getAmount(args, "amount"); 108 final String isoCode = OfbizCurrencyTransform.getArg(args, "isoCode"); 109 final String locale = OfbizCurrencyTransform.getArg(args, "locale"); 110 111 return new Writer (out) { 112 public void write(char cbuf[], int off, int len) { 113 buf.append(cbuf, off, len); 114 } 115 116 public void flush() throws IOException { 117 out.flush(); 118 } 119 120 public void close() throws IOException { 121 try { 122 if (Debug.verboseOn()) Debug.logVerbose("parms: " + amount + " " + isoCode + " " + locale, module); 123 if (locale.length() < 1) { 124 Environment env = Environment.getCurrentEnvironment(); 126 BeanModel req = (BeanModel) env.getVariable("request"); 127 if (req != null) { 128 HttpServletRequest request = (HttpServletRequest ) req.getWrappedObject(); 129 out.write(UtilFormatOut.formatCurrency(amount, isoCode, UtilHttp.getLocale(request))); 130 } else { 131 out.write(UtilFormatOut.formatCurrency(amount, isoCode, env.getLocale())); 132 } 133 } else { 134 out.write(UtilFormatOut.formatCurrency(amount.doubleValue(), isoCode, new Locale (locale))); 135 } 136 } catch (TemplateModelException e) { 137 throw new IOException (e.getMessage()); 138 } 139 } 140 }; 141 } 142 } 143 | Popular Tags |