1 package org
.argeo
.util
;
3 import java
.io
.BufferedReader
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
6 import java
.io
.InputStreamReader
;
7 import java
.util
.ArrayList
;
8 import java
.util
.Collections
;
11 import org
.argeo
.ArgeoException
;
14 * Parses a CSV file interpreting the first line as a header. The
15 * {@link #parse(InputStream)} method and the setters are synchronized so that
16 * the object cannot be modified when parsing.
18 public abstract class CsvParser
{
19 private char separator
= ',';
20 private char quote
= '\"';
22 private Boolean noHeader
= false;
23 private Boolean strictLineAsLongAsHeader
= true;
26 * Actually process a parsed line. If
27 * {@link #setStrictLineAsLongAsHeader(Boolean)} is true (default) the
28 * header and the tokens are guaranteed to have the same size.
31 * the current line number, starts at 1 (the header, if header
32 * processing is enabled, the first line otherwise)
34 * the read-only header or null if {@link #setNoHeader(Boolean)}
35 * is true (default is false)
39 protected abstract void processLine(Integer lineNumber
,
40 List
<String
> header
, List
<String
> tokens
);
42 public synchronized void parse(InputStream in
) {
46 public synchronized void parse(InputStream in
, String encoding
) {
47 BufferedReader reader
= null;
48 Integer lineCount
= 0;
51 reader
= new BufferedReader(new InputStreamReader(in
));
53 reader
= new BufferedReader(new InputStreamReader(in
, encoding
));
54 List
<String
> header
= null;
56 String headerStr
= reader
.readLine();
57 if (headerStr
== null)// empty file
60 header
= new ArrayList
<String
>();
61 StringBuffer currStr
= new StringBuffer("");
62 Boolean wasInquote
= false;
63 while (parseLine(headerStr
, header
, currStr
, wasInquote
)) {
64 headerStr
= reader
.readLine();
65 if (headerStr
== null)
69 header
= Collections
.unmodifiableList(header
);
73 lines
: while ((line
= reader
.readLine()) != null) {
74 line
= preProcessLine(line
);
80 List
<String
> tokens
= new ArrayList
<String
>();
81 StringBuffer currStr
= new StringBuffer("");
82 Boolean wasInquote
= false;
83 sublines
: while (parseLine(line
, tokens
, currStr
, wasInquote
)) {
84 line
= reader
.readLine();
89 if (!noHeader
&& strictLineAsLongAsHeader
) {
90 int headerSize
= header
.size();
91 int tokenSize
= tokens
.size();
92 if (tokenSize
== 1 && line
.trim().equals(""))
93 continue lines
;// empty line
94 if (headerSize
!= tokenSize
) {
95 throw new ArgeoException("Token size " + tokenSize
96 + " is different from header size "
97 + headerSize
+ " at line " + lineCount
98 + ", line: " + line
+ ", header: " + header
99 + ", tokens: " + tokens
);
102 processLine(lineCount
, header
, tokens
);
104 } catch (ArgeoException e
) {
106 } catch (IOException e
) {
107 throw new ArgeoException("Cannot parse CSV file (line: "
108 + lineCount
+ ")", e
);
113 } catch (Exception e2
) {
120 * Called before each (logical) line is processed, giving a change to modify
121 * it (typically for cleaning dirty files). To be overridden, return the
122 * line unchanged by default. Skip the line if 'null' is returned.
124 protected String
preProcessLine(String line
) {
129 * Parses a line character by character for performance purpose
131 * @return whether to continue parsing this line
133 protected Boolean
parseLine(String str
, List
<String
> tokens
,
134 StringBuffer currStr
, Boolean wasInquote
) {
135 // List<String> tokens = new ArrayList<String>();
137 // System.out.println("#LINE: " + str);
140 currStr
.append('\n');
142 char[] arr
= str
.toCharArray();
143 boolean inQuote
= wasInquote
;
144 // StringBuffer currStr = new StringBuffer("");
145 for (int i
= 0; i
< arr
.length
; i
++) {
147 if (c
== separator
) {
149 tokens
.add(currStr
.toString());
150 // System.out.println("# TOKEN: " + currStr);
151 currStr
.delete(0, currStr
.length());
153 // we don't remove separator that are in a quoted substring
155 // .println("IN QUOTE, got a separator: [" + c + "]");
158 } else if (c
== quote
) {
159 if (inQuote
&& (i
+ 1) < arr
.length
&& arr
[i
+ 1] == quote
) {
160 // case of double quote
161 currStr
.append(quote
);
164 inQuote
= inQuote ?
false : true;
172 tokens
.add(currStr
.toString());
173 // System.out.println("# TOKEN: " + currStr);
176 // throw new ArgeoException("Missing quote at the end of the line "
177 // + str + " (parsed: " + tokens + ")");
185 public char getSeparator() {
189 public synchronized void setSeparator(char separator
) {
190 this.separator
= separator
;
193 public char getQuote() {
197 public synchronized void setQuote(char quote
) {
201 public Boolean
getNoHeader() {
205 public synchronized void setNoHeader(Boolean noHeader
) {
206 this.noHeader
= noHeader
;
209 public Boolean
getStrictLineAsLongAsHeader() {
210 return strictLineAsLongAsHeader
;
213 public synchronized void setStrictLineAsLongAsHeader(
214 Boolean strictLineAsLongAsHeader
) {
215 this.strictLineAsLongAsHeader
= strictLineAsLongAsHeader
;