KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > connection > JRExtendedBeanDataSource


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * JRExtendedBeanDataSource.java
28  *
29  * Created on 22 giugno 2004, 14.55
30  *
31  */

32
33 package it.businesslogic.ireport.connection;
34
35
36 import java.util.*;
37
38
39 /**
40  * The XML datasource define a row as a path.
41  * A path can be truncated.
42  * @author Administrator
43  */

44 public class JRExtendedBeanDataSource implements net.sf.jasperreports.engine.JRDataSource {
45
46     private Object JavaDoc[] beans = null;
47     private int index = -1;
48     
49     public JRExtendedBeanDataSource(Vector beans)
50     {
51         this.beans = beans.toArray();
52     }
53     
54     public JRExtendedBeanDataSource(Object JavaDoc[] beans)
55     {
56         this.beans = beans;
57     }
58     
59     public Object JavaDoc getFieldValue(net.sf.jasperreports.engine.JRField jRField) throws net.sf.jasperreports.engine.JRException {
60         
61         String JavaDoc path = jRField.getDescription();
62         
63         Object JavaDoc val = getPathValue(beans[index], path);
64         
65         if (val == null) return null;
66         if (val.getClass().isAssignableFrom( jRField.getValueClass() )) return val;
67         return null;
68                 
69     }
70     
71      public boolean next() throws net.sf.jasperreports.engine.JRException {
72
73         index++;
74         if (index >= beans.length) return false;
75         return true;
76     }
77     
78    
79     private Object JavaDoc getPathValue(Object JavaDoc bean, String JavaDoc path)
80     {
81         System.out.println("Looking for " + path + " in " + bean);
82         
83         if (path == null || bean == null || path.trim().length() == 0)
84         {
85             return bean;
86         }
87     if (path.startsWith(".")) path = path.substring(1);
88         if (path.trim().length() == 0) return bean;
89         
90         path = path.trim();
91         String JavaDoc attr_name = getNextNodeName(path );
92         
93         
94         /*
95         java.lang.reflect.Method m = null;
96         try {
97             m = bean.getClass().getMethod("get" + attr_name, new Class[]{});
98         } catch (Exception ex){ return null; }
99         
100         if (m.getReturnType().isArray())
101         {
102             if (path.equals(attr_name))
103             {
104                 try {
105                      return new JRExtendedBeanDataSource( (Object[])m.invoke(bean, new Object[]{}) );
106                 } catch (Exception ex){ return null; }
107             }
108             else return null;
109         }
110         else
111         {
112             if (path.equals(attr_name))
113             {
114                 try {
115                     return m.invoke(bean, new Object[]{});
116                 } catch (Exception ex){ return null; }
117             }
118             else
119             {
120                 Object child_bean = null;
121                 try {
122                     child_bean = m.invoke(bean, new Object[]{});
123                 } catch (Exception ex){ return null; }
124                 path = path.substring(attr_name.length());
125                 return getPathValue(child_bean, path );
126             }
127         }
128          */

129         Class JavaDoc clazz = null;
130         try {
131             clazz = org.apache.commons.beanutils.PropertyUtils.getPropertyType(bean,attr_name);
132         } catch (Exception JavaDoc ex){ return null; }
133     
134         if (clazz == null) // Attribute not found!!!!
135
{
136             return null;
137         }
138         else if ( clazz.isArray() )
139         {
140             try {
141                      return new JRExtendedBeanDataSource( (Object JavaDoc[])org.apache.commons.beanutils.PropertyUtils.getProperty(bean, attr_name) );
142                 } catch (Exception JavaDoc ex){ return null; }
143         }
144         else
145         {
146             if (path.equals(attr_name))
147             {
148                 try {
149                     return org.apache.commons.beanutils.PropertyUtils.getProperty(bean, attr_name);
150                 } catch (Exception JavaDoc ex){ return null; }
151             }
152             else
153             {
154                 Object JavaDoc child_bean = null;
155                 try {
156                     child_bean = org.apache.commons.beanutils.PropertyUtils.getProperty(bean, attr_name);
157                 } catch (Exception JavaDoc ex){ return null; }
158                 path = path.substring(attr_name.length());
159                 return getPathValue(child_bean, path );
160             }
161         }
162     }
163         
164     private static String JavaDoc getNextNodeName( String JavaDoc path)
165     {
166         
167         if (path == null || path.length() ==0) return "";
168         if (path.startsWith(".")) path = path.substring(1);
169         
170         String JavaDoc childToTake = path;
171         if (path.indexOf(".") >= 0)
172         {
173                 childToTake = path.substring(0,path.indexOf("."));
174         }
175                
176         return childToTake;
177     }
178 }
179
Popular Tags