KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > servlets > CSVReportServlet


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/servlets/CSVReportServlet.java,v 1.4 2004/10/24 13:47:30 hkollmann Exp $
3  * $Revision: 1.4 $
4  * $Date: 2004/10/24 13:47:30 $
5  *
6  * DbForms - a Rapid Application Development Framework
7  * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23
24 package org.dbforms.servlets;
25
26 import org.dbforms.servlets.reports.LineReportServletAbstract;
27
28 import java.io.OutputStream JavaDoc;
29 import java.io.OutputStreamWriter JavaDoc;
30 import java.io.PrintWriter JavaDoc;
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: &lt;db:gotoButton destTable="web_parts"
41  * destination=" /reports/Artikel"/&gt; or for one record: &lt;db:gotoButton
42  * destTable="web_parts" keyToDestPos="currentRow"
43  * destination="/reports/Artikel" /&gt; Servlet mapping must be set to handle
44  * all /reports by this servlet!!! &lt;servlet/&gt;
45  * &lt;servlet-name/&gt;startreport&lt;/servlet-name/&gt;
46  * &lt;display-name/&gt;startreport&lt;/display-name/&gt;
47  * &lt;servlet-class/&gt;org.dbforms.StartReportServlet&lt;/servlet-class/&gt;
48  * &lt;/servlet&gt; &lt;servlet-mapping/&gt;
49  * &lt;servlet-name/&gt;startreport&lt;/servlet-name/&gt;
50  * &lt;url-pattern/&gt;/reports/&lt;/url-pattern/&gt; &lt;/servlet-mapping&gt;
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 &ltc:set var="jasper.rsv" value="${rsv_xxxxx}"
57  * scope="session" /&gt
58  *
59  * @author Neal Katz
60  */

61 public class CSVReportServlet extends LineReportServletAbstract {
62
63     private static final char Q = '\"';
64
65     private static final String JavaDoc DEFAULTMIMETYPE = "text/comma-separated-values;charset=ASCII";
66
67     private String JavaDoc clean(String JavaDoc s) {
68         s = s.replaceAll("\"", "\\\"");
69         return s;
70     }
71
72     protected String JavaDoc getMimeType() {
73         return DEFAULTMIMETYPE;
74     }
75
76     protected String JavaDoc getFileExtension() {
77         return ".csv";
78     }
79
80     private PrintWriter JavaDoc pw;
81
82     protected void openStream(OutputStream JavaDoc out) throws Exception JavaDoc {
83         OutputStreamWriter JavaDoc osw;
84         try {
85             osw = new OutputStreamWriter JavaDoc(out, "UTF8");
86         } catch (Exception JavaDoc e) {
87             osw = new OutputStreamWriter JavaDoc(out);
88         }
89         pw = new PrintWriter JavaDoc(osw);
90     }
91
92     protected void closeStream(OutputStream JavaDoc out) throws Exception JavaDoc {
93         pw.flush();
94         pw.close();
95     }
96
97     protected void writeData(Object JavaDoc[] data) throws Exception JavaDoc {
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 }
112
Popular Tags