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) {
75 List
<String
> tokens
= new ArrayList
<String
>();
76 StringBuffer currStr
= new StringBuffer("");
77 Boolean wasInquote
= false;
78 while (parseLine(line
, tokens
, currStr
, wasInquote
)) {
79 line
= reader
.readLine();
84 if (!noHeader
&& strictLineAsLongAsHeader
) {
85 int headerSize
= header
.size();
86 int tokenSize
= tokens
.size();
87 if (tokenSize
== 1 && line
.trim().equals(""))
88 continue lines
;// empty line
89 if (headerSize
!= tokenSize
) {
90 throw new ArgeoException("Token size " + tokenSize
91 + " is different from header size "
92 + headerSize
+ " at line " + lineCount
93 + ", line: " + line
+ ", header: " + header
94 + ", tokens: " + tokens
);
97 processLine(lineCount
, header
, tokens
);
99 } catch (ArgeoException e
) {
101 } catch (IOException e
) {
102 throw new ArgeoException("Cannot parse CSV file (line: "
103 + lineCount
+ ")", e
);
108 } catch (Exception e2
) {
115 * Parses a line character by character for performance purpose
117 * @return whether to continue parsing this line
119 protected Boolean
parseLine(String str
, List
<String
> tokens
,
120 StringBuffer currStr
, Boolean wasInquote
) {
121 // List<String> tokens = new ArrayList<String>();
123 // System.out.println("#LINE: " + str);
126 currStr
.append('\n');
128 char[] arr
= str
.toCharArray();
129 boolean inQuote
= wasInquote
;
130 // StringBuffer currStr = new StringBuffer("");
131 for (int i
= 0; i
< arr
.length
; i
++) {
133 if (c
== separator
) {
135 tokens
.add(currStr
.toString());
136 // System.out.println("# TOKEN: " + currStr);
137 currStr
.delete(0, currStr
.length());
139 // we don't remove separator that are in a quoted substring
141 // .println("IN QUOTE, got a separator: [" + c + "]");
144 } else if (c
== quote
) {
145 if (inQuote
&& (i
+ 1) < arr
.length
&& arr
[i
+ 1] == quote
) {
146 // case of double quote
147 currStr
.append(quote
);
150 inQuote
= inQuote ?
false : true;
158 tokens
.add(currStr
.toString());
159 // System.out.println("# TOKEN: " + currStr);
162 // throw new ArgeoException("Missing quote at the end of the line "
163 // + str + " (parsed: " + tokens + ")");
171 public char getSeparator() {
175 public synchronized void setSeparator(char separator
) {
176 this.separator
= separator
;
179 public char getQuote() {
183 public synchronized void setQuote(char quote
) {
187 public Boolean
getNoHeader() {
191 public synchronized void setNoHeader(Boolean noHeader
) {
192 this.noHeader
= noHeader
;
195 public Boolean
getStrictLineAsLongAsHeader() {
196 return strictLineAsLongAsHeader
;
199 public synchronized void setStrictLineAsLongAsHeader(
200 Boolean strictLineAsLongAsHeader
) {
201 this.strictLineAsLongAsHeader
= strictLineAsLongAsHeader
;