root / tags / v1_0_2_Build_915 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / VectorialFileAdapter.java @ 12217
History | View | Annotate | Download (7.94 KB)
1 | 1100 | fjp | /* 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 | 214 | fernando | package com.iver.cit.gvsig.fmap.layers; |
42 | |||
43 | 1828 | fernando | import java.io.File; |
44 | import java.io.IOException; |
||
45 | 442 | vcaballero | |
46 | 1828 | fernando | import com.hardcode.driverManager.DriverLoadException; |
47 | 408 | fernando | import com.hardcode.gdbms.engine.data.DataSource; |
48 | import com.hardcode.gdbms.engine.data.DataSourceFactory; |
||
49 | import com.hardcode.gdbms.engine.data.NoSuchTableException; |
||
50 | 1836 | fernando | import com.hardcode.gdbms.engine.data.driver.DriverException; |
51 | 1828 | fernando | import com.hardcode.gdbms.engine.data.driver.ObjectDriver; |
52 | 3248 | fjp | import com.hardcode.gdbms.engine.values.Value; |
53 | import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
||
54 | import com.iver.cit.gvsig.fmap.core.IFeature; |
||
55 | 305 | fjp | import com.iver.cit.gvsig.fmap.core.IGeometry; |
56 | 214 | fernando | import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
57 | 504 | fernando | import com.iver.cit.gvsig.fmap.drivers.ExternalData; |
58 | 214 | fernando | import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver; |
59 | 229 | vcaballero | |
60 | |||
61 | 214 | fernando | /**
|
62 | 229 | vcaballero | * Adapta un driver de fichero vectorial a la interfaz vectorial, manteniendo
|
63 | * adem?s el estado necesario por una capa vectorial de fichero (el nombre del
|
||
64 | * fichero)
|
||
65 | 214 | fernando | */
|
66 | 3601 | fjp | public class VectorialFileAdapter extends VectorialAdapter { |
67 | 504 | fernando | private boolean driverInitialized = false; |
68 | private File file; |
||
69 | 3959 | caballero | private SelectableDataSource ds;
|
70 | 546 | fernando | private String dataSourceName; |
71 | 3959 | caballero | |
72 | 1327 | fjp | /**
|
73 | * <code>reference_count</code> lleva un contador de referencias a este
|
||
74 | 3959 | caballero | * adaptador, de forma que si la cuenta de referencias no es cero, no
|
75 | 1327 | fjp | * abrimos otra vez el adaptador porque se supone que est? abierto.
|
76 | */
|
||
77 | private int reference_count = 0; |
||
78 | 229 | vcaballero | |
79 | 504 | fernando | /**
|
80 | * Crea un nuevo VectorialFileAdapter.
|
||
81 | *
|
||
82 | 1034 | vcaballero | * @param file Fichero.
|
83 | 504 | fernando | */
|
84 | public VectorialFileAdapter(File file) { |
||
85 | this.file = file;
|
||
86 | } |
||
87 | 214 | fernando | |
88 | 504 | fernando | /**
|
89 | 1034 | vcaballero | * Devuelve driver.
|
90 | 504 | fernando | *
|
91 | 1034 | vcaballero | * @return VectorialFileDriver.
|
92 | 504 | fernando | */
|
93 | 3936 | fjp | VectorialFileDriver getFileDriver() { |
94 | 504 | fernando | return (VectorialFileDriver) getDriver();
|
95 | } |
||
96 | 229 | vcaballero | |
97 | 504 | fernando | /**
|
98 | * incrementa el contador de las veces que se ha abierto el fichero.
|
||
99 | * Solamente cuando el contador est? a cero pide al driver que abra el
|
||
100 | * fichero
|
||
101 | *
|
||
102 | * @throws DriverIOException
|
||
103 | */
|
||
104 | 4580 | azabala | public synchronized void start() throws DriverIOException { |
105 | 504 | fernando | try {
|
106 | 1327 | fjp | if (reference_count == 0) |
107 | { |
||
108 | 11006 | jmvivo | //System.out.println("====" + hashCode() + " Abrir el fichero: ["+ reference_count + "]"+ file.getAbsolutePath() );
|
109 | getFileDriver().open(file); |
||
110 | 3959 | caballero | |
111 | 1327 | fjp | if (!driverInitialized) {
|
112 | getFileDriver().initialize(); |
||
113 | driverInitialized = true;
|
||
114 | } |
||
115 | 11006 | jmvivo | getRecordset().start(); |
116 | 1327 | fjp | } |
117 | reference_count++; |
||
118 | 229 | vcaballero | |
119 | 504 | fernando | } catch (IOException e) { |
120 | throw new DriverIOException(e); |
||
121 | 11006 | jmvivo | } catch (DriverException e) {
|
122 | // TODO Auto-generated catch block
|
||
123 | throw new DriverIOException(e); |
||
124 | 504 | fernando | } |
125 | } |
||
126 | 214 | fernando | |
127 | 504 | fernando | /**
|
128 | * decrementa el contador de n?mero de aperturas y cuando llega a cero pide
|
||
129 | * al driver que cierre el fichero
|
||
130 | *
|
||
131 | * @throws DriverIOException
|
||
132 | */
|
||
133 | 4580 | azabala | public synchronized void stop() throws DriverIOException { |
134 | 504 | fernando | try {
|
135 | 11006 | jmvivo | if (reference_count == 1) |
136 | 1327 | fjp | { |
137 | 11006 | jmvivo | //System.out.println("====" + hashCode() + " Cerrar el fichero: ["+ reference_count + "]"+ file.getAbsolutePath() );
|
138 | 1327 | fjp | getFileDriver().close(); |
139 | 11006 | jmvivo | getRecordset().stop(); |
140 | 1327 | fjp | } |
141 | else
|
||
142 | if (reference_count < 0) |
||
143 | 3959 | caballero | throw new RuntimeException("Contador de referencias de driver =" |
144 | 1327 | fjp | + reference_count + ". Demasiados stop().");
|
145 | reference_count--; |
||
146 | 504 | fernando | } catch (IOException e) { |
147 | throw new DriverIOException(e); |
||
148 | 11006 | jmvivo | } catch (DriverLoadException e) {
|
149 | // TODO Auto-generated catch block
|
||
150 | e.printStackTrace(); |
||
151 | } catch (DriverException e) {
|
||
152 | // TODO Auto-generated catch block
|
||
153 | e.printStackTrace(); |
||
154 | 504 | fernando | } |
155 | } |
||
156 | 214 | fernando | |
157 | 504 | fernando | /**
|
158 | 4580 | azabala | * Is synchronized to allow thread safe access to features stored
|
159 | * in files.
|
||
160 | *
|
||
161 | 504 | fernando | * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int)
|
162 | */
|
||
163 | 4580 | azabala | public synchronized IGeometry getShape(int index) throws DriverIOException { |
164 | 504 | fernando | try {
|
165 | return getFileDriver().getShape(index);
|
||
166 | } catch (IOException e) { |
||
167 | throw new DriverIOException(e); |
||
168 | } |
||
169 | } |
||
170 | 214 | fernando | |
171 | 504 | fernando | /**
|
172 | * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeType()
|
||
173 | */
|
||
174 | public int getShapeType() throws DriverIOException { |
||
175 | return getFileDriver().getShapeType();
|
||
176 | } |
||
177 | 408 | fernando | |
178 | 504 | fernando | /**
|
179 | * @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#getRecordset()
|
||
180 | */
|
||
181 | 3959 | caballero | public SelectableDataSource getRecordset() throws DriverLoadException { |
182 | 508 | fernando | try {
|
183 | 1034 | vcaballero | if (ds == null) { |
184 | VectorialFileDriver driver = (VectorialFileDriver) getDriver(); |
||
185 | 442 | vcaballero | |
186 | 1034 | vcaballero | if (driver instanceof ExternalData) { |
187 | ExternalData ed = (ExternalData) driver; |
||
188 | File dataFile = ed.getDataFile(file);
|
||
189 | String driverName = ed.getDataDriverName();
|
||
190 | 442 | vcaballero | |
191 | 1828 | fernando | String name = LayerFactory.getDataSourceFactory().addFileDataSource(driverName,
|
192 | 2217 | fernando | dataFile.getAbsolutePath()); |
193 | 11008 | jmvivo | // CHEMA: AUTOMATIC DATA SOURCE
|
194 | //ds = new SelectableDataSource(LayerFactory.getDataSourceFactory().createRandomDataSource(name, DataSourceFactory.MANUAL_OPENING));
|
||
195 | ds = new SelectableDataSource(LayerFactory.getDataSourceFactory().createRandomDataSource(name, DataSourceFactory.AUTOMATIC_OPENING));
|
||
196 | 1828 | fernando | } else if (driver instanceof ObjectDriver) { |
197 | 2217 | fernando | String name = LayerFactory.getDataSourceFactory().addDataSource((ObjectDriver)driver);
|
198 | 11008 | jmvivo | // CHEMA: AUTOMATIC DATA SOURCE
|
199 | //ds = new SelectableDataSource(LayerFactory.getDataSourceFactory().createRandomDataSource(name, DataSourceFactory.MANUAL_OPENING));
|
||
200 | ds = new SelectableDataSource(LayerFactory.getDataSourceFactory().createRandomDataSource(name, DataSourceFactory.AUTOMATIC_OPENING));
|
||
201 | 1034 | vcaballero | } else {
|
202 | return null; |
||
203 | } |
||
204 | 463 | fernando | } |
205 | 504 | fernando | } catch (NoSuchTableException e) {
|
206 | throw new RuntimeException( |
||
207 | "Error de implementaci?n, se ha a?adido una tabla y luego esa tabla no ha podido ser cargada");
|
||
208 | 1836 | fernando | } catch (DriverException e) {
|
209 | throw new RuntimeException(e); |
||
210 | 504 | fernando | } |
211 | |||
212 | 508 | fernando | return ds;
|
213 | 504 | fernando | } |
214 | 442 | vcaballero | |
215 | 504 | fernando | /**
|
216 | 1034 | vcaballero | * Devuelve el fichero.
|
217 | 504 | fernando | *
|
218 | 1034 | vcaballero | * @return Fichero.
|
219 | 504 | fernando | */
|
220 | public File getFile() { |
||
221 | return file;
|
||
222 | } |
||
223 | 4580 | azabala | /**
|
224 | * Returns the feature whose index is numReg
|
||
225 | * <br>
|
||
226 | * Is synchronized to do thread safe accessing to features
|
||
227 | * stored in files.
|
||
228 | * @param numReg index of feature
|
||
229 | * @return feature
|
||
230 | *
|
||
231 | */
|
||
232 | public synchronized IFeature getFeature(int numReg) throws com.iver.cit.gvsig.fmap.DriverException |
||
233 | 3248 | fjp | { |
234 | IGeometry geom; |
||
235 | IFeature feat = null;
|
||
236 | try {
|
||
237 | geom = getShape(numReg); |
||
238 | DataSource rs = getRecordset();
|
||
239 | Value[] regAtt = new Value[rs.getFieldCount()]; |
||
240 | for (int fieldId=0; fieldId < rs.getFieldCount(); fieldId++ ) |
||
241 | 3959 | caballero | { |
242 | 3248 | fjp | regAtt[fieldId] = rs.getFieldValue(numReg, fieldId); |
243 | } |
||
244 | 3959 | caballero | |
245 | 3248 | fjp | feat = new DefaultFeature(geom, regAtt, "" + numReg); |
246 | } catch (DriverIOException e) {
|
||
247 | throw new com.iver.cit.gvsig.fmap.DriverException(e); |
||
248 | } catch (DriverLoadException e) {
|
||
249 | throw new com.iver.cit.gvsig.fmap.DriverException(e); |
||
250 | } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
||
251 | throw new com.iver.cit.gvsig.fmap.DriverException(e); |
||
252 | } |
||
253 | return feat;
|
||
254 | } |
||
255 | 3959 | caballero | |
256 | 214 | fernando | } |