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 / GMLReader.java @ 47634

History | View | Annotate | Download (3.67 KB)

1
/*
2
 * To change this license theHeader, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.fmap.dal.store.csv.simplereaders;
7

    
8
import java.io.File;
9
import java.io.IOException;
10
import java.io.Reader;
11
import java.nio.charset.Charset;
12
import java.util.List;
13
import org.apache.commons.io.FilenameUtils;
14
import org.gvsig.fmap.dal.store.csv.CSVStoreParameters;
15
import org.gvsig.fmap.dal.store.csv.xml.GfsFile;
16
import org.gvsig.fmap.dal.store.csv.xml.XMLFileAsList;
17
import org.slf4j.Logger;
18
import org.slf4j.LoggerFactory;
19

    
20
/**
21
 *
22
 * @author gvSIG Team
23
 */
24
public class GMLReader extends AbstractSimpleReader {
25

    
26
    private static final Logger LOGGER = LoggerFactory.getLogger(GMLReader.class);
27

    
28
    private XMLFileAsList gml;
29
    private int columns;
30
    private String[] header;
31
    private int currentElement = 0;
32
    private final CSVStoreParameters parameters;
33
    private GfsFile gfs;
34

    
35
    public GMLReader(Reader reader, CSVStoreParameters csvParameters) throws IOException {
36
        File gmlFile = csvParameters.getFile();
37
        File gmlIdx = new File(FilenameUtils.removeExtension(gmlFile.getName())+".gmlidx");
38
        File gfsFile = new File(FilenameUtils.removeExtension(gmlFile.getName())+".gfs");
39
        gfs = new GfsFile();
40
        if(gmlIdx.exists()){
41
            //TODO: Comparar fechas del gfs y gml
42
            gfs.load(gfsFile);
43
        } else {
44
            gfs.fetch(gmlFile);
45
            gfs.save(gfsFile);
46
        }
47
        
48
        this.gml = new XMLFileAsList(
49
                gmlFile, 
50
                gmlIdx, 
51
                Charset.forName(CSVStoreParameters.getCharset(csvParameters)), 
52
                gfs.getBaseElementPath(),
53
                gfs.getGeometryElementPaths(),
54
                gfs.getPropertiesPaths()
55
        );
56
        this.columns = -1;
57
        this.header = null;
58
        this.parameters = csvParameters;
59
    }
60

    
61
    @Override
62
    public String[] getHeader() throws IOException {
63
        if (this.header == null) {
64
            String[] theHeader = new String[gfs.size()];
65
            int i = 0;
66
            for (GfsFile.PropertyDefn item : gfs) {
67
                theHeader[i++] = item.getName();
68
            }
69
            this.header = theHeader;
70
        }
71
        return this.header;
72
    }
73

    
74
    @Override
75
    public int getColumnsCount() throws IOException {
76
        if (this.columns <= 0) {
77
            this.columns = this.getHeader().length;
78
        }
79
        return this.columns;
80
    }
81

    
82
    @Override
83
    public List<String> read() throws IOException {
84
        List<String> values = this.read(currentElement);
85
        if (values == null) {
86
            return null;
87
        }
88
        currentElement += 1;
89
        return values;
90
    }
91

    
92
    public List<String> read(int rowNumber) throws IOException {
93
        if (rowNumber < gml.size()) {
94
            List<String> values = gml.get(rowNumber);
95
            return values;
96
        }
97
        return null;
98
    }
99

    
100
    @Override
101
    public void close() throws IOException {
102
        this.gml.close();
103
        this.gml = null;
104
        this.gfs = null;
105
    }
106

    
107
    @Override
108
    public List<String> skip(int lines) throws IOException {
109
        this.currentElement += lines;
110
        if (this.currentElement > this.gml.size()) {
111
            return null;
112
        }
113
        return read(this.currentElement);
114
    }
115

    
116
    @Override
117
    public int getLine() {
118
        if (this.gml == null) {
119
            return 0;
120
        }
121
        return this.currentElement;
122
    }
123

    
124
    @Override
125
    public List<String> nextRowValues() {
126
        try {
127
            return this.read();
128
        } catch (IOException ex) {
129
            throw new RuntimeException(ex);
130
        }
131
    }
132
}