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: * URLEncodeExpression.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.io.UnsupportedEncodingException;
032:
033: import org.jfree.report.function.AbstractExpression;
034: import org.jfree.report.util.UTFEncodingUtil;
035:
036: /**
037: * Performs an URL encoding on the value read from the given field. As the URL-encoding schema is a binary
038: * encoding, a real character encoding must be given as well. If not defined otherwise, ISO-8859-1 is used.
039: *
040: * @author Thomas Morgner
041: */
042: public class URLEncodeExpression extends AbstractExpression {
043: /**
044: * The field name from where to read the string that should be URL-encoded.
045: */
046: private String field;
047: /**
048: * The character-encoding that should be used for the URL-encoding of the value.
049: */
050: private String encoding;
051:
052: /**
053: * Default Constructor.
054: */
055: public URLEncodeExpression() {
056: encoding = "ISO-8859-1";
057: }
058:
059: /**
060: * Returns the name of the datarow-column from where to read the string value.
061: *
062: * @return the field.
063: */
064: public String getField() {
065: return field;
066: }
067:
068: /**
069: * Defines the name of the datarow-column from where to read the string value.
070: *
071: * @param field the field.
072: */
073: public void setField(final String field) {
074: this .field = field;
075: }
076:
077: /**
078: * Returns the defined character encoding that is used to transform the Java-Unicode strings into bytes.
079: *
080: * @return the encoding.
081: */
082: public String getEncoding() {
083: return encoding;
084: }
085:
086: /**
087: * Defines the character encoding that is used to transform the Java-Unicode strings into bytes.
088: *
089: * @param encoding the encoding.
090: */
091: public void setEncoding(final String encoding) {
092: this .encoding = encoding;
093: }
094:
095: /**
096: * Encodes the value read from the defined field. The value is converted to a string using the "toString" method.
097: *
098: * @return the value of the function.
099: */
100: public Object getValue() {
101: final Object value = getDataRow().get(getField());
102: if (value == null) {
103: return null;
104: }
105: try {
106: return UTFEncodingUtil.encode(String.valueOf(value),
107: encoding);
108: } catch (UnsupportedEncodingException e) {
109: return null;
110: }
111: }
112:
113: }
|