Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libGDBMS / src / com / hardcode / gdbms / driver / dbf / DbaseFile.java @ 1956

History | View | Annotate | Download (8.37 KB)

1
/*
2
 * Created on 16-feb-2004
3
 *
4
 * To change the template for this generated file go to
5
 * Window>Preferences>Java>Code Generation>Code and Comments
6
 */
7
package com.hardcode.gdbms.driver.dbf;
8

    
9

    
10
/**
11
 */
12
import java.io.File;
13
import java.io.FileInputStream;
14
import java.io.IOException;
15

    
16
import java.nio.ByteBuffer;
17
import java.nio.channels.FileChannel;
18

    
19

    
20
/**
21
 * Class to read and write data to a dbase III format file. Creation date:
22
 * (5/15/2001 5:15:13 PM)
23
 */
24
public class DbaseFile {
25
        // Header information for the DBase File
26
        private DbaseFileHeader myHeader;
27
        private FileInputStream fin;
28
        private FileChannel channel;
29
        private ByteBuffer buffer;
30

    
31
        // Retrieve number of records in the DbaseFile
32
        public int getRecordCount() {
33
                return myHeader.getNumRecords();
34
        }
35

    
36
        /**
37
         * DOCUMENT ME!
38
         *
39
         * @return DOCUMENT ME!
40
         */
41
        public int getFieldCount() {
42
                return myHeader.getNumFields();
43
        }
44

    
45
        /**
46
         * DOCUMENT ME!
47
         *
48
         * @param rowIndex DOCUMENT ME!
49
         * @param fieldId DOCUMENT ME!
50
         *
51
         * @return DOCUMENT ME!
52
         */
53
        public boolean getBooleanFieldValue(int rowIndex, int fieldId) {
54
                int recordOffset = (myHeader.getRecordLength() * rowIndex) +
55
                        myHeader.getHeaderLength() + 1;
56

    
57
                //Se calcula el offset del campo
58
                int fieldOffset = 0;
59

    
60
                for (int i = 0; i < (fieldId - 1); i++) {
61
                        fieldOffset += myHeader.getFieldLength(i);
62
                }
63

    
64
                buffer.position(recordOffset + fieldOffset);
65

    
66
                char bool = (char) buffer.get();
67

    
68
                return ((bool == 't') || (bool == 'T') || (bool == 'Y') ||
69
                (bool == 'y'));
70
        }
71

    
72
        /**
73
         * DOCUMENT ME!
74
         *
75
         * @param rowIndex DOCUMENT ME!
76
         * @param fieldId DOCUMENT ME!
77
         *
78
         * @return DOCUMENT ME!
79
         */
80
        public String getStringFieldValue(int rowIndex, int fieldId) {
81
                int recordOffset = (myHeader.getRecordLength() * rowIndex) +
82
                        myHeader.getHeaderLength() + 1;
83

    
84
                //Se calcula el offset del campo
85
                int fieldOffset = 0;
86

    
87
                for (int i = 0; i < fieldId; i++) {
88
                        fieldOffset += myHeader.getFieldLength(i);
89
                }
90

    
91
                buffer.position(recordOffset + fieldOffset);
92

    
93
                byte[] data = new byte[myHeader.getFieldLength(fieldId)];
94
                buffer.get(data);
95

    
96
                return new String(data);
97
        }
98

    
99
        // Retrieve the record at the given index
100

    
101
        /*    public Object[] getRecord(long inIndex) throws IOException {
102
           long nRecordOffset = (myHeader.getRecordLength() * inIndex) +
103
               myHeader.getHeaderLength();
104
           // retrieve the record length
105
           int tempNumFields = myHeader.getNumFields();
106
           // storage for the actual values
107
           Object[] tempRow = new Object[tempNumFields];
108
               buffer.position((int) nRecordOffset);
109
               // read the deleted flag
110
               char tempDeleted = (char) buffer.get();
111
               // read the record length
112
               int tempRecordLength = 1; // for the deleted character just read.
113
               // read the Fields
114
               for (int j = 0; j < tempNumFields; j++) {
115
                   // find the length of the field.
116
                   int tempFieldLength = myHeader.getFieldLength(j);
117
                   tempRecordLength = tempRecordLength + tempFieldLength;
118
                   // find the field type
119
                   char tempFieldType = myHeader.getFieldType(j);
120
                   //System.out.print("Reading Name="+myHeader.getFieldName(j)+" Type="+tempFieldType +" Length="+tempFieldLength);
121
                   // read the data.
122
                   Object tempObject = null;
123
                   switch (tempFieldType) {
124
                   case 'L': // logical data type, one character (T,t,F,f,Y,y,N,n)
125
                       char tempChar = (char) buffer.get();
126
                       if ((tempChar == 'T') || (tempChar == 't') ||
127
                               (tempChar == 'Y') || (tempChar == 'y')) {
128
                           tempObject = new Boolean(true);
129
                       } else {
130
                           tempObject = new Boolean(false);
131
                       }
132
                       break;
133
                   case 'C': // character record.
134
                       byte[] sbuffer = new byte[tempFieldLength];
135
                                           buffer.get(sbuffer);
136
                       tempObject = new String(sbuffer, "ISO-8859-1").trim();
137
                       break;
138
                   case 'D': // date data type.
139
                       byte[] dbuffer = new byte[8];
140
                                           buffer.get(dbuffer);
141
                       String tempString = new String(dbuffer, 0, 4);
142
                       try {
143
                           int tempYear = Integer.parseInt(tempString);
144
                           tempString = new String(dbuffer, 4, 2);
145
                           int tempMonth = Integer.parseInt(tempString) - 1;
146
                           tempString = new String(dbuffer, 6, 2);
147
                           int tempDay = Integer.parseInt(tempString);
148
                           Calendar c = Calendar.getInstance();
149
                           c.set(Calendar.YEAR, tempYear);
150
                           c.set(Calendar.MONTH, tempMonth);
151
                           c.set(Calendar.DAY_OF_MONTH, tempDay);
152
                           tempObject = c.getTime();
153
                       } catch (NumberFormatException e) {
154
                       }
155
                       break;
156
                   case 'M': // memo field.
157
                       byte[] mbuffer = new byte[10];
158
                                           buffer.get(mbuffer);
159
                       break;
160
                   case 'N': // number
161
                   case 'F': // floating point number
162
                       byte[] fbuffer = new byte[tempFieldLength];
163
                                           buffer.get(fbuffer);
164
                       try {
165
                           tempString = new String(fbuffer);
166
                           tempObject = Double.valueOf(tempString.trim());
167
                       } catch (NumberFormatException e) {
168
                       }
169
                       break;
170
                   default:
171
                       byte[] defbuffer = new byte[tempFieldLength];
172
                                           buffer.get(defbuffer);
173
                       System.out.println("Do not know how to parse Field type " +
174
                           tempFieldType);
175
                   }
176
                   tempRow[j] = tempObject;
177
                   //                                System.out.println(" Data="+tempObject);
178
               }
179
               // ensure that the full record has been read.
180
               if (tempRecordLength < myHeader.getRecordLength()) {
181
                   byte[] tempbuff = new byte[myHeader.getRecordLength() -
182
                       tempRecordLength];
183
                   buffer.get(tempbuff);
184
                   /* if (tempTelling){
185
                           System.out.println("DBF File has "+(myHeader.getRecordLength()-tempRecordLength)+" extra bytes per record");
186
                           tempTelling = false;
187
                   } */
188
        /*           }
189
           return tempRow;
190
           }
191
         */
192

    
193
        /**
194
         * Retrieve the name of the given column.
195
         *
196
         * @param inIndex DOCUMENT ME!
197
         *
198
         * @return DOCUMENT ME!
199
         */
200
        public String getFieldName(int inIndex) {
201
                return myHeader.getFieldName(inIndex).trim();
202
        }
203

    
204
        /**
205
         * Retrieve the type of the given column.
206
         *
207
         * @param inIndex DOCUMENT ME!
208
         *
209
         * @return DOCUMENT ME!
210
         */
211
        public char getFieldType(int inIndex) {
212
                return myHeader.getFieldType(inIndex);
213
        }
214

    
215
        /**
216
         * Retrieve the length of the given column.
217
         *
218
         * @param inIndex DOCUMENT ME!
219
         *
220
         * @return DOCUMENT ME!
221
         */
222
        public int getFieldLength(int inIndex) {
223
                return myHeader.getFieldLength(inIndex);
224
        }
225

    
226
        /*
227
         * Retrieve the value of the given column as string.
228
         *
229
         * @param idField DOCUMENT ME!
230
         * @param idRecord DOCUMENT ME!
231
         *
232
         * @return DOCUMENT ME!
233
         *
234
           public Object getFieldValue(int idField, long idRecord) throws IOException {
235
               Object[] tmpReg = getRecord(idRecord);
236
               return tmpReg[idField];
237
           }
238
         */
239
        /*
240
         * DOCUMENT ME!
241
         *
242
         * @param idField DOCUMENT ME!
243
         * @param idRecord DOCUMENT ME!
244
         *
245
         * @return DOCUMENT ME!
246
         *
247
           public double getFieldValueAsDouble(int idField, int idRecord) throws IOException {
248
               Object[] tmpReg = getRecord(idRecord);
249
               return (double) Double.parseDouble(tmpReg[idField].toString());
250
           }
251
         */
252

    
253
        /**
254
         * Retrieve the location of the decimal point.
255
         *
256
         * @param inIndex DOCUMENT ME!
257
         *
258
         * @return DOCUMENT ME!
259
         */
260
        public int getFieldDecimalLength(int inIndex) {
261
                return myHeader.getFieldDecimalCount(inIndex);
262
        }
263

    
264
        /**
265
         * read the DBF file into memory.
266
         *
267
         * @param file DOCUMENT ME!
268
         *
269
         * @throws IOException DOCUMENT ME!
270
         */
271
        public void open(File file) throws IOException {
272
                fin = new FileInputStream(file);
273
                channel = fin.getChannel();
274

    
275
                buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
276

    
277
                // create the header to contain the header information.
278
                myHeader = new DbaseFileHeader();
279
                myHeader.readHeader(buffer);
280
        }
281

    
282
        /**
283
         * Removes all data from the dataset
284
         *
285
         * @throws IOException DOCUMENT ME!
286
         */
287
        public void close() throws IOException {
288
                fin.close();
289
                channel.close();
290
                buffer = null;
291
        }
292
}