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 / CSVReader.java @ 43983
History | View | Annotate | Download (5.3 KB)
1 |
package org.gvsig.fmap.dal.store.csv.simplereaders; |
---|---|
2 |
|
3 |
import java.io.IOException; |
4 |
import java.io.InputStreamReader; |
5 |
import java.util.List; |
6 |
import org.apache.commons.lang3.StringUtils; |
7 |
import org.gvsig.fmap.dal.store.csv.CSVStoreParameters; |
8 |
import org.gvsig.tools.dynobject.DynObject; |
9 |
import org.slf4j.Logger; |
10 |
import org.slf4j.LoggerFactory; |
11 |
import org.supercsv.comment.CommentStartsWith; |
12 |
import org.supercsv.io.CsvListReader; |
13 |
import org.supercsv.prefs.CsvPreference; |
14 |
import org.supercsv.quote.QuoteMode; |
15 |
|
16 |
public class CSVReader implements SimpleReader { |
17 |
|
18 |
//
|
19 |
// http://supercsv.sourceforge.net/examples_reading.html
|
20 |
// http://supercsv.sourceforge.net/apidocs/index.html
|
21 |
//
|
22 |
private static final Logger logger = LoggerFactory.getLogger(CSVReader.class); |
23 |
|
24 |
private CsvListReader reader;
|
25 |
private final CSVStoreParameters parameters; |
26 |
private List<String> nextLine; |
27 |
private int columns; |
28 |
|
29 |
public CSVReader(InputStreamReader in, CSVStoreParameters parameters) { |
30 |
this(parameters);
|
31 |
this.reader = new CsvListReader(in, getCSVPreferences()); |
32 |
} |
33 |
|
34 |
public CSVReader(CSVStoreParameters parameters) {
|
35 |
this.reader = null; |
36 |
this.parameters = parameters;
|
37 |
this.reader = null; |
38 |
this.nextLine = null; |
39 |
this.columns = 0; |
40 |
} |
41 |
|
42 |
public CSVStoreParameters getParameters() {
|
43 |
return this.parameters; |
44 |
} |
45 |
|
46 |
@Override
|
47 |
public String[] getHeader() throws IOException { |
48 |
return this.reader.getHeader(true); |
49 |
} |
50 |
|
51 |
@Override
|
52 |
public int getColumnsCount() throws IOException { |
53 |
if( this.columns <= 0 ) { |
54 |
this.columns = reader.length();
|
55 |
if( this.columns <= 0 ) { |
56 |
this.nextLine = this.reader.read(); |
57 |
this.columns = reader.length();
|
58 |
} |
59 |
} |
60 |
return this.columns; |
61 |
} |
62 |
|
63 |
@Override
|
64 |
public List<String> read() throws IOException { |
65 |
List<String> line; |
66 |
if( this.nextLine != null ) { |
67 |
line = this.nextLine;
|
68 |
this.nextLine = null; |
69 |
} else {
|
70 |
line = this.reader.read();
|
71 |
} |
72 |
return line;
|
73 |
} |
74 |
|
75 |
@Override
|
76 |
public void close() throws IOException { |
77 |
this.reader.close();
|
78 |
} |
79 |
|
80 |
@Override
|
81 |
public List<String> skip(int lines) throws IOException { |
82 |
if( lines <= 0 ) { |
83 |
return null; |
84 |
} |
85 |
if( this.nextLine != null ) { |
86 |
this.nextLine = null; |
87 |
lines--; |
88 |
} |
89 |
List<String> row = null; |
90 |
for ( int i = 0; i < lines; i++ ) { |
91 |
row = reader.read(); |
92 |
} |
93 |
return row;
|
94 |
} |
95 |
|
96 |
public CsvPreference getCSVPreferences() {
|
97 |
try {
|
98 |
String s;
|
99 |
char quoteChar;
|
100 |
int delimiterChar;
|
101 |
String endOfLineSymbols;
|
102 |
|
103 |
DynObject params = this.getParameters();
|
104 |
|
105 |
CsvPreference.Builder builder; |
106 |
|
107 |
CsvPreference defaultPreference = CSVStoreParameters |
108 |
.getPredefinedCSVPreferences(params); |
109 |
if ( defaultPreference == null ) { |
110 |
defaultPreference = CsvPreference.STANDARD_PREFERENCE; |
111 |
} |
112 |
|
113 |
endOfLineSymbols = CSVStoreParameters.getRecordSeparator(params); |
114 |
if ( StringUtils.isBlank(endOfLineSymbols) ) {
|
115 |
endOfLineSymbols = defaultPreference.getEndOfLineSymbols(); |
116 |
} |
117 |
s = CSVStoreParameters.getQuoteCharacter(params); |
118 |
if ( StringUtils.isBlank(s) ) {
|
119 |
quoteChar = (char) defaultPreference.getQuoteChar();
|
120 |
} else {
|
121 |
quoteChar = s.charAt(0);
|
122 |
} |
123 |
s = CSVStoreParameters.getDelimiter(params); |
124 |
if ( StringUtils.isBlank(s) ) {
|
125 |
delimiterChar = defaultPreference.getDelimiterChar(); |
126 |
} else {
|
127 |
delimiterChar = s.charAt(0);
|
128 |
} |
129 |
|
130 |
builder = new CsvPreference.Builder(quoteChar, delimiterChar,
|
131 |
endOfLineSymbols); |
132 |
|
133 |
s = CSVStoreParameters.getCommentStartMarker(params); |
134 |
if ( !StringUtils.isBlank(s) ) {
|
135 |
CommentStartsWith cs = new CommentStartsWith(s);
|
136 |
builder.skipComments(cs); |
137 |
} |
138 |
|
139 |
builder.surroundingSpacesNeedQuotes(CSVStoreParameters |
140 |
.getSurroundingSpacesNeedQuotes(params)); |
141 |
QuoteMode quoteMode = CSVStoreParameters.getQuoteMode(params); |
142 |
if ( quoteMode != null ) { |
143 |
builder.useQuoteMode(quoteMode); |
144 |
} |
145 |
return builder.build();
|
146 |
} catch (Exception e) { |
147 |
logger.warn("Can't make preferences for CSV '" + getFullFileName()
|
148 |
+ "'.", e);
|
149 |
return null; |
150 |
} |
151 |
} |
152 |
|
153 |
private String getFullFileName() { |
154 |
// Usar solo para mostrar mensajes en el logger.
|
155 |
String s;
|
156 |
try {
|
157 |
s = getParameters().getFile().getAbsolutePath(); |
158 |
} catch (Exception e2) { |
159 |
s = "(unknow)";
|
160 |
} |
161 |
return s;
|
162 |
} |
163 |
|
164 |
@Override
|
165 |
public int getLine() { |
166 |
if( this.reader==null ) { |
167 |
return 0; |
168 |
} |
169 |
return this.reader.getLineNumber(); |
170 |
} |
171 |
|
172 |
@Override
|
173 |
public List<String> nextRowValues() { |
174 |
try {
|
175 |
return this.read(); |
176 |
} catch (IOException ex) { |
177 |
throw new RuntimeException(ex); |
178 |
} |
179 |
} |
180 |
|
181 |
} |