A TextFormatExpression uses a java.text.MessageFormat to concat and format one or more values evaluated from an
expression, function or report datasource.
The TextFormatExpression uses the pattern property to define the global format-pattern used when evaluating the
expression. The dataRow fields used to fill the expressions placeholders are defined in a list of properties where
the property-names are numbers. The property counting starts at "0".
.
allows to specify named field-references in the
pattern, which greatly simplifies the pattern-definition.
| Method from org.jfree.report.function.TextFormatExpression Detail: |
public String getEncoding() {
return encoding;
}
Returns the defined character encoding that is used to transform the Java-Unicode strings into bytes. |
public String[] getField() {
return (String[]) fields.toArray(new String[fields.size()]);
}
Returns all defined fields as array of strings. |
public String getField(int index) {
return (String) fields.get(index);
}
Returns the defined field at the given index-position. |
public int getFieldCount() {
return fields.size();
}
Returns the number of fields defined in this expression. |
protected Object[] getFieldValues(Object[] retval) throws UnsupportedEncodingException {
final int size = fields.size();
if (retval == null || retval.length != size)
{
retval = new Object[size];
}
final DataRow dataRow = getDataRow();
for (int i = 0; i < size; i++)
{
final String field = (String) fields.get(i);
if (field == null)
{
retval[i] = null;
continue;
}
final Object fieldValue = dataRow.get(field);
if (isUrlEncodeValues())
{
if (fieldValue == null)
{
retval[i] = null;
}
else if (fieldValue instanceof Date)
{
retval[i] = fieldValue;
}
else if (fieldValue instanceof Number)
{
retval[i] = fieldValue;
}
else if (isUrlEncodeValues())
{
retval[i] = URLEncoder.encode(String.valueOf(fieldValue), encoding);
}
else
{
retval[i] = fieldValue;
}
}
else
{
retval[i] = fieldValue;
}
}
return retval;
}
Collects the values of all fields defined in the fieldList. |
public Expression getInstance() {
final TextFormatExpression tex = (TextFormatExpression) super.getInstance();
tex.fields = (ArrayList) fields.clone();
tex.fieldValues = null;
tex.oldFieldValues = null;
tex.cachedResult = null;
return tex;
}
Return a completly separated copy of this function. The copy does no longer share any changeable objects with the
original function. |
public String getPattern() {
return pattern;
}
Returns the pattern defined for this expression. |
public Object getValue() {
if (fields.isEmpty())
{
return getPattern();
}
final ResourceBundleFactory factory = getResourceBundleFactory();
if (messageFormat == null || ObjectUtilities.equal(locale, factory.getLocale()) == false)
{
messageFormat = new FastMessageFormat(getPattern(), factory.getLocale());
this.locale = factory.getLocale();
}
try
{
if (oldFieldValues == null || oldFieldValues.length != fields.size())
{
oldFieldValues = new Object[fields.size()];
}
else if (fieldValues != null && fieldValues.length == oldFieldValues.length)
{
System.arraycopy(fieldValues, 0, oldFieldValues, 0, fields.size());
}
fieldValues = getFieldValues(fieldValues);
final String result;
if (cachedResult != null &&
Arrays.equals(oldFieldValues, fieldValues))
{
result = cachedResult;
}
else
{
result = messageFormat.format(fieldValues);
cachedResult = result;
}
if (isUrlEncodeResult())
{
return URLEncoder.encode(result, getEncoding());
}
else
{
return result;
}
}
catch (UnsupportedEncodingException e)
{
TextFormatExpression.logger.debug("Unsupported Encoding: " + encoding);
return null;
}
}
Evaluates the expression by collecting all values defined in the fieldlist from the datarow. The collected values
are then parsed and formated by the MessageFormat-object. |
public boolean isUrlEncodeResult() {
return urlEncodeResult;
}
Queries, whether the formatted result-string will be URL encoded. |
public boolean isUrlEncodeValues() {
return urlEncodeData;
}
Queries, whether the values read from the data-row should be URL encoded. |
public void setEncoding(String encoding) {
if (encoding == null)
{
throw new NullPointerException();
}
this.encoding = encoding;
}
Defines the character encoding that is used to transform the Java-Unicode strings into bytes. |
public void setField(String[] fields) {
this.fields.clear();
this.fields.addAll(Arrays.asList(fields));
fieldValues = null;
oldFieldValues = null;
cachedResult = null;
}
Defines all fields as array. This completely replaces any previously defined fields. |
public void setField(int index,
String field) {
if (fields.size() == index)
{
fields.add(field);
}
else
{
fields.set(index, field);
}
fieldValues = null;
oldFieldValues = null;
cachedResult = null;
}
Defines the field in the field-list at the given index. |
public void setPattern(String pattern) {
if (pattern == null)
{
throw new NullPointerException();
}
this.messageFormat = null;
this.pattern = pattern;
this.cachedResult = null;
}
Defines the pattern for this expression. The pattern syntax is defined by the java.text.MessageFormat object and
the given pattern string has to be valid according to the rules defined there. |
public void setUrlEncodeResult(boolean urlEncodeResult) {
this.urlEncodeResult = urlEncodeResult;
}
Defines, whether the formatted result-string will be URL encoded. |
public void setUrlEncodeValues(boolean urlEncode) {
this.urlEncodeData = urlEncode;
}
Defines, whether the values read from the data-row should be URL encoded. Dates and Number objects are never
encoded. |