| 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: ConverterRegistry.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.awt.Color; |
| 34 |
import java.math.BigDecimal; |
| 35 |
import java.math.BigInteger; |
| 36 |
import java.util.HashMap; |
| 37 |
|
| 38 |
public final class ConverterRegistry |
| 39 |
{ |
| 40 |
private static ConverterRegistry instance; |
| 41 |
private HashMap registeredClasses; |
| 42 |
|
| 43 |
public synchronized static ConverterRegistry getInstance () |
| 44 |
{ |
| 45 |
if (instance == null) |
| 46 |
{ |
| 47 |
instance = new ConverterRegistry(); |
| 48 |
} |
| 49 |
return instance; |
| 50 |
} |
| 51 |
|
| 52 |
private ConverterRegistry () |
| 53 |
{ |
| 54 |
registeredClasses = new HashMap(); |
| 55 |
registeredClasses.put(BigDecimal.class, new BigDecimalValueConverter()); |
| 56 |
registeredClasses.put(BigInteger.class, new BigIntegerValueConverter()); |
| 57 |
registeredClasses.put(Boolean.class, new BooleanValueConverter()); |
| 58 |
registeredClasses.put(Boolean.TYPE, new BooleanValueConverter()); |
| 59 |
registeredClasses.put(Byte.class, new ByteValueConverter()); |
| 60 |
registeredClasses.put(Byte.TYPE, new ByteValueConverter()); |
| 61 |
registeredClasses.put(Character.class, new CharacterValueConverter()); |
| 62 |
registeredClasses.put(Character.TYPE, new CharacterValueConverter()); |
| 63 |
registeredClasses.put(Color.class, new ColorValueConverter()); |
| 64 |
registeredClasses.put(Double.class, new DoubleValueConverter()); |
| 65 |
registeredClasses.put(Double.TYPE, new DoubleValueConverter()); |
| 66 |
registeredClasses.put(Float.class, new FloatValueConverter()); |
| 67 |
registeredClasses.put(Float.TYPE, new FloatValueConverter()); |
| 68 |
registeredClasses.put(Integer.class, new IntegerValueConverter()); |
| 69 |
registeredClasses.put(Integer.TYPE, new IntegerValueConverter()); |
| 70 |
registeredClasses.put(Long.class, new LongValueConverter()); |
| 71 |
registeredClasses.put(Long.TYPE, new LongValueConverter()); |
| 72 |
registeredClasses.put(Short.class, new ShortValueConverter()); |
| 73 |
registeredClasses.put(Short.TYPE, new ShortValueConverter()); |
| 74 |
registeredClasses.put(String.class, new StringValueConverter()); |
| 75 |
registeredClasses.put(Number.class, new BigDecimalValueConverter()); |
| 76 |
registeredClasses.put(Class.class, new ClassValueConverter()); |
| 77 |
} |
| 78 |
|
| 79 |
public ValueConverter getValueConverter (final Class c) |
| 80 |
{ |
| 81 |
final ValueConverter plain = (ValueConverter) registeredClasses.get(c); |
| 82 |
if (plain != null) |
| 83 |
{ |
| 84 |
return plain; |
| 85 |
} |
| 86 |
if (c.isArray()) |
| 87 |
{ |
| 88 |
final Class componentType = c.getComponentType(); |
| 89 |
final ValueConverter componentConverter = getValueConverter(componentType); |
| 90 |
if (componentConverter != null) |
| 91 |
{ |
| 92 |
return new ArrayValueConverter(componentType, componentConverter); |
| 93 |
} |
| 94 |
} |
| 95 |
return null; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Converts an object to an attribute value. |
| 100 |
* |
| 101 |
* @param o the object. |
| 102 |
* @return the attribute value. |
| 103 |
* @throws BeanException if there was an error during the conversion. |
| 104 |
*/ |
| 105 |
public static String toAttributeValue (final Object o) throws BeanException |
| 106 |
{ |
| 107 |
if (o == null) |
| 108 |
{ |
| 109 |
return null; |
| 110 |
} |
| 111 |
final ValueConverter vc = |
| 112 |
ConverterRegistry.getInstance().getValueConverter(o.getClass()); |
| 113 |
if (vc == null) |
| 114 |
{ |
| 115 |
return null; |
| 116 |
} |
| 117 |
return vc.toAttributeValue(o); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Converts a string to a property value. |
| 122 |
* |
| 123 |
* @param s the string. |
| 124 |
* @return a property value. |
| 125 |
* @throws BeanException if there was an error during the conversion. |
| 126 |
*/ |
| 127 |
public static Object toPropertyValue (final String s, final Class c) throws BeanException |
| 128 |
{ |
| 129 |
if (s == null) |
| 130 |
{ |
| 131 |
return null; |
| 132 |
} |
| 133 |
final ValueConverter vc = |
| 134 |
ConverterRegistry.getInstance().getValueConverter(c); |
| 135 |
if (vc == null) |
| 136 |
{ |
| 137 |
return null; |
| 138 |
} |
| 139 |
return vc.toPropertyValue(s); |
| 140 |
} |
| 141 |
|
| 142 |
} |