KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > data > JRCsvDataSourceProvider


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.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  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine.data;
29
30 import java.text.DateFormat JavaDoc;
31 import java.text.NumberFormat JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.io.File JavaDoc;
34 import java.io.Reader JavaDoc;
35 import java.io.FileNotFoundException JavaDoc;
36 import java.io.InputStreamReader JavaDoc;
37 import java.io.FileReader JavaDoc;
38
39 import net.sf.jasperreports.engine.JRDataSourceProvider;
40 import net.sf.jasperreports.engine.JRDataSource;
41 import net.sf.jasperreports.engine.JasperReport;
42 import net.sf.jasperreports.engine.JRException;
43 import net.sf.jasperreports.engine.JRField;
44
45 /**
46  * @author Ionut Nedelcu (ionutned@users.sourceforge.net)
47  * @version $Id: JRCsvDataSourceProvider.java 1522 2006-12-13 11:46:17 +0200 (Wed, 13 Dec 2006) shertage $
48  */

49 public class JRCsvDataSourceProvider implements JRDataSourceProvider
50 {
51     private Reader JavaDoc reader;
52
53     private DateFormat JavaDoc dateFormat;
54     private char fieldDelimiter;
55     private String JavaDoc recordDelimiter;
56     private String JavaDoc[] columnNames;
57     private NumberFormat JavaDoc numberFormat;
58
59     /**
60      * @param stream an input stream containing CSV data
61      */

62     public JRCsvDataSourceProvider(InputStream JavaDoc stream)
63     {
64         this(new InputStreamReader JavaDoc(stream));
65     }
66
67
68     /**
69      * Builds a datasource instance.
70      * @param file a file containing CSV data
71      */

72     public JRCsvDataSourceProvider(File JavaDoc file) throws FileNotFoundException JavaDoc
73     {
74         this(new FileReader JavaDoc(file));
75     }
76
77
78     /**
79      * Builds a datasource instance.
80      * @param reader a <tt>Reader</tt> instance, for reading the stream
81      */

82     public JRCsvDataSourceProvider(Reader JavaDoc reader)
83     {
84         this.reader = reader;
85     }
86
87     /**
88      *
89      */

90     public boolean supportsGetFieldsOperation()
91     {
92         return false;
93     }
94
95
96     /**
97      *
98      */

99     public JRField[] getFields(JasperReport report) throws JRException, UnsupportedOperationException JavaDoc
100     {
101         return null;
102     }
103
104
105     /**
106      *
107      */

108     public JRDataSource create(JasperReport report) throws JRException
109     {
110         JRCsvDataSource ds;
111         if (reader != null)
112              ds = new JRCsvDataSource(reader);
113         else {
114             throw new JRException("Cannot find a source to read the data from");
115         }
116
117         ds.setDateFormat(dateFormat);
118         ds.setNumberFormat(numberFormat);
119         ds.setFieldDelimiter(fieldDelimiter);
120         ds.setRecordDelimiter(recordDelimiter);
121         ds.setColumnNames(columnNames);
122
123         return ds;
124     }
125
126
127     /**
128      *
129      */

130     public void dispose(JRDataSource dataSource) throws JRException
131     {
132     }
133
134     public String JavaDoc[] getColumnNames()
135     {
136         return columnNames;
137     }
138
139     public void setColumnNames(String JavaDoc[] columnNames)
140     {
141         this.columnNames = columnNames;
142     }
143
144     public DateFormat JavaDoc getDateFormat()
145     {
146         return dateFormat;
147     }
148
149     public void setDateFormat(DateFormat JavaDoc dateFormat)
150     {
151         this.dateFormat = dateFormat;
152     }
153
154     public char getFieldDelimiter()
155     {
156         return fieldDelimiter;
157     }
158
159     public void setFieldDelimiter(char fieldDelimiter)
160     {
161         this.fieldDelimiter = fieldDelimiter;
162     }
163
164     public String JavaDoc getRecordDelimiter()
165     {
166         return recordDelimiter;
167     }
168
169     public void setRecordDelimiter(String JavaDoc recordDelimiter)
170     {
171         this.recordDelimiter = recordDelimiter;
172     }
173
174
175     public NumberFormat JavaDoc getNumberFormat()
176     {
177         return numberFormat;
178     }
179
180
181     public void setNumberFormat(NumberFormat JavaDoc numberFormat)
182     {
183         this.numberFormat = numberFormat;
184     }
185 }
186
Popular Tags