KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > csv > CsvParserImpl


1 /*
2  *
3  * The DbUnit Database Testing Framework
4  * Copyright (C)2002-2004, DbUnit.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */

21
22 package org.dbunit.dataset.csv;
23
24 import org.dbunit.dataset.csv.handlers.*;
25
26 import java.io.*;
27 import java.text.CharacterIterator JavaDoc;
28 import java.text.StringCharacterIterator JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.List JavaDoc;
31
32 public class CsvParserImpl implements CsvParser {
33
34     private Pipeline pipeline;
35
36     public CsvParserImpl() {
37         resetThePipeline();
38     }
39
40     private void resetThePipeline() {
41         pipeline = new Pipeline();
42         getPipeline().putFront(SeparatorHandler.ENDPIECE());
43         getPipeline().putFront(EscapeHandler.ACCEPT());
44         getPipeline().putFront(IsAlnumHandler.QUOTE());
45         getPipeline().putFront(QuoteHandler.QUOTE());
46         getPipeline().putFront(EscapeHandler.ESCAPE());
47         getPipeline().putFront(WhitespacesHandler.IGNORE());
48         getPipeline().putFront(TransparentHandler.IGNORE());
49     }
50
51     public List JavaDoc parse(String JavaDoc csv) throws PipelineException, IllegalInputCharacterException {
52
53         getPipeline().resetProducts();
54         CharacterIterator JavaDoc iterator = new StringCharacterIterator JavaDoc(csv);
55         for (char c = iterator.first(); c != CharacterIterator.DONE; c = iterator.next()) {
56             getPipeline().handle(c);
57         }
58         getPipeline().noMoreInput();
59         getPipeline().thePieceIsDone();
60         return getPipeline().getProducts();
61     }
62
63     public List JavaDoc parse(File file) throws IOException, CsvParserException {
64
65         BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
66         LineNumberReader lineNumberReader = new LineNumberReader(reader);
67         List JavaDoc rows = new ArrayList JavaDoc();
68
69         List JavaDoc columnsInFirstLine = parseFirstLine(lineNumberReader, file, rows);
70         parseTheData(columnsInFirstLine, lineNumberReader, rows);
71         return rows;
72     }
73
74     private List JavaDoc parseFirstLine(LineNumberReader lineNumberReader, File file, List JavaDoc rows) throws IOException, CsvParserException {
75         String JavaDoc firstLine = lineNumberReader.readLine();
76         if (firstLine == null)
77             throw new CsvParserException("The null first line in file " + file.getAbsolutePath());
78
79         final List JavaDoc columnsInFirstLine = parse(firstLine);
80         rows.add(columnsInFirstLine);
81         return columnsInFirstLine;
82     }
83
84     private void parseTheData(final List JavaDoc columnsInFirstLine, LineNumberReader lineNumberReader, List JavaDoc rows) throws IOException, CsvParserException {
85         int nColumns = columnsInFirstLine.size();
86         List JavaDoc columns;
87         while ((columns = collectExpectedNumberOfColumns(nColumns, lineNumberReader)) != null) {
88             rows.add(columns);
89         }
90     }
91
92     private List JavaDoc collectExpectedNumberOfColumns(int expectedNumberOfColumns, LineNumberReader lineNumberReader) throws IOException, CsvParserException {
93
94         List JavaDoc columns = null;
95         int columnsCollectedSoFar = 0;
96         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
97         String JavaDoc anotherLine = lineNumberReader.readLine();
98         if(anotherLine == null)
99             return null;
100         boolean shouldProceed = false;
101         while (columnsCollectedSoFar < expectedNumberOfColumns) {
102             try {
103                 buffer.append(anotherLine);
104                 columns = parse(buffer.toString());
105                 columnsCollectedSoFar = columns.size();
106             } catch (IllegalStateException JavaDoc e) {
107                 resetThePipeline();
108                 anotherLine = lineNumberReader.readLine();
109                 if(anotherLine == null)
110                     break;
111                 buffer.append("\n");
112                 shouldProceed = true;
113             }
114             if (!shouldProceed)
115                 break;
116         }
117         if (columnsCollectedSoFar != expectedNumberOfColumns) {
118             String JavaDoc message = new StringBuffer JavaDoc("Expected ").append(expectedNumberOfColumns)
119                     .append(" columns on line ").append(lineNumberReader.getLineNumber())
120                     .append(", got ").append(columnsCollectedSoFar).append(". Offending line: ").append(buffer).toString();
121             throw new CsvParserException(message);
122         }
123         return columns;
124     }
125
126     Pipeline getPipeline() {
127         return pipeline;
128     }
129
130     void setPipeline(Pipeline pipeline) {
131         this.pipeline = pipeline;
132     }
133 }
134
Popular Tags