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 @ 43283
History | View | Annotate | Download (3.51 KB)
1 | 41617 | jjdelcerro | |
---|---|---|---|
2 | package org.gvsig.fmap.dal.store.csv.simplereaders; |
||
3 | |||
4 | import java.io.BufferedReader; |
||
5 | import java.io.IOException; |
||
6 | 43283 | jjdelcerro | import java.io.InputStreamReader; |
7 | 41617 | jjdelcerro | import java.util.ArrayList; |
8 | import java.util.List; |
||
9 | 42775 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
10 | 41617 | jjdelcerro | 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 | 41643 | jjdelcerro | // private final CSVStoreParameters parameters;
|
20 | private final String commentStartMarker; |
||
21 | private int currentLine =1; |
||
22 | 41617 | jjdelcerro | |
23 | 43283 | jjdelcerro | public FixedLenReader(InputStreamReader reader, CSVStoreParameters parameters) { |
24 | 41643 | jjdelcerro | // this.parameters = parameters;
|
25 | 41617 | jjdelcerro | 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 | 42775 | jjdelcerro | @Override
|
44 | public int getColumnsCount() throws IOException { |
||
45 | return this.fieldCount; |
||
46 | } |
||
47 | |||
48 | 41617 | jjdelcerro | public List<String> read() throws IOException { |
49 | String line = this.reader.readLine(); |
||
50 | 41643 | jjdelcerro | this.currentLine++;
|
51 | 42775 | jjdelcerro | while( !StringUtils.isEmpty(line) && !StringUtils.isEmpty(this.commentStartMarker) && line.startsWith(this.commentStartMarker) ) { |
52 | 41617 | jjdelcerro | line = this.reader.readLine();
|
53 | 41643 | jjdelcerro | this.currentLine++;
|
54 | 41617 | jjdelcerro | } |
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 | 41643 | jjdelcerro | 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 | 41617 | jjdelcerro | } |
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 | 41643 | jjdelcerro | this.currentLine++;
|
94 | while( line!=null && this.commentStartMarker!=null && line.startsWith(this.commentStartMarker) ) { |
||
95 | 41617 | jjdelcerro | line = this.reader.readLine();
|
96 | 41643 | jjdelcerro | this.currentLine++;
|
97 | 41617 | jjdelcerro | } |
98 | } |
||
99 | return this.parse(line); |
||
100 | } |
||
101 | |||
102 | 41643 | jjdelcerro | public int getLine() { |
103 | return this.currentLine; |
||
104 | } |
||
105 | 41617 | jjdelcerro | } |