1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.dbforms.servlets;
25
26 import org.dbforms.servlets.reports.AbstractLineReportServlet;
27
28 import java.io.OutputStream;
29 import java.io.OutputStreamWriter;
30 import java.io.PrintWriter;
31
32 /***
33 * This servlet generates a comma separated values file (CSV). Data is read from
34 * the current dbForm, a Collection or a ResultSetVector The template file is in
35 * the reports directory with a .cr extension it consists of two lines of text,
36 * the first is a list of stirngs to use as header the second a list of comma
37 * separated field names. both lines must have the same number of elements.
38 * Normally the output file will use the first row as the headers. But if no
39 * header line is provided, then no headers are output and every row is data.
40 * usage: with a simple goto button: <db:gotoButton destTable="web_parts"
41 * destination=" /reports/Artikel"/> or for one record: <db:gotoButton
42 * destTable="web_parts" keyToDestPos="currentRow"
43 * destination="/reports/Artikel" /> Servlet mapping must be set to handle
44 * all /reports by this servlet!!! <servlet/>
45 * <servlet-name/>startreport</servlet-name/>
46 * <display-name/>startreport</display-name/>
47 * <servlet-class/>org.dbforms.StartReportServlet</servlet-class/>
48 * </servlet> <servlet-mapping/>
49 * <servlet-name/>startreport</servlet-name/>
50 * <url-pattern/>/reports/</url-pattern/> </servlet-mapping>
51 * web.xml optional parameters reportdirs list of directories to search for
52 * report file reportMimeType mime type to send to browser Parameters
53 * filename=xyz.csv name the output file Support for grabbing data from a
54 * Collection or an existing ResultSetVector set session variable "jasper.input"
55 * to use a Collection object set session variable "jasper.rsv" to use a
56 * ResultSetVector object ex <c:set var="jasper.rsv" value="${rsv_xxxxx}"
57 * scope="session" />
58 *
59 * @author Neal Katz
60 */
61 public class CSVReportServlet extends AbstractLineReportServlet {
62
63 private static final char Q = '\"';
64
65 private static final String DEFAULTMIMETYPE = "text/comma-separated-values;charset=ASCII";
66
67 private String clean(String s) {
68 s = s.replaceAll("\"", "//\"");
69 return s;
70 }
71
72 protected String getMimeType() {
73 return DEFAULTMIMETYPE;
74 }
75
76 protected String getFileExtension() {
77 return ".csv";
78 }
79
80 private PrintWriter pw;
81
82 protected void openStream(OutputStream out) throws Exception {
83 OutputStreamWriter osw;
84 try {
85 osw = new OutputStreamWriter(out, "UTF8");
86 } catch (Exception e) {
87 osw = new OutputStreamWriter(out);
88 }
89 pw = new PrintWriter(osw);
90 }
91
92 protected void closeStream(OutputStream out) throws Exception {
93 pw.flush();
94 pw.close();
95 }
96
97 protected void writeData(Object[] data) throws Exception {
98 for (int i = 0; i < data.length; i++) {
99 if (i > 0) {
100 pw.print(',');
101 }
102 if (data[i] != null) {
103 pw.print(Q + clean(data[i].toString()) + Q);
104 } else {
105 pw.print(Q + "" + Q);
106 }
107 }
108 pw.println();
109 }
110
111 }