/projects/jasperreports-3.7.4/demo/samples/list/src/ListApp.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 474 lines · 292 code · 101 blank · 81 comment · 16 complexity · be51f8639cf9e9cf9b020158b884e0b1 MD5 · raw file

  1. /*
  2. * JasperReports - Free Java Reporting Library.
  3. * Copyright (C) 2001 - 2009 Jaspersoft Corporation. All rights reserved.
  4. * http://www.jaspersoft.com
  5. *
  6. * Unless you have purchased a commercial license agreement from Jaspersoft,
  7. * the following license terms apply:
  8. *
  9. * This program is part of JasperReports.
  10. *
  11. * JasperReports is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * JasperReports is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. import java.io.File;
  25. import net.sf.jasperreports.engine.JRException;
  26. import net.sf.jasperreports.engine.JRExporterParameter;
  27. import net.sf.jasperreports.engine.JasperExportManager;
  28. import net.sf.jasperreports.engine.JasperFillManager;
  29. import net.sf.jasperreports.engine.JasperPrint;
  30. import net.sf.jasperreports.engine.JasperPrintManager;
  31. import net.sf.jasperreports.engine.export.JExcelApiExporter;
  32. import net.sf.jasperreports.engine.export.JRCsvExporter;
  33. import net.sf.jasperreports.engine.export.JRRtfExporter;
  34. import net.sf.jasperreports.engine.export.JRXhtmlExporter;
  35. import net.sf.jasperreports.engine.export.JRXlsExporter;
  36. import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
  37. import net.sf.jasperreports.engine.export.oasis.JROdsExporter;
  38. import net.sf.jasperreports.engine.export.oasis.JROdtExporter;
  39. import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter;
  40. import net.sf.jasperreports.engine.export.ooxml.JRPptxExporter;
  41. import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
  42. import net.sf.jasperreports.engine.util.AbstractSampleApp;
  43. import net.sf.jasperreports.engine.util.JRLoader;
  44. /**
  45. * @author Lucian Chirita (lucianc@users.sourceforge.net)
  46. * @version $Id: ListApp.java 3150 2009-10-23 16:07:06Z lucianc $
  47. */
  48. public class ListApp extends AbstractSampleApp
  49. {
  50. /**
  51. *
  52. */
  53. public static void main(String[] args)
  54. {
  55. main(new ListApp(), args);
  56. }
  57. /**
  58. *
  59. */
  60. public void test() throws JRException
  61. {
  62. fill();
  63. pdf();
  64. xmlEmbed();
  65. xml();
  66. html();
  67. rtf();
  68. xls();
  69. jxl();
  70. csv();
  71. odt();
  72. ods();
  73. docx();
  74. xlsx();
  75. pptx();
  76. xhtml();
  77. }
  78. /**
  79. *
  80. */
  81. public void fill() throws JRException
  82. {
  83. File[] files = getFiles(new File("build/reports"), "jasper");
  84. for(int i = 0; i < files.length; i++)
  85. {
  86. File reportFile = files[i];
  87. long start = System.currentTimeMillis();
  88. JasperFillManager.fillReportToFile(
  89. reportFile.getAbsolutePath(),
  90. null,
  91. getDemoHsqldbConnection()
  92. );
  93. System.err.println("Report : " + reportFile + ". Filling time : " + (System.currentTimeMillis() - start));
  94. }
  95. }
  96. /**
  97. *
  98. */
  99. public void print() throws JRException
  100. {
  101. File[] files = getFiles(new File("build/reports"), "jrprint");
  102. for(int i = 0; i < files.length; i++)
  103. {
  104. File reportFile = files[i];
  105. long start = System.currentTimeMillis();
  106. JasperPrintManager.printReport(
  107. reportFile.getAbsolutePath(),
  108. true
  109. );
  110. System.err.println("Report : " + reportFile + ". Printing time : " + (System.currentTimeMillis() - start));
  111. }
  112. }
  113. /**
  114. *
  115. */
  116. public void pdf() throws JRException
  117. {
  118. File[] files = getFiles(new File("build/reports"), "jrprint");
  119. for(int i = 0; i < files.length; i++)
  120. {
  121. File reportFile = files[i];
  122. long start = System.currentTimeMillis();
  123. JasperExportManager.exportReportToPdfFile(
  124. reportFile.getAbsolutePath()
  125. );
  126. System.err.println("Report : " + reportFile + ". PDF creation time : " + (System.currentTimeMillis() - start));
  127. }
  128. }
  129. /**
  130. *
  131. */
  132. public void xml() throws JRException
  133. {
  134. File[] files = getFiles(new File("build/reports"), "jrprint");
  135. for(int i = 0; i < files.length; i++)
  136. {
  137. File reportFile = files[i];
  138. long start = System.currentTimeMillis();
  139. JasperExportManager.exportReportToXmlFile(
  140. reportFile.getAbsolutePath(),
  141. false
  142. );
  143. System.err.println("Report : " + reportFile + ". XML creation time : " + (System.currentTimeMillis() - start));
  144. }
  145. }
  146. /**
  147. *
  148. */
  149. public void xmlEmbed() throws JRException
  150. {
  151. File[] files = getFiles(new File("build/reports"), "jrprint");
  152. for(int i = 0; i < files.length; i++)
  153. {
  154. File reportFile = files[i];
  155. long start = System.currentTimeMillis();
  156. JasperExportManager.exportReportToXmlFile(
  157. reportFile.getAbsolutePath(),
  158. true
  159. );
  160. System.err.println("Report : " + reportFile + ". XML creation time : " + (System.currentTimeMillis() - start));
  161. }
  162. }
  163. /**
  164. *
  165. */
  166. public void html() throws JRException
  167. {
  168. File[] files = getFiles(new File("build/reports"), "jrprint");
  169. for(int i = 0; i < files.length; i++)
  170. {
  171. File reportFile = files[i];
  172. long start = System.currentTimeMillis();
  173. JasperExportManager.exportReportToHtmlFile(
  174. reportFile.getAbsolutePath()
  175. );
  176. System.err.println("Report : " + reportFile + ". HTML creation time : " + (System.currentTimeMillis() - start));
  177. }
  178. }
  179. /**
  180. *
  181. */
  182. public void rtf() throws JRException
  183. {
  184. File[] files = getFiles(new File("build/reports"), "jrprint");
  185. for(int i = 0; i < files.length; i++)
  186. {
  187. long start = System.currentTimeMillis();
  188. File sourceFile = files[i];
  189. JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
  190. File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".rtf");
  191. JRRtfExporter exporter = new JRRtfExporter();
  192. exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
  193. exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
  194. exporter.exportReport();
  195. System.err.println("Report : " + sourceFile + ". RTF creation time : " + (System.currentTimeMillis() - start));
  196. }
  197. }
  198. /**
  199. *
  200. */
  201. public void xls() throws JRException
  202. {
  203. File[] files = getFiles(new File("build/reports"), "jrprint");
  204. for(int i = 0; i < files.length; i++)
  205. {
  206. long start = System.currentTimeMillis();
  207. File sourceFile = files[i];
  208. JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
  209. File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".xls");
  210. JRXlsExporter exporter = new JRXlsExporter();
  211. exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
  212. exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
  213. exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
  214. exporter.exportReport();
  215. System.err.println("Report : " + sourceFile + ". XLS creation time : " + (System.currentTimeMillis() - start));
  216. }
  217. }
  218. /**
  219. *
  220. */
  221. public void jxl() throws JRException
  222. {
  223. File[] files = getFiles(new File("build/reports"), "jrprint");
  224. for(int i = 0; i < files.length; i++)
  225. {
  226. long start = System.currentTimeMillis();
  227. File sourceFile = files[i];
  228. JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
  229. File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".jxl.xls");
  230. JExcelApiExporter exporter = new JExcelApiExporter();
  231. exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
  232. exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
  233. exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
  234. exporter.exportReport();
  235. System.err.println("Report : " + sourceFile + ". XLS creation time : " + (System.currentTimeMillis() - start));
  236. }
  237. }
  238. /**
  239. *
  240. */
  241. public void csv() throws JRException
  242. {
  243. File[] files = getFiles(new File("build/reports"), "jrprint");
  244. for(int i = 0; i < files.length; i++)
  245. {
  246. long start = System.currentTimeMillis();
  247. File sourceFile = files[i];
  248. JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
  249. File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".csv");
  250. JRCsvExporter exporter = new JRCsvExporter();
  251. exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
  252. exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
  253. exporter.exportReport();
  254. System.err.println("Report : " + sourceFile + ". CSV creation time : " + (System.currentTimeMillis() - start));
  255. }
  256. }
  257. /**
  258. *
  259. */
  260. public void odt() throws JRException
  261. {
  262. File[] files = getFiles(new File("build/reports"), "jrprint");
  263. for(int i = 0; i < files.length; i++)
  264. {
  265. long start = System.currentTimeMillis();
  266. File sourceFile = files[i];
  267. JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
  268. File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".odt");
  269. JROdtExporter exporter = new JROdtExporter();
  270. exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
  271. exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
  272. exporter.exportReport();
  273. System.err.println("Report : " + sourceFile + ". ODT creation time : " + (System.currentTimeMillis() - start));
  274. }
  275. }
  276. /**
  277. *
  278. */
  279. public void ods() throws JRException
  280. {
  281. File[] files = getFiles(new File("build/reports"), "jrprint");
  282. for(int i = 0; i < files.length; i++)
  283. {
  284. long start = System.currentTimeMillis();
  285. File sourceFile = files[i];
  286. JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
  287. File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".ods");
  288. JROdsExporter exporter = new JROdsExporter();
  289. exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
  290. exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
  291. exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
  292. exporter.exportReport();
  293. System.err.println("Report : " + sourceFile + ". ODT creation time : " + (System.currentTimeMillis() - start));
  294. }
  295. }
  296. /**
  297. *
  298. */
  299. public void docx() throws JRException
  300. {
  301. File[] files = getFiles(new File("build/reports"), "jrprint");
  302. for(int i = 0; i < files.length; i++)
  303. {
  304. long start = System.currentTimeMillis();
  305. File sourceFile = files[i];
  306. JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
  307. File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".docx");
  308. JRDocxExporter exporter = new JRDocxExporter();
  309. exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
  310. exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
  311. exporter.exportReport();
  312. System.err.println("Report : " + sourceFile + ". DOCX creation time : " + (System.currentTimeMillis() - start));
  313. }
  314. }
  315. /**
  316. *
  317. */
  318. public void xlsx() throws JRException
  319. {
  320. File[] files = getFiles(new File("build/reports"), "jrprint");
  321. for(int i = 0; i < files.length; i++)
  322. {
  323. long start = System.currentTimeMillis();
  324. File sourceFile = files[i];
  325. JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
  326. File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".xlsx");
  327. JRXlsxExporter exporter = new JRXlsxExporter();
  328. exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
  329. exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
  330. exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
  331. exporter.exportReport();
  332. System.err.println("Report : " + sourceFile + ". XLSX creation time : " + (System.currentTimeMillis() - start));
  333. }
  334. }
  335. /**
  336. *
  337. */
  338. public void pptx() throws JRException
  339. {
  340. File[] files = getFiles(new File("build/reports"), "jrprint");
  341. for(int i = 0; i < files.length; i++)
  342. {
  343. long start = System.currentTimeMillis();
  344. File sourceFile = files[i];
  345. JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
  346. File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".pptx");
  347. JRPptxExporter exporter = new JRPptxExporter();
  348. exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
  349. exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
  350. exporter.exportReport();
  351. System.err.println("Report : " + sourceFile + ". PPTX creation time : " + (System.currentTimeMillis() - start));
  352. }
  353. }
  354. /**
  355. *
  356. */
  357. public void xhtml() throws JRException
  358. {
  359. File[] files = getFiles(new File("build/reports"), "jrprint");
  360. for(int i = 0; i < files.length; i++)
  361. {
  362. long start = System.currentTimeMillis();
  363. File sourceFile = files[i];
  364. JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
  365. File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".x.html");
  366. JRXhtmlExporter exporter = new JRXhtmlExporter();
  367. exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
  368. exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
  369. exporter.exportReport();
  370. System.err.println("Report : " + sourceFile + ". XHTML creation time : " + (System.currentTimeMillis() - start));
  371. }
  372. }
  373. }