import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Vector; import dori.jasper.engine.JRBand; import dori.jasper.engine.JRElement; import dori.jasper.engine.JRException; import dori.jasper.engine.JRGroup; import dori.jasper.engine.JRImage; import dori.jasper.engine.JRParameter; import dori.jasper.engine.util.JRLoader; /* * Created on 31/10/2003 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ /** * @author bsutton * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public abstract class JasperReport { static public final int FORMAT_HTML = 1; static public final int FORMAT_PDF = 2; private File m_filReport; private File m_filImagePath; private String m_strTitle; protected dori.jasper.engine.JasperReport m_report; protected Map m_mapParameters = new HashMap(); JasperReport( String strReportPath, String strImagePath, String strReportTitle, Map mapParameters) throws JRException, IOException { this(new File(strReportPath), new File(strImagePath), strReportTitle, mapParameters); } JasperReport(File filReport, File filImagePath, String strReportTitle, Map mapParameters) throws JRException, IOException { m_filReport = filReport; m_filImagePath = filImagePath; m_strTitle = strReportTitle; // Load the report. m_report = (dori.jasper.engine.JasperReport) JRLoader.loadObject( filReport.getPath()); m_mapParameters.put("ReportTitle", m_strTitle); m_mapParameters.put("DocumentBase", filImagePath.getCanonicalPath()); if (mapParameters != null) setParameters(mapParameters); } void setParameter(String strKey, Object oValue) { m_mapParameters.put(strKey, oValue); } void setParameters(Map mapParameters) { m_mapParameters.putAll(mapParameters); } void validate() throws JRException { // Check that all of the reports required parameters have been set. HashMap mapParameters = new HashMap(m_mapParameters); JRParameter[] aParams = m_report.getParameters(); for (int i = 0; i < aParams.length; i++) { String strName = aParams[i].getName(); if (!m_mapParameters.containsKey(strName)) throw new JRException( "Required Jasper Parameter " + strName + " not passed to report."); mapParameters.remove(strName); } // Check that there is a JRParameter for every parameter passed in if (mapParameters.size() != 0) throw new JRException( "The following parameters were not found in the Report: " + mapParameters.toString()); } /** * @return */ public Iterator getImageFiles() { JRBand[] aBands = getBands(); Vector vImages = new Vector(); for (int i = 0; i < aBands.length; i++) { JRElement[] elements = aBands[i].getElements(); if (elements != null) { for (int j = 0; j < elements.length; j++) { if (elements[j] instanceof JRImage) { JRImage image = (JRImage) elements[j]; String strExpression = image.getExpression().getText(); File filImage = new File(evaluate(strExpression)); vImages.add(filImage); } } } } return vImages.iterator(); } /** * @param strExpression * TODO: enhance evaluate to emulate the Jasper expression evaulation as * per dori.jasper.engine.fill.evaluateImage(). */ private String evaluate(String strExpression) { if (strExpression.charAt(0) == '"' && strExpression.charAt(strExpression.length() - 1) == '"') strExpression = strExpression.substring(1, strExpression.length() - 1); // Replace a double backslash with a single backslash int nLen = 0; while (nLen != strExpression.length()) { nLen = strExpression.length(); strExpression = strExpression.replaceAll("\\\\\\\\", "\\\\"); } return strExpression; } public JRBand[] getBands() { Vector vBands = new Vector(); vBands.add(m_report.getBackground()); vBands.add(m_report.getColumnFooter()); vBands.add(m_report.getColumnHeader()); vBands.add(m_report.getDetail()); vBands.add(m_report.getPageFooter()); vBands.add(m_report.getPageHeader()); vBands.add(m_report.getSummary()); vBands.add(m_report.getTitle()); JRGroup[] groups = m_report.getGroups(); if (groups != null) { for (int i = 0; i < groups.length; i++) { vBands.add(groups[i].getGroupFooter()); vBands.add(groups[i].getGroupHeader()); } } JRBand[] aBand = new JRBand[1]; aBand = (JRBand[]) vBands.toArray(aBand); return aBand; } }