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: * SubStringExpression.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 org.jfree.report.function.AbstractExpression;
032:
033: /**
034: * Creates a substring of the given text. If the expression has an ellipsis defined, and the given start
035: * position and length are greater than the string's length, the ellipsis-text will be appended to indicate
036: * that this substring result is only a partial match.
037: *
038: * A similiar functionality can be achived using the built-in formula support.
039: *
040: * @author Thomas Morgner
041: */
042: public class SubStringExpression extends AbstractExpression {
043: /** The field from where to read the string. */
044: private String field;
045: /** The ellipse-text that marks partial results. */
046: private String ellipsis;
047: /** The start-index position. */
048: private int start;
049: /** The length of the substring. */
050: private int length;
051:
052: /**
053: * Default Constructor.
054: */
055: public SubStringExpression() {
056: }
057:
058: /**
059: * Returns the name of the datarow-column from where to read the string value.
060: *
061: * @return the field.
062: */
063: public String getField() {
064: return field;
065: }
066:
067: /**
068: * Defines the name of the datarow-column from where to read the string value.
069: *
070: * @param field the field.
071: */
072: public void setField(final String field) {
073: this .field = field;
074: }
075:
076: /**
077: * Returns the ellipsis-text that indicates partial values.
078: * @return the ellipsis.
079: */
080: public String getEllipsis() {
081: return ellipsis;
082: }
083:
084: /**
085: * Defines the ellipsis-text that indicates partial values.
086: * @param ellipsis the ellipsis.
087: */
088: public void setEllipsis(final String ellipsis) {
089: this .ellipsis = ellipsis;
090: }
091:
092: /**
093: * Returns the sub-string's start position.
094: * @return the start position.
095: */
096: public int getStart() {
097: return start;
098: }
099:
100: /**
101: * Defines the sub-string's start position. The start position cannot be negative.
102: * @param start the start position.
103: */
104: public void setStart(final int start) {
105: if (length < 0) {
106: throw new IndexOutOfBoundsException(
107: "String start position cannot be negative.");
108: }
109: this .start = start;
110: }
111:
112: /**
113: * Returns the sub-string's length.
114: * @return the length.
115: */
116: public int getLength() {
117: return length;
118: }
119:
120: /**
121: * Defines the sub-string's length. The length cannot be negative.
122: * @param length the length.
123: */
124: public void setLength(final int length) {
125: if (length < 0) {
126: throw new IndexOutOfBoundsException(
127: "String length cannot be negative.");
128: }
129: this .length = length;
130: }
131:
132: /**
133: * Computes the sub-string.
134: *
135: * @return the value of the function.
136: */
137: public Object getValue() {
138: final Object raw = getDataRow().get(getField());
139: if (raw == null) {
140: return null;
141: }
142: final String text = String.valueOf(raw);
143:
144: // the text is not large enough to fit the start-bounds. Return nothing,
145: // but indicate that there would have been some more content ...
146: if (start >= text.length()) {
147: return appendEllipsis(null);
148: }
149:
150: // the text fully fits into the given range. No clipping needed ...
151: if ((start + length) > text.length()) {
152: return appendEllipsis(text.substring(start));
153: }
154:
155: return text.substring(start, start + length);
156: }
157:
158: /**
159: * Appends the ellipsis, if one is defined.
160: *
161: * @param value the computed value (without the ellipsis).
162: * @return the computed value with the ellipsis.
163: */
164: private String appendEllipsis(final String value) {
165: if (ellipsis == null) {
166: return value;
167: }
168: if (value == null) {
169: return ellipsis;
170: }
171: return value + ellipsis;
172: }
173: }
|