Statistics
| Revision:

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 @ 45083

History | View | Annotate | Download (5.63 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
        if( line!=null ) {
73
            for (int i = 0; i < line.size(); i++) {
74
                String s = line.get(i);
75
                if( s!=null ) {
76
                    s = s.replaceAll("\\\\n","\n");
77
                    s = s.replaceAll("\\\\r","\r");
78
                    line.set(i, s);
79
                }
80
            }
81
        }
82
        return line;
83
    }
84

    
85
    @Override
86
    public void close() throws IOException {
87
        this.reader.close();
88
    }
89

    
90
    @Override
91
    public List<String> skip(int lines) throws IOException {
92
        if( lines <= 0 ) {
93
            return null;
94
        }
95
        if( this.nextLine != null ) {
96
            this.nextLine = null;
97
            lines--;
98
        }
99
        List<String> row = null;
100
        for ( int i = 0; i < lines; i++ ) {
101
            row = reader.read();
102
        }
103
        return row;
104
    }
105

    
106
    public final CsvPreference getCSVPreferences() {
107
        try {
108
            String s;
109
            char quoteChar;
110
            int delimiterChar;
111
            String endOfLineSymbols;
112

    
113
            DynObject params = this.getParameters();
114

    
115
            CsvPreference.Builder builder;
116

    
117
            CsvPreference defaultPreference = CSVStoreParameters
118
                    .getPredefinedCSVPreferences(params);
119
            if ( defaultPreference == null ) {
120
                defaultPreference = CsvPreference.STANDARD_PREFERENCE;
121
            }
122

    
123
            endOfLineSymbols = CSVStoreParameters.getRecordSeparator(params);
124
            if ( StringUtils.isBlank(endOfLineSymbols) ) {
125
                endOfLineSymbols = defaultPreference.getEndOfLineSymbols();
126
            }
127
            s = CSVStoreParameters.getQuoteCharacter(params);
128
            if ( StringUtils.isBlank(s) ) {
129
                quoteChar = (char) defaultPreference.getQuoteChar();
130
            } else {
131
                quoteChar = s.charAt(0);
132
            }
133
            s = CSVStoreParameters.getDelimiter(params);
134
            if ( StringUtils.isBlank(s) ) {
135
                delimiterChar = defaultPreference.getDelimiterChar();
136
            } else {
137
                delimiterChar = s.charAt(0);
138
            }
139

    
140
            builder = new CsvPreference.Builder(quoteChar, delimiterChar,
141
                    endOfLineSymbols);
142

    
143
            s = CSVStoreParameters.getCommentStartMarker(params);
144
            if ( !StringUtils.isBlank(s) ) {
145
                CommentStartsWith cs = new CommentStartsWith(s);
146
                builder.skipComments(cs);
147
            }
148

    
149
            builder.surroundingSpacesNeedQuotes(CSVStoreParameters
150
                    .getSurroundingSpacesNeedQuotes(params));
151
            QuoteMode quoteMode = CSVStoreParameters.getQuoteMode(params);
152
            if ( quoteMode != null ) {
153
                builder.useQuoteMode(quoteMode);
154
            }
155
            return builder.build();
156
        } catch (Exception e) {
157
            LOGGER.warn("Can't make preferences for CSV '" + getFullFileName()
158
                    + "'.", e);
159
            return null;
160
        }
161
    }
162
    
163
    private String getFullFileName() {
164
        // Usar solo para mostrar mensajes en el logger.
165
        String s;
166
        try {
167
            s = getParameters().getFile().getAbsolutePath();
168
        } catch (Exception e2) {
169
            s = "(unknow)";
170
        }
171
        return s;        
172
    }
173

    
174
    @Override
175
    public int getLine() {
176
        if( this.reader==null ) {
177
            return 0;
178
        }
179
        return this.reader.getLineNumber();
180
    }
181

    
182
    @Override
183
    public List<String> nextRowValues() {
184
        try {
185
            return this.read();
186
        } catch (IOException ex) {
187
            throw new RuntimeException(ex);
188
        }
189
    }
190

    
191
}