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

History | View | Annotate | Download (4.04 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.gvsig.tools.task.SimpleTaskStatus;
19
import org.gvsig.tools.util.GetItemWithSize64;
20
import org.slf4j.Logger;
21
import org.slf4j.LoggerFactory;
22

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

    
29
    private static final Logger LOGGER = LoggerFactory.getLogger(GMLReader.class);
30

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

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

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

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

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

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

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

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

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

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

    
136
    @Override
137
    public GetItemWithSize64<List<String>>  getVirtualRows(SimpleTaskStatus status) {
138
        return this.gml;
139
    }
140
}