Sophie

Sophie

distrib > Fedora > 17 > i386 > by-pkgid > cf79c2d0ed7fa2678c515072ee6f31e4 > files > 31

jasperreports-manual-4.0.2-4.fc17.noarch.rpm

<html xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jr="http://jasperreports.sourceforge.net/jasperreports">
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JasperReports 4.0.2 - CSV Data Source Sample</title>
<style type="text/css">
.title {
	font-family: Arial, Verdana, Helvetica, sans-serif;
	font-size: 28px;
	font-weight: normal;
}

.toc {
	font-family: Courier New, Courier, serif;
	font-size: 12px;
	font-weight: normal;
}

.name {
	font-family: Courier New, Courier, serif;
	font-size: 16px;
	font-weight: bold;
}

.label {
	font-family: Arial, Verdana, Helvetica, sans-serif;
	font-size: 12px;
	font-weight: bold;
	font-style: italic;
}

.description {
	font-family: Arial, Verdana, Helvetica, sans-serif;
	font-size: 12px;
	font-weight: normal;
}

.value {
	font-family: Courier New, Courier, serif;
	font-size: 12px;
	font-weight: normal;
}

.element {
	font-family: Courier New, Courier, serif;
	font-size: 12px;
	font-weight: normal;
}

.attribute {
	font-family: Courier New, Courier, serif;
	font-size: 12px;
	font-weight: bold;
}

.code {
	font-family: Courier New, Courier, serif;
	font-size: 12px;
	font-weight: normal;
}

.copy {
	font-decoration: none;
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 8pt;
	font-style: normal;
	color: #000000;
}

.subtitle {
	font-family: inherit;
	font-size: inherit;
	font-style: inherit;
	font-weight: bold;
	text-decoration: none;
	color: inherit;
}

</style>
</head>
<body bgcolor="#FFFFFF">
<a name="top"></a>
<table cellspacing="0" cellpadding="0" border="0" width="100%">
<tr>
<td colspan="2" align="right"><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>
<br>
</td>
</tr>
<tr>
<td colspan="2">
<hr size="1">
</td>
</tr>
<tr valign="middle">
<td nowrap="true"><span class="title">JasperReports - CSV Data Source Sample (version 4.0.2)</span></td><td align="right"><img src="../../resources/jasperreports.png" border="0"></td>
</tr>
<tr>
<td colspan="2">
<hr size="1">
</td>
</tr>
</table>
<br>
<span class="description"><span class="description">Shows how the CSV data source and the CSV query executer implementations could be used to fill reports.</span></span>
<br>
<br>
<span class="element"><a href="http://sourceforge.net/projects/jasperreports/files/jasperreports/JasperReports%204.0.2/jasperreports-4.0.2-project.zip/download" target="_blank">Download All Sample Source Files</a></span>
<br>
<span class="element"><a href="http://jasperforge.org/scm/viewvc.php/tags/jr-4-0-2/jasperreports/demo/samples/csvdatasource/?root=jasperreports" target="_blank">Browse Sample Source Files on SVN</a></span>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="width: 20px;">
<br>
</td><td>
<br>
</td>
</tr>
<tr>
<td colspan="2"><span class="label">Main Features in This Sample</span></td>
</tr>
<tr>
<td>
<br>
</td><td><span class="element"><a href="#csvdatasource">CSV Data Source</a></span></td>
</tr>
<tr>
<td>
<br>
</td><td><span class="element"><a href="#csvqueryexecuter">CSV Query Executer</a></span></td>
</tr>
<tr>
<td colspan="2">
<br>
</td>
</tr>
<tr>
<td colspan="2"><span class="label">Secondary Features</span></td>
</tr>
<tr>
<td></td><td><span class="element"><a href="../datasource/index.html#datasources">Data Sources</a></span></td>
</tr>
<tr>
<td></td><td><span class="element"><a href="../hibernate/index.html#queryexecuters">Query Executers</a></span></td>
</tr>
</table>
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td><img src="../../resources/px.gif" border="0" width="20" height="1"></td><td><img src="../../resources/px.gif" border="0" width="20" height="1"></td><td><img src="../../resources/px.gif" border="0" width="20" height="1"></td><td><img src="../../resources/px.gif" border="0" width="20" height="1"></td><td width="80%">
<br>
</td><td width="20%">
<br>
</td>
</tr>
<tr>
<td colspan="6" align="right"><a name="csvdatasource"></a><a href="#top" class="toc">top</a></td>
</tr>
<tr>
<td colspan="6">
<hr size="1">
</td>
</tr>
<tr valign="top">
<td><img src="../../resources/jr-16x16.png" border="0"></td><td colspan="4"><span class="name">CSV Data Source</span></td><td align="right"><span class="copy">Documented by 
	<a href="mailto:shertage@users.sourceforge.net" class="copy">Sanda Zaharia</a></span></td>
</tr>
<tr>
<td colspan="6">
<br>
</td>
</tr>
<tr valign="top">
<td>
<br>
</td><td nowrap="true"><span class="label">Description / Goal</span></td><td>
<br>
</td><td colspan="3"><span class="description">
How to fill a report using data from a CSV file.
    </span></td>
</tr>
<tr valign="top">
<td>
<br>
</td><td colspan="1"><span class="label">Since</span></td><td>
<br>
</td><td colspan="3"><span class="description">1.2.0</span></td>
</tr>
<tr valign="top">
<td>
<br>
</td><td nowrap="true"><span class="label">Other Samples</span></td><td>
<br>
</td><td colspan="3">
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td><span class="element"><a href="../datasource/index.html">/demo/samples/datasource</a></span></td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="6">
<br>
</td>
</tr>
<tr>
<td>
<br>
</td><td colspan="5"><span class="description">
<b>CSV Data Sources</b>

<br>

<br>
Report filling is one of the basic steps during the report generation. After the report compilation, 
significant report data are read from the report data source, or calculated from report expressions, 
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 
is filled section by section. 
<br>
Data sources are very useful when data come as a set of structured records, either extracted from a 
relational database, or loaded from specific files. In order to become more familiar with data source 
objects please consult the <a href="../datasources.html#datasources" target="_blank" class="element">Data Sources</a> section.
<br>
Sometimes data that users need to fill the report with is found in plain text files, in a 
certain format, such as the popular CSV (comma-separated value).
<br>
JasperReports provides an implementation for such a data source, by wrapping the CSV 
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>. 
The CSV data source usually needs to read a file from disk, or at least from an input stream. 
<br>
CSV is a file format with very few formatting rules: data rows are separated by a record 
delimiter (text sequence) and fields inside each row are separated by a field delimiter 
(character). Fields containing delimiter characters can be placed inside quotes. If fields 
contain quotes themselves, these are duplicated (for example, <span class="code"><code>"John ""Doe"""</code></span> will be 
displayed as <span class="code"><code>John "Doe"</code></span>).
<br>
The most common delimiters for CSV files are:
<ul>

<li>comma (<span class="code"><code>,</code></span>) - as field delimiter</li>

<li>newline (<span class="code"><code>\n</code></span>) - as record delimiter</li>

</ul>
Users can override these default values by calling <span class="code"><code>setFieldDelimiter(char)</code></span> and 
<span class="code"><code>setRecordDelimiter(String)</code></span> of the <span class="code"><code>JRCsvDataSource</code></span> class. 
<br>
There are two categories of CSV files:
<ol>

<li>files with a header record containing column names, all the other records containing field values;</li>

<li>files without a header record; all records contain only field values. This is the default category.</li>

</ol>
For the files in the first category, column names are read from the first row in the CSV file. 
But the engine should be informed that the input file belongs to the 
first category. This can be done by calling the <span class="code"><code>setUseFirstRowAsHeader(true)</code></span> method of the <span class="code"><code>JRCsvDataSource</code></span>.
<br>
If files belong to the second category (default), column names have to be provided separately, taking into account that 
the column names order should be the same as the column order in a data record. If this is not possible, 
the default naming convention is to use the <span class="code"><code>COLUMN_</code></span> prefix followed by the zero-based column index.
<br>
For instance, if is known that a particular column is the third column in the record 
(index=2), then one could name the corresponding field "COLUMN_2" and use the 
column data without problems.
<br> 
Another problem when working with CSV files is related to data types. Handling data types 
for fields in CSV data sources is special since the CSV file format 
does not provide such information. This kind of matter is solved by trying to match each field in 
the data source to its corresponding report field type. For number and date/time fields, 
converting text values to <span class="code"><code>java.lang.Number</code></span> and <span class="code"><code>java.util.Date</code></span> values respectively 
requires parsing using format objects. This is controlled by specifying the date and 
number format objects to be used with the <span class="code"><code>JRCsvDataSource</code></span> instance by calling its 
<span class="code"><code>setDateFormat(DateFormat)</code></span> and <span class="code"><code>setNumberFormat(NumberFormat)</code></span> methods 
before passing it to the report-filling process.
<br>

<br>

<b>CSV Data Source Example</b>

<br>

<br>
In our example data records are stored in the /data/CsvDataSource.txt file. Although the file extension is <span class="code"><code>.txt</code></span>, 
it contains structured data according to the CSV rules:
<pre>
  "Dallas",47,"Janet Fuller","445 Upland Pl.","Trial"
  "Lyon",38,"Andrew Heiniger","347 College Av.","Active"
  "Dallas",43,"Susanne Smith","2 Upland Pl.","Active"
  "Berne",22,"Bill Ott","250 - 20th Ave.","Active"
  "Boston",32,"Michael Ott","339 College Av.","Trial"
  "Dallas",4,"Sylvia Ringer","365 College Av.","Active"
  "Boston",23,"Julia Heiniger","358 College Av.","Active"
  "Chicago",39,"Mary Karsen","202 College Av.","Active"
  "Dallas",40,"Susanne Miller","440 - 20th Ave.","Trial"
  "Berne",9,"James Schneider","277 Seventh Av.","Active"
  "Dallas",36,"John Steel","276 Upland Pl.","Suspended"
  "Chicago",35,"George Karsen","412 College Av.","Suspended"
  "Dallas",37,"Michael Clancy","19 Seventh Av.","Deleted"
  "Lyon",2,"Anne Miller","20 Upland Pl.","Active"
  "Dallas",0,"Laura Steel","429 Seventh Av.","Active"
  "Lyon",28,"Susanne White","74 - 20th Ave.","Deleted"
  "Paris",5,"Laura Miller","294 Seventh Av.","Active"
  "Lyon",17,"Laura Ott","443 Seventh Av.","Active"
  "New York",46,"Andrew May","172 Seventh Av.","Active"
  "New York",44,"Sylvia Ott","361 College Av.","Active"
  "Dallas",19,"Susanne Heiniger","86 - 20th Ave.","Active"
  "Chicago",11,"Julia White","412 Upland Pl.","Active"
  "Dallas",10,"Anne Fuller","135 Upland Pl.","Active"
  "New York",41,"Bill King","546 College Av.","Deleted"
  "Oslo",45,"Janet May","396 Seventh Av.","Active"
  "Paris",18,"Sylvia Fuller","158 - 20th Ave.","Trial"
  "San Francisco",48,"Robert White","549 Seventh Av.","Active"
  "Paris",25,"Sylvia Steel","269 College Av.","Suspended"
  "San Francisco",7,"James Peterson","231 Upland Pl.","Active"
  "Oslo",42,"Robert Ott","503 Seventh Av.","Trial"
</pre>
The file has no header row with column names. Column names are set independently, as shown in the 
<span class="code"><code>getDataSource()</code></span> method in the /src/CsvDataSourceApp.java file: 
<pre>
  private static JRCsvDataSource getDataSource() throws JRException
  {
    String[] columnNames = new String[]{"city", "id", "name", "address", "state"};
    JRCsvDataSource ds = new JRCsvDataSource(JRLoader.getLocationInputStream("data/CsvDataSource.txt"));
    ds.setRecordDelimiter("\r\n");
    ds.setColumnNames(columnNames);
    return ds;
  }
</pre>
The five column names are: <span class="code"><code>city</code></span>, <span class="code"><code>id</code></span>, <span class="code"><code>name</code></span>, <span class="code"><code>address</code></span> and <span class="code"><code>state</code></span>, 
in this particular order. Field names are identical to the column names defined here.
<br>
The <span class="code"><code>JRCsvDataSource</code></span> object prepared above is passed to the engine at fill time (see again the /src/CsvDataSourceApp.java file):
<pre>
  public void fill() throws JRException
  {
    long start = System.currentTimeMillis();
    //Preparing parameters
    Map parameters = new HashMap();
    parameters.put("ReportTitle", "Address Report");
    parameters.put("DataFile", "CsvDataSource.txt - CSV data source");
    Set states = new HashSet();
    states.add("Active");
    states.add("Trial");
    parameters.put("IncludedStates", states);

    JasperFillManager.fillReportToFile("build/reports/CsvDataSourceReport.jasper", parameters, getDataSource());
    System.err.println("Filling time : " + (System.currentTimeMillis() - start));
  }
</pre>
The <span class="code"><code>IncludedStates</code></span> parameter defined above is used for data filtering. Only records with <span class="code"><code>Active</code></span> 
or <span class="code"><code>Trial</code></span> states will be taken into account:
<br>

<br>

<span class="code"><code>
&nbsp;&nbsp;&lt;parameter name="IncludedStates" class="java.util.Set"/&gt;
<br>

<br>
&nbsp;&nbsp;...
<br>

<br>
&nbsp;&nbsp;&lt;filterExpression&gt;&lt;![CDATA[$P{IncludedStates}.contains($F{state}) ? Boolean.TRUE : Boolean.FALSE]]&gt;&lt;/filterExpression&gt;
</code></span>

<br>

<br>

<b>Running the Sample</b>

<br>

<br>
Running the sample requires the <a href="http://ant.apache.org/" target="_blank" class="element">Apache Ant</a> library. Make sure that <span class="code"><code>ant</code></span> is already installed on your system (version 1.5 or later).
<br>
In a command prompt/terminal window set the current folder to <span class="code"><code>demo/samples/csvdatasource</code></span> within the JasperReports source project and run the <span class="code"><code>&gt; ant test view</code></span> command.
<br>
It will generate all supported document types containing the sample report in the <span class="code"><code>demo/samples/csvdatasource/build/reports</code></span> directory. 
<br>
Then the report will open in the JasperReports internal viewer.
    </span></td>
</tr>
<tr>
<td colspan="6">
<br>
</td>
</tr>
<tr>
<td colspan="6" align="right"><a name="csvqueryexecuter"></a><a href="#top" class="toc">top</a></td>
</tr>
<tr>
<td colspan="6">
<hr size="1">
</td>
</tr>
<tr valign="top">
<td><img src="../../resources/jr-16x16.png" border="0"></td><td colspan="4"><span class="name">CSV Query Executer</span></td><td align="right"><span class="copy">Documented by 
	<a href="mailto:" class="copy"></a></span></td>
</tr>
<tr>
<td colspan="6">
<br>
</td>
</tr>
<tr valign="top">
<td>
<br>
</td><td nowrap="true"><span class="label">Description / Goal</span></td><td>
<br>
</td><td colspan="3"><span class="description">
How to fill a report using the CSV query executer.
    </span></td>
</tr>
<tr valign="top">
<td>
<br>
</td><td colspan="1"><span class="label">Since</span></td><td>
<br>
</td><td colspan="3"><span class="description">4.0.0</span></td>
</tr>
<tr valign="top">
<td>
<br>
</td><td nowrap="true"><span class="label">Other Samples</span></td><td>
<br>
</td><td colspan="3">
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td><span class="element"><a href="../ejbql/index.html">/demo/samples/ejbql</a></span></td>
</tr>
<tr>
<td><span class="element"><a href="../hibernate/index.html">/demo/samples/hibernate</a></span></td>
</tr>
<tr>
<td><span class="element"><a href="../mondrian/index.html">/demo/samples/mondrian</a></span></td>
</tr>
<tr>
<td><span class="element"><a href="../xmldatasource/index.html">/demo/samples/xmldatasource</a></span></td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="6">
<br>
</td>
</tr>
<tr>
<td>
<br>
</td><td colspan="5"><span class="description">
[Under Construction]
    </span></td>
</tr>
<tr>
<td colspan="6">
<br>
</td>
</tr>
</table>
<br>
<table cellspacing="0" cellpadding="0" border="0" width="100%">
<tr>
<td>
<hr size="1">
</td>
</tr>
<tr>
<td align="center"><span class="copy">&copy; 2001-2010 Jaspersoft Corporation <a href="http://www.jaspersoft.com" target="_blank" class="copy">www.jaspersoft.com</a></span></td>
</tr>
</table>
</body>
</html>