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 | 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 | 47638 | jjdelcerro | package org.gvsig.fmap.dal.store.gml.simplereaders; |
7 | 45563 | jolivas | |
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 | 47638 | jjdelcerro | 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 | 45563 | jolivas | import org.slf4j.Logger; |
19 | import org.slf4j.LoggerFactory; |
||
20 | |||
21 | /**
|
||
22 | *
|
||
23 | 47634 | fdiaz | * @author gvSIG Team
|
24 | 45563 | jolivas | */
|
25 | 47634 | fdiaz | public class GMLReader extends AbstractSimpleReader { |
26 | 45567 | jolivas | |
27 | 47634 | fdiaz | private static final Logger LOGGER = LoggerFactory.getLogger(GMLReader.class); |
28 | 45567 | jolivas | |
29 | 47634 | fdiaz | private XMLFileAsList gml;
|
30 | 45563 | jolivas | private int columns; |
31 | private String[] header; |
||
32 | private int currentElement = 0; |
||
33 | 47638 | jjdelcerro | private final SimpleReaderStoreParameters parameters; |
34 | 47634 | fdiaz | private GfsFile gfs;
|
35 | 45563 | jolivas | |
36 | 47638 | jjdelcerro | public GMLReader(Reader reader, SimpleReaderStoreParameters theParameters) throws IOException { |
37 | File gmlFile = theParameters.getFile();
|
||
38 | 47634 | fdiaz | 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 | 47638 | jjdelcerro | Charset.forName(SimpleReaderStoreParameters.getCharset(theParameters)),
|
53 | 47634 | fdiaz | gfs.getBaseElementPath(), |
54 | gfs.getGeometryElementPaths(), |
||
55 | gfs.getPropertiesPaths() |
||
56 | ); |
||
57 | 45567 | jolivas | this.columns = -1; |
58 | this.header = null; |
||
59 | 47638 | jjdelcerro | this.parameters = theParameters;
|
60 | 45567 | jolivas | } |
61 | 45563 | jolivas | |
62 | @Override
|
||
63 | public String[] getHeader() throws IOException { |
||
64 | 45567 | jolivas | if (this.header == null) { |
65 | 47634 | fdiaz | String[] theHeader = new String[gfs.size()]; |
66 | 45563 | jolivas | int i = 0; |
67 | 47634 | fdiaz | for (GfsFile.PropertyDefn item : gfs) {
|
68 | theHeader[i++] = item.getName(); |
||
69 | 45563 | jolivas | } |
70 | this.header = theHeader;
|
||
71 | } |
||
72 | return this.header; |
||
73 | } |
||
74 | |||
75 | @Override
|
||
76 | public int getColumnsCount() throws IOException { |
||
77 | 45567 | jolivas | if (this.columns <= 0) { |
78 | this.columns = this.getHeader().length; |
||
79 | 45563 | jolivas | } |
80 | return this.columns; |
||
81 | } |
||
82 | |||
83 | @Override
|
||
84 | public List<String> read() throws IOException { |
||
85 | List<String> values = this.read(currentElement); |
||
86 | 45567 | jolivas | if (values == null) { |
87 | 45563 | jolivas | return null; |
88 | } |
||
89 | 45567 | jolivas | currentElement += 1;
|
90 | 45563 | jolivas | return values;
|
91 | } |
||
92 | |||
93 | public List<String> read(int rowNumber) throws IOException { |
||
94 | 47634 | fdiaz | if (rowNumber < gml.size()) {
|
95 | List<String> values = gml.get(rowNumber); |
||
96 | 45563 | jolivas | return values;
|
97 | } |
||
98 | return null; |
||
99 | } |
||
100 | 45567 | jolivas | |
101 | 45563 | jolivas | @Override
|
102 | public void close() throws IOException { |
||
103 | 47634 | fdiaz | this.gml.close();
|
104 | this.gml = null; |
||
105 | this.gfs = null; |
||
106 | 45563 | jolivas | } |
107 | |||
108 | @Override
|
||
109 | public List<String> skip(int lines) throws IOException { |
||
110 | this.currentElement += lines;
|
||
111 | 47634 | fdiaz | if (this.currentElement > this.gml.size()) { |
112 | 45563 | jolivas | return null; |
113 | } |
||
114 | return read(this.currentElement); |
||
115 | } |
||
116 | |||
117 | @Override
|
||
118 | public int getLine() { |
||
119 | 47634 | fdiaz | if (this.gml == null) { |
120 | 45563 | jolivas | return 0; |
121 | 45567 | jolivas | } |
122 | 45563 | jolivas | 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 | 45567 | jolivas | } |
133 | 45563 | jolivas | } |