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: * TokenizeStringExpression.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.function.strings;
030:
031: import java.util.StringTokenizer;
032:
033: import org.jfree.report.function.AbstractExpression;
034:
035: /**
036: * Tokenizes a string and replaces all occurences of the delimeter with the value given in replacement. An optional
037: * prefix and suffix can be appended to the string.
038: *
039: * @author Thomas Morgner
040: */
041: public class TokenizeStringExpression extends AbstractExpression {
042: /**
043: * The field from where to read the original value.
044: */
045: private String field;
046: /**
047: * The delimeter value.
048: */
049: private String delimeter;
050: /**
051: * The replacement value.
052: */
053: private String replacement;
054: /**
055: * The (optional) prefix value.
056: */
057: private String prefix;
058: /**
059: * The (optional) suffix value.
060: */
061: private String suffix;
062:
063: /**
064: * Default Constructor.
065: */
066: public TokenizeStringExpression() {
067: }
068:
069: /**
070: * Returns the name of the datarow-column from where to read the string value.
071: *
072: * @return the field.
073: */
074: public String getField() {
075: return field;
076: }
077:
078: /**
079: * Defines the name of the datarow-column from where to read the string value.
080: *
081: * @param field the field.
082: */
083: public void setField(final String field) {
084: this .field = field;
085: }
086:
087: /**
088: * Returns the delimeter string.
089: *
090: * @return the delimeter.
091: */
092: public String getDelimeter() {
093: return delimeter;
094: }
095:
096: /**
097: * Defines the delimeter string.
098: *
099: * @param delimeter the delimeter.
100: */
101: public void setDelimeter(final String delimeter) {
102: this .delimeter = delimeter;
103: }
104:
105: /**
106: * Returns the replacement for the delimter.
107: *
108: * @return the replacement text.
109: */
110: public String getReplacement() {
111: return replacement;
112: }
113:
114: /**
115: * Defines the replacement for the delimter.
116: *
117: * @param replacement the replacement text.
118: */
119: public void setReplacement(final String replacement) {
120: this .replacement = replacement;
121: }
122:
123: /**
124: * Returns the prefix text.
125: *
126: * @return the prefix text.
127: */
128: public String getPrefix() {
129: return prefix;
130: }
131:
132: /**
133: * Defines the prefix text.
134: *
135: * @param prefix the prefix text.
136: */
137: public void setPrefix(final String prefix) {
138: this .prefix = prefix;
139: }
140:
141: /**
142: * Returns the suffix text.
143: *
144: * @return the suffix text.
145: */
146: public String getSuffix() {
147: return suffix;
148: }
149:
150: /**
151: * Defines the suffix text.
152: *
153: * @param suffix the suffix text.
154: */
155: public void setSuffix(final String suffix) {
156: this .suffix = suffix;
157: }
158:
159: /**
160: * Computes the tokenized string. Replaces
161: *
162: * @return the value of the function.
163: */
164: public Object getValue() {
165: final Object raw = getDataRow().get(getField());
166: if (raw == null) {
167: return null;
168: }
169: final String text = String.valueOf(raw);
170:
171: final StringBuffer buffer = new StringBuffer();
172: if (prefix != null) {
173: buffer.append(prefix);
174: }
175:
176: if (delimeter != null) {
177: final StringTokenizer strtok = new StringTokenizer(text,
178: delimeter, false);
179: while (strtok.hasMoreTokens()) {
180: final String o = strtok.nextToken();
181: buffer.append(o);
182: if (replacement != null && strtok.hasMoreTokens()) {
183: buffer.append(replacement);
184: }
185: }
186: }
187:
188: if (suffix != null) {
189: buffer.append(suffix);
190: }
191: return buffer.toString();
192: }
193: }
|