| 1 |
/** |
| 2 |
* ======================================== |
| 3 |
* JFreeReport : a free Java report library |
| 4 |
* ======================================== |
| 5 |
* |
| 6 |
* Project Info: http://reporting.pentaho.org/ |
| 7 |
* |
| 8 |
* (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors. |
| 9 |
* |
| 10 |
* This library is free software; you can redistribute it and/or modify it under the terms |
| 11 |
* of the GNU Lesser General Public License as published by the Free Software Foundation; |
| 12 |
* either version 2.1 of the License, or (at your option) any later version. |
| 13 |
* |
| 14 |
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 |
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 16 |
* See the GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License along with this |
| 19 |
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
| 20 |
* Boston, MA 02111-1307, USA. |
| 21 |
* |
| 22 |
* [Java is a trademark or registered trademark of Sun Microsystems, Inc. |
| 23 |
* in the United States and other countries.] |
| 24 |
* |
| 25 |
* ------------ |
| 26 |
* $Id: ArrayValueConverter.java 10756 2009-12-02 15:58:24Z tmorgner $ |
| 27 |
* ------------ |
| 28 |
* (C) Copyright 2000-2005, by Object Refinery Limited. |
| 29 |
* (C) Copyright 2005-2007, by Pentaho Corporation. |
| 30 |
*/ |
| 31 |
package org.jfree.report.util.beans; |
| 32 |
|
| 33 |
import java.lang.reflect.Array; |
| 34 |
import java.util.ArrayList; |
| 35 |
|
| 36 |
import org.jfree.report.util.CSVTokenizer; |
| 37 |
|
| 38 |
/** |
| 39 |
* An ValueConverter that handles Arrays. Conversion to arrays is done |
| 40 |
* using a CSV string. |
| 41 |
* |
| 42 |
* @author Thomas Morgner |
| 43 |
*/ |
| 44 |
public class ArrayValueConverter implements ValueConverter |
| 45 |
{ |
| 46 |
/** The converter for the array elements. */ |
| 47 |
private ValueConverter elementConverter; |
| 48 |
/** The element type. */ |
| 49 |
private Class elementType; |
| 50 |
|
| 51 |
/** |
| 52 |
* Creates a new ArrayValueConverter for the given element type and |
| 53 |
* array type. |
| 54 |
* @param arrayClass the array type |
| 55 |
* @param elementConverter the value converter for the array elements. |
| 56 |
*/ |
| 57 |
public ArrayValueConverter (final Class arrayClass, |
| 58 |
final ValueConverter elementConverter) |
| 59 |
{ |
| 60 |
if (elementConverter == null) |
| 61 |
{ |
| 62 |
throw new NullPointerException("elementConverter must not be null"); |
| 63 |
} |
| 64 |
if (arrayClass == null) |
| 65 |
{ |
| 66 |
throw new NullPointerException("arrayClass must not be null"); |
| 67 |
} |
| 68 |
this.elementType = arrayClass; |
| 69 |
this.elementConverter = elementConverter; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Converts an object to an attribute value. |
| 74 |
* |
| 75 |
* @param o the object. |
| 76 |
* @return the attribute value. |
| 77 |
* @throws BeanException if there was an error during the conversion. |
| 78 |
*/ |
| 79 |
public String toAttributeValue (final Object o) throws BeanException |
| 80 |
{ |
| 81 |
final int size = Array.getLength(o); |
| 82 |
final StringBuffer buffer = new StringBuffer(); |
| 83 |
for (int i = 0; i < size; i++) |
| 84 |
{ |
| 85 |
if (i != 0) |
| 86 |
{ |
| 87 |
buffer.append(","); |
| 88 |
} |
| 89 |
buffer.append(elementConverter.toAttributeValue(Array.get(o, i))); |
| 90 |
} |
| 91 |
return buffer.toString(); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Converts a string to a property value. |
| 96 |
* |
| 97 |
* @param s the string. |
| 98 |
* @return a property value. |
| 99 |
* @throws BeanException if there was an error during the conversion. |
| 100 |
*/ |
| 101 |
public Object toPropertyValue (final String s) throws BeanException |
| 102 |
{ |
| 103 |
final CSVTokenizer tokenizer = new CSVTokenizer(s); |
| 104 |
final ArrayList elements = new ArrayList(); |
| 105 |
while (tokenizer.hasMoreTokens()) |
| 106 |
{ |
| 107 |
final String token = tokenizer.nextToken(); |
| 108 |
elements.add(elementConverter.toPropertyValue(token)); |
| 109 |
} |
| 110 |
final Object retval = |
| 111 |
Array.newInstance(elementType, elements.size()); |
| 112 |
for (int i = 0; i < elements.size(); i++) |
| 113 |
{ |
| 114 |
final Object o = elements.get(i); |
| 115 |
Array.set(retval, i, o); |
| 116 |
} |
| 117 |
return retval; |
| 118 |
} |
| 119 |
} |