@Override protected void configureReport(JasperReportBuilder rb) { try { rb.setLocale(Locale.ENGLISH) .columns( column1 = col.column("Column1", field("field1", type.stringType()).setDescription("@field1")), column2 = col.column("Column2", field("field2", type.integerType())), column3 = col.column("Column3", field("field3", type.bigDecimalType()))) .setQuery("/data/row1", QueryLanguage.XPATH) .setParameter(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT, JRXmlUtils.parse(XmlReportTest.class.getResourceAsStream("data.xml"))) .summary(cmp.subreport(createSubreport())); } catch (JRException e) { e.printStackTrace(); Assert.fail(e.getMessage()); } }
/** * */ public void fill() throws JRException { long start = System.currentTimeMillis(); Map<String, Object> params = new HashMap<String, Object>(); Document document = JRXmlUtils.parse(JRLoader.getLocationInputStream("data/northwind.xml")); params.put(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT, document); params.put(JRXPathQueryExecuterFactory.XML_DATE_PATTERN, "yyyy-MM-dd"); params.put(JRXPathQueryExecuterFactory.XML_NUMBER_PATTERN, "#,##0.##"); params.put(JRXPathQueryExecuterFactory.XML_LOCALE, Locale.ENGLISH); params.put(JRParameter.REPORT_LOCALE, Locale.US); JasperFillManager.fillReportToFile("build/reports/CustomersReport.jasper", params); System.err.println("Filling time : " + (System.currentTimeMillis() - start)); }
protected Map<String, Object> readDataFile(String xmlDataFile) throws JRException { Document document = JRXmlUtils.parse(JRLoader.getLocationInputStream(xmlDataFile)); Map<String, Object> params = new HashMap<String, Object>(); params.put(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT, document); params.put(JRXPathQueryExecuterFactory.XML_DATE_PATTERN, "yyyy-MM-dd"); params.put(JRXPathQueryExecuterFactory.XML_NUMBER_PATTERN, "#,##0.##"); params.put(JRXPathQueryExecuterFactory.XML_LOCALE, Locale.ENGLISH); params.put(JRParameter.REPORT_LOCALE, Locale.US); return params; }
@Override public void contributeParameters(Map<String, Object> parameters) throws JRException { RemoteXmlDataAdapter remoteXmlDataAdapter = getRemoteXmlDataAdapter(); if (remoteXmlDataAdapter != null) { if (remoteXmlDataAdapter.isUseConnection()) { String fileName = remoteXmlDataAdapter.getFileName(); if (fileName.toLowerCase().startsWith("https://") || fileName.toLowerCase().startsWith("http://") || fileName.toLowerCase().startsWith("file:")) { // JRXPathQueryExecuterFactory.XML_URL not available. // Once this is available, remove XML_URL from this class. parameters.put(XML_URL, fileName); } else { InputStream dataStream = RepositoryUtil.getInstance(getJasperReportsContext()).getInputStreamFromLocation(remoteXmlDataAdapter.getFileName()); try { Document document = JRXmlUtils.parse(dataStream, remoteXmlDataAdapter.isNamespaceAware()); parameters.put(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT, document); } finally { try { dataStream.close(); } catch (IOException e) { log.warn("Failed to close input stream for " + remoteXmlDataAdapter.getFileName()); } } } Locale locale = remoteXmlDataAdapter.getLocale(); if (locale != null) { parameters.put(JRXPathQueryExecuterFactory.XML_LOCALE, locale); } TimeZone timeZone = remoteXmlDataAdapter.getTimeZone(); if (timeZone != null) { parameters.put(JRXPathQueryExecuterFactory.XML_TIME_ZONE, timeZone); } String datePattern = remoteXmlDataAdapter.getDatePattern(); if (datePattern != null && datePattern.trim().length()>0) { parameters.put(JRXPathQueryExecuterFactory.XML_DATE_PATTERN, datePattern); } String numberPattern = remoteXmlDataAdapter.getNumberPattern(); if (numberPattern != null && numberPattern.trim().length()>0) { parameters.put(JRXPathQueryExecuterFactory.XML_NUMBER_PATTERN, numberPattern); } } } }
public JasperPrint fillReport(ReportEngineInput input) throws ProviderException { Connection conn = null; Report report = input.getReport(); Map<String,Object> parameters = input.getParameters(); ReportDataSource dataSource = report.getDataSource(); try { JasperReport jr = null; if (report.isQueryReport()) return fillQueryReport(report, parameters, input.getExportType()); jr = (JasperReport) JRLoader .loadObject(directoryProvider.getReportDirectory() + report.getFile()); List<ReportParameterMap> subReports = report.getSubReportParameters(); if (subReports != null && subReports.size() > 0) { Iterator<ReportParameterMap> iterator = report.getSubReportParameters().iterator(); while (iterator.hasNext()) { ReportParameterMap rpMap = iterator.next(); JasperReport subReport = (JasperReport) JRLoader.loadObject(directoryProvider .getReportDirectory() + rpMap.getReportParameter().getData()); parameters.put(rpMap.getReportParameter().getName(), subReport); } } JasperPrint jp = null; // create new HashMap to send to JasperReports in order to // fix serialization problems Map<String,Object> jasperReportMap = new HashMap<String,Object>(parameters); if (input.getXmlInput() != null) { ByteArrayInputStream stream = new ByteArrayInputStream(input.getXmlInput().getBytes()); Document document = JRXmlUtils.parse(stream); jasperReportMap.put(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT, document); jp = JasperFillManager.fillReport(jr, jasperReportMap); } else if (dataSource == null) { jp = JasperFillManager.fillReport(jr, jasperReportMap, new JREmptyDataSource()); } else { conn = dataSourceProvider.getConnection(dataSource.getId()); jp = JasperFillManager.fillReport(jr, jasperReportMap, conn); } if (jp == null || jp.getPages().size() < 1) throw new ProviderException(LocalStrings.ERROR_REPORT_EMPTY); return jp; } catch (Exception e) { if (!e.getMessage().equals(LocalStrings.ERROR_REPORT_EMPTY)) log.error("JasperReportEngine.fillReport", e); throw new ProviderException(e.getMessage()); } finally { try { if (conn != null) conn.close(); } catch (Exception ex) { log.error("Error closing connection: " + ex.getMessage()); } } }
/** * This method is call before the datasource is used and permit to add special parameters to the map * */ @SuppressWarnings("unchecked") @Override public java.util.Map getSpecialParameters(java.util.Map map) throws net.sf.jasperreports.engine.JRException { if (isUseConnection()) { /* if (this.getFilename().toLowerCase().startsWith("https://") || this.getFilename().toLowerCase().startsWith("http://") || this.getFilename().toLowerCase().startsWith("file:")) { map.put(JRXPathQueryExecuterFactory.XML_URL, this.getFilename()); } else { */ Document document = JRXmlUtils.parse(new File( this.getFilename())); map.put(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT, document); //} if (getLocale()!=null) { map.put(JRXPathQueryExecuterFactory.XML_LOCALE, getLocale()); } if (getTimeZone() != null) { map.put(JRXPathQueryExecuterFactory.XML_TIME_ZONE, getTimeZone()); } if (getDatePattern() != null && getDatePattern().trim().length()>0) { map.put(JRXPathQueryExecuterFactory.XML_DATE_PATTERN, getDatePattern()); } if (getNumberPattern() != null && getNumberPattern().trim().length()>0) { map.put(JRXPathQueryExecuterFactory.XML_NUMBER_PATTERN, getNumberPattern()); } } return map; }