OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 19 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
19 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.utils.text;
15
 
16
/**
17
 * Copyright 2005 Bytecode Pty Ltd.
18
 *
19
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
20
 * in compliance with the License. You may obtain a copy of the License at
21
 *
22
 * http://www.apache.org/licenses/LICENSE-2.0
23
 *
24
 * Unless required by applicable law or agreed to in writing, software distributed under the License
25
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
26
 * or implied. See the License for the specific language governing permissions and limitations under
27
 * the License.
28
 */
29
 
30
import java.io.BufferedReader;
31
import java.io.Closeable;
151 ilm 32
import java.io.FileReader;
19 ilm 33
import java.io.IOException;
34
import java.io.Reader;
35
import java.util.ArrayList;
36
import java.util.List;
37
 
38
/**
39
 * A very simple CSV reader released under a commercial-friendly license.
40
 *
41
 * @author Glen Smith
42
 *
43
 */
44
public class CSVReader implements Closeable {
45
 
46
    private BufferedReader br;
47
 
48
    private boolean hasNext = true;
49
 
50
    private CSVParser parser;
51
 
52
    private int skipLines;
53
 
54
    private boolean linesSkiped;
55
 
56
    /**
57
     * The default line to start reading.
58
     */
59
    public static final int DEFAULT_SKIP_LINES = 0;
60
 
61
    /**
62
     * Constructs CSVReader using a comma for the separator.
63
     *
64
     * @param reader the reader to an underlying CSV source.
65
     */
66
    public CSVReader(Reader reader) {
67
        this(reader, CSVParser.DEFAULT_SEPARATOR, CSVParser.DEFAULT_QUOTE_CHARACTER, CSVParser.DEFAULT_ESCAPE_CHARACTER);
68
    }
69
 
70
    /**
71
     * Constructs CSVReader with supplied separator.
72
     *
73
     * @param reader the reader to an underlying CSV source.
74
     * @param separator the delimiter to use for separating entries.
75
     */
76
    public CSVReader(Reader reader, char separator) {
77
        this(reader, separator, CSVParser.DEFAULT_QUOTE_CHARACTER, CSVParser.DEFAULT_ESCAPE_CHARACTER);
78
    }
79
 
80
    /**
81
     * Constructs CSVReader with supplied separator and quote char.
82
     *
83
     * @param reader the reader to an underlying CSV source.
84
     * @param separator the delimiter to use for separating entries
85
     * @param quotechar the character to use for quoted elements
86
     */
87
    public CSVReader(Reader reader, char separator, char quotechar) {
88
        this(reader, separator, quotechar, CSVParser.DEFAULT_ESCAPE_CHARACTER, DEFAULT_SKIP_LINES, CSVParser.DEFAULT_STRICT_QUOTES);
89
    }
90
 
91
    /**
92
     * Constructs CSVReader with supplied separator, quote char and quote handling behavior.
93
     *
94
     * @param reader the reader to an underlying CSV source.
95
     * @param separator the delimiter to use for separating entries
96
     * @param quotechar the character to use for quoted elements
97
     * @param strictQuotes sets if characters outside the quotes are ignored
98
     */
99
    public CSVReader(Reader reader, char separator, char quotechar, boolean strictQuotes) {
100
        this(reader, separator, quotechar, CSVParser.DEFAULT_ESCAPE_CHARACTER, DEFAULT_SKIP_LINES, strictQuotes);
101
    }
102
 
103
    /**
104
     * Constructs CSVReader with supplied separator and quote char.
105
     *
106
     * @param reader the reader to an underlying CSV source.
107
     * @param separator the delimiter to use for separating entries
108
     * @param quotechar the character to use for quoted elements
109
     * @param escape the character to use for escaping a separator or quote
110
     */
111
 
112
    public CSVReader(Reader reader, char separator, char quotechar, char escape) {
113
        this(reader, separator, quotechar, escape, DEFAULT_SKIP_LINES, CSVParser.DEFAULT_STRICT_QUOTES);
114
    }
115
 
116
    /**
117
     * Constructs CSVReader with supplied separator and quote char.
118
     *
119
     * @param reader the reader to an underlying CSV source.
120
     * @param separator the delimiter to use for separating entries
121
     * @param quotechar the character to use for quoted elements
122
     * @param line the line number to skip for start reading
123
     */
124
    public CSVReader(Reader reader, char separator, char quotechar, int line) {
125
        this(reader, separator, quotechar, CSVParser.DEFAULT_ESCAPE_CHARACTER, line, CSVParser.DEFAULT_STRICT_QUOTES);
126
    }
127
 
128
    /**
129
     * Constructs CSVReader with supplied separator and quote char.
130
     *
131
     * @param reader the reader to an underlying CSV source.
132
     * @param separator the delimiter to use for separating entries
133
     * @param quotechar the character to use for quoted elements
134
     * @param escape the character to use for escaping a separator or quote
135
     * @param line the line number to skip for start reading
136
     */
137
    public CSVReader(Reader reader, char separator, char quotechar, char escape, int line) {
138
        this(reader, separator, quotechar, escape, line, CSVParser.DEFAULT_STRICT_QUOTES);
139
    }
140
 
141
    /**
142
     * Constructs CSVReader with supplied separator and quote char.
143
     *
144
     * @param reader the reader to an underlying CSV source.
145
     * @param separator the delimiter to use for separating entries
146
     * @param quotechar the character to use for quoted elements
147
     * @param escape the character to use for escaping a separator or quote
148
     * @param line the line number to skip for start reading
149
     * @param strictQuotes sets if characters outside the quotes are ignored
150
     */
151
    public CSVReader(Reader reader, char separator, char quotechar, char escape, int line, boolean strictQuotes) {
152
        this(reader, separator, quotechar, escape, line, strictQuotes, CSVParser.DEFAULT_IGNORE_LEADING_WHITESPACE);
153
    }
154
 
155
    /**
156
     * Constructs CSVReader with supplied separator and quote char.
157
     *
158
     * @param reader the reader to an underlying CSV source.
159
     * @param separator the delimiter to use for separating entries
160
     * @param quotechar the character to use for quoted elements
161
     * @param escape the character to use for escaping a separator or quote
162
     * @param line the line number to skip for start reading
163
     * @param strictQuotes sets if characters outside the quotes are ignored
164
     * @param ignoreLeadingWhiteSpace it true, parser should ignore white space before a quote in a
165
     *        field
166
     */
167
    public CSVReader(Reader reader, char separator, char quotechar, char escape, int line, boolean strictQuotes, boolean ignoreLeadingWhiteSpace) {
168
        this.br = new BufferedReader(reader);
169
        this.parser = new CSVParser(separator, quotechar, escape, strictQuotes, ignoreLeadingWhiteSpace);
170
        this.skipLines = line;
171
    }
172
 
173
    /**
174
     * Reads the entire file into a List with each element being a String[] of tokens.
175
     *
176
     * @return a List of String[], with each String[] representing a line of the file.
177
     *
178
     * @throws IOException if bad things happen during the read
179
     */
180
    public List<String[]> readAll() throws IOException {
181
 
182
        List<String[]> allElements = new ArrayList<String[]>();
183
        while (hasNext) {
184
            String[] nextLineAsTokens = readNext();
185
            if (nextLineAsTokens != null)
186
                allElements.add(nextLineAsTokens);
187
        }
188
        return allElements;
189
 
190
    }
191
 
192
    /**
193
     * Reads the next line from the buffer and converts to a string array.
194
     *
195
     * @return a string array with each comma-separated element as a separate entry.
196
     *
197
     * @throws IOException if bad things happen during the read
198
     */
199
    public String[] readNext() throws IOException {
200
 
201
        String[] result = null;
202
        do {
203
            String nextLine = getNextLine();
204
            if (!hasNext) {
205
                return result; // should throw if still pending?
206
            }
207
            String[] r = parser.parseLineMulti(nextLine);
208
            if (r.length > 0) {
209
                if (result == null) {
210
                    result = r;
211
                } else {
212
                    String[] t = new String[result.length + r.length];
213
                    System.arraycopy(result, 0, t, 0, result.length);
214
                    System.arraycopy(r, 0, t, result.length, r.length);
215
                    result = t;
216
                }
217
            }
218
        } while (parser.isPending());
219
        return result;
220
    }
221
 
222
    /**
223
     * Reads the next line from the file.
224
     *
225
     * @return the next line from the file without trailing newline
226
     * @throws IOException if bad things happen during the read
227
     */
228
    private String getNextLine() throws IOException {
229
        if (!this.linesSkiped) {
230
            for (int i = 0; i < skipLines; i++) {
231
                br.readLine();
232
            }
233
            this.linesSkiped = true;
234
        }
235
        String nextLine = br.readLine();
236
        if (nextLine == null) {
237
            hasNext = false;
238
        }
239
        return hasNext ? nextLine : null;
240
    }
241
 
242
    /**
243
     * Closes the underlying reader.
244
     *
245
     * @throws IOException if the close fails
246
     */
247
    public void close() throws IOException {
248
        br.close();
249
    }
250
 
151 ilm 251
    public static void main(String[] args) throws IOException {
252
        CSVReader reader = new CSVReader(new FileReader("n:\\Sans nom 1.csv"));
253
        String[] s = reader.readNext();
254
        for (int i = 0; i < s.length; i++) {
255
            System.err.println("CSVReader.main() :" + i + " : " + s[i]);
256
        }
257
    }
19 ilm 258
}