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

History | View | Annotate | Download (2.75 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 String commentStartMarker;
20

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

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

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

    
49
    public List<String> parse(String line) throws IOException {
50
        if( line == null ) {
51
            return null;
52
        }
53
        for ( int i = 0; i < this.fieldCount; i++ ) {
54
            CSVStoreParameters.FieldDefinition fieldDefinition = this.fieldsDefinition[i];
55
            String value;
56
            if ( fieldDefinition.getToEndOfLine() ) {
57
                value = line.substring(fieldDefinition.getStart());
58
            } else {
59
                value = line.substring(
60
                        fieldDefinition.getStart(),
61
                        fieldDefinition.getEnd()
62
                );
63
            }
64
            this.fields.set(i, value);
65
        }
66
        return this.fields;
67
    }
68

    
69
    public void close() throws IOException {
70
        this.reader.close();
71
        this.reader = null;
72
    }
73

    
74
    public List<String> skip(int lines) throws IOException {
75
        String line = null;
76
        for ( int i = 0; i < lines; i++ ) {
77
            line = this.reader.readLine();
78
            while( line!=null && line.startsWith(this.commentStartMarker) ) {
79
                line = this.reader.readLine();
80
            }
81
        }
82
        return this.parse(line);
83
    }
84

    
85
}