KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > webapp > ftl > OfbizCurrencyTransform


1 /*
2  * $Id: OfbizCurrencyTransform.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.webapp.ftl;
26
27 import java.io.IOException JavaDoc;
28 import java.io.Writer JavaDoc;
29 import java.util.Locale JavaDoc;
30 import java.util.Map JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
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 /**
47  * OfbizCurrencyTransform - Freemarker Transform for content links
48  *
49  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
50  * @author <a HREF="mailto:ray.barlow@makeyour-point.com">Ray Barlow</a>
51  * @version $Rev: 5462 $
52  * @since 3.0
53  */

54 public class OfbizCurrencyTransform implements TemplateTransformModel {
55
56     public static final String JavaDoc module = OfbizCurrencyTransform.class.getName();
57
58     private static String JavaDoc getArg(Map JavaDoc args, String JavaDoc key) {
59         String JavaDoc result = "";
60         Object JavaDoc 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 JavaDoc getAmount(Map JavaDoc args, String JavaDoc key) {
78         if (args.containsKey(key)) {
79             Object JavaDoc o = args.get(key);
80             if (Debug.verboseOn()) Debug.logVerbose("Amount Object : " + o.getClass().getName(), module);
81
82             // handle nulls better
83
if (o == null) {
84                 o = new Double JavaDoc(0.00);
85             }
86
87             if (o instanceof NumberModel) {
88                 NumberModel s = (NumberModel) o;
89                 return new Double JavaDoc( s.getAsNumber().doubleValue() );
90             }
91             if (o instanceof SimpleNumber) {
92                 SimpleNumber s = (SimpleNumber) o;
93                 return new Double JavaDoc( s.getAsNumber().doubleValue() );
94             }
95             if (o instanceof SimpleScalar) {
96                 SimpleScalar s = (SimpleScalar) o;
97                 return new Double JavaDoc( s.getAsString() );
98             }
99             return new Double JavaDoc( o.toString() );
100         }
101         return new Double JavaDoc(0.00);
102     }
103     
104     public Writer JavaDoc getWriter(final Writer JavaDoc out, Map JavaDoc args) {
105         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
106
107         final Double JavaDoc amount = OfbizCurrencyTransform.getAmount(args, "amount");
108         final String JavaDoc isoCode = OfbizCurrencyTransform.getArg(args, "isoCode");
109         final String JavaDoc locale = OfbizCurrencyTransform.getArg(args, "locale");
110
111         return new Writer JavaDoc(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 JavaDoc {
117                 out.flush();
118             }
119
120             public void close() throws IOException JavaDoc {
121                 try {
122                     if (Debug.verboseOn()) Debug.logVerbose("parms: " + amount + " " + isoCode + " " + locale, module);
123                     if (locale.length() < 1) {
124                         // Load the locale from the session
125
Environment env = Environment.getCurrentEnvironment();
126                         BeanModel req = (BeanModel) env.getVariable("request");
127                         if (req != null) {
128                             HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) 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 JavaDoc(locale)));
135                     }
136                 } catch (TemplateModelException e) {
137                     throw new IOException JavaDoc(e.getMessage());
138                 }
139             }
140         };
141     }
142 }
143
Popular Tags