svn-gvsig-desktop / tags / v1_0_2_Build_896 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / VectorialFileAdapter.java @ 10391
History | View | Annotate | Download (7 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 | getFileDriver().open(file); |
||
109 | 3959 | caballero | |
110 | 1327 | fjp | if (!driverInitialized) {
|
111 | getFileDriver().initialize(); |
||
112 | driverInitialized = true;
|
||
113 | } |
||
114 | } |
||
115 | reference_count++; |
||
116 | 229 | vcaballero | |
117 | 504 | fernando | } catch (IOException e) { |
118 | throw new DriverIOException(e); |
||
119 | } |
||
120 | } |
||
121 | 214 | fernando | |
122 | 504 | fernando | /**
|
123 | * decrementa el contador de n?mero de aperturas y cuando llega a cero pide
|
||
124 | * al driver que cierre el fichero
|
||
125 | *
|
||
126 | * @throws DriverIOException
|
||
127 | */
|
||
128 | 4580 | azabala | public synchronized void stop() throws DriverIOException { |
129 | 504 | fernando | try {
|
130 | 1327 | fjp | if (reference_count == 0) |
131 | { |
||
132 | getFileDriver().close(); |
||
133 | } |
||
134 | else
|
||
135 | if (reference_count < 0) |
||
136 | 3959 | caballero | throw new RuntimeException("Contador de referencias de driver =" |
137 | 1327 | fjp | + reference_count + ". Demasiados stop().");
|
138 | reference_count--; |
||
139 | 504 | fernando | } catch (IOException e) { |
140 | throw new DriverIOException(e); |
||
141 | } |
||
142 | } |
||
143 | 214 | fernando | |
144 | 504 | fernando | /**
|
145 | 4580 | azabala | * Is synchronized to allow thread safe access to features stored
|
146 | * in files.
|
||
147 | *
|
||
148 | 504 | fernando | * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int)
|
149 | */
|
||
150 | 4580 | azabala | public synchronized IGeometry getShape(int index) throws DriverIOException { |
151 | 504 | fernando | try {
|
152 | return getFileDriver().getShape(index);
|
||
153 | } catch (IOException e) { |
||
154 | throw new DriverIOException(e); |
||
155 | } |
||
156 | } |
||
157 | 214 | fernando | |
158 | 504 | fernando | /**
|
159 | * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeType()
|
||
160 | */
|
||
161 | public int getShapeType() throws DriverIOException { |
||
162 | return getFileDriver().getShapeType();
|
||
163 | } |
||
164 | 408 | fernando | |
165 | 504 | fernando | /**
|
166 | * @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#getRecordset()
|
||
167 | */
|
||
168 | 3959 | caballero | public SelectableDataSource getRecordset() throws DriverLoadException { |
169 | 508 | fernando | try {
|
170 | 1034 | vcaballero | if (ds == null) { |
171 | VectorialFileDriver driver = (VectorialFileDriver) getDriver(); |
||
172 | 442 | vcaballero | |
173 | 1034 | vcaballero | if (driver instanceof ExternalData) { |
174 | ExternalData ed = (ExternalData) driver; |
||
175 | File dataFile = ed.getDataFile(file);
|
||
176 | String driverName = ed.getDataDriverName();
|
||
177 | 442 | vcaballero | |
178 | 1828 | fernando | String name = LayerFactory.getDataSourceFactory().addFileDataSource(driverName,
|
179 | 2217 | fernando | dataFile.getAbsolutePath()); |
180 | 9887 | fjp | ds = new SelectableDataSource(LayerFactory.getDataSourceFactory().createRandomDataSource(name, DataSourceFactory.MANUAL_OPENING));
|
181 | 1828 | fernando | } else if (driver instanceof ObjectDriver) { |
182 | 2217 | fernando | String name = LayerFactory.getDataSourceFactory().addDataSource((ObjectDriver)driver);
|
183 | 9887 | fjp | ds = new SelectableDataSource(LayerFactory.getDataSourceFactory().createRandomDataSource(name, DataSourceFactory.MANUAL_OPENING));
|
184 | 1034 | vcaballero | } else {
|
185 | return null; |
||
186 | } |
||
187 | 463 | fernando | } |
188 | 504 | fernando | } catch (NoSuchTableException e) {
|
189 | throw new RuntimeException( |
||
190 | "Error de implementaci?n, se ha a?adido una tabla y luego esa tabla no ha podido ser cargada");
|
||
191 | 1836 | fernando | } catch (DriverException e) {
|
192 | throw new RuntimeException(e); |
||
193 | 504 | fernando | } |
194 | |||
195 | 508 | fernando | return ds;
|
196 | 504 | fernando | } |
197 | 442 | vcaballero | |
198 | 504 | fernando | /**
|
199 | 1034 | vcaballero | * Devuelve el fichero.
|
200 | 504 | fernando | *
|
201 | 1034 | vcaballero | * @return Fichero.
|
202 | 504 | fernando | */
|
203 | public File getFile() { |
||
204 | return file;
|
||
205 | } |
||
206 | 4580 | azabala | /**
|
207 | * Returns the feature whose index is numReg
|
||
208 | * <br>
|
||
209 | * Is synchronized to do thread safe accessing to features
|
||
210 | * stored in files.
|
||
211 | * @param numReg index of feature
|
||
212 | * @return feature
|
||
213 | *
|
||
214 | */
|
||
215 | public synchronized IFeature getFeature(int numReg) throws com.iver.cit.gvsig.fmap.DriverException |
||
216 | 3248 | fjp | { |
217 | IGeometry geom; |
||
218 | IFeature feat = null;
|
||
219 | try {
|
||
220 | geom = getShape(numReg); |
||
221 | DataSource rs = getRecordset();
|
||
222 | Value[] regAtt = new Value[rs.getFieldCount()]; |
||
223 | for (int fieldId=0; fieldId < rs.getFieldCount(); fieldId++ ) |
||
224 | 3959 | caballero | { |
225 | 3248 | fjp | regAtt[fieldId] = rs.getFieldValue(numReg, fieldId); |
226 | } |
||
227 | 3959 | caballero | |
228 | 3248 | fjp | feat = new DefaultFeature(geom, regAtt, "" + numReg); |
229 | } catch (DriverIOException e) {
|
||
230 | throw new com.iver.cit.gvsig.fmap.DriverException(e); |
||
231 | } catch (DriverLoadException e) {
|
||
232 | throw new com.iver.cit.gvsig.fmap.DriverException(e); |
||
233 | } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
||
234 | throw new com.iver.cit.gvsig.fmap.DriverException(e); |
||
235 | } |
||
236 | return feat;
|
||
237 | } |
||
238 | 3959 | caballero | |
239 | 214 | fernando | } |