KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > data > JRAbstractBeanDataSourceProvider


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * 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,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28  package net.sf.jasperreports.engine.data;
29
30 import java.beans.BeanInfo JavaDoc;
31 import java.beans.IndexedPropertyDescriptor JavaDoc;
32 import java.beans.IntrospectionException JavaDoc;
33 import java.beans.Introspector JavaDoc;
34 import java.beans.PropertyDescriptor JavaDoc;
35 import java.util.ArrayList JavaDoc;
36
37 import net.sf.jasperreports.engine.JRDataSourceProvider;
38 import net.sf.jasperreports.engine.JRException;
39 import net.sf.jasperreports.engine.JRField;
40 import net.sf.jasperreports.engine.JasperReport;
41 import net.sf.jasperreports.engine.design.JRDesignField;
42
43
44 /**
45  * The base implementation for JRBeanXXXDataSource providers. It provides a common
46  * implementation for bean properties introspection.
47  *
48  * @author Peter Severin (peter_s@sourceforge.net, contact@jasperassistant.com)
49  * @version $Id: JRAbstractBeanDataSourceProvider.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
50  */

51 public abstract class JRAbstractBeanDataSourceProvider implements JRDataSourceProvider
52 {
53
54     /** The introspected bean class */
55     private Class JavaDoc beanClass;
56
57     /**
58      * Creates the provider. Superclasses must pass a valid class that will be
59      * used to introspect the available bean fields.
60      * @param beanClass the bean class to be introspected.
61      */

62     public JRAbstractBeanDataSourceProvider(Class JavaDoc beanClass) {
63         if (beanClass == null)
64             throw new NullPointerException JavaDoc("beanClass must not be null");
65
66         this.beanClass = beanClass;
67     }
68
69     /**
70      * @see net.sf.jasperreports.engine.JRDataSourceProvider#supportsGetFieldsOperation()
71      */

72     public boolean supportsGetFieldsOperation() {
73         return true;
74     }
75     
76     /**
77      * @see net.sf.jasperreports.engine.JRDataSourceProvider#getFields(net.sf.jasperreports.engine.JasperReport)
78      */

79     public JRField[] getFields(JasperReport report) throws JRException {
80         BeanInfo JavaDoc beanInfo = null;
81
82         try {
83             beanInfo = Introspector.getBeanInfo(beanClass);
84         } catch (IntrospectionException JavaDoc e) {
85             throw new JRException(e);
86         }
87
88         PropertyDescriptor JavaDoc[] descriptors = beanInfo.getPropertyDescriptors();
89         if(descriptors != null) {
90             ArrayList JavaDoc fields = new ArrayList JavaDoc(descriptors.length);
91             
92             for (int i = 0; i < descriptors.length; i++) {
93                 PropertyDescriptor JavaDoc descriptor = descriptors[i];
94                 
95                 if (!(descriptor instanceof IndexedPropertyDescriptor JavaDoc) && descriptor.getReadMethod() != null) {
96                     JRDesignField field = new JRDesignField();
97                     field.setValueClassName(normalizeClass(descriptor.getPropertyType()).getName());
98                     field.setName(descriptor.getName());
99                     
100                     fields.add(field);
101                 }
102             }
103     
104             return (JRField[]) fields.toArray(new JRField[fields.size()]);
105         }
106
107         return new JRField[0];
108     }
109
110     /**
111      * Converts a primitive class to its object counterpart
112      */

113     private static Class JavaDoc normalizeClass(Class JavaDoc clazz) {
114         if(clazz.isPrimitive()) {
115             if(clazz == boolean.class)
116                 return Boolean JavaDoc.class;
117             if(clazz == byte.class)
118                 return Byte JavaDoc.class;
119             if(clazz == char.class)
120                 return Character JavaDoc.class;
121             if(clazz == short.class)
122                 return Short JavaDoc.class;
123             if(clazz == int.class)
124                 return Integer JavaDoc.class;
125             if(clazz == long.class)
126                 return Long JavaDoc.class;
127             if(clazz == float.class)
128                 return Float JavaDoc.class;
129             if(clazz == double.class)
130                 return Double JavaDoc.class;
131         }
132         
133         return clazz;
134     }
135 }
136
Popular Tags