Statistics
| Revision:

root / branches / Mobile_Compatible_Hito_1 / libFMap / src-file / org / gvsig / data / datastores / vectorial / file / shp_jni / simplify / DbfDiskDataSource.java @ 21841

History | View | Annotate | Download (8.8 KB)

1
package org.gvsig.data.datastores.vectorial.file.shp_jni.simplify;
2

    
3
import java.io.File;
4
import java.io.IOException;
5
import java.sql.Types;
6
import java.util.Date;
7

    
8
import org.apache.log4j.Logger;
9
import org.gvsig.compatible.StringUtil;
10
import org.gvsig.data.datastores.vectorial.file.shp_util.DBFDataSource;
11
import org.gvsig.exceptions.DriverException;
12

    
13
import es.prodevelop.gvsig.mobile.fmap.driver.vect.shp.ShpReader;
14

    
15

    
16
/**
17
 * This class implements a JNI-based DataSource which is based on a DBF file not
18
 * loaded into memory.
19
 * 
20
 * @see es.prodevelop.gvsig.mobile.fmap.driver.vect.shp.ShpReader
21
 * @see java.sql.Types
22
 * 
23
 * @author jldominguez
24
 * 
25
 */
26
public class DbfDiskDataSource implements DBFDataSource {
27

    
28
        private static Logger logger = Logger.getLogger(DbfDiskDataSource.class);
29

    
30
        private File dbFile = null;
31

    
32
        // private DbaseFileNIO dbFileAccess = null;
33

    
34
        private int fieldCount = 0;
35

    
36
        private int[] fieldTypes = null;
37

    
38
        private int[] fieldWidths = null;
39

    
40
        private String[] fieldNames = null;
41

    
42
        private long fileHandler = -1;
43

    
44
        private int rowCount = 0;
45

    
46
        /**
47
         * Constructor
48
         * 
49
         * @param _dbFile
50
         *            the DBF file to read
51
         */
52
        public DbfDiskDataSource(File _dbFile) {
53
                dbFile = _dbFile;
54
        }
55

    
56
        /**
57
         * Initializes the dfata source (reads the file metadata)
58
         */
59
        public void start() throws DriverException {
60

    
61
                try {
62
                        fileHandler = ShpReader.openDbfFile(dbFile.getCanonicalPath(),
63
                                        false);
64
                        if (fileHandler != 0) {
65
                                fieldCount = ShpReader.getDbfFileFieldCount(fileHandler);
66
                                fieldTypes = dbfTypesToSqlTypes(ShpReader.getDbfFileFieldTypes(
67
                                                fileHandler, fieldCount));
68
                                fieldNames = commaSeparatedToStringArray(ShpReader
69
                                                .getDbfFileFieldNames(fileHandler, fieldCount));
70
                                fieldWidths = ShpReader.getDbfFileFieldWidths(fileHandler,
71
                                                fieldCount);
72
                                rowCount = ShpReader.getDbfFileRowCount(fileHandler);
73
                        }
74
                } catch (IOException e) {
75
                        logger.error("Error while opening db file "
76
                                        + dbFile.getAbsolutePath() + ": " + e.getMessage());
77
                        throw new DriverException(e.getMessage());
78
                }
79
        }
80

    
81
        /**
82
         * Utility conversion method.
83
         * 
84
         * @param commasep
85
         *            s comma separated list of values
86
         * @return the Strings in an array
87
         */
88
        public static String[] commaSeparatedToStringArray(String commasep) {
89
                String[] resp = StringUtil.splitString(commasep, ",");
90
                if (resp.length > 0)
91
                        resp[0] = resp[0].substring(1);
92
                return resp;
93
        }
94

    
95
        private int[] dbfTypesToSqlTypes(char[] types) {
96
                int size = types.length;
97
                int[] resp = new int[size];
98

    
99
                for (int i = 0; i < size; i++) {
100

    
101
                        resp[i] = Types.VARCHAR;
102
                        char c = types[i];
103
                        if (c == 'C')
104
                                resp[i] = Types.VARCHAR;
105
                        if (c == 'D')
106
                                resp[i] = Types.VARCHAR;
107
                        if (c == 'F')
108
                                resp[i] = Types.DOUBLE;
109
                        if (c == 'N')
110
                                resp[i] = Types.DOUBLE;
111
                        if (c == 'L')
112
                                resp[i] = Types.BOOLEAN;
113
                }
114
                return resp;
115
        }
116

    
117
        /**
118
         * Stops the datasource. Tries to close the DBF file.
119
         */
120
        public void stop() throws DriverException {
121
                if (!ShpReader.closeDbfFile(fileHandler)) {
122
                        throw new DriverException("While closing DBF file: "
123
                                        + dbFile.getAbsolutePath());
124
                }
125
        }
126

    
127
        /**
128
         * @return the data source generic name
129
         */
130
        public String getName() {
131
                return "DBF Data Source";
132
        }
133

    
134
        /**
135
         * @return an array representing the WHERE filter. Currently unused.
136
         */
137
        public long[] getWhereFilter() throws IOException {
138
                logger.warn("Method getWhereFilter() was called, returned new long[0]");
139
                return new long[0];
140
        }
141

    
142
        /**
143
         * @return a string representing the datasource. Unused. Simply redirects to
144
         *         <code>getName()</code>
145
         */
146
        public String getAsString() throws DriverException {
147
                logger.warn("Method getAsString() was called, returned getName()");
148
                return getName();
149
        }
150

    
151
        /**
152
         * Called when the layer is removed. Tries to prevent memory leaks.
153
         * 
154
         */
155
        public void remove() throws DriverException {
156
                logger.warn("Method remove() was called, nothing done.");
157
        }
158

    
159
        /**
160
         * @return an array with the indices of the primary keys.
161
         */
162
        public int[] getPrimaryKeys() throws DriverException {
163
                logger
164
                                .warn("Method getPrimaryKeys() was called, returned all indices.");
165
                int[] resp = new int[fieldCount];
166
                for (int i = 0; i < fieldCount; i++)
167
                        resp[i] = i;
168
                return resp;
169
        }
170

    
171
        /**
172
         * Gets the name of one of the fields that compose the PK.
173
         * 
174
         * @param arg0
175
         *            the index of the PK field of interest
176
         * @return the name of the field of interest
177
         */
178
        public String getPKName(int arg0) throws DriverException {
179
                logger.warn("Method getPKName(" + arg0 + ") was called, returned NULL");
180
                return null;
181
        }
182

    
183
        /**
184
         * @return the names of the fields that compose the PK.
185
         * 
186
         */
187
        public String[] getPKNames() throws DriverException {
188
                logger.warn("Method getPKNames() was called, returned NULL");
189
                return null;
190
        }
191

    
192
        /**
193
         * Gets the type of one of the fields that compose the PK.
194
         * 
195
         * @param arg0
196
         *            the index of the field of interest
197
         * @return the type of the field of interest
198
         */
199
        public int getPKType(int arg0) throws DriverException {
200
                logger.warn("Method getPKType(" + arg0
201
                                + ") was called, returned getFieldType(...)");
202
                return fieldTypes[arg0];
203
        }
204

    
205
        /**
206
         * @return the cumber of fields that compose the PK.
207
         */
208
        public int getPKCardinality() throws DriverException {
209
                return fieldCount;
210
        }
211

    
212
        private Object getValueOfItsType(int fieldindex, int row) {
213

    
214
                Object resp = null;
215

    
216
                int type = fieldTypes[fieldindex];
217
                switch (type) {
218

    
219
                case Types.DOUBLE:
220
                        double val = 0;
221
                        try {
222
                                val = ShpReader.getDbfNumberFieldValue(fileHandler, row,
223
                                                fieldindex);
224
                        } catch (Throwable th) {
225
                                logger.debug("Error: " + th.getMessage());
226
                        }
227
                        resp = new Double(val);
228
                        break;
229

    
230
                case Types.VARCHAR:
231
                        resp = ShpReader.getDbfStringFieldValue(fileHandler, row, fieldindex);
232
                        break;
233

    
234
                case Types.DATE:
235

    
236
                        resp = Date.parse(ShpReader.getDbfStringFieldValue(fileHandler, row, fieldindex));
237
                        break;
238

    
239
                case Types.BOOLEAN:
240

    
241
                        resp = new Boolean(ShpReader.getDbfBooleanFieldValue(fileHandler, row, fieldindex));
242
                        break;
243
                }
244

    
245
                return resp;
246
        }
247

    
248
//        /**
249
//         * Gets a row of this data source.
250
//         * 
251
//         * @param rowind
252
//         *            the index of the row of interest
253
//         * @return the row as an array of values
254
//         */
255
//        public Value[] getRow(long rowind) throws DriverException {
256
//                Value[] resp = new Value[fieldCount];
257
//                for (int i = 0; i < fieldCount; i++) {
258
//                        resp[i] = getValueOfItsType(i, (int) rowind);
259
//                }
260
//                return resp;
261
//        }
262

    
263
        /**
264
         * @return an array with all the field names
265
         */
266
        public String[] getFieldNames() throws DriverException {
267
                return fieldNames;
268
        }
269

    
270
        /**
271
         * Gets the idnex of a field, provided its name.
272
         * 
273
         * @param arg0
274
         *            the name of the field of interest
275
         * @return the index of the field of interest
276
         */
277
        public int getFieldIndexByName(String arg0) throws DriverException {
278
                for (int i = 0; i < fieldCount; i++) {
279
                        if (arg0.compareToIgnoreCase(fieldNames[i]) == 0)
280
                                return i;
281
                }
282
                logger.error("Field name not found: " + arg0);
283
                return -1;
284
        }
285

    
286
        /**
287
         * Gets whether the referenced field is virtual or not.
288
         * 
289
         * @param arg0
290
         *            the index of the field of interest
291
         * @return whether it is a virtual field.
292
         * 
293
         */
294
        public boolean isVirtualField(int arg0) throws DriverException {
295
                return false;
296
        }
297

    
298
//        /**
299
//         * @return the associated driver, currently unused.
300
//         */
301
//        public Driver getDriver() {
302
//                logger.warn("Method getDriver() returned NULL.");
303
//                return null;
304
//        }
305

    
306
        /**
307
         * Reloads the data source (stops and starts)
308
         */
309
        public void reload() throws DriverException, IOException {
310
                stop();
311
                start();
312
        }
313

    
314
        /**
315
         * Gets a value from thsi data source.
316
         * 
317
         * @param arg0
318
         *            the row of interest
319
         * @param arg1
320
         *            the index of the field of interest
321
         * 
322
         * @return the value of the given row and field
323
         */
324
        public Object getFieldValue(long arg0, int arg1) throws DriverException {
325
                return getValueOfItsType(arg1, (int) arg0);
326
        }
327

    
328
        /**
329
         * @return the number of fields in this datasource
330
         */
331
        public int getFieldCount() throws DriverException {
332
                return fieldCount;
333
        }
334

    
335
        /**
336
         * Gets the name of the field with the given index.
337
         * 
338
         * @param arg0
339
         *            the index of the field of interest
340
         * @return the name of the field
341
         */
342
        public String getFieldName(int arg0) throws DriverException {
343
                return fieldNames[arg0];
344
        }
345

    
346
        /**
347
         * @return the number of rows in this data source
348
         */
349
        public long getRowCount() throws DriverException {
350
                return rowCount;
351
        }
352

    
353
        /**
354
         * Gets the type of the field with the given index.
355
         * 
356
         * @param arg0
357
         *            the index of the field of interest
358
         * @return teh field type
359
         */
360
        public int getFieldType(int arg0) throws DriverException {
361
                return fieldTypes[arg0];
362
        }
363

    
364
        /**
365
         * Gets the width of the field with the given index.
366
         * 
367
         * @param arg0
368
         *            the index of the field of interest
369
         * @return teh field width
370
         */
371
        public int getFieldWidth(int arg0) throws DriverException {
372
                return fieldWidths[arg0];
373
        }
374
}