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 @ 47652
History | View | Annotate | Download (7.17 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 | 47646 | fdiaz | import org.apache.commons.io.IOUtils; |
15 | 47652 | fdiaz | import org.apache.commons.lang3.StringUtils; |
16 | 47646 | fdiaz | import org.cresques.cts.IProjection; |
17 | import org.gvsig.fmap.crs.CRSFactory; |
||
18 | 47652 | fdiaz | import org.gvsig.fmap.dal.store.gml.GMLStoreParameters; |
19 | 47638 | jjdelcerro | import org.gvsig.fmap.dal.store.gml.virtualrows.GfsFile; |
20 | import org.gvsig.fmap.dal.store.gml.virtualrows.XMLFileAsList; |
||
21 | import org.gvsig.fmap.dal.store.simplereader.SimpleReaderStoreParameters; |
||
22 | import org.gvsig.fmap.dal.store.simplereader.simplereaders.AbstractSimpleReader; |
||
23 | 47652 | fdiaz | import org.gvsig.tools.ToolsLocator; |
24 | import org.gvsig.tools.dataTypes.DataType; |
||
25 | import org.gvsig.tools.dataTypes.DataTypesManager; |
||
26 | import org.gvsig.tools.namestranslator.NamesTranslator; |
||
27 | 47643 | jjdelcerro | import org.gvsig.tools.task.SimpleTaskStatus; |
28 | import org.gvsig.tools.util.GetItemWithSize64; |
||
29 | 45563 | jolivas | import org.slf4j.Logger; |
30 | import org.slf4j.LoggerFactory; |
||
31 | |||
32 | /**
|
||
33 | *
|
||
34 | 47634 | fdiaz | * @author gvSIG Team
|
35 | 45563 | jolivas | */
|
36 | 47634 | fdiaz | public class GMLReader extends AbstractSimpleReader { |
37 | 45567 | jolivas | |
38 | 47634 | fdiaz | private static final Logger LOGGER = LoggerFactory.getLogger(GMLReader.class); |
39 | 45567 | jolivas | |
40 | 47634 | fdiaz | private XMLFileAsList gml;
|
41 | 45563 | jolivas | private int columns; |
42 | private String[] header; |
||
43 | private int currentElement = 0; |
||
44 | 47652 | fdiaz | private final GMLStoreParameters parameters; |
45 | 47634 | fdiaz | private GfsFile gfs;
|
46 | 45563 | jolivas | |
47 | 47652 | fdiaz | public GMLReader(Reader reader, GMLStoreParameters theParameters) throws IOException { |
48 | 47638 | jjdelcerro | File gmlFile = theParameters.getFile();
|
49 | 47643 | jjdelcerro | File gmlIdx = new File(FilenameUtils.removeExtension(gmlFile.getAbsolutePath())+".gmlidx"); |
50 | File gfsFile = new File(FilenameUtils.removeExtension(gmlFile.getAbsolutePath())+".gfs"); |
||
51 | 47634 | fdiaz | gfs = new GfsFile();
|
52 | 47643 | jjdelcerro | if(gfsFile.exists()){
|
53 | 47634 | fdiaz | //TODO: Comparar fechas del gfs y gml
|
54 | gfs.load(gfsFile); |
||
55 | } else {
|
||
56 | gfs.fetch(gmlFile); |
||
57 | gfs.save(gfsFile); |
||
58 | } |
||
59 | |||
60 | 47646 | fdiaz | this.gml = null; |
61 | 45567 | jolivas | this.columns = -1; |
62 | this.header = null; |
||
63 | 47638 | jjdelcerro | this.parameters = theParameters;
|
64 | 45567 | jolivas | } |
65 | 47646 | fdiaz | |
66 | private XMLFileAsList getGml(){
|
||
67 | if(this.gml == null){ |
||
68 | this.gml = (XMLFileAsList) this.getVirtualRows(SimpleTaskStatus.FAKE_STATUS); |
||
69 | } |
||
70 | return this.gml; |
||
71 | } |
||
72 | 45563 | jolivas | |
73 | @Override
|
||
74 | public String[] getHeader() throws IOException { |
||
75 | 45567 | jolivas | if (this.header == null) { |
76 | 47646 | fdiaz | List<String> geoms = gfs.getGeometryElementPaths(); |
77 | String[] theHeader = new String[gfs.size()+(geoms==null?0:geoms.size())]; |
||
78 | 45563 | jolivas | int i = 0; |
79 | 47652 | fdiaz | DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager(); |
80 | 47634 | fdiaz | for (GfsFile.PropertyDefn item : gfs) {
|
81 | 47652 | fdiaz | DataType dataType = dataTypesManager.get(item.getType()); |
82 | theHeader[i++] = item.getName()+"/"+dataType.getName();
|
||
83 | 45563 | jolivas | } |
84 | 47646 | fdiaz | if(geoms != null) { |
85 | List<String> srss = gfs.getGeometryElementSrss(); |
||
86 | 47652 | fdiaz | if(this.parameters.getGeometryCombineMode() == XMLFileAsList.COMBINE_FIRST_NOT_NULL){ |
87 | String srs = srss.get(0); |
||
88 | for (int j = 0; j < srss.size(); j++) { |
||
89 | if(!StringUtils.equalsIgnoreCase(srs, srss.get(j))){
|
||
90 | srs = null;
|
||
91 | break;
|
||
92 | } |
||
93 | } |
||
94 | IProjection proj = null;
|
||
95 | if(srs != null){ |
||
96 | proj = CRSFactory.getCRS(srs); |
||
97 | } |
||
98 | if(proj == null){ |
||
99 | theHeader[i++] = FilenameUtils.getBaseName(getGeometryAttributeName())+"/Geometry";
|
||
100 | 47646 | fdiaz | } else {
|
101 | 47652 | fdiaz | theHeader[i++] = FilenameUtils.getBaseName(getGeometryAttributeName())+"/Geometry/set/srs="+proj.getAbrev().replace(":", "@"); |
102 | 47646 | fdiaz | } |
103 | 47652 | fdiaz | } else {
|
104 | for (int j = 0; j < srss.size(); j++) { |
||
105 | IProjection srs = CRSFactory.getCRS(srss.get(j)); |
||
106 | if(srs == null){ |
||
107 | theHeader[i++] = FilenameUtils.getBaseName(geoms.get(j))+"/Geometry";
|
||
108 | } else {
|
||
109 | theHeader[i++] = FilenameUtils.getBaseName(geoms.get(j))+"/Geometry/set/srs="+srs.getAbrev().replace(":", "@"); |
||
110 | } |
||
111 | } |
||
112 | 47646 | fdiaz | } |
113 | } |
||
114 | 45563 | jolivas | this.header = theHeader;
|
115 | } |
||
116 | return this.header; |
||
117 | } |
||
118 | 47652 | fdiaz | |
119 | private String getGeometryAttributeName() { |
||
120 | NamesTranslator translator = NamesTranslator.createBaseTranslator(); |
||
121 | for (GfsFile.PropertyDefn item : gfs) {
|
||
122 | translator.addSource(item.getName()); |
||
123 | } |
||
124 | String s = translator.getSuggestion("Geometry"); |
||
125 | return s;
|
||
126 | } |
||
127 | 45563 | jolivas | |
128 | @Override
|
||
129 | public int getColumnsCount() throws IOException { |
||
130 | 45567 | jolivas | if (this.columns <= 0) { |
131 | this.columns = this.getHeader().length; |
||
132 | 45563 | jolivas | } |
133 | return this.columns; |
||
134 | } |
||
135 | |||
136 | @Override
|
||
137 | public List<String> read() throws IOException { |
||
138 | List<String> values = this.read(currentElement); |
||
139 | 45567 | jolivas | if (values == null) { |
140 | 45563 | jolivas | return null; |
141 | } |
||
142 | 45567 | jolivas | currentElement += 1;
|
143 | 45563 | jolivas | return values;
|
144 | } |
||
145 | |||
146 | public List<String> read(int rowNumber) throws IOException { |
||
147 | 47646 | fdiaz | if (rowNumber < this.getGml().size64()) { |
148 | List<String> values = this.getGml().get64(rowNumber); |
||
149 | 45563 | jolivas | return values;
|
150 | } |
||
151 | return null; |
||
152 | } |
||
153 | 45567 | jolivas | |
154 | 45563 | jolivas | @Override
|
155 | public void close() throws IOException { |
||
156 | 47646 | fdiaz | IOUtils.closeQuietly(this.gml);
|
157 | 47634 | fdiaz | this.gml = null; |
158 | this.gfs = null; |
||
159 | 45563 | jolivas | } |
160 | |||
161 | @Override
|
||
162 | public List<String> skip(int lines) throws IOException { |
||
163 | this.currentElement += lines;
|
||
164 | 47646 | fdiaz | if (this.currentElement > this.getGml().size64()) { |
165 | 45563 | jolivas | return null; |
166 | } |
||
167 | return read(this.currentElement); |
||
168 | } |
||
169 | |||
170 | @Override
|
||
171 | public int getLine() { |
||
172 | 47646 | fdiaz | if (this.getGml() == null) { |
173 | 45563 | jolivas | return 0; |
174 | 45567 | jolivas | } |
175 | 45563 | jolivas | return this.currentElement; |
176 | } |
||
177 | |||
178 | @Override
|
||
179 | public List<String> nextRowValues() { |
||
180 | try {
|
||
181 | return this.read(); |
||
182 | } catch (IOException ex) { |
||
183 | throw new RuntimeException(ex); |
||
184 | } |
||
185 | 45567 | jolivas | } |
186 | 47643 | jjdelcerro | |
187 | @Override
|
||
188 | 47646 | fdiaz | public GetItemWithSize64<List<String>> getVirtualRows(SimpleTaskStatus status) { |
189 | try {
|
||
190 | File gmlFile = this.parameters.getFile(); |
||
191 | File gmlIdx = new File(FilenameUtils.removeExtension(gmlFile.getAbsolutePath()) + ".gmlidx"); |
||
192 | |||
193 | XMLFileAsList x = new XMLFileAsList(
|
||
194 | gmlFile, |
||
195 | gmlIdx, |
||
196 | Charset.forName(SimpleReaderStoreParameters.getCharset(this.parameters)), |
||
197 | gfs.getBaseElementPath(), |
||
198 | gfs.getGeometryElementPaths(), |
||
199 | gfs.getPropertiesPaths() |
||
200 | ); |
||
201 | 47652 | fdiaz | |
202 | x.setCombineMode(this.parameters.getGeometryCombineMode());
|
||
203 | 47646 | fdiaz | |
204 | return x;
|
||
205 | } catch (IOException ex) { |
||
206 | throw new RuntimeException("Can't get virtual rows", ex); |
||
207 | } |
||
208 | 47643 | jjdelcerro | } |
209 | 45563 | jolivas | } |