001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.engine.fill;
029:
030: import java.text.MessageFormat;
031: import java.util.Locale;
032: import java.util.Map;
033: import java.util.MissingResourceException;
034: import java.util.ResourceBundle;
035:
036: import net.sf.jasperreports.engine.JRException;
037: import net.sf.jasperreports.engine.JRExpression;
038: import net.sf.jasperreports.engine.JRParameter;
039: import net.sf.jasperreports.engine.JRReport;
040: import net.sf.jasperreports.engine.JRRuntimeException;
041:
042: /**
043: * Base class for the dynamically generated expression evaluator classes.
044: *
045: * @author Lucian Chirita (lucianc@users.sourceforge.net)
046: * @version $Id: JREvaluator.java 1470 2006-11-08 16:07:35Z teodord $
047: */
048: public abstract class JREvaluator {
049: /**
050: * The resource bundle parameter.
051: */
052: private JRFillParameter resourceBundle = null;
053:
054: /**
055: * The resource missing type.
056: */
057: private byte whenResourceMissingType;
058:
059: /**
060: * The report Locale used when parsing the bundle message.
061: */
062: private JRFillParameter locale;
063:
064: /**
065: * Default constructor.
066: */
067: protected JREvaluator() {
068: }
069:
070: /**
071: * Initializes the evaluator by setting the parameter, field and variable objects.
072: *
073: * @param parametersMap the parameters indexed by name
074: * @param fieldsMap the fields indexed by name
075: * @param variablesMap the variables indexed by name
076: * @param resourceMissingType the resource missing type
077: * @throws JRException
078: */
079: public void init(Map parametersMap, Map fieldsMap,
080: Map variablesMap, byte resourceMissingType)
081: throws JRException {
082: this .whenResourceMissingType = resourceMissingType;
083: this .resourceBundle = (JRFillParameter) parametersMap
084: .get(JRParameter.REPORT_RESOURCE_BUNDLE);
085: this .locale = (JRFillParameter) parametersMap
086: .get(JRParameter.REPORT_LOCALE);
087: customizedInit(parametersMap, fieldsMap, variablesMap);
088: }
089:
090: /**
091: * Constructs a message using a pattern with one parameter.
092: *
093: * @param pattern the message pattern
094: * @param arg0 the message parameter
095: * @return the constructed message
096: * @see MessageFormat#format(java.lang.Object[],java.lang.StringBuffer, java.text.FieldPosition)
097: */
098: public String msg(String pattern, Object arg0) {
099: return getMessageFormat(pattern).format(new Object[] { arg0 },
100: new StringBuffer(), null).toString();
101: }
102:
103: /**
104: * Constructs a message using a pattern with two parameters.
105: *
106: * @param pattern the message pattern
107: * @param arg0 the first message parameter
108: * @param arg1 the second message paramter
109: * @return the constructed message
110: * @see MessageFormat#format(java.lang.Object[],java.lang.StringBuffer, java.text.FieldPosition)
111: */
112: public String msg(String pattern, Object arg0, Object arg1) {
113: return getMessageFormat(pattern).format(
114: new Object[] { arg0, arg1 }, new StringBuffer(), null)
115: .toString();
116: }
117:
118: /**
119: * Constructs a message using a pattern with three parameters.
120: *
121: * @param pattern the message pattern
122: * @param arg0 the first message parameter
123: * @param arg1 the second message paramter
124: * @param arg2 the third parameter
125: * @return the constructed message
126: * @see MessageFormat#format(java.lang.Object[],java.lang.StringBuffer, java.text.FieldPosition)
127: */
128: public String msg(String pattern, Object arg0, Object arg1,
129: Object arg2) {
130: return getMessageFormat(pattern).format(
131: new Object[] { arg0, arg1, arg2 }, new StringBuffer(),
132: null).toString();
133: }
134:
135: /**
136: * Constructs a message using a pattern with an Object array parameter.
137: *
138: * @param pattern the message pattern
139: * @param args the parameter Object array
140: * @return the constructed message
141: * @see MessageFormat#format(java.lang.Object[],java.lang.StringBuffer, java.text.FieldPosition)
142: */
143: public String msg(String pattern, Object[] args) {
144: return getMessageFormat(pattern).format(args,
145: new StringBuffer(), null).toString();
146: }
147:
148: /**
149: * Returns a string for a given key from the resource bundle associated with the evaluator.
150: *
151: * @param key the key
152: * @return the string for the given key
153: * @see ResourceBundle#getString(java.lang.String)
154: */
155: public String str(String key) {
156: String str = null;
157:
158: try {
159: str = ((ResourceBundle) resourceBundle.getValue())
160: .getString(key);
161: } catch (NullPointerException e) {
162: str = handleMissingResource(key, e);
163: } catch (MissingResourceException e) {
164: str = handleMissingResource(key, e);
165: }
166:
167: return str;
168: }
169:
170: /**
171: *
172: */
173: public Object evaluate(JRExpression expression)
174: throws JRExpressionEvalException {
175: Object value = null;
176:
177: if (expression != null) {
178: try {
179: value = evaluate(expression.getId());
180: } catch (NullPointerException e) {
181: } catch (OutOfMemoryError e) {
182: throw e;
183: } catch (Throwable e) {
184: throw new JRExpressionEvalException(expression, e);
185: }
186: }
187:
188: return value;
189: }
190:
191: /**
192: *
193: */
194: public Object evaluateOld(JRExpression expression)
195: throws JRExpressionEvalException {
196: Object value = null;
197:
198: if (expression != null) {
199: try {
200: value = evaluateOld(expression.getId());
201: } catch (NullPointerException e) {
202: } catch (OutOfMemoryError e) {
203: throw e;
204: } catch (Throwable e) {
205: throw new JRExpressionEvalException(expression, e);
206: }
207: }
208:
209: return value;
210: }
211:
212: /**
213: *
214: */
215: public Object evaluateEstimated(JRExpression expression)
216: throws JRExpressionEvalException {
217: Object value = null;
218:
219: if (expression != null) {
220: try {
221: value = evaluateEstimated(expression.getId());
222: } catch (NullPointerException e) {
223: } catch (OutOfMemoryError e) {
224: throw e;
225: } catch (Throwable e) {
226: throw new JRExpressionEvalException(expression, e);
227: }
228: }
229:
230: return value;
231: }
232:
233: /**
234: * Handles the case when a resource is missing.
235: *
236: * @param key
237: * the resource key
238: * @param e
239: * the exception
240: * @return the value to use for the resource
241: * @throws JRRuntimeException
242: * when the resource missing handling type is Error
243: */
244: protected String handleMissingResource(String key, Exception e)
245: throws JRRuntimeException {
246: String str;
247: switch (whenResourceMissingType) {
248: case JRReport.WHEN_RESOURCE_MISSING_TYPE_EMPTY: {
249: str = "";
250: break;
251: }
252: case JRReport.WHEN_RESOURCE_MISSING_TYPE_KEY: {
253: str = key;
254: break;
255: }
256: case JRReport.WHEN_RESOURCE_MISSING_TYPE_ERROR: {
257: throw new JRRuntimeException(
258: "Resource nout found for key \"" + key + "\".", e);
259: }
260: case JRReport.WHEN_RESOURCE_MISSING_TYPE_NULL:
261: default: {
262: str = null;
263: break;
264: }
265: }
266:
267: return str;
268: }
269:
270: /**
271: * Initializes the parameters, fields and variables of the evaluator.
272: *
273: * @param parametersMap the parameters indexed by name
274: * @param fieldsMap the fields indexed by name
275: * @param variablesMap the variables indexed by name
276: * @throws JRException
277: */
278: protected abstract void customizedInit(Map parametersMap,
279: Map fieldsMap, Map variablesMap) throws JRException;
280:
281: /**
282: * Evaluates an expression using current fields and variables values.
283: *
284: * @param id the expression id
285: * @return the result of the evaluation
286: * @throws Throwable
287: * @see net.sf.jasperreports.engine.JRExpression#EVALUATION_DEFAULT
288: * @see JRFillVariable#getValue()
289: * @see JRFillField#getValue()
290: */
291: protected abstract Object evaluate(int id) throws Throwable;
292:
293: /**
294: * Evaluates an expression using old fields and variables values.
295: *
296: * @param id the expression id
297: * @return the result of the evaluation
298: * @throws Throwable
299: * @see net.sf.jasperreports.engine.JRExpression#EVALUATION_OLD
300: * @see JRFillVariable#getOldValue()
301: * @see JRFillField#getOldValue()
302: */
303: protected abstract Object evaluateOld(int id) throws Throwable;
304:
305: /**
306: * Evaluates an expression using estimated variables values.
307: *
308: * @param id the expression id
309: * @return the result of the evaluation
310: * @throws Throwable
311: * @see net.sf.jasperreports.engine.JRExpression#EVALUATION_ESTIMATED
312: * @see JRFillVariable#getEstimatedValue()
313: */
314: protected abstract Object evaluateEstimated(int id)
315: throws Throwable;
316:
317: /**
318: *
319: */
320: private MessageFormat getMessageFormat(String pattern) {
321: MessageFormat messageFormat = new MessageFormat("");
322: messageFormat.setLocale((Locale) locale.getValue());
323: messageFormat.applyPattern(pattern);
324: return messageFormat;
325: }
326:
327: }
|