PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jasperreports-3.7.4/dist/docs/sample.reference/csvdatasource/index.html

https://gitlab.com/essere.lab.public/qualitas.class-corpus
HTML | 365 lines | 338 code | 27 blank | 0 comment | 0 complexity | 834ea13bf5ae514acea023b5904e6179 MD5 | raw file
  1. <html xmlns:jr="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  2. <head>
  3. <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
  4. <title>JasperReports 3.7.4 - CSV Data Source Sample</title>
  5. <style type="text/css">
  6. .title {
  7. font-family: Arial, Verdana, Helvetica, sans-serif;
  8. font-size: 28px;
  9. font-weight: normal;
  10. }
  11. .toc {
  12. font-family: Courier New, Courier, serif;
  13. font-size: 12px;
  14. font-weight: normal;
  15. }
  16. .name {
  17. font-family: Courier New, Courier, serif;
  18. font-size: 16px;
  19. font-weight: bold;
  20. }
  21. .label {
  22. font-family: Arial, Verdana, Helvetica, sans-serif;
  23. font-size: 12px;
  24. font-weight: bold;
  25. font-style: italic;
  26. }
  27. .description {
  28. font-family: Arial, Verdana, Helvetica, sans-serif;
  29. font-size: 12px;
  30. font-weight: normal;
  31. }
  32. .value {
  33. font-family: Courier New, Courier, serif;
  34. font-size: 12px;
  35. font-weight: normal;
  36. }
  37. .element {
  38. font-family: Courier New, Courier, serif;
  39. font-size: 12px;
  40. font-weight: normal;
  41. }
  42. .attribute {
  43. font-family: Courier New, Courier, serif;
  44. font-size: 12px;
  45. font-weight: bold;
  46. }
  47. .code {
  48. font-family: Courier New, Courier, serif;
  49. font-size: 12px;
  50. font-weight: normal;
  51. }
  52. </style>
  53. </head>
  54. <body bgcolor="#FFFFFF">
  55. <a name="top"></a>
  56. <table width="100%" border="0" cellpadding="0" cellspacing="0">
  57. <tr>
  58. <td align="right" colspan="2"><span class="element"><a href="../../sample.reference.html">Sample Reference</a> - <a href="../../schema.reference.html">Schema Reference</a> - <a href="../../config.reference.html">Configuration Reference</a> - <a href="http://jasperreports.sourceforge.net/api/index.html">API (Javadoc)</a></span>
  59. <br>
  60. </td>
  61. </tr>
  62. <tr>
  63. <td colspan="2">
  64. <hr size="1">
  65. </td>
  66. </tr>
  67. <tr valign="middle">
  68. <td nowrap="true"><span class="title">JasperReports - CSV Data Source Sample (version 3.7.4)</span></td><td align="right"><img border="0" src="../../resources/jasperreports.png"></td>
  69. </tr>
  70. <tr>
  71. <td colspan="2">
  72. <hr size="1">
  73. </td>
  74. </tr>
  75. </table>
  76. <br>
  77. <span class="description"><span class="description">Shows how the CSV data source implementation could be used to fill reports.</span></span>
  78. <br>
  79. <br>
  80. <span class="element"><a href="http://sourceforge.net/projects/jasperreports/files/jasperreports/JasperReports%203.7.4/jasperreports-3.7.4-project.zip/download" target="_blank">Download All Sample Source Files</a></span>
  81. <br>
  82. <span class="element"><a href="http://jasperforge.org/scm/viewvc.php/tags/jr-3-7-4/jasperreports/demo/samples/csvdatasource/?root=jasperreports" target="_blank">Browse Sample Source Files on SVN</a></span>
  83. <table cellspacing="0" cellpadding="0" border="0" width="100%">
  84. <tr>
  85. <td style="width: 20px;">
  86. <br>
  87. </td><td>
  88. <br>
  89. </td>
  90. </tr>
  91. <tr>
  92. <td colspan="2"><span class="label">Main Features in This Sample</span></td>
  93. </tr>
  94. <tr>
  95. <td>
  96. <br>
  97. </td><td><span class="element"><a href="#csvdatasource">CSV Data Source</a></span></td>
  98. </tr>
  99. <tr>
  100. <td colspan="2">
  101. <br>
  102. </td>
  103. </tr>
  104. <tr>
  105. <td colspan="2"><span class="label">Secondary Features</span></td>
  106. </tr>
  107. <tr>
  108. <td></td><td><span class="element"><a href="../datasource/index.html#datasources">Data Sources</a></span></td>
  109. </tr>
  110. </table>
  111. <table border="0" cellpadding="0" cellspacing="0" width="100%">
  112. <tr>
  113. <td><img height="1" width="20" border="0" src="../../resources/px.gif"></td><td><img height="1" width="20" border="0" src="../../resources/px.gif"></td><td><img height="1" width="20" border="0" src="../../resources/px.gif"></td><td><img height="1" width="20" border="0" src="../../resources/px.gif"></td><td width="100%">
  114. <br>
  115. </td>
  116. </tr>
  117. <tr>
  118. <td align="right" colspan="5"><a name="csvdatasource"></a><a class="toc" href="#top">top</a></td>
  119. </tr>
  120. <tr>
  121. <td colspan="5">
  122. <hr size="1">
  123. </td>
  124. </tr>
  125. <tr valign="top">
  126. <td><img border="0" src="../../resources/jr-16x16.png"></td><td colspan="4"><span class="name">CSV Data Source</span></td>
  127. </tr>
  128. <tr>
  129. <td colspan="5">
  130. <br>
  131. </td>
  132. </tr>
  133. <tr valign="top">
  134. <td>
  135. <br>
  136. </td><td nowrap="true"><span class="label">Description / Goal</span></td><td>
  137. <br>
  138. </td><td colspan="2"><span class="description">
  139. How to fill a report using data from a CSV file.
  140. </span></td>
  141. </tr>
  142. <tr valign="top">
  143. <td>
  144. <br>
  145. </td><td colspan="1"><span class="label">Since</span></td><td>
  146. <br>
  147. </td><td colspan="2"><span class="description">1.2.0</span></td>
  148. </tr>
  149. <tr valign="top">
  150. <td>
  151. <br>
  152. </td><td nowrap="true" colspan="1"><span class="label">Other Samples</span></td><td>
  153. <br>
  154. </td><td colspan="2">
  155. <table border="0" cellpadding="0" cellspacing="0" width="100%">
  156. <tr>
  157. <td><span class="element"><a href="../datasource/index.html">/demo/samples/datasource</a></span></td>
  158. </tr>
  159. </table>
  160. </td>
  161. </tr>
  162. <tr>
  163. <td colspan="5">
  164. <br>
  165. </td>
  166. </tr>
  167. <tr>
  168. <td>
  169. <br>
  170. </td><td colspan="4"><span class="description">
  171. <b>CSV Data Sources</b>
  172. <br>
  173. <br>
  174. Report filling is one of the basic steps during the report generation. After the report compilation,
  175. significant report data are read from the report data source, or calculated from report expressions,
  176. and the generated <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JasperReport.html" target="_blank">JasperReport</a></span> object
  177. is filled section by section.
  178. <br>
  179. Data sources are very useful when data come as a set of structured records, either extracted from a
  180. relational database, or loaded from specific files. In order to become more familiar with data source
  181. objects please consult the <a href="../datasources.html#datasources" target="_blank"><span class="element">Data Sources</span></a> section.
  182. <br>
  183. Sometimes data that users need to fill the report with is found in plain text files, in a
  184. certain format, such as the popular CSV (comma-separated value).
  185. <br>
  186. JasperReports provides an implementation for such a data source, by wrapping the CSV
  187. data from a text file into a <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/data/JRCsvDataSource.html" target="_blank">JRCsvDataSource</a></span>.
  188. The CSV data source usually needs to read a file from disk, or at least from an input stream.
  189. <br>
  190. CSV is a file format with very few formatting rules: data rows are separated by a record
  191. delimiter (text sequence) and fields inside each row are separated by a field delimiter
  192. (character). Fields containing delimiter characters can be placed inside quotes. If fields
  193. contain quotes themselves, these are duplicated (for example, <code>"John ""Doe"""</code> will be
  194. displayed as <code>John "Doe"</code>).
  195. <br>
  196. The most common delimiters for CSV files are:
  197. <ul>
  198. <li>comma (<code>,</code>) - as field delimiter</li>
  199. <li>newline (<code>\n</code>) - as record delimiter</li>
  200. </ul>
  201. Users can override these default values by calling <code>setFieldDelimiter(char)</code> and
  202. <code>setRecordDelimiter(String)</code> of the <code>JRCsvDataSource</code> class.
  203. <br>
  204. There are two categories of CSV files:
  205. <ol>
  206. <li>files with a header record containing column names, all the other records containing field values;</li>
  207. <li>files without a header record; all records contain only field values. This is the default category.</li>
  208. </ol>
  209. For the files in the first category, column names are read from the first row in the CSV file.
  210. But the engine should be informed that the input file belongs to the
  211. first category. This can be done by calling the <code>setUseFirstRowAsHeader(true)</code> method of the <code>JRCsvDataSource</code>.
  212. <br>
  213. If files belong to the second category (default), column names have to be provided separately, taking into account that
  214. the column names order should be the same as the column order in a data record. If this is not possible,
  215. the default naming convention is to use the <code>COLUMN_</code> prefix followed by the zero-based column index.
  216. <br>
  217. For instance, if is known that a particular column is the third column in the record
  218. (index=2), then one could name the corresponding field "COLUMN_2" and use the
  219. column data without problems.
  220. <br>
  221. Another problem when working with CSV files is related to data types. Handling data types
  222. for fields in CSV data sources is special since the CSV file format
  223. does not provide such information. This kind of matter is solved by trying to match each field in
  224. the data source to its corresponding report field type. For number and date/time fields,
  225. converting text values to <code>java.lang.Number</code> and <code>java.util.Date</code> values respectively
  226. requires parsing using format objects. This is controlled by specifying the date and
  227. number format objects to be used with the <code>JRCsvDataSource</code> instance by calling its
  228. <code>setDateFormat(DateFormat)</code> and <code>setNumberFormat(NumberFormat)</code> methods
  229. before passing it to the report-filling process.
  230. <br>
  231. <br>
  232. <b>CSV Data Source Example</b>
  233. <br>
  234. <br>
  235. In our example data records are stored in the /data/CsvDataSource.txt file. Although the file extension is <code>.txt</code>,
  236. it contains structured data according to the CSV rules:
  237. <pre>
  238. "Dallas",47,"Janet Fuller","445 Upland Pl.","Trial"
  239. "Lyon",38,"Andrew Heiniger","347 College Av.","Active"
  240. "Dallas",43,"Susanne Smith","2 Upland Pl.","Active"
  241. "Berne",22,"Bill Ott","250 - 20th Ave.","Active"
  242. "Boston",32,"Michael Ott","339 College Av.","Trial"
  243. "Dallas",4,"Sylvia Ringer","365 College Av.","Active"
  244. "Boston",23,"Julia Heiniger","358 College Av.","Active"
  245. "Chicago",39,"Mary Karsen","202 College Av.","Active"
  246. "Dallas",40,"Susanne Miller","440 - 20th Ave.","Trial"
  247. "Berne",9,"James Schneider","277 Seventh Av.","Active"
  248. "Dallas",36,"John Steel","276 Upland Pl.","Suspended"
  249. "Chicago",35,"George Karsen","412 College Av.","Suspended"
  250. "Dallas",37,"Michael Clancy","19 Seventh Av.","Deleted"
  251. "Lyon",2,"Anne Miller","20 Upland Pl.","Active"
  252. "Dallas",0,"Laura Steel","429 Seventh Av.","Active"
  253. "Lyon",28,"Susanne White","74 - 20th Ave.","Deleted"
  254. "Paris",5,"Laura Miller","294 Seventh Av.","Active"
  255. "Lyon",17,"Laura Ott","443 Seventh Av.","Active"
  256. "New York",46,"Andrew May","172 Seventh Av.","Active"
  257. "New York",44,"Sylvia Ott","361 College Av.","Active"
  258. "Dallas",19,"Susanne Heiniger","86 - 20th Ave.","Active"
  259. "Chicago",11,"Julia White","412 Upland Pl.","Active"
  260. "Dallas",10,"Anne Fuller","135 Upland Pl.","Active"
  261. "New York",41,"Bill King","546 College Av.","Deleted"
  262. "Oslo",45,"Janet May","396 Seventh Av.","Active"
  263. "Paris",18,"Sylvia Fuller","158 - 20th Ave.","Trial"
  264. "San Francisco",48,"Robert White","549 Seventh Av.","Active"
  265. "Paris",25,"Sylvia Steel","269 College Av.","Suspended"
  266. "San Francisco",7,"James Peterson","231 Upland Pl.","Active"
  267. "Oslo",42,"Robert Ott","503 Seventh Av.","Trial"
  268. </pre>
  269. The file has no header row with column names. Column names are set independently, as shown in the
  270. <code>getDataSource()</code> method in the /src/CsvDataSourceApp.java file:
  271. <pre>
  272. private static JRCsvDataSource getDataSource() throws JRException
  273. {
  274. String[] columnNames = new String[]{"city", "id", "name", "address", "state"};
  275. JRCsvDataSource ds = new JRCsvDataSource(JRLoader.getLocationInputStream("data/CsvDataSource.txt"));
  276. ds.setRecordDelimiter("\r\n");
  277. ds.setColumnNames(columnNames);
  278. return ds;
  279. }
  280. </pre>
  281. The five column names are: <code>city</code>, <code>id</code>, <code>name</code>, <code>address</code> and <code>state</code>,
  282. in this particular order. Field names are identical to the column names defined here.
  283. <br>
  284. The <code>JRCsvDataSource</code> object prepared above is passed to the engine at fill time (see again the /src/CsvDataSourceApp.java file):
  285. <pre>
  286. public void fill() throws JRException
  287. {
  288. long start = System.currentTimeMillis();
  289. //Preparing parameters
  290. Map parameters = new HashMap();
  291. parameters.put("ReportTitle", "Address Report");
  292. parameters.put("DataFile", "CsvDataSource.txt - CSV data source");
  293. Set states = new HashSet();
  294. states.add("Active");
  295. states.add("Trial");
  296. parameters.put("IncludedStates", states);
  297. JasperFillManager.fillReportToFile("build/reports/CsvDataSourceReport.jasper", parameters, getDataSource());
  298. System.err.println("Filling time : " + (System.currentTimeMillis() - start));
  299. }
  300. </pre>
  301. The <code>IncludedStates</code> parameter defined above is used for data filtering. Only records with <code>Active</code>
  302. or <code>Trial</code> states will be taken into account:
  303. <br>
  304. <br>
  305. <code>
  306. &nbsp;&nbsp;&lt;parameter name="IncludedStates" class="java.util.Set"/&gt;
  307. <br>
  308. <br>
  309. &nbsp;&nbsp;...
  310. <br>
  311. <br>
  312. &nbsp;&nbsp;&lt;filterExpression&gt;&lt;![CDATA[$P{IncludedStates}.contains($F{state}) ? Boolean.TRUE : Boolean.FALSE]]&gt;&lt;/filterExpression&gt;
  313. </code>
  314. <br>
  315. <br>
  316. In order to figure out more on CSV data sources, just test this sample by running from the command line the <code>ant test view</code> command.
  317. It will generate all supported document types containing the sample report in the /build/reports directory, and then the report will be open with JasperReports internal viewer.
  318. </span></td>
  319. </tr>
  320. <tr>
  321. <td colspan="5">
  322. <br>
  323. </td>
  324. </tr>
  325. </table>
  326. <br>
  327. <table width="100%" border="0" cellpadding="0" cellspacing="0">
  328. <tr>
  329. <td>
  330. <hr size="1">
  331. </td>
  332. </tr>
  333. <tr>
  334. <td align="center"><span style="font-decoration:none;font-family:Verdana,Arial,Helvetica,sans-serif;font-size:8pt;font-style:normal;color:#000000;">&copy; 2001-2010 Jaspersoft Corporation <a style="color:#000000;" target="_blank" href="http://www.jaspersoft.com">www.jaspersoft.com</a></span></td>
  335. </tr>
  336. </table>
  337. </body>
  338. </html>