1 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 ; 16 17 23 public class OgnlValueStackDataSource implements JRDataSource { 24 26 29 private static Log log = LogFactory.getLog(OgnlValueStackDataSource.class); 30 31 33 Iterator iterator; 34 OgnlValueStack valueStack; 35 boolean firstTimeThrough = true; 36 37 39 45 public OgnlValueStackDataSource(OgnlValueStack valueStack, String dataSource) { 46 this.valueStack = valueStack; 47 48 Object dataSourceValue = valueStack.findValue(dataSource); 49 50 if (dataSourceValue != null) { 51 if (MakeIterator.isIterable(dataSourceValue)) { 52 iterator = MakeIterator.convert(dataSourceValue); 53 } else { 54 Object [] array = new Object [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 65 76 public Object getFieldValue(JRField field) throws JRException { 77 String expression = field.getDescription(); 81 82 if (expression == null) { 83 expression = field.getName(); 85 } 86 87 Object 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, expression); 96 } else { 97 return value; 98 } 99 } 100 101 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 |