svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.csv / src / main / java / org / gvsig / fmap / dal / store / csv / simplereaders / CSVReaderSuperCSV.java @ 46054
History | View | Annotate | Download (7.68 KB)
1 |
package org.gvsig.fmap.dal.store.csv.simplereaders; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.IOException; |
5 |
import java.io.InputStreamReader; |
6 |
import java.io.Reader; |
7 |
import java.util.List; |
8 |
import org.apache.commons.io.FilenameUtils; |
9 |
import org.apache.commons.io.IOUtils; |
10 |
import org.apache.commons.lang3.StringUtils; |
11 |
import org.gvsig.fmap.dal.store.csv.CSVStoreParameters; |
12 |
import org.gvsig.fmap.dal.store.csv.virtualrows.RandomAccessFileIndex; |
13 |
import org.gvsig.fmap.dal.store.csv.virtualrows.RandomAccessFileReader; |
14 |
import static org.gvsig.fmap.dal.store.csv.virtualrows.RandomAccessFileReader.FILTER_NONE; |
15 |
import org.gvsig.fmap.dal.store.csv.virtualrows.SuperCSVList; |
16 |
import org.gvsig.tools.dynobject.DynObject; |
17 |
import org.gvsig.tools.task.SimpleTaskStatus; |
18 |
import org.gvsig.tools.util.GetItemWithSize64; |
19 |
import org.slf4j.Logger; |
20 |
import org.slf4j.LoggerFactory; |
21 |
import org.supercsv.comment.CommentStartsWith; |
22 |
import org.supercsv.io.CsvListReader; |
23 |
import org.supercsv.prefs.CsvPreference; |
24 |
import org.supercsv.quote.QuoteMode; |
25 |
|
26 |
public class CSVReaderSuperCSV extends AbstractSimpleReader { |
27 |
|
28 |
//
|
29 |
// http://supercsv.sourceforge.net/examples_reading.html
|
30 |
// http://supercsv.sourceforge.net/apidocs/index.html
|
31 |
//
|
32 |
private static final Logger LOGGER = LoggerFactory.getLogger(CSVReaderSuperCSV.class); |
33 |
|
34 |
private CsvListReader reader;
|
35 |
private final CSVStoreParameters parameters; |
36 |
private List<String> nextLine; |
37 |
private int columns; |
38 |
|
39 |
public CSVReaderSuperCSV(Reader in, CSVStoreParameters parameters) { |
40 |
this(parameters);
|
41 |
this.reader = new CsvListReader(in, getCSVPreferences()); |
42 |
} |
43 |
|
44 |
public CSVReaderSuperCSV(CSVStoreParameters parameters) {
|
45 |
this.reader = null; |
46 |
this.parameters = parameters;
|
47 |
this.reader = null; |
48 |
this.nextLine = null; |
49 |
this.columns = 0; |
50 |
} |
51 |
|
52 |
public CSVStoreParameters getParameters() {
|
53 |
return this.parameters; |
54 |
} |
55 |
|
56 |
@Override
|
57 |
public String[] getHeader() throws IOException { |
58 |
return this.reader.getHeader(true); |
59 |
} |
60 |
|
61 |
@Override
|
62 |
public int getColumnsCount() throws IOException { |
63 |
if( this.columns <= 0 ) { |
64 |
this.columns = reader.length();
|
65 |
if( this.columns <= 0 ) { |
66 |
this.nextLine = this.reader.read(); |
67 |
this.columns = reader.length();
|
68 |
} |
69 |
} |
70 |
return this.columns; |
71 |
} |
72 |
|
73 |
@Override
|
74 |
public GetItemWithSize64<List<String>> getVirtualRows(SimpleTaskStatus status) { |
75 |
RandomAccessFileReader theReader = null;
|
76 |
RandomAccessFileIndex theIndex = null;
|
77 |
try {
|
78 |
CSVStoreParameters params = getParameters(); |
79 |
File data_file = CSVStoreParameters.getFile(params);
|
80 |
if( data_file.length()< 10*1024*1024 ) { |
81 |
return null; |
82 |
} |
83 |
|
84 |
String charset = CSVStoreParameters.getCharset(params);
|
85 |
File index_file = new File(FilenameUtils.removeExtension(data_file.getAbsolutePath()) + ".idx"); |
86 |
|
87 |
theReader = new RandomAccessFileReader(data_file, charset);
|
88 |
theIndex = theReader.createOrOpenIndexOfLines(index_file, false, FILTER_NONE, status);
|
89 |
|
90 |
SuperCSVList list = new SuperCSVList(
|
91 |
theReader, |
92 |
theIndex, |
93 |
CSVStoreParameters.isFirstLineHeader(getParameters())?1:0 |
94 |
); |
95 |
|
96 |
list.setPreferences(this.getCSVPreferences());
|
97 |
return list;
|
98 |
} catch (IOException ex) { |
99 |
return null; |
100 |
} finally {
|
101 |
// We do not close the index or the reader because we need it to remain open
|
102 |
// IOUtils.closeQuietly(theReader);
|
103 |
// IOUtils.closeQuietly(theIndex);
|
104 |
} |
105 |
} |
106 |
|
107 |
@Override
|
108 |
public List<String> read() throws IOException { |
109 |
List<String> line; |
110 |
if( this.nextLine != null ) { |
111 |
line = this.nextLine;
|
112 |
this.nextLine = null; |
113 |
} else {
|
114 |
line = this.reader.read();
|
115 |
} |
116 |
if( line!=null ) { |
117 |
for (int i = 0; i < line.size(); i++) { |
118 |
String s = line.get(i);
|
119 |
// System.out.println("CSVReaderSuperCSV.read(): bef s ='"+s+"'");
|
120 |
if( s!=null ) { |
121 |
s = s.replaceAll("\\\\n","\n"); |
122 |
s = s.replaceAll("\\\\r","\r"); |
123 |
line.set(i, s); |
124 |
// System.out.println("CSVReaderSuperCSV.read(): aft s ='"+s+"'");
|
125 |
} |
126 |
} |
127 |
} |
128 |
return line;
|
129 |
} |
130 |
|
131 |
@Override
|
132 |
public void close() throws IOException { |
133 |
this.reader.close();
|
134 |
} |
135 |
|
136 |
@Override
|
137 |
public List<String> skip(int lines) throws IOException { |
138 |
if( lines <= 0 ) { |
139 |
return null; |
140 |
} |
141 |
if( this.nextLine != null ) { |
142 |
this.nextLine = null; |
143 |
lines--; |
144 |
} |
145 |
List<String> row = null; |
146 |
for ( int i = 0; i < lines; i++ ) { |
147 |
row = reader.read(); |
148 |
} |
149 |
return row;
|
150 |
} |
151 |
|
152 |
public final CsvPreference getCSVPreferences() { |
153 |
try {
|
154 |
String s;
|
155 |
char quoteChar;
|
156 |
int delimiterChar;
|
157 |
String endOfLineSymbols;
|
158 |
|
159 |
DynObject params = this.getParameters();
|
160 |
|
161 |
CsvPreference.Builder builder; |
162 |
|
163 |
CsvPreference defaultPreference = CSVStoreParameters |
164 |
.getPredefinedCSVPreferences(params); |
165 |
if ( defaultPreference == null ) { |
166 |
defaultPreference = CsvPreference.STANDARD_PREFERENCE; |
167 |
} |
168 |
|
169 |
endOfLineSymbols = CSVStoreParameters.getRecordSeparator(params); |
170 |
if ( StringUtils.isBlank(endOfLineSymbols) ) {
|
171 |
endOfLineSymbols = defaultPreference.getEndOfLineSymbols(); |
172 |
} |
173 |
s = CSVStoreParameters.getQuoteCharacter(params); |
174 |
if ( StringUtils.isBlank(s) ) {
|
175 |
quoteChar = (char) defaultPreference.getQuoteChar();
|
176 |
} else {
|
177 |
quoteChar = s.charAt(0);
|
178 |
} |
179 |
s = CSVStoreParameters.getDelimiter(params); |
180 |
if ( StringUtils.isBlank(s) ) {
|
181 |
delimiterChar = defaultPreference.getDelimiterChar(); |
182 |
} else {
|
183 |
delimiterChar = s.charAt(0);
|
184 |
} |
185 |
|
186 |
builder = new CsvPreference.Builder(quoteChar, delimiterChar,
|
187 |
endOfLineSymbols); |
188 |
|
189 |
s = CSVStoreParameters.getCommentStartMarker(params); |
190 |
if ( !StringUtils.isBlank(s) ) {
|
191 |
CommentStartsWith cs = new CommentStartsWith(s);
|
192 |
builder.skipComments(cs); |
193 |
} |
194 |
|
195 |
builder.surroundingSpacesNeedQuotes(CSVStoreParameters |
196 |
.getSurroundingSpacesNeedQuotes(params)); |
197 |
QuoteMode quoteMode = CSVStoreParameters.getQuoteMode(params); |
198 |
if ( quoteMode != null ) { |
199 |
builder.useQuoteMode(quoteMode); |
200 |
} |
201 |
return builder.build();
|
202 |
} catch (Exception e) { |
203 |
LOGGER.warn("Can't make preferences for CSV '" + getFullFileName()
|
204 |
+ "'.", e);
|
205 |
return null; |
206 |
} |
207 |
} |
208 |
|
209 |
private String getFullFileName() { |
210 |
// Usar solo para mostrar mensajes en el logger.
|
211 |
String s;
|
212 |
try {
|
213 |
s = getParameters().getFile().getAbsolutePath(); |
214 |
} catch (Exception e2) { |
215 |
s = "(unknow)";
|
216 |
} |
217 |
return s;
|
218 |
} |
219 |
|
220 |
@Override
|
221 |
public int getLine() { |
222 |
if( this.reader==null ) { |
223 |
return 0; |
224 |
} |
225 |
return this.reader.getLineNumber(); |
226 |
} |
227 |
|
228 |
@Override
|
229 |
public List<String> nextRowValues() { |
230 |
try {
|
231 |
return this.read(); |
232 |
} catch (IOException ex) { |
233 |
throw new RuntimeException(ex); |
234 |
} |
235 |
} |
236 |
|
237 |
} |