svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / VectorialFileAdapter.java @ 3601
History | View | Annotate | Download (7.3 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package com.iver.cit.gvsig.fmap.layers; |
42 |
|
43 |
import java.awt.geom.Rectangle2D; |
44 |
import java.io.File; |
45 |
import java.io.IOException; |
46 |
|
47 |
import com.hardcode.driverManager.DriverLoadException; |
48 |
import com.hardcode.gdbms.engine.data.DataSource; |
49 |
import com.hardcode.gdbms.engine.data.DataSourceFactory; |
50 |
import com.hardcode.gdbms.engine.data.NoSuchTableException; |
51 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
52 |
import com.hardcode.gdbms.engine.data.driver.ObjectDriver; |
53 |
import com.hardcode.gdbms.engine.values.Value; |
54 |
import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
55 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
56 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
57 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
58 |
import com.iver.cit.gvsig.fmap.drivers.ExternalData; |
59 |
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver; |
60 |
import com.iver.cit.gvsig.fmap.write.FileWriterDriver; |
61 |
|
62 |
|
63 |
/**
|
64 |
* Adapta un driver de fichero vectorial a la interfaz vectorial, manteniendo
|
65 |
* adem?s el estado necesario por una capa vectorial de fichero (el nombre del
|
66 |
* fichero)
|
67 |
*/
|
68 |
public class VectorialFileAdapter extends VectorialAdapter { |
69 |
private boolean driverInitialized = false; |
70 |
private File file; |
71 |
private FileWriterDriver writeDriver;
|
72 |
private DataSource ds; |
73 |
private String dataSourceName; |
74 |
|
75 |
/**
|
76 |
* <code>reference_count</code> lleva un contador de referencias a este
|
77 |
* adaptador, de forma que si la cuenta de referencias no es cero, no
|
78 |
* abrimos otra vez el adaptador porque se supone que est? abierto.
|
79 |
*/
|
80 |
private int reference_count = 0; |
81 |
|
82 |
/**
|
83 |
* Crea un nuevo VectorialFileAdapter.
|
84 |
*
|
85 |
* @param file Fichero.
|
86 |
*/
|
87 |
public VectorialFileAdapter(File file) { |
88 |
this.file = file;
|
89 |
} |
90 |
|
91 |
/**
|
92 |
* Devuelve driver.
|
93 |
*
|
94 |
* @return VectorialFileDriver.
|
95 |
*/
|
96 |
private VectorialFileDriver getFileDriver() {
|
97 |
return (VectorialFileDriver) getDriver();
|
98 |
} |
99 |
|
100 |
/**
|
101 |
* incrementa el contador de las veces que se ha abierto el fichero.
|
102 |
* Solamente cuando el contador est? a cero pide al driver que abra el
|
103 |
* fichero
|
104 |
*
|
105 |
* @throws DriverIOException
|
106 |
*/
|
107 |
public void start() throws DriverIOException { |
108 |
try {
|
109 |
if (reference_count == 0) |
110 |
{ |
111 |
getFileDriver().open(file); |
112 |
|
113 |
if (!driverInitialized) {
|
114 |
getFileDriver().initialize(); |
115 |
driverInitialized = true;
|
116 |
} |
117 |
} |
118 |
reference_count++; |
119 |
|
120 |
} catch (IOException e) { |
121 |
throw new DriverIOException(e); |
122 |
} |
123 |
} |
124 |
|
125 |
/**
|
126 |
* decrementa el contador de n?mero de aperturas y cuando llega a cero pide
|
127 |
* al driver que cierre el fichero
|
128 |
*
|
129 |
* @throws DriverIOException
|
130 |
*/
|
131 |
public void stop() throws DriverIOException { |
132 |
try {
|
133 |
if (reference_count == 0) |
134 |
{ |
135 |
getFileDriver().close(); |
136 |
} |
137 |
else
|
138 |
if (reference_count < 0) |
139 |
throw new RuntimeException("Contador de referencias de driver =" |
140 |
+ reference_count + ". Demasiados stop().");
|
141 |
reference_count--; |
142 |
} catch (IOException e) { |
143 |
throw new DriverIOException(e); |
144 |
} |
145 |
} |
146 |
|
147 |
/**
|
148 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int)
|
149 |
*/
|
150 |
public IGeometry getShape(int index) throws DriverIOException { |
151 |
try {
|
152 |
return getFileDriver().getShape(index);
|
153 |
} catch (IOException e) { |
154 |
throw new DriverIOException(e); |
155 |
} |
156 |
} |
157 |
|
158 |
/**
|
159 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeCount()
|
160 |
*/
|
161 |
public int getShapeCount() throws DriverIOException { |
162 |
try {
|
163 |
return getFileDriver().getShapeCount();
|
164 |
} catch (IOException e) { |
165 |
throw new DriverIOException(e); |
166 |
} |
167 |
} |
168 |
|
169 |
/**
|
170 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getFullExtent()
|
171 |
*/
|
172 |
public Rectangle2D getFullExtent() throws DriverIOException { |
173 |
try {
|
174 |
return (Rectangle2D)getFileDriver().getFullExtent().clone(); |
175 |
} catch (IOException e) { |
176 |
throw new DriverIOException(e); |
177 |
} |
178 |
} |
179 |
|
180 |
|
181 |
/**
|
182 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeType()
|
183 |
*/
|
184 |
public int getShapeType() throws DriverIOException { |
185 |
return getFileDriver().getShapeType();
|
186 |
} |
187 |
|
188 |
/**
|
189 |
* @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#getRecordset()
|
190 |
*/
|
191 |
public DataSource getRecordset() throws DriverLoadException { |
192 |
try {
|
193 |
if (ds == null) { |
194 |
VectorialFileDriver driver = (VectorialFileDriver) getDriver(); |
195 |
|
196 |
if (driver instanceof ExternalData) { |
197 |
ExternalData ed = (ExternalData) driver; |
198 |
File dataFile = ed.getDataFile(file);
|
199 |
String driverName = ed.getDataDriverName();
|
200 |
|
201 |
String name = LayerFactory.getDataSourceFactory().addFileDataSource(driverName,
|
202 |
dataFile.getAbsolutePath()); |
203 |
ds = LayerFactory.getDataSourceFactory().createRandomDataSource(name, DataSourceFactory.AUTOMATIC_OPENING); |
204 |
} else if (driver instanceof ObjectDriver) { |
205 |
String name = LayerFactory.getDataSourceFactory().addDataSource((ObjectDriver)driver);
|
206 |
ds = LayerFactory.getDataSourceFactory().createRandomDataSource(name, DataSourceFactory.AUTOMATIC_OPENING); |
207 |
} else {
|
208 |
return null; |
209 |
} |
210 |
} |
211 |
} catch (NoSuchTableException e) {
|
212 |
throw new RuntimeException( |
213 |
"Error de implementaci?n, se ha a?adido una tabla y luego esa tabla no ha podido ser cargada");
|
214 |
} catch (DriverException e) {
|
215 |
throw new RuntimeException(e); |
216 |
} |
217 |
|
218 |
return ds;
|
219 |
} |
220 |
|
221 |
/**
|
222 |
* Devuelve el fichero.
|
223 |
*
|
224 |
* @return Fichero.
|
225 |
*/
|
226 |
public File getFile() { |
227 |
return file;
|
228 |
} |
229 |
|
230 |
public IFeature getFeature(int numReg) throws com.iver.cit.gvsig.fmap.DriverException |
231 |
{ |
232 |
IGeometry geom; |
233 |
IFeature feat = null;
|
234 |
try {
|
235 |
geom = getShape(numReg); |
236 |
DataSource rs = getRecordset();
|
237 |
Value[] regAtt = new Value[rs.getFieldCount()]; |
238 |
for (int fieldId=0; fieldId < rs.getFieldCount(); fieldId++ ) |
239 |
{ |
240 |
regAtt[fieldId] = rs.getFieldValue(numReg, fieldId); |
241 |
} |
242 |
|
243 |
feat = new DefaultFeature(geom, regAtt, "" + numReg); |
244 |
} catch (DriverIOException e) {
|
245 |
throw new com.iver.cit.gvsig.fmap.DriverException(e); |
246 |
} catch (DriverLoadException e) {
|
247 |
throw new com.iver.cit.gvsig.fmap.DriverException(e); |
248 |
} catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
249 |
throw new com.iver.cit.gvsig.fmap.DriverException(e); |
250 |
} |
251 |
return feat;
|
252 |
} |
253 |
|
254 |
} |