KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > views > jasperreports > OgnlValueStackDataSource


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.views.jasperreports;
6
7 import com.opensymphony.webwork.util.MakeIterator;
8 import com.opensymphony.xwork.util.OgnlValueStack;
9 import net.sf.jasperreports.engine.JRDataSource;
10 import net.sf.jasperreports.engine.JRException;
11 import net.sf.jasperreports.engine.JRField;
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14
15 import java.util.Iterator JavaDoc;
16
17 /**
18  * Ported to WebWork2.
19  *
20  * @author <a HREF="hermanns@aixcept.de">Rainer Hermanns</a>
21  * @version $Id: OgnlValueStackDataSource.java,v 1.6 2004/12/05 22:19:44 mbogaert Exp $
22  */

23 public class OgnlValueStackDataSource implements JRDataSource {
24     //~ Static fields/initializers /////////////////////////////////////////////
25

26     /**
27      * Logger for this class
28      */

29     private static Log log = LogFactory.getLog(OgnlValueStackDataSource.class);
30
31     //~ Instance fields ////////////////////////////////////////////////////////
32

33     Iterator iterator;
34     OgnlValueStack valueStack;
35     boolean firstTimeThrough = true;
36
37     //~ Constructors ///////////////////////////////////////////////////////////
38

39     /**
40      * Create a value stack data source on the given iterable property
41      *
42      * @param valueStack The value stack to base the data source on
43      * @param dataSource The property to iterate over for the report
44      */

45     public OgnlValueStackDataSource(OgnlValueStack valueStack, String JavaDoc dataSource) {
46         this.valueStack = valueStack;
47
48         Object JavaDoc dataSourceValue = valueStack.findValue(dataSource);
49
50         if (dataSourceValue != null) {
51             if (MakeIterator.isIterable(dataSourceValue)) {
52                 iterator = MakeIterator.convert(dataSourceValue);
53             } else {
54                 Object JavaDoc[] array = new Object JavaDoc[1];
55                 array[0] = dataSourceValue;
56                 iterator = MakeIterator.convert(array);
57             }
58         } else {
59             log.warn("Data source value for data source " + dataSource + " was null");
60         }
61     }
62
63     //~ Methods ////////////////////////////////////////////////////////////////
64

65     /**
66      * Get the value of a given field
67      *
68      * @param field The field to get the value for. The expression language to get the value
69      * of the field is either taken from the description property or from the name of the field
70      * if the description is <code>null</code>.
71      * @return an <code>Object</code> containing the field value or a new
72      * <code>OgnlValueStackDataSource</code> object if the field value evaluates to
73      * an object that can be iterated over.
74      * @throws JRException if there is a problem obtaining the value
75      */

76     public Object JavaDoc getFieldValue(JRField field) throws JRException {
77         //TODO: move the code to return a OgnlValueStackDataSource to a seperate
78
// method when and if the JRDataSource interface is updated to support
79
// this.
80
String JavaDoc expression = field.getDescription();
81
82         if (expression == null) {
83             //Description is optional so use the field name as a default
84
expression = field.getName();
85         }
86
87         Object JavaDoc value = valueStack.findValue(expression);
88
89         if (log.isDebugEnabled()) {
90             log.debug("field: " + field.getName() + "/" + value);
91         }
92
93         if (MakeIterator.isIterable(value)) {
94             // return new OgnlValueStackDataSource(this.valueStack, field.getName());
95
return new OgnlValueStackDataSource(this.valueStack, expression);
96         } else {
97             return value;
98         }
99     }
100
101     /**
102      * Is there any more data
103      *
104      * @return <code>true</code> if there are more elements to iterate over and
105      * <code>false</code> otherwise
106      * @throws JRException if there is a problem determining whether there
107      * is more data
108      */

109     public boolean next() throws JRException {
110         if (firstTimeThrough) {
111             firstTimeThrough = false;
112         } else {
113             valueStack.pop();
114         }
115
116         if ((iterator != null) && (iterator.hasNext())) {
117             valueStack.push(iterator.next());
118             log.debug("Pushed next value: " + valueStack.findValue("."));
119
120             return true;
121         } else {
122             log.debug("No more values");
123
124             return false;
125         }
126     }
127 }
128
Popular Tags