KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > fill > JREvaluator


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine.fill;
29
30 import java.text.MessageFormat JavaDoc;
31 import java.util.Locale JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.MissingResourceException JavaDoc;
34 import java.util.ResourceBundle JavaDoc;
35
36 import net.sf.jasperreports.engine.JRException;
37 import net.sf.jasperreports.engine.JRExpression;
38 import net.sf.jasperreports.engine.JRParameter;
39 import net.sf.jasperreports.engine.JRReport;
40 import net.sf.jasperreports.engine.JRRuntimeException;
41
42 /**
43  * Base class for the dynamically generated expression evaluator classes.
44  *
45  * @author Lucian Chirita (lucianc@users.sourceforge.net)
46  * @version $Id: JREvaluator.java 1470 2006-11-08 18:07:35 +0200 (Wed, 08 Nov 2006) teodord $
47  */

48 public abstract class JREvaluator
49 {
50     /**
51      * The resource bundle parameter.
52      */

53     private JRFillParameter resourceBundle = null;
54     
55     /**
56      * The resource missing type.
57      */

58     private byte whenResourceMissingType;
59
60     /**
61      * The report Locale used when parsing the bundle message.
62      */

63     private JRFillParameter locale;
64     
65     /**
66      * Default constructor.
67      */

68     protected JREvaluator()
69     {
70     }
71
72     
73     /**
74      * Initializes the evaluator by setting the parameter, field and variable objects.
75      *
76      * @param parametersMap the parameters indexed by name
77      * @param fieldsMap the fields indexed by name
78      * @param variablesMap the variables indexed by name
79      * @param resourceMissingType the resource missing type
80      * @throws JRException
81      */

82     public void init(Map JavaDoc parametersMap, Map JavaDoc fieldsMap, Map JavaDoc variablesMap, byte resourceMissingType) throws JRException
83     {
84         this.whenResourceMissingType = resourceMissingType;
85         this.resourceBundle = (JRFillParameter)parametersMap.get(JRParameter.REPORT_RESOURCE_BUNDLE);
86         this.locale = (JRFillParameter)parametersMap.get(JRParameter.REPORT_LOCALE);
87         customizedInit(parametersMap, fieldsMap, variablesMap);
88     }
89
90     
91     /**
92      * Constructs a message using a pattern with one parameter.
93      *
94      * @param pattern the message pattern
95      * @param arg0 the message parameter
96      * @return the constructed message
97      * @see MessageFormat#format(java.lang.Object[],java.lang.StringBuffer, java.text.FieldPosition)
98      */

99     public String JavaDoc msg(String JavaDoc pattern, Object JavaDoc arg0)
100     {
101         return getMessageFormat(pattern).format(new Object JavaDoc[] { arg0 }, new StringBuffer JavaDoc(), null).toString();
102     }
103
104     /**
105      * Constructs a message using a pattern with two parameters.
106      *
107      * @param pattern the message pattern
108      * @param arg0 the first message parameter
109      * @param arg1 the second message paramter
110      * @return the constructed message
111      * @see MessageFormat#format(java.lang.Object[],java.lang.StringBuffer, java.text.FieldPosition)
112      */

113     public String JavaDoc msg(String JavaDoc pattern, Object JavaDoc arg0, Object JavaDoc arg1)
114     {
115         return getMessageFormat(pattern).format(new Object JavaDoc[] { arg0, arg1 }, new StringBuffer JavaDoc(), null).toString();
116     }
117
118     
119     /**
120      * Constructs a message using a pattern with three parameters.
121      *
122      * @param pattern the message pattern
123      * @param arg0 the first message parameter
124      * @param arg1 the second message paramter
125      * @param arg2 the third parameter
126      * @return the constructed message
127      * @see MessageFormat#format(java.lang.Object[],java.lang.StringBuffer, java.text.FieldPosition)
128      */

129     public String JavaDoc msg(String JavaDoc pattern, Object JavaDoc arg0, Object JavaDoc arg1, Object JavaDoc arg2)
130     {
131         return getMessageFormat(pattern).format(new Object JavaDoc[] { arg0, arg1, arg2 }, new StringBuffer JavaDoc(), null).toString();
132     }
133
134     /**
135      * Constructs a message using a pattern with an Object array parameter.
136      *
137      * @param pattern the message pattern
138      * @param args the parameter Object array
139      * @return the constructed message
140      * @see MessageFormat#format(java.lang.Object[],java.lang.StringBuffer, java.text.FieldPosition)
141      */

142     public String JavaDoc msg(String JavaDoc pattern, Object JavaDoc[] args)
143     {
144         return getMessageFormat(pattern).format(args, new StringBuffer JavaDoc(), null).toString();
145     }
146     
147     /**
148      * Returns a string for a given key from the resource bundle associated with the evaluator.
149      *
150      * @param key the key
151      * @return the string for the given key
152      * @see ResourceBundle#getString(java.lang.String)
153      */

154     public String JavaDoc str(String JavaDoc key)
155     {
156         String JavaDoc str = null;
157
158         try
159         {
160             str = ((ResourceBundle JavaDoc) resourceBundle.getValue()).getString(key);
161         }
162         catch (NullPointerException JavaDoc e)
163         {
164             str = handleMissingResource(key, e);
165         }
166         catch (MissingResourceException JavaDoc e)
167         {
168             str = handleMissingResource(key, e);
169         }
170
171         return str;
172     }
173
174
175     /**
176      *
177      */

178     public Object JavaDoc evaluate(JRExpression expression) throws JRExpressionEvalException
179     {
180         Object JavaDoc value = null;
181         
182         if (expression != null)
183         {
184             try
185             {
186                 value = evaluate(expression.getId());
187             }
188             catch (NullPointerException JavaDoc e)
189             {
190             }
191             catch (OutOfMemoryError JavaDoc e)
192             {
193                 throw e;
194             }
195             catch (Throwable JavaDoc e)
196             {
197                 throw new JRExpressionEvalException(expression, e);
198             }
199         }
200         
201         return value;
202     }
203     
204
205     /**
206      *
207      */

208     public Object JavaDoc evaluateOld(JRExpression expression) throws JRExpressionEvalException
209     {
210         Object JavaDoc value = null;
211         
212         if (expression != null)
213         {
214             try
215             {
216                 value = evaluateOld(expression.getId());
217             }
218             catch (NullPointerException JavaDoc e)
219             {
220             }
221             catch (OutOfMemoryError JavaDoc e)
222             {
223                 throw e;
224             }
225             catch (Throwable JavaDoc e)
226             {
227                 throw new JRExpressionEvalException(expression, e);
228             }
229         }
230         
231         return value;
232     }
233
234
235     /**
236      *
237      */

238     public Object JavaDoc evaluateEstimated(JRExpression expression) throws JRExpressionEvalException
239     {
240         Object JavaDoc value = null;
241         
242         if (expression != null)
243         {
244             try
245             {
246                 value = evaluateEstimated(expression.getId());
247             }
248             catch (NullPointerException JavaDoc e)
249             {
250             }
251             catch (OutOfMemoryError JavaDoc e)
252             {
253                 throw e;
254             }
255             catch (Throwable JavaDoc e)
256             {
257                 throw new JRExpressionEvalException(expression, e);
258             }
259         }
260         
261         return value;
262     }
263
264     
265     /**
266      * Handles the case when a resource is missing.
267      *
268      * @param key
269      * the resource key
270      * @param e
271      * the exception
272      * @return the value to use for the resource
273      * @throws JRRuntimeException
274      * when the resource missing handling type is Error
275      */

276     protected String JavaDoc handleMissingResource(String JavaDoc key, Exception JavaDoc e) throws JRRuntimeException
277     {
278         String JavaDoc str;
279         switch (whenResourceMissingType)
280         {
281             case JRReport.WHEN_RESOURCE_MISSING_TYPE_EMPTY:
282             {
283                 str = "";
284                 break;
285             }
286             case JRReport.WHEN_RESOURCE_MISSING_TYPE_KEY:
287             {
288                 str = key;
289                 break;
290             }
291             case JRReport.WHEN_RESOURCE_MISSING_TYPE_ERROR:
292             {
293                 throw new JRRuntimeException("Resource nout found for key \"" + key + "\".", e);
294             }
295             case JRReport.WHEN_RESOURCE_MISSING_TYPE_NULL:
296             default:
297             {
298                 str = null;
299                 break;
300             }
301         }
302
303         return str;
304     }
305
306
307     /**
308      * Initializes the parameters, fields and variables of the evaluator.
309      *
310      * @param parametersMap the parameters indexed by name
311      * @param fieldsMap the fields indexed by name
312      * @param variablesMap the variables indexed by name
313      * @throws JRException
314      */

315     protected abstract void customizedInit(Map JavaDoc parametersMap, Map JavaDoc fieldsMap, Map JavaDoc variablesMap) throws JRException;
316
317
318     /**
319      * Evaluates an expression using current fields and variables values.
320      *
321      * @param id the expression id
322      * @return the result of the evaluation
323      * @throws Throwable
324      * @see net.sf.jasperreports.engine.JRExpression#EVALUATION_DEFAULT
325      * @see JRFillVariable#getValue()
326      * @see JRFillField#getValue()
327      */

328     protected abstract Object JavaDoc evaluate(int id) throws Throwable JavaDoc;
329
330
331     /**
332      * Evaluates an expression using old fields and variables values.
333      *
334      * @param id the expression id
335      * @return the result of the evaluation
336      * @throws Throwable
337      * @see net.sf.jasperreports.engine.JRExpression#EVALUATION_OLD
338      * @see JRFillVariable#getOldValue()
339      * @see JRFillField#getOldValue()
340      */

341     protected abstract Object JavaDoc evaluateOld(int id) throws Throwable JavaDoc;
342
343
344     /**
345      * Evaluates an expression using estimated variables values.
346      *
347      * @param id the expression id
348      * @return the result of the evaluation
349      * @throws Throwable
350      * @see net.sf.jasperreports.engine.JRExpression#EVALUATION_ESTIMATED
351      * @see JRFillVariable#getEstimatedValue()
352      */

353     protected abstract Object JavaDoc evaluateEstimated(int id) throws Throwable JavaDoc;
354
355
356     /**
357      *
358      */

359     private MessageFormat JavaDoc getMessageFormat(String JavaDoc pattern)
360     {
361         MessageFormat JavaDoc messageFormat = new MessageFormat JavaDoc("");
362         messageFormat.setLocale((Locale JavaDoc)locale.getValue());
363         messageFormat.applyPattern(pattern);
364         return messageFormat;
365     }
366
367 }
368
Popular Tags