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 | 45563 | jolivas | /*
|
---|---|---|---|
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 | 47634 | fdiaz | import java.io.File; |
9 | 45563 | jolivas | import java.io.IOException; |
10 | 46054 | omartinez | import java.io.Reader; |
11 | 47634 | fdiaz | import java.nio.charset.Charset; |
12 | 45563 | jolivas | import java.util.List; |
13 | 47634 | fdiaz | import org.apache.commons.io.FilenameUtils; |
14 | 45567 | jolivas | import org.gvsig.fmap.dal.store.csv.CSVStoreParameters; |
15 | 47634 | fdiaz | import org.gvsig.fmap.dal.store.csv.xml.GfsFile; |
16 | import org.gvsig.fmap.dal.store.csv.xml.XMLFileAsList; |
||
17 | 45563 | jolivas | import org.slf4j.Logger; |
18 | import org.slf4j.LoggerFactory; |
||
19 | |||
20 | /**
|
||
21 | *
|
||
22 | 47634 | fdiaz | * @author gvSIG Team
|
23 | 45563 | jolivas | */
|
24 | 47634 | fdiaz | public class GMLReader extends AbstractSimpleReader { |
25 | 45567 | jolivas | |
26 | 47634 | fdiaz | private static final Logger LOGGER = LoggerFactory.getLogger(GMLReader.class); |
27 | 45567 | jolivas | |
28 | 47634 | fdiaz | private XMLFileAsList gml;
|
29 | 45563 | jolivas | private int columns; |
30 | private String[] header; |
||
31 | private int currentElement = 0; |
||
32 | 45567 | jolivas | private final CSVStoreParameters parameters; |
33 | 47634 | fdiaz | private GfsFile gfs;
|
34 | 45563 | jolivas | |
35 | 47634 | fdiaz | 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 | 45567 | jolivas | this.columns = -1; |
57 | this.header = null; |
||
58 | this.parameters = csvParameters;
|
||
59 | } |
||
60 | 45563 | jolivas | |
61 | @Override
|
||
62 | public String[] getHeader() throws IOException { |
||
63 | 45567 | jolivas | if (this.header == null) { |
64 | 47634 | fdiaz | String[] theHeader = new String[gfs.size()]; |
65 | 45563 | jolivas | int i = 0; |
66 | 47634 | fdiaz | for (GfsFile.PropertyDefn item : gfs) {
|
67 | theHeader[i++] = item.getName(); |
||
68 | 45563 | jolivas | } |
69 | this.header = theHeader;
|
||
70 | } |
||
71 | return this.header; |
||
72 | } |
||
73 | |||
74 | @Override
|
||
75 | public int getColumnsCount() throws IOException { |
||
76 | 45567 | jolivas | if (this.columns <= 0) { |
77 | this.columns = this.getHeader().length; |
||
78 | 45563 | jolivas | } |
79 | return this.columns; |
||
80 | } |
||
81 | |||
82 | @Override
|
||
83 | public List<String> read() throws IOException { |
||
84 | List<String> values = this.read(currentElement); |
||
85 | 45567 | jolivas | if (values == null) { |
86 | 45563 | jolivas | return null; |
87 | } |
||
88 | 45567 | jolivas | currentElement += 1;
|
89 | 45563 | jolivas | return values;
|
90 | } |
||
91 | |||
92 | public List<String> read(int rowNumber) throws IOException { |
||
93 | 47634 | fdiaz | if (rowNumber < gml.size()) {
|
94 | List<String> values = gml.get(rowNumber); |
||
95 | 45563 | jolivas | return values;
|
96 | } |
||
97 | return null; |
||
98 | } |
||
99 | 45567 | jolivas | |
100 | 45563 | jolivas | @Override
|
101 | public void close() throws IOException { |
||
102 | 47634 | fdiaz | this.gml.close();
|
103 | this.gml = null; |
||
104 | this.gfs = null; |
||
105 | 45563 | jolivas | } |
106 | |||
107 | @Override
|
||
108 | public List<String> skip(int lines) throws IOException { |
||
109 | this.currentElement += lines;
|
||
110 | 47634 | fdiaz | if (this.currentElement > this.gml.size()) { |
111 | 45563 | jolivas | return null; |
112 | } |
||
113 | return read(this.currentElement); |
||
114 | } |
||
115 | |||
116 | @Override
|
||
117 | public int getLine() { |
||
118 | 47634 | fdiaz | if (this.gml == null) { |
119 | 45563 | jolivas | return 0; |
120 | 45567 | jolivas | } |
121 | 45563 | jolivas | 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 | 45567 | jolivas | } |
132 | 45563 | jolivas | } |