Source Code Cross Referenced for CsvProducer.java in  » Testing » DbUnit » org » dbunit » dataset » 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 » Testing » DbUnit » org.dbunit.dataset.csv 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *
003:         * The DbUnit Database Testing Framework
004:         * Copyright (C)2002-2004, DbUnit.org
005:         *
006:         * This library is free software; you can redistribute it and/or
007:         * modify it under the terms of the GNU Lesser General Public
008:         * License as published by the Free Software Foundation; either
009:         * version 2.1 of the License, or (at your option) any later version.
010:         *
011:         * This library 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 GNU
014:         * Lesser General Public License for more details.
015:         *
016:         * You should have received a copy of the GNU Lesser General Public
017:         * License along with this library; if not, write to the Free Software
018:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
019:         *
020:         */
021:
022:        package org.dbunit.dataset.csv;
023:
024:        import org.slf4j.Logger;
025:        import org.slf4j.LoggerFactory;
026:
027:        import java.io.BufferedReader;
028:        import java.io.File;
029:        import java.io.IOException;
030:        import java.io.InputStream;
031:        import java.io.InputStreamReader;
032:        import java.net.URL;
033:        import java.util.ArrayList;
034:        import java.util.Iterator;
035:        import java.util.List;
036:
037:        import org.dbunit.dataset.Column;
038:        import org.dbunit.dataset.DataSetException;
039:        import org.dbunit.dataset.DefaultTableMetaData;
040:        import org.dbunit.dataset.ITableMetaData;
041:        import org.dbunit.dataset.csv.handlers.PipelineException;
042:        import org.dbunit.dataset.datatype.DataType;
043:        import org.dbunit.dataset.stream.DefaultConsumer;
044:        import org.dbunit.dataset.stream.IDataSetConsumer;
045:        import org.dbunit.dataset.stream.IDataSetProducer;
046:
047:        /**
048:         * @author Federico Spinazzi
049:         * @since Sep 17, 2003
050:         * @version $Revision: 558 $
051:         */
052:
053:        public class CsvProducer implements  IDataSetProducer {
054:
055:            /**
056:             * Logger for this class
057:             */
058:            private static final Logger logger = LoggerFactory
059:                    .getLogger(CsvProducer.class);
060:
061:            private static final IDataSetConsumer EMPTY_CONSUMER = new DefaultConsumer();
062:            private IDataSetConsumer _consumer = EMPTY_CONSUMER;
063:            private String _theDirectory;
064:
065:            public CsvProducer(String theDirectory) {
066:                _theDirectory = theDirectory;
067:            }
068:
069:            public CsvProducer(File theDirectory) {
070:                _theDirectory = theDirectory.getAbsolutePath();
071:            }
072:
073:            public void setConsumer(IDataSetConsumer consumer)
074:                    throws DataSetException {
075:                logger.debug("setConsumer(consumer) - start");
076:
077:                _consumer = consumer;
078:            }
079:
080:            public void produce() throws DataSetException {
081:                logger.debug("produce() - start");
082:
083:                File dir = new File(_theDirectory);
084:
085:                if (!dir.isDirectory()) {
086:                    throw new DataSetException("'" + _theDirectory
087:                            + "' should be a directory");
088:                }
089:
090:                _consumer.startDataSet();
091:                try {
092:                    List tableSpecs = CsvProducer.getTables(dir.toURL(),
093:                            "table-ordering.txt");
094:                    for (Iterator tableIter = tableSpecs.iterator(); tableIter
095:                            .hasNext();) {
096:                        String table = (String) tableIter.next();
097:                        try {
098:                            produceFromFile(new File(dir, table + ".csv"));
099:                        } catch (CsvParserException e) {
100:                            logger.error("produce()", e);
101:
102:                            throw new DataSetException(
103:                                    "error producing dataset for table '"
104:                                            + table + "'", e);
105:                        } catch (DataSetException e) {
106:                            logger.error("produce()", e);
107:
108:                            throw new DataSetException(
109:                                    "error producing dataset for table '"
110:                                            + table + "'", e);
111:                        }
112:
113:                    }
114:                    _consumer.endDataSet();
115:                } catch (IOException e) {
116:                    logger.error("produce()", e);
117:
118:                    throw new DataSetException("error getting list of tables",
119:                            e);
120:                }
121:            }
122:
123:            private void produceFromFile(File theDataFile)
124:                    throws DataSetException, CsvParserException {
125:                logger.debug("produceFromFile(theDataFile=" + theDataFile
126:                        + ") - start");
127:
128:                try {
129:                    CsvParser parser = new CsvParserImpl();
130:                    List readData = parser.parse(theDataFile);
131:                    List readColumns = ((List) readData.get(0));
132:                    Column[] columns = new Column[readColumns.size()];
133:
134:                    for (int i = 0; i < readColumns.size(); i++) {
135:                        columns[i] = new Column((String) readColumns.get(i),
136:                                DataType.UNKNOWN);
137:                    }
138:
139:                    String tableName = theDataFile.getName().substring(0,
140:                            theDataFile.getName().indexOf(".csv"));
141:                    ITableMetaData metaData = new DefaultTableMetaData(
142:                            tableName, columns);
143:                    _consumer.startTable(metaData);
144:                    for (int i = 1; i < readData.size(); i++) {
145:                        List rowList = (List) readData.get(i);
146:                        Object[] row = rowList.toArray();
147:                        for (int col = 0; col < row.length; col++) {
148:                            row[col] = row[col].equals(CsvDataSetWriter.NULL) ? null
149:                                    : row[col];
150:                        }
151:                        _consumer.row(row);
152:                    }
153:                    _consumer.endTable();
154:                } catch (PipelineException e) {
155:                    logger.error("produceFromFile()", e);
156:
157:                    throw new DataSetException(e);
158:                } catch (IllegalInputCharacterException e) {
159:                    logger.error("produceFromFile()", e);
160:
161:                    throw new DataSetException(e);
162:                } catch (IOException e) {
163:                    logger.error("produceFromFile()", e);
164:
165:                    throw new DataSetException(e);
166:                }
167:            }
168:
169:            /**
170:             * Get a list of tables that this producer will create
171:             * @return a list of Strings, where each item is a CSV file relative to the base URL
172:             * @throws IOException when IO on the base URL has issues.
173:             */
174:            public static List getTables(URL base, String tableList)
175:                    throws IOException {
176:                logger.debug("getTables(base=" + base + ", tableList="
177:                        + tableList + ") - start");
178:
179:                List orderedNames = new ArrayList();
180:                InputStream tableListStream = new URL(base, tableList)
181:                        .openStream();
182:                BufferedReader reader = new BufferedReader(
183:                        new InputStreamReader(tableListStream));
184:                String line = null;
185:                while ((line = reader.readLine()) != null) {
186:                    String table = line.trim();
187:                    if (table.length() > 0) {
188:                        orderedNames.add(table);
189:                    }
190:                }
191:                reader.close();
192:                return orderedNames;
193:            }
194:
195:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.