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: * ResourceFileFilter.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.util.ResourceBundle;
032:
033: import org.jfree.report.ResourceBundleFactory;
034: import org.jfree.report.function.ExpressionRuntime;
035: import org.jfree.util.Log;
036:
037: /**
038: * Lookup a key from a datasource using a ResourceBundle.
039: * <p/>
040: * Filters a given datasource and uses the datasource value as key for a ResourceBundle.
041: *
042: * @author Thomas Morgner
043: */
044: public class ResourceFileFilter implements DataFilter {
045: /**
046: * the used resource bundle.
047: */
048: private String resourceIdentifier;
049:
050: /**
051: * the filtered data source.
052: */
053: private DataSource dataSource;
054:
055: /**
056: * Creates a new ResourceFileFilter.
057: */
058: public ResourceFileFilter() {
059: }
060:
061: /**
062: * Returns the name of the used resource bundle.
063: *
064: * @return the name of the resourcebundle
065: * @see org.jfree.report.ResourceBundleFactory#getResourceBundle(String)
066: */
067: public String getResourceIdentifier() {
068: return resourceIdentifier;
069: }
070:
071: /**
072: * Defines the name of the used resource bundle. If undefined, all calls to {@link
073: * org.jfree.report.filter.ResourceFileFilter#getValue(ExpressionRuntime)} will result in <code>null</code> values.
074: *
075: * @param resourceIdentifier the resource bundle name
076: */
077: public void setResourceIdentifier(final String resourceIdentifier) {
078: this .resourceIdentifier = resourceIdentifier;
079: }
080:
081: /**
082: * Returns the current value for the data source. The method will return null, if no datasource or no resource bundle
083: * is defined or if the datasource's value is null.
084: * <p/>
085: * The value read from the dataSource is looked up in the given resourcebundle using the
086: * <code>ResourceBundle.getObject()</code> method. If the lookup fails, null is returned.
087: *
088: * @param runtime the expression runtime that is used to evaluate formulas and expressions when computing the value of
089: * this filter.
090: * @return the value or null, if the value could not be looked up.
091: */
092: public Object getValue(final ExpressionRuntime runtime) {
093: if (dataSource == null) {
094: return null;
095: }
096: if (runtime == null) {
097: return null;
098: }
099: final Object value = dataSource.getValue(runtime);
100: if (value == null) {
101: return null;
102: }
103: final String svalue = String.valueOf(value);
104:
105: try {
106: final String resourceId;
107: if (resourceIdentifier != null) {
108: resourceId = resourceIdentifier;
109: } else {
110: resourceId = runtime
111: .getConfiguration()
112: .getConfigProperty(
113: ResourceBundleFactory.DEFAULT_RESOURCE_BUNDLE_CONFIG_KEY);
114: }
115:
116: if (resourceId == null) {
117: return null;
118: }
119:
120: final ResourceBundleFactory resourceBundleFactory = runtime
121: .getResourceBundleFactory();
122: final ResourceBundle bundle = resourceBundleFactory
123: .getResourceBundle(resourceId);
124: if (bundle != null) {
125: return bundle.getObject(svalue);
126: }
127: } catch (Exception e) {
128: // on errors return null.
129: Log.info(new Log.SimpleMessage(
130: "Failed to retrive the value for key ", svalue));
131: }
132: return null;
133: }
134:
135: /**
136: * Clones this <code>DataSource</code>.
137: *
138: * @return the clone.
139: * @throws CloneNotSupportedException this should never happen.
140: */
141: public Object clone() throws CloneNotSupportedException {
142: final ResourceFileFilter filter = (ResourceFileFilter) super
143: .clone();
144: filter.dataSource = (DataSource) dataSource.clone();
145: return filter;
146: }
147:
148: /**
149: * Returns the assigned DataSource for this Target.
150: *
151: * @return The datasource.
152: */
153: public DataSource getDataSource() {
154: return dataSource;
155: }
156:
157: /**
158: * Assigns a DataSource for this Target.
159: *
160: * @param ds The data source.
161: */
162: public void setDataSource(final DataSource ds) {
163: this.dataSource = ds;
164: }
165:
166: }
|