svn-gvsig-desktop / tags / v2_0_0_Build_2058 / extensions / extGPE-gvSIG / src / org / gvsig / fmap / dal / store / gpe / GPEStoreProvider.java @ 39232
History | View | Annotate | Download (11.8 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2009 {Iver T.I.} {Task}
|
26 |
*/
|
27 |
|
28 |
package org.gvsig.fmap.dal.store.gpe; |
29 |
|
30 |
import java.io.File; |
31 |
import java.util.Map; |
32 |
|
33 |
import org.cresques.cts.IProjection; |
34 |
import org.slf4j.Logger; |
35 |
import org.slf4j.LoggerFactory; |
36 |
|
37 |
import org.gvsig.fmap.dal.DALLocator; |
38 |
import org.gvsig.fmap.dal.DataManager; |
39 |
import org.gvsig.fmap.dal.DataServerExplorer; |
40 |
import org.gvsig.fmap.dal.DataStore; |
41 |
import org.gvsig.fmap.dal.DataStoreParameters; |
42 |
import org.gvsig.fmap.dal.DataTypes; |
43 |
import org.gvsig.fmap.dal.FileHelper; |
44 |
import org.gvsig.fmap.dal.exception.DataException; |
45 |
import org.gvsig.fmap.dal.exception.InitializeException; |
46 |
import org.gvsig.fmap.dal.exception.OpenException; |
47 |
import org.gvsig.fmap.dal.exception.ReadException; |
48 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
49 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
50 |
import org.gvsig.fmap.dal.feature.FeatureType; |
51 |
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProvider; |
52 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
53 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
54 |
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider; |
55 |
import org.gvsig.fmap.dal.resource.exception.ResourceExecuteException; |
56 |
import org.gvsig.fmap.dal.resource.file.FileResource; |
57 |
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer; |
58 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
59 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
60 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
61 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
62 |
import org.gvsig.fmap.dal.store.gpe.handlers.FmapContentHandler; |
63 |
import org.gvsig.fmap.dal.store.gpe.handlers.FmapErrorHandler; |
64 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
65 |
import org.gvsig.fmap.geom.primitive.Envelope; |
66 |
import org.gvsig.gpe.lib.api.GPELocator; |
67 |
import org.gvsig.gpe.lib.api.GPEManager; |
68 |
import org.gvsig.tools.dynobject.DynObject; |
69 |
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException; |
70 |
import org.gvsig.xmlschema.lib.api.XMLSchemaLocator; |
71 |
import org.gvsig.xmlschema.lib.api.XMLSchemaManager; |
72 |
|
73 |
/**
|
74 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
|
75 |
*/
|
76 |
public class GPEStoreProvider extends AbstractFeatureStoreProvider implements |
77 |
ResourceConsumer { |
78 |
public static final String NAME = "GPE"; |
79 |
public static final String DESCRIPTION = "GPE file"; |
80 |
public static final String METADATA_DEFINITION_NAME = NAME; |
81 |
|
82 |
private static Logger logger = LoggerFactory.getLogger(GPEStoreProvider.class); |
83 |
|
84 |
protected File m_Fich; |
85 |
protected boolean isOpen = false; |
86 |
//private GPEStoreParameters gpeParams;
|
87 |
private ResourceProvider gpeResource;
|
88 |
|
89 |
//Managers to manage the parsing process
|
90 |
protected GPEManager gpeManager = null; |
91 |
protected XMLSchemaManager xmlSchemaManager = null; |
92 |
|
93 |
//The content handler and the error handler
|
94 |
protected FmapContentHandler contentHandler;
|
95 |
protected FmapErrorHandler errorHandler;
|
96 |
|
97 |
public GPEStoreProvider(DataStoreParameters params,
|
98 |
DataStoreProviderServices storeServices) |
99 |
throws InitializeException {
|
100 |
this(params, storeServices, FileHelper
|
101 |
.newMetadataContainer(METADATA_DEFINITION_NAME)); |
102 |
} |
103 |
|
104 |
protected GPEStoreProvider(DataStoreParameters params,
|
105 |
DataStoreProviderServices storeServices, DynObject metadata) |
106 |
throws InitializeException {
|
107 |
|
108 |
super(params, storeServices, metadata);
|
109 |
|
110 |
this.setDynValue("Envelope", params.getDynValue("Envelope")); |
111 |
|
112 |
gpeManager = GPELocator.getGPEManager(); |
113 |
xmlSchemaManager = XMLSchemaLocator.getXMLSchemaManager(); |
114 |
|
115 |
retrieveFile(); |
116 |
gpeResource = this.createResource(FileResource.NAME,
|
117 |
new Object[] { m_Fich.getAbsolutePath() }); |
118 |
gpeResource.addConsumer(this);
|
119 |
this.initFeatureType();
|
120 |
} |
121 |
|
122 |
protected void retrieveFile() throws InitializeException{ |
123 |
m_Fich = getGPEParameters().getFile(); |
124 |
} |
125 |
|
126 |
protected void initFeatureType() throws InitializeException { |
127 |
try {
|
128 |
|
129 |
this.open();
|
130 |
} catch (DataException e) {
|
131 |
throw new InitializeException(this.getProviderName(), e); |
132 |
} |
133 |
} |
134 |
|
135 |
private GPEStoreParameters getGPEParameters() {
|
136 |
return (GPEStoreParameters) this.getParameters(); |
137 |
} |
138 |
|
139 |
public Object getDynValue(String name) throws DynFieldNotFoundException { |
140 |
if (DataStore.METADATA_CRS.equalsIgnoreCase(name)) {
|
141 |
IProjection resp = this.getSRS();
|
142 |
if (resp != null) { |
143 |
return resp;
|
144 |
} |
145 |
} |
146 |
return super.getDynValue(name); |
147 |
} |
148 |
|
149 |
/* (non-Javadoc)
|
150 |
* @see org.gvsig.fmap.dal.resource.spi.ResourceConsumer#closeResourceRequested(org.gvsig.fmap.dal.resource.spi.ResourceProvider)
|
151 |
*/
|
152 |
public boolean closeResourceRequested(ResourceProvider resource) { |
153 |
// TODO Auto-generated method stub
|
154 |
return false; |
155 |
} |
156 |
|
157 |
/* (non-Javadoc)
|
158 |
* @see org.gvsig.fmap.dal.resource.spi.ResourceConsumer#resourceChanged(org.gvsig.fmap.dal.resource.spi.ResourceProvider)
|
159 |
*/
|
160 |
public void resourceChanged(ResourceProvider resource) { |
161 |
// TODO Auto-generated method stub
|
162 |
|
163 |
} |
164 |
|
165 |
/* (non-Javadoc)
|
166 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#canWriteGeometry(int)
|
167 |
*/
|
168 |
public boolean canWriteGeometry(int geometryType, int geometrySubtype) |
169 |
throws DataException {
|
170 |
// TODO Auto-generated method stub
|
171 |
return false; |
172 |
} |
173 |
|
174 |
/* (non-Javadoc)
|
175 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#createNewOID()
|
176 |
*/
|
177 |
public Object createNewOID() { |
178 |
return contentHandler.createNewOID();
|
179 |
} |
180 |
|
181 |
/* (non-Javadoc)
|
182 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#createSet(org.gvsig.fmap.dal.feature.FeatureQuery)
|
183 |
*/
|
184 |
public FeatureSetProvider createSet(FeatureQuery query)
|
185 |
throws DataException {
|
186 |
return new GPESetProvider(this, query); |
187 |
} |
188 |
|
189 |
protected FeatureProvider internalGetFeatureProviderByReference(
|
190 |
FeatureReferenceProviderServices reference) throws DataException {
|
191 |
return internalGetFeatureProviderByReference(reference,
|
192 |
this
|
193 |
.getFeatureStore() |
194 |
.getDefaultFeatureType()); |
195 |
} |
196 |
|
197 |
public FeatureProvider internalGetFeatureProviderByReference(
|
198 |
FeatureReferenceProviderServices reference, FeatureType featureType) |
199 |
throws DataException {
|
200 |
Map featureSet = contentHandler.getFeatureSet();
|
201 |
if (featureSet.containsKey(reference.getOID())){
|
202 |
return (FeatureProvider)featureSet.get(reference.getOID());
|
203 |
} |
204 |
return null; |
205 |
} |
206 |
|
207 |
FeatureProvider getFeatureProviderByIndex(Long index, FeatureType fType)
|
208 |
throws DataException {
|
209 |
Map featureSet = contentHandler.getFeatureSet();
|
210 |
if (featureSet.containsKey(index)){
|
211 |
return (FeatureProvider)featureSet.get(index);
|
212 |
} |
213 |
return null; |
214 |
} |
215 |
|
216 |
public void resourcesBegin() throws ResourceExecuteException { |
217 |
// Nothing to do
|
218 |
} |
219 |
|
220 |
public void resourcesEnd() { |
221 |
// Nothing to do
|
222 |
} |
223 |
|
224 |
/* (non-Javadoc)
|
225 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#getFeatureReferenceOIDType()
|
226 |
*/
|
227 |
public int getOIDType() { |
228 |
return DataTypes.STRING;
|
229 |
} |
230 |
|
231 |
/* (non-Javadoc)
|
232 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#getName()
|
233 |
*/
|
234 |
public String getProviderName() { |
235 |
return NAME;
|
236 |
} |
237 |
|
238 |
/* (non-Javadoc)
|
239 |
* @see org.gvsig.fmap.dal.spi.DataStoreProvider#getSourceId()
|
240 |
*/
|
241 |
public Object getSourceId() { |
242 |
return getGPEParameters().getFile();
|
243 |
} |
244 |
|
245 |
public String getName() { |
246 |
String name = this.getGPEParameters().getFile().getName(); |
247 |
int n = name.lastIndexOf("."); |
248 |
if( n<1 ) { |
249 |
return name;
|
250 |
} |
251 |
return name.substring(0, n); |
252 |
} |
253 |
|
254 |
public String getFullName() { |
255 |
return this.getGPEParameters().getFile().getAbsolutePath(); |
256 |
} |
257 |
|
258 |
/* (non-Javadoc)
|
259 |
* @see org.gvsig.fmap.dal.spi.DataStoreProvider#open()
|
260 |
*/
|
261 |
public void open() throws OpenException { |
262 |
if (isOpen) {
|
263 |
return;
|
264 |
} |
265 |
try {
|
266 |
errorHandler = new FmapErrorHandler();
|
267 |
contentHandler = new FmapContentHandler(errorHandler,
|
268 |
getStoreServices(), this, m_Fich,
|
269 |
getSRS(), |
270 |
getGeometryType()); |
271 |
if (getStoreServices().getDefaultFeatureType() != null){ |
272 |
contentHandler.setFeatureType(getStoreServices().getDefaultFeatureType().getEditable()); |
273 |
} |
274 |
contentHandler.setEnvelope((Envelope)getDynValue("Envelope"));
|
275 |
isOpen = true;
|
276 |
gpeManager.parse(contentHandler, errorHandler, m_Fich.toURI()); |
277 |
} catch (Exception e) { |
278 |
throw new OpenException("Imposible create a parser",e); |
279 |
} |
280 |
} |
281 |
|
282 |
protected IProjection getSRS(){
|
283 |
return getGPEParameters().getCRS();
|
284 |
} |
285 |
|
286 |
protected int getGeometryType() { |
287 |
return TYPES.GEOMETRY;
|
288 |
} |
289 |
|
290 |
/* (non-Javadoc)
|
291 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#createSet(org.gvsig.fmap.dal.feature.FeatureQuery, org.gvsig.fmap.dal.feature.FeatureType)
|
292 |
*/
|
293 |
public FeatureSetProvider createSet(FeatureQuery query,
|
294 |
FeatureType featureType) throws DataException {
|
295 |
return new GPESetProvider(this, query, featureType); |
296 |
} |
297 |
|
298 |
public long getFeatureCount() throws DataException { |
299 |
return contentHandler.getFeaturesCount();
|
300 |
} |
301 |
|
302 |
/* (non-Javadoc)
|
303 |
* @see org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProvider#getEnvelope()
|
304 |
*/
|
305 |
public Envelope getEnvelope() throws DataException { |
306 |
this.open();
|
307 |
return (Envelope) this.getDynValue("Envelope"); |
308 |
} |
309 |
|
310 |
public void setEnvelope(Envelope envelope) { |
311 |
this.setDynValue("Envelope", envelope); |
312 |
} |
313 |
|
314 |
public ResourceProvider getResource() {
|
315 |
return gpeResource;
|
316 |
} |
317 |
|
318 |
public DataServerExplorer getExplorer() throws ReadException, |
319 |
ValidateDataParametersException { |
320 |
DataManager manager = DALLocator.getDataManager(); |
321 |
FilesystemServerExplorerParameters params; |
322 |
try {
|
323 |
params = (FilesystemServerExplorerParameters) manager |
324 |
.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
325 |
params.setRoot(this.getGPEParameters().getFile().getParent());
|
326 |
return manager.createServerExplorer(params);
|
327 |
} catch (DataException e) {
|
328 |
throw new ReadException(this.getProviderName(), e); |
329 |
} catch (ValidateDataParametersException e) {
|
330 |
// TODO Auto-generated catch block
|
331 |
throw new ReadException(this.getProviderName(), e); |
332 |
} |
333 |
} |
334 |
|
335 |
/**
|
336 |
* @return the contentHandler
|
337 |
*/
|
338 |
public FmapContentHandler getContentHandler() {
|
339 |
return contentHandler;
|
340 |
} |
341 |
|
342 |
/**
|
343 |
* @return the errorHandler
|
344 |
*/
|
345 |
public FmapErrorHandler getErrorHandler() {
|
346 |
return errorHandler;
|
347 |
} |
348 |
|
349 |
} |