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: * StringFilter.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.filter;
030:
031: import org.jfree.report.function.ExpressionRuntime;
032:
033: /**
034: * A filter that returns the value from a data source as a String. The value is converted
035: * to an String using String.valueOf () which uses Object.toString() to convert the object
036: * into the string.
037: * <p/>
038: * You can specify a default string to return when the value from the data source is
039: * <code>null</code>. Initially the string 'null' is used.
040: *
041: * @author Thomas Morgner
042: */
043: public class StringFilter implements DataFilter, RawDataSource {
044: /**
045: * The data source for this filter.
046: */
047: private DataSource source;
048:
049: /**
050: * The string used to represent a null value.
051: */
052: private String nullvalue;
053:
054: /**
055: * Default constructor.
056: */
057: public StringFilter() {
058: }
059:
060: /**
061: * Sets the string used to represent a null value.
062: *
063: * @param nullvalue the null value.
064: */
065: public void setNullValue(final String nullvalue) {
066: this .nullvalue = nullvalue;
067: }
068:
069: /**
070: * Returns the string used to represent a null value.
071: *
072: * @return the string.
073: */
074: public String getNullValue() {
075: return nullvalue;
076: }
077:
078: /**
079: * Returns the value obtained from the data source. <P> The filter ensures that the
080: * returned value is a String, even though the return type is Object (as required by the
081: * DataSource interface).
082: *
083: * @param runtime the expression runtime that is used to evaluate formulas and expressions when computing the value of
084: * this filter.
085: * @return the string.
086: */
087: public Object getValue(final ExpressionRuntime runtime) {
088: final DataSource ds = getDataSource();
089: if (ds == null) {
090: return getNullValue();
091: }
092: final Object o = ds.getValue(runtime);
093: if (o == null) {
094: return getNullValue();
095: }
096: if (o instanceof String) {
097: return o;
098: }
099: return String.valueOf(o);
100: }
101:
102: /**
103: * Returns the data source for this filter.
104: *
105: * @return the data source.
106: */
107: public DataSource getDataSource() {
108: return source;
109: }
110:
111: /**
112: * Sets the data source for this filter.
113: *
114: * @param ds the data source.
115: */
116: public void setDataSource(final DataSource ds) {
117: if (ds == null) {
118: throw new NullPointerException();
119: }
120: source = ds;
121: }
122:
123: /**
124: * Clones the filter.
125: *
126: * @return a clone.
127: *
128: * @throws CloneNotSupportedException this should never happen.
129: */
130: public Object clone() throws CloneNotSupportedException {
131: final StringFilter f = (StringFilter) super .clone();
132: if (source != null) {
133: f.source = (DataSource) source.clone();
134: }
135: return f;
136: }
137:
138: public Object getRawValue(final ExpressionRuntime runtime) {
139: if (source instanceof RawDataSource) {
140: final RawDataSource rawDataSource = (RawDataSource) source;
141: return rawDataSource.getRawValue(runtime);
142: }
143: return source.getValue(runtime);
144: }
145:
146: public FormatSpecification getFormatString(
147: final ExpressionRuntime runtime,
148: FormatSpecification formatSpecification) {
149: if (source instanceof RawDataSource) {
150: final RawDataSource rds = (RawDataSource) source;
151: return rds.getFormatString(runtime, formatSpecification);
152: }
153: if (formatSpecification == null) {
154: formatSpecification = new FormatSpecification();
155: }
156: formatSpecification.redefine(
157: FormatSpecification.TYPE_UNDEFINED, null);
158: return formatSpecification;
159: }
160: }
|