001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * 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,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.engine.data;
029:
030: import java.beans.BeanInfo;
031: import java.beans.IndexedPropertyDescriptor;
032: import java.beans.IntrospectionException;
033: import java.beans.Introspector;
034: import java.beans.PropertyDescriptor;
035: import java.util.ArrayList;
036:
037: import net.sf.jasperreports.engine.JRDataSourceProvider;
038: import net.sf.jasperreports.engine.JRException;
039: import net.sf.jasperreports.engine.JRField;
040: import net.sf.jasperreports.engine.JasperReport;
041: import net.sf.jasperreports.engine.design.JRDesignField;
042:
043: /**
044: * The base implementation for JRBeanXXXDataSource providers. It provides a common
045: * implementation for bean properties introspection.
046: *
047: * @author Peter Severin (peter_s@sourceforge.net, contact@jasperassistant.com)
048: * @version $Id: JRAbstractBeanDataSourceProvider.java 1229 2006-04-19 10:27:35Z teodord $
049: */
050: public abstract class JRAbstractBeanDataSourceProvider implements
051: JRDataSourceProvider {
052:
053: /** The introspected bean class */
054: private Class beanClass;
055:
056: /**
057: * Creates the provider. Superclasses must pass a valid class that will be
058: * used to introspect the available bean fields.
059: * @param beanClass the bean class to be introspected.
060: */
061: public JRAbstractBeanDataSourceProvider(Class beanClass) {
062: if (beanClass == null)
063: throw new NullPointerException("beanClass must not be null");
064:
065: this .beanClass = beanClass;
066: }
067:
068: /**
069: * @see net.sf.jasperreports.engine.JRDataSourceProvider#supportsGetFieldsOperation()
070: */
071: public boolean supportsGetFieldsOperation() {
072: return true;
073: }
074:
075: /**
076: * @see net.sf.jasperreports.engine.JRDataSourceProvider#getFields(net.sf.jasperreports.engine.JasperReport)
077: */
078: public JRField[] getFields(JasperReport report) throws JRException {
079: BeanInfo beanInfo = null;
080:
081: try {
082: beanInfo = Introspector.getBeanInfo(beanClass);
083: } catch (IntrospectionException e) {
084: throw new JRException(e);
085: }
086:
087: PropertyDescriptor[] descriptors = beanInfo
088: .getPropertyDescriptors();
089: if (descriptors != null) {
090: ArrayList fields = new ArrayList(descriptors.length);
091:
092: for (int i = 0; i < descriptors.length; i++) {
093: PropertyDescriptor descriptor = descriptors[i];
094:
095: if (!(descriptor instanceof IndexedPropertyDescriptor)
096: && descriptor.getReadMethod() != null) {
097: JRDesignField field = new JRDesignField();
098: field.setValueClassName(normalizeClass(
099: descriptor.getPropertyType()).getName());
100: field.setName(descriptor.getName());
101:
102: fields.add(field);
103: }
104: }
105:
106: return (JRField[]) fields
107: .toArray(new JRField[fields.size()]);
108: }
109:
110: return new JRField[0];
111: }
112:
113: /**
114: * Converts a primitive class to its object counterpart
115: */
116: private static Class normalizeClass(Class clazz) {
117: if (clazz.isPrimitive()) {
118: if (clazz == boolean.class)
119: return Boolean.class;
120: if (clazz == byte.class)
121: return Byte.class;
122: if (clazz == char.class)
123: return Character.class;
124: if (clazz == short.class)
125: return Short.class;
126: if (clazz == int.class)
127: return Integer.class;
128: if (clazz == long.class)
129: return Long.class;
130: if (clazz == float.class)
131: return Float.class;
132: if (clazz == double.class)
133: return Double.class;
134: }
135:
136: return clazz;
137: }
138: }
|