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 / gml / simplereaders / GMLReader.java @ 47638

History | View | Annotate | Download (3.81 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.gml.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.gml.virtualrows.GfsFile;
15
import org.gvsig.fmap.dal.store.gml.virtualrows.XMLFileAsList;
16
import org.gvsig.fmap.dal.store.simplereader.SimpleReaderStoreParameters;
17
import org.gvsig.fmap.dal.store.simplereader.simplereaders.AbstractSimpleReader;
18
import org.slf4j.Logger;
19
import org.slf4j.LoggerFactory;
20

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

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

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

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

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

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

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

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

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

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

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

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