001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either 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, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * FormatParser.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.filter;
030:
031: import java.text.Format;
032: import java.text.ParseException;
033:
034: import org.jfree.report.function.ExpressionRuntime;
035:
036: /**
037: * A format parser tries to parse a string into an object. If the value returned by the datasource is no string, a
038: * string is formed using String.valueOf (Object). This string is fed into the java.text.Format of this FormatParser and
039: * the parsed object is returned.
040: * <p/>
041: * What class of object is returned, is determined by the given format. If parsing failed, the defined NullValue is
042: * returned.
043: *
044: * @author Thomas Morgner
045: */
046: public class FormatParser implements DataFilter {
047: /**
048: * The format used to create the string representation of the data.
049: */
050: private Format format;
051:
052: /**
053: * The datasource from where the data is obtained.
054: */
055: private DataSource datasource;
056:
057: /**
058: * The object used to represent null.
059: */
060: private Object nullvalue;
061:
062: /**
063: * DefaultConstructor.
064: */
065: public FormatParser() {
066: }
067:
068: /**
069: * Sets the format for the filter.
070: *
071: * @param format The format.
072: * @throws NullPointerException if the given format is null
073: */
074: public void setFormatter(final Format format) {
075: if (format == null) {
076: throw new NullPointerException();
077: }
078: this .format = format;
079: }
080:
081: /**
082: * Returns the format for the filter.
083: *
084: * @return The format.
085: */
086: public Format getFormatter() {
087: return this .format;
088: }
089:
090: /**
091: * Returns the parsed object. The value is read using the data source given and parsed using the formatter of this
092: * object. The parsing is guaranteed to completly form the target object or to return the defined NullValue.
093: * <p/>
094: * If the given datasource does not return a string, the returned object is transformed into a string using
095: * String.valueOf (Object) and then parsed.
096: * <p/>
097: * If format, datasource or object are null, the NullValue is returned.
098: *
099: * @param runtime the expression runtime that is used to evaluate formulas and expressions when computing the value of
100: * this filter.
101: * @return The formatted value.
102: */
103: public Object getValue(final ExpressionRuntime runtime) {
104: final Format f = getFormatter();
105: if (f == null) {
106: return getNullValue();
107: }
108:
109: final DataSource ds = getDataSource();
110: if (ds == null) {
111: return getNullValue();
112: }
113:
114: final Object o = ds.getValue(runtime);
115: if (o == null) {
116: return getNullValue();
117: }
118:
119: if (isValidOutput(o)) {
120: return o;
121: }
122:
123: try {
124: return f.parseObject(String.valueOf(o));
125: } catch (ParseException e) {
126: return null;
127: }
128: }
129:
130: /**
131: * Checks whether the given value is already a valid result. IF the datasource already returned a valid value, and no
132: * parsing is required, a parser can skip the parsing process by returning true in this function.
133: *
134: * @param o the object to parse.
135: * @return false as this class does not know anything about the format of input or result objects.
136: */
137: protected boolean isValidOutput(final Object o) {
138: return false;
139: }
140:
141: /**
142: * Returns the data source for the filter.
143: *
144: * @return The data source.
145: */
146: public DataSource getDataSource() {
147: return datasource;
148: }
149:
150: /**
151: * Sets the data source.
152: *
153: * @param ds The data source.
154: */
155: public void setDataSource(final DataSource ds) {
156: if (ds == null) {
157: throw new NullPointerException();
158: }
159: this .datasource = ds;
160: }
161:
162: /**
163: * Sets the value that will be displayed if the data source supplies a null value. The nullValue itself can be null to
164: * cover the case when no reasonable default value can be defined.
165: *
166: * @param nullvalue The value returned when the parsing failed.
167: */
168: public void setNullValue(final Object nullvalue) {
169: this .nullvalue = nullvalue;
170: }
171:
172: /**
173: * Returns the object representing a null value from the data source. This value will also be returned when parsing
174: * failed or no parser or datasource is set at all.
175: *
176: * @return The value returned when the parsing failed.
177: */
178: public Object getNullValue() {
179: return nullvalue;
180: }
181:
182: /**
183: * Clones the parser.
184: *
185: * @return a clone.
186: * @throws CloneNotSupportedException this should never happen.
187: */
188: public Object clone() throws CloneNotSupportedException {
189: final FormatParser p = (FormatParser) super .clone();
190: if (datasource != null) {
191: p.datasource = (DataSource) datasource.clone();
192: }
193: if (format != null) {
194: p.format = (Format) format.clone();
195: }
196: return p;
197: }
198:
199: }
|