001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.engine.data;
029:
030: import java.text.DateFormat;
031: import java.text.NumberFormat;
032: import java.io.InputStream;
033: import java.io.File;
034: import java.io.Reader;
035: import java.io.FileNotFoundException;
036: import java.io.InputStreamReader;
037: import java.io.FileReader;
038:
039: import net.sf.jasperreports.engine.JRDataSourceProvider;
040: import net.sf.jasperreports.engine.JRDataSource;
041: import net.sf.jasperreports.engine.JasperReport;
042: import net.sf.jasperreports.engine.JRException;
043: import net.sf.jasperreports.engine.JRField;
044:
045: /**
046: * @author Ionut Nedelcu (ionutned@users.sourceforge.net)
047: * @version $Id: JRCsvDataSourceProvider.java 1522 2006-12-13 09:46:17Z shertage $
048: */
049: public class JRCsvDataSourceProvider implements JRDataSourceProvider {
050: private Reader reader;
051:
052: private DateFormat dateFormat;
053: private char fieldDelimiter;
054: private String recordDelimiter;
055: private String[] columnNames;
056: private NumberFormat numberFormat;
057:
058: /**
059: * @param stream an input stream containing CSV data
060: */
061: public JRCsvDataSourceProvider(InputStream stream) {
062: this (new InputStreamReader(stream));
063: }
064:
065: /**
066: * Builds a datasource instance.
067: * @param file a file containing CSV data
068: */
069: public JRCsvDataSourceProvider(File file)
070: throws FileNotFoundException {
071: this (new FileReader(file));
072: }
073:
074: /**
075: * Builds a datasource instance.
076: * @param reader a <tt>Reader</tt> instance, for reading the stream
077: */
078: public JRCsvDataSourceProvider(Reader reader) {
079: this .reader = reader;
080: }
081:
082: /**
083: *
084: */
085: public boolean supportsGetFieldsOperation() {
086: return false;
087: }
088:
089: /**
090: *
091: */
092: public JRField[] getFields(JasperReport report) throws JRException,
093: UnsupportedOperationException {
094: return null;
095: }
096:
097: /**
098: *
099: */
100: public JRDataSource create(JasperReport report) throws JRException {
101: JRCsvDataSource ds;
102: if (reader != null)
103: ds = new JRCsvDataSource(reader);
104: else {
105: throw new JRException(
106: "Cannot find a source to read the data from");
107: }
108:
109: ds.setDateFormat(dateFormat);
110: ds.setNumberFormat(numberFormat);
111: ds.setFieldDelimiter(fieldDelimiter);
112: ds.setRecordDelimiter(recordDelimiter);
113: ds.setColumnNames(columnNames);
114:
115: return ds;
116: }
117:
118: /**
119: *
120: */
121: public void dispose(JRDataSource dataSource) throws JRException {
122: }
123:
124: public String[] getColumnNames() {
125: return columnNames;
126: }
127:
128: public void setColumnNames(String[] columnNames) {
129: this .columnNames = columnNames;
130: }
131:
132: public DateFormat getDateFormat() {
133: return dateFormat;
134: }
135:
136: public void setDateFormat(DateFormat dateFormat) {
137: this .dateFormat = dateFormat;
138: }
139:
140: public char getFieldDelimiter() {
141: return fieldDelimiter;
142: }
143:
144: public void setFieldDelimiter(char fieldDelimiter) {
145: this .fieldDelimiter = fieldDelimiter;
146: }
147:
148: public String getRecordDelimiter() {
149: return recordDelimiter;
150: }
151:
152: public void setRecordDelimiter(String recordDelimiter) {
153: this .recordDelimiter = recordDelimiter;
154: }
155:
156: public NumberFormat getNumberFormat() {
157: return numberFormat;
158: }
159:
160: public void setNumberFormat(NumberFormat numberFormat) {
161: this.numberFormat = numberFormat;
162: }
163: }
|