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 / FixedLenReader.java @ 46054

History | View | Annotate | Download (3.71 KB)

1

    
2
package org.gvsig.fmap.dal.store.csv.simplereaders;
3

    
4
import java.io.BufferedReader;
5
import java.io.IOException;
6
import java.io.InputStreamReader;
7
import java.io.Reader;
8
import java.util.ArrayList;
9
import java.util.List;
10
import org.apache.commons.lang3.StringUtils;
11
import org.gvsig.fmap.dal.store.csv.CSVStoreParameters;
12

    
13

    
14
public class FixedLenReader implements SimpleReader {
15

    
16
    BufferedReader reader = null;
17
    CSVStoreParameters.FieldDefinition fieldsDefinition[] = null;
18
    List fields = null;
19
    private final int fieldCount;
20
//    private final CSVStoreParameters parameters;
21
    private final String commentStartMarker;
22
    private int currentLine =1;
23

    
24
    public FixedLenReader(Reader reader, CSVStoreParameters parameters) {
25
//        this.parameters = parameters;
26
        this.reader = new BufferedReader(reader);
27
        this.fieldsDefinition = CSVStoreParameters.getFieldsDefinition(parameters);
28
        this.fieldCount = this.fieldsDefinition.length;
29
        this.fields = new ArrayList(this.fieldCount);
30
        for( int i=0; i<this.fieldCount; i++ ) {
31
            this.fields.add(null);
32
        }
33
        this.commentStartMarker = CSVStoreParameters.getCommentStartMarker(parameters);
34
    }
35

    
36
    public String[] getHeader() throws IOException {
37
        String[] header = new String[this.fieldCount];
38
        for( int i=0; i<this.fieldCount; i++ ) {
39
            header[i] = Character.toString((char) (i+'A')).toUpperCase();
40
        }
41
        return header;
42
    }
43

    
44
    @Override
45
    public int getColumnsCount() throws IOException {
46
        return this.fieldCount;
47
    }
48

    
49
    public List<String> read() throws IOException {
50
        String line = this.reader.readLine();
51
        this.currentLine++;
52
        while( !StringUtils.isEmpty(line) && !StringUtils.isEmpty(this.commentStartMarker) && line.startsWith(this.commentStartMarker) ) {
53
            line = this.reader.readLine();
54
            this.currentLine++;
55
        }
56
        return this.parse(line);
57
    }
58

    
59
    public List<String> parse(String line) throws IOException {
60
        if( line == null ) {
61
            return null;
62
        }
63
        for ( int i = 0; i < this.fieldCount; i++ ) {
64
            CSVStoreParameters.FieldDefinition fieldDefinition = this.fieldsDefinition[i];
65
            String value = null;
66
            try {
67
                if ( fieldDefinition.getToEndOfLine() ) {
68
                    value = line.substring(fieldDefinition.getStart());
69
                } else {
70
                    value = line.substring(
71
                            fieldDefinition.getStart(),
72
                            fieldDefinition.getEnd()
73
                    );
74
                }
75
            } catch(Exception ex) {
76
                // Ignore errors
77
                // Probablemente por que las lineas no tienen tantos caracteres como
78
                // se han indicado en la definicion.
79
            }
80
            this.fields.set(i, value);
81
        }
82
        return this.fields;
83
    }
84

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

    
90
    public List<String> skip(int lines) throws IOException {
91
        String line = null;
92
        for ( int i = 0; i < lines; i++ ) {
93
            line = this.reader.readLine();
94
            this.currentLine++;
95
            while( line!=null && this.commentStartMarker!=null && line.startsWith(this.commentStartMarker) ) {
96
                line = this.reader.readLine();
97
                this.currentLine++;
98
            }
99
        }
100
        return this.parse(line);
101
    }
102

    
103
    public int getLine() {
104
        return this.currentLine;
105
    }
106

    
107
    @Override
108
    public List<String> nextRowValues() {
109
        try {
110
            return this.read();
111
        } catch (IOException ex) {
112
            throw new RuntimeException(ex);
113
        }
114
    }
115
}