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

History | View | Annotate | Download (5.36 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.apache.commons.io.IOUtils;
15
import org.cresques.cts.IProjection;
16
import org.gvsig.fmap.crs.CRSFactory;
17
import org.gvsig.fmap.dal.store.gml.virtualrows.GfsFile;
18
import org.gvsig.fmap.dal.store.gml.virtualrows.XMLFileAsList;
19
import org.gvsig.fmap.dal.store.simplereader.SimpleReaderStoreParameters;
20
import org.gvsig.fmap.dal.store.simplereader.simplereaders.AbstractSimpleReader;
21
import org.gvsig.tools.task.SimpleTaskStatus;
22
import org.gvsig.tools.util.GetItemWithSize64;
23
import org.slf4j.Logger;
24
import org.slf4j.LoggerFactory;
25

    
26
/**
27
 *
28
 * @author gvSIG Team
29
 */
30
public class GMLReader extends AbstractSimpleReader {
31

    
32
    private static final Logger LOGGER = LoggerFactory.getLogger(GMLReader.class);
33

    
34
    private XMLFileAsList gml;
35
    private int columns;
36
    private String[] header;
37
    private int currentElement = 0;
38
    private final SimpleReaderStoreParameters parameters;
39
    private GfsFile gfs;
40

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

    
67
    @Override
68
    public String[] getHeader() throws IOException {
69
        if (this.header == null) {
70
            List<String> geoms = gfs.getGeometryElementPaths();
71
            String[] theHeader = new String[gfs.size()+(geoms==null?0:geoms.size())];
72
            int i = 0;
73
            for (GfsFile.PropertyDefn item : gfs) {
74
                theHeader[i++] = item.getName();
75
            }
76
            if(geoms != null) {
77
                List<String> srss = gfs.getGeometryElementSrss();
78
                for (int j = 0; j < geoms.size(); j++) {
79
                    IProjection srs = CRSFactory.getCRS(srss.get(j));
80
                    if(srs == null){
81
                        theHeader[i++] = FilenameUtils.getBaseName(geoms.get(j));
82
                    } else {
83
                        theHeader[i++] = FilenameUtils.getBaseName(geoms.get(j))+"/Geometry/set/srs="+srs.getAbrev().replace(":", "@");
84
                    }
85
                }
86
            }
87
            this.header = theHeader;
88
        }
89
        return this.header;
90
    }
91

    
92
    @Override
93
    public int getColumnsCount() throws IOException {
94
        if (this.columns <= 0) {
95
            this.columns = this.getHeader().length;
96
        }
97
        return this.columns;
98
    }
99

    
100
    @Override
101
    public List<String> read() throws IOException {
102
        List<String> values = this.read(currentElement);
103
        if (values == null) {
104
            return null;
105
        }
106
        currentElement += 1;
107
        return values;
108
    }
109

    
110
    public List<String> read(int rowNumber) throws IOException {
111
        if (rowNumber < this.getGml().size64()) {
112
            List<String> values = this.getGml().get64(rowNumber);
113
            return values;
114
        }
115
        return null;
116
    }
117

    
118
    @Override
119
    public void close() throws IOException {
120
        IOUtils.closeQuietly(this.gml);
121
        this.gml = null;
122
        this.gfs = null;
123
    }
124

    
125
    @Override
126
    public List<String> skip(int lines) throws IOException {
127
        this.currentElement += lines;
128
        if (this.currentElement > this.getGml().size64()) {
129
            return null;
130
        }
131
        return read(this.currentElement);
132
    }
133

    
134
    @Override
135
    public int getLine() {
136
        if (this.getGml() == null) {
137
            return 0;
138
        }
139
        return this.currentElement;
140
    }
141

    
142
    @Override
143
    public List<String> nextRowValues() {
144
        try {
145
            return this.read();
146
        } catch (IOException ex) {
147
            throw new RuntimeException(ex);
148
        }
149
    }
150

    
151
    @Override
152
    public GetItemWithSize64<List<String>> getVirtualRows(SimpleTaskStatus status) {
153
        try {
154
            File gmlFile = this.parameters.getFile();
155
            File gmlIdx = new File(FilenameUtils.removeExtension(gmlFile.getAbsolutePath()) + ".gmlidx");
156

    
157
            XMLFileAsList x = new XMLFileAsList(
158
                gmlFile,
159
                gmlIdx,
160
                Charset.forName(SimpleReaderStoreParameters.getCharset(this.parameters)),
161
                gfs.getBaseElementPath(),
162
                gfs.getGeometryElementPaths(),
163
                gfs.getPropertiesPaths()
164
            );
165

    
166
            return x;
167
        } catch (IOException ex) {
168
            throw new RuntimeException("Can't get virtual rows", ex);
169
        }
170
    }
171
}