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

History | View | Annotate | Download (3.31 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.gvsig.fmap.dal.store.csv.CSVStoreParameters;
10

    
11

    
12
public class FixedLenReader implements SimpleReader {
13

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

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

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

    
42
    public List<String> read() throws IOException {
43
        String line = this.reader.readLine();
44
        this.currentLine++;
45
        while( line!=null && this.commentStartMarker!=null && line.startsWith(this.commentStartMarker) ) {
46
            line = this.reader.readLine();
47
            this.currentLine++;
48
        }
49
        return this.parse(line);
50
    }
51

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

    
78
    public void close() throws IOException {
79
        this.reader.close();
80
        this.reader = null;
81
    }
82

    
83
    public List<String> skip(int lines) throws IOException {
84
        String line = null;
85
        for ( int i = 0; i < lines; i++ ) {
86
            line = this.reader.readLine();
87
            this.currentLine++;
88
            while( line!=null && this.commentStartMarker!=null && line.startsWith(this.commentStartMarker) ) {
89
                line = this.reader.readLine();
90
                this.currentLine++;
91
            }
92
        }
93
        return this.parse(line);
94
    }
95

    
96
    public int getLine() {
97
        return this.currentLine;
98
    }
99
}