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

History | View | Annotate | Download (3.49 KB)

1

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

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

    
12

    
13
public class FixedLenReader implements SimpleReader {
14

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

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

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

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

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

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

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

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

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