001: package net.sourceforge.squirrel_sql.plugins.dataimport.importer.csv;
002:
003: /*
004: * Copyright (C) 2007 Thorsten Mürell
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: */
020:
021: import java.io.File;
022: import java.io.FileInputStream;
023: import java.io.IOException;
024: import java.io.InputStreamReader;
025: import java.text.DateFormat;
026: import java.text.ParseException;
027: import java.text.SimpleDateFormat;
028: import java.util.Date;
029:
030: import javax.swing.JComponent;
031: import javax.swing.JOptionPane;
032:
033: import net.sourceforge.squirrel_sql.fw.util.StringManager;
034: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
035: import net.sourceforge.squirrel_sql.plugins.dataimport.importer.IFileImporter;
036: import net.sourceforge.squirrel_sql.plugins.dataimport.importer.UnsupportedFormatException;
037:
038: import com.csvreader.CsvReader;
039:
040: /**
041: * This class implements the IFileImporter interface for reading CSV files.
042: *
043: * @author Thorsten Mürell
044: */
045: public class CSVFileImporter implements IFileImporter {
046: private static final StringManager stringMgr = StringManagerFactory
047: .getStringManager(CSVFileImporter.class);
048:
049: private CSVSettingsBean settings = null;
050: private File importFile = null;
051: private CsvReader reader = null;
052:
053: /**
054: * The standard constructor
055: *
056: * @param importFile The import file
057: */
058: public CSVFileImporter(File importFile) {
059: this .importFile = importFile;
060: this .settings = new CSVSettingsBean();
061: }
062:
063: /*
064: * (non-Javadoc)
065: * @see net.sourceforge.squirrel_sql.plugins.dataimport.importer.IFileImporter#open()
066: */
067: public boolean open() throws IOException {
068: reset();
069: return true;
070: }
071:
072: /*
073: * (non-Javadoc)
074: * @see net.sourceforge.squirrel_sql.plugins.dataimport.importer.IFileImporter#close()
075: */
076: public boolean close() throws IOException {
077: if (reader != null) {
078: reader.close();
079: }
080: return true;
081: }
082:
083: /* (non-Javadoc)
084: * @see net.sourceforge.squirrel_sql.plugins.dataimport.importer.IFileImporter#getPreview(int)
085: */
086: public String[][] getPreview(int noOfLines) throws IOException {
087: CsvReader csvReader = new CsvReader(new InputStreamReader(
088: new FileInputStream(importFile), settings
089: .getImportCharset()), settings.getSeperator());
090: String[][] data = new String[noOfLines][];
091:
092: int row = 0;
093: int columns = -1;
094: while (csvReader.readRecord() && row < noOfLines) {
095: if (columns == -1) {
096: columns = csvReader.getColumnCount();
097: }
098: data[row] = new String[columns];
099: for (int i = 0; i < columns; i++) {
100: data[row][i] = csvReader.get(i);
101: }
102: row++;
103: }
104: csvReader.close();
105:
106: String[][] outData = new String[row][];
107: for (int i = 0; i < row; i++) {
108: outData[i] = data[i];
109: }
110: return outData;
111: }
112:
113: /*
114: * (non-Javadoc)
115: * @see net.sourceforge.squirrel_sql.plugins.dataimport.importer.IFileImporter#getRows()
116: */
117: public int getRows() {
118: return -1;
119: }
120:
121: /*
122: * (non-Javadoc)
123: * @see net.sourceforge.squirrel_sql.plugins.dataimport.importer.IFileImporter#next()
124: */
125: public boolean next() throws IOException {
126: return reader.readRecord();
127: }
128:
129: /*
130: * (non-Javadoc)
131: * @see net.sourceforge.squirrel_sql.plugins.dataimport.importer.IFileImporter#reset()
132: */
133: public boolean reset() throws IOException {
134: if (reader != null) {
135: reader.close();
136: }
137: reader = new CsvReader(new InputStreamReader(
138: new FileInputStream(importFile), settings
139: .getImportCharset()), settings.getSeperator());
140: return true;
141: }
142:
143: /*
144: * (non-Javadoc)
145: * @see net.sourceforge.squirrel_sql.plugins.dataimport.importer.IFileImporter#getString(int)
146: */
147: public String getString(int column) throws IOException {
148: return reader.get(column);
149: }
150:
151: /*
152: * (non-Javadoc)
153: * @see net.sourceforge.squirrel_sql.plugins.dataimport.importer.IFileImporter#getLong(int)
154: */
155: public Long getLong(int column) throws IOException,
156: UnsupportedFormatException {
157: try {
158: return Long.parseLong(reader.get(column));
159: } catch (NumberFormatException nfe) {
160: throw new UnsupportedFormatException();
161: }
162: }
163:
164: /*
165: * (non-Javadoc)
166: * @see net.sourceforge.squirrel_sql.plugins.dataimport.importer.IFileImporter#getInt(int)
167: */
168: public Integer getInt(int column) throws IOException,
169: UnsupportedFormatException {
170: try {
171: return Integer.parseInt(reader.get(column));
172: } catch (NumberFormatException nfe) {
173: throw new UnsupportedFormatException();
174: }
175: }
176:
177: /*
178: * (non-Javadoc)
179: * @see net.sourceforge.squirrel_sql.plugins.dataimport.importer.IFileImporter#getDate(int)
180: */
181: public Date getDate(int column) throws IOException,
182: UnsupportedFormatException {
183: Date d = null;
184: try {
185: DateFormat f = new SimpleDateFormat(settings
186: .getDateFormat());
187: d = f.parse(reader.get(column));
188: } catch (IllegalArgumentException e) {
189: //i18n[CSVFileImporter.invalidDateFormat=Invalid date format given]
190: JOptionPane.showMessageDialog(null, stringMgr
191: .getString("CSVFileImporter.invalidDateFormat"));
192: throw new UnsupportedFormatException();
193: } catch (ParseException pe) {
194: throw new UnsupportedFormatException();
195: }
196: return d;
197: }
198:
199: /*
200: * (non-Javadoc)
201: * @see net.sourceforge.squirrel_sql.plugins.dataimport.importer.IFileImporter#getConfigurationPanel()
202: */
203: public JComponent getConfigurationPanel() {
204: return new CSVSettingsPanel(settings);
205: }
206: }
|