Source Code Cross Referenced for CSVFileImporter.java in  » Database-Client » squirrel-sql-2.6.5a » net » sourceforge » squirrel_sql » plugins » dataimport » importer » csv » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database Client » squirrel sql 2.6.5a » net.sourceforge.squirrel_sql.plugins.dataimport.importer.csv 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
ww___w_.__ja___v___a_2___s___._com___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.