001 /**
002 * ========================================
003 * JFreeReport : a free Java report library
004 * ========================================
005 *
006 * Project Info: http://reporting.pentaho.org/
007 *
008 * (C) Copyright 2000-2007, by Object Refinery Limited, 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 * $Id: ArrayValueConverter.java,v 1.6 2007/04/01 18:49:34 taqua Exp $
027 * ------------
028 * (C) Copyright 2000-2005, by Object Refinery Limited.
029 * (C) Copyright 2005-2007, by Pentaho Corporation.
030 */
031 package org.jfree.report.util.beans;
032
033 import java.lang.reflect.Array;
034 import java.util.ArrayList;
035
036 import org.jfree.report.util.CSVTokenizer;
037
038 /**
039 * An ValueConverter that handles Arrays. Conversion to arrays is done
040 * using a CSV string.
041 *
042 * @author Thomas Morgner
043 */
044 public class ArrayValueConverter implements ValueConverter
045 {
046 /** The converter for the array elements. */
047 private ValueConverter elementConverter;
048 /** The element type. */
049 private Class elementType;
050
051 /**
052 * Creates a new ArrayValueConverter for the given element type and
053 * array type.
054 * @param arrayClass the array type
055 * @param elementConverter the value converter for the array elements.
056 */
057 public ArrayValueConverter (final Class arrayClass,
058 final ValueConverter elementConverter)
059 {
060 if (elementConverter == null)
061 {
062 throw new NullPointerException("elementConverter must not be null");
063 }
064 if (arrayClass == null)
065 {
066 throw new NullPointerException("arrayClass must not be null");
067 }
068 this.elementType = arrayClass;
069 this.elementConverter = elementConverter;
070 }
071
072 /**
073 * Converts an object to an attribute value.
074 *
075 * @param o the object.
076 * @return the attribute value.
077 * @throws BeanException if there was an error during the conversion.
078 */
079 public String toAttributeValue (final Object o) throws BeanException
080 {
081 final int size = Array.getLength(o);
082 final StringBuffer buffer = new StringBuffer();
083 for (int i = 0; i < size; i++)
084 {
085 if (i != 0)
086 {
087 buffer.append(",");
088 }
089 buffer.append(elementConverter.toAttributeValue(Array.get(o, i)));
090 }
091 return buffer.toString();
092 }
093
094 /**
095 * Converts a string to a property value.
096 *
097 * @param s the string.
098 * @return a property value.
099 * @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 }