001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * JRExtendedBeanDataSource.java
028: *
029: * Created on 22 giugno 2004, 14.55
030: *
031: */
032:
033: package it.businesslogic.ireport.connection;
034:
035: import java.util.*;
036:
037: /**
038: * The XML datasource define a row as a path.
039: * A path can be truncated.
040: * @author Administrator
041: */
042: public class JRExtendedBeanDataSource implements
043: net.sf.jasperreports.engine.JRDataSource {
044:
045: private Object[] beans = null;
046: private int index = -1;
047:
048: public JRExtendedBeanDataSource(Vector beans) {
049: this .beans = beans.toArray();
050: }
051:
052: public JRExtendedBeanDataSource(Object[] beans) {
053: this .beans = beans;
054: }
055:
056: public Object getFieldValue(
057: net.sf.jasperreports.engine.JRField jRField)
058: throws net.sf.jasperreports.engine.JRException {
059:
060: String path = jRField.getDescription();
061:
062: Object val = getPathValue(beans[index], path);
063:
064: if (val == null)
065: return null;
066: if (val.getClass().isAssignableFrom(jRField.getValueClass()))
067: return val;
068: return null;
069:
070: }
071:
072: public boolean next()
073: throws net.sf.jasperreports.engine.JRException {
074:
075: index++;
076: if (index >= beans.length)
077: return false;
078: return true;
079: }
080:
081: private Object getPathValue(Object bean, String path) {
082: System.out.println("Looking for " + path + " in " + bean);
083:
084: if (path == null || bean == null || path.trim().length() == 0) {
085: return bean;
086: }
087: if (path.startsWith("."))
088: path = path.substring(1);
089: if (path.trim().length() == 0)
090: return bean;
091:
092: path = path.trim();
093: String attr_name = getNextNodeName(path);
094:
095: /*
096: java.lang.reflect.Method m = null;
097: try {
098: m = bean.getClass().getMethod("get" + attr_name, new Class[]{});
099: } catch (Exception ex){ return null; }
100:
101: if (m.getReturnType().isArray())
102: {
103: if (path.equals(attr_name))
104: {
105: try {
106: return new JRExtendedBeanDataSource( (Object[])m.invoke(bean, new Object[]{}) );
107: } catch (Exception ex){ return null; }
108: }
109: else return null;
110: }
111: else
112: {
113: if (path.equals(attr_name))
114: {
115: try {
116: return m.invoke(bean, new Object[]{});
117: } catch (Exception ex){ return null; }
118: }
119: else
120: {
121: Object child_bean = null;
122: try {
123: child_bean = m.invoke(bean, new Object[]{});
124: } catch (Exception ex){ return null; }
125: path = path.substring(attr_name.length());
126: return getPathValue(child_bean, path );
127: }
128: }
129: */
130: Class clazz = null;
131: try {
132: clazz = org.apache.commons.beanutils.PropertyUtils
133: .getPropertyType(bean, attr_name);
134: } catch (Exception ex) {
135: return null;
136: }
137:
138: if (clazz == null) // Attribute not found!!!!
139: {
140: return null;
141: } else if (clazz.isArray()) {
142: try {
143: return new JRExtendedBeanDataSource(
144: (Object[]) org.apache.commons.beanutils.PropertyUtils
145: .getProperty(bean, attr_name));
146: } catch (Exception ex) {
147: return null;
148: }
149: } else {
150: if (path.equals(attr_name)) {
151: try {
152: return org.apache.commons.beanutils.PropertyUtils
153: .getProperty(bean, attr_name);
154: } catch (Exception ex) {
155: return null;
156: }
157: } else {
158: Object child_bean = null;
159: try {
160: child_bean = org.apache.commons.beanutils.PropertyUtils
161: .getProperty(bean, attr_name);
162: } catch (Exception ex) {
163: return null;
164: }
165: path = path.substring(attr_name.length());
166: return getPathValue(child_bean, path);
167: }
168: }
169: }
170:
171: private static String getNextNodeName(String path) {
172:
173: if (path == null || path.length() == 0)
174: return "";
175: if (path.startsWith("."))
176: path = path.substring(1);
177:
178: String childToTake = path;
179: if (path.indexOf(".") >= 0) {
180: childToTake = path.substring(0, path.indexOf("."));
181: }
182:
183: return childToTake;
184: }
185: }
|