Statistics
| Revision:

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

History | View | Annotate | Download (8.5 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
import java.io.File;
12
import java.io.FileInputStream;
13
import java.io.IOException;
14
import java.nio.ByteBuffer;
15
import java.nio.channels.FileChannel;
16

    
17

    
18

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

    
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
        public boolean getBooleanFieldValue(int rowIndex, int fieldId){
46
                int recordOffset = (myHeader.getRecordLength() * rowIndex) +
47
                        myHeader.getHeaderLength()+1;
48
                
49
                //Se calcula el offset del campo
50
                int fieldOffset = 0;
51
                
52
                for (int i = 0; i < fieldId-1; i++){
53
                        fieldOffset += myHeader.getFieldLength(i);
54
                }
55
                
56
                buffer.position(recordOffset + fieldOffset);
57
                
58
                char bool = (char) buffer.get();
59
                
60
                return ((bool == 't') || (bool == 'T') || (bool == 'Y') || (bool == 'y'));
61
        }
62

    
63
        public String getStringFieldValue(int rowIndex, int fieldId){
64
                int recordOffset = (myHeader.getRecordLength() * rowIndex) +
65
                        myHeader.getHeaderLength()+1;
66
                
67
                //Se calcula el offset del campo
68
                int fieldOffset = 0;
69
                
70
                for (int i = 0; i < fieldId; i++){
71
                        fieldOffset += myHeader.getFieldLength(i);
72
                }
73
                
74
                buffer.position(recordOffset + fieldOffset);
75
                
76
                byte[] data = new byte[myHeader.getFieldLength(fieldId)];
77
                buffer.get(data);
78
                
79
                return new String(data);
80
        }
81

    
82
    // Retrieve the record at the given index
83
/*    public Object[] getRecord(long inIndex) throws IOException {
84
        long nRecordOffset = (myHeader.getRecordLength() * inIndex) +
85
            myHeader.getHeaderLength();
86

87
        // retrieve the record length
88
        int tempNumFields = myHeader.getNumFields();
89

90
        // storage for the actual values
91
        Object[] tempRow = new Object[tempNumFields];
92

93
            buffer.position((int) nRecordOffset);
94

95
            // read the deleted flag
96
            char tempDeleted = (char) buffer.get();
97

98
            // read the record length
99
            int tempRecordLength = 1; // for the deleted character just read.
100

101
            // read the Fields
102
            for (int j = 0; j < tempNumFields; j++) {
103
                // find the length of the field.
104
                int tempFieldLength = myHeader.getFieldLength(j);
105
                tempRecordLength = tempRecordLength + tempFieldLength;
106

107
                // find the field type
108
                char tempFieldType = myHeader.getFieldType(j);
109

110
                //System.out.print("Reading Name="+myHeader.getFieldName(j)+" Type="+tempFieldType +" Length="+tempFieldLength);
111
                // read the data.
112
                Object tempObject = null;
113

114
                switch (tempFieldType) {
115
                case 'L': // logical data type, one character (T,t,F,f,Y,y,N,n)
116

117
                    char tempChar = (char) buffer.get();
118

119
                    if ((tempChar == 'T') || (tempChar == 't') ||
120
                            (tempChar == 'Y') || (tempChar == 'y')) {
121
                        tempObject = new Boolean(true);
122
                    } else {
123
                        tempObject = new Boolean(false);
124
                    }
125

126
                    break;
127

128
                case 'C': // character record.
129

130
                    byte[] sbuffer = new byte[tempFieldLength];
131
                                        buffer.get(sbuffer);
132
                    tempObject = new String(sbuffer, "ISO-8859-1").trim();
133

134
                    break;
135

136
                case 'D': // date data type.
137

138
                    byte[] dbuffer = new byte[8];
139
                                        buffer.get(dbuffer);
140

141
                    String tempString = new String(dbuffer, 0, 4);
142

143
                    try {
144
                        int tempYear = Integer.parseInt(tempString);
145
                        tempString = new String(dbuffer, 4, 2);
146

147
                        int tempMonth = Integer.parseInt(tempString) - 1;
148
                        tempString = new String(dbuffer, 6, 2);
149

150
                        int tempDay = Integer.parseInt(tempString);
151
                        Calendar c = Calendar.getInstance();
152
                        c.set(Calendar.YEAR, tempYear);
153
                        c.set(Calendar.MONTH, tempMonth);
154
                        c.set(Calendar.DAY_OF_MONTH, tempDay);
155
                        tempObject = c.getTime();
156
                    } catch (NumberFormatException e) {
157
                    }
158

159
                    break;
160

161
                case 'M': // memo field.
162

163
                    byte[] mbuffer = new byte[10];
164
                                        buffer.get(mbuffer);
165

166
                    break;
167

168
                case 'N': // number
169
                case 'F': // floating point number
170

171
                    byte[] fbuffer = new byte[tempFieldLength];
172
                                        buffer.get(fbuffer);
173

174
                    try {
175
                        tempString = new String(fbuffer);
176
                        tempObject = Double.valueOf(tempString.trim());
177
                    } catch (NumberFormatException e) {
178
                    }
179

180
                    break;
181

182
                default:
183

184
                    byte[] defbuffer = new byte[tempFieldLength];
185
                                        buffer.get(defbuffer);
186
                    System.out.println("Do not know how to parse Field type " +
187
                        tempFieldType);
188
                }
189

190
                tempRow[j] = tempObject;
191

192
                //                                System.out.println(" Data="+tempObject);
193
            }
194

195
            // ensure that the full record has been read.
196
            if (tempRecordLength < myHeader.getRecordLength()) {
197
                byte[] tempbuff = new byte[myHeader.getRecordLength() -
198
                    tempRecordLength];
199
                buffer.get(tempbuff);
200

201
                /* if (tempTelling){
202
                        System.out.println("DBF File has "+(myHeader.getRecordLength()-tempRecordLength)+" extra bytes per record");
203
                        tempTelling = false;
204
                } */
205
 /*           }
206

207
        return tempRow;
208
    }
209
*/
210
    /**
211
     * Retrieve the name of the given column.
212
     *
213
     * @param inIndex DOCUMENT ME!
214
     *
215
     * @return DOCUMENT ME!
216
     */
217
    public String getFieldName(int inIndex) {
218
        return myHeader.getFieldName(inIndex).trim();
219
    }
220

    
221
    /**
222
     * Retrieve the type of the given column.
223
     *
224
     * @param inIndex DOCUMENT ME!
225
     *
226
     * @return DOCUMENT ME!
227
     */
228
    public char getFieldType(int inIndex) {
229
        return myHeader.getFieldType(inIndex);
230
    }
231

    
232
    /**
233
     * Retrieve the length of the given column.
234
     *
235
     * @param inIndex DOCUMENT ME!
236
     *
237
     * @return DOCUMENT ME!
238
     */
239
    public int getFieldLength(int inIndex) {
240
        return myHeader.getFieldLength(inIndex);
241
    }
242

    
243
    /*
244
     * Retrieve the value of the given column as string.
245
     *
246
     * @param idField DOCUMENT ME!
247
     * @param idRecord DOCUMENT ME!
248
     *
249
     * @return DOCUMENT ME!
250
     *
251
    public Object getFieldValue(int idField, long idRecord) throws IOException {
252
        Object[] tmpReg = getRecord(idRecord);
253

254
        return tmpReg[idField];
255
    }
256
*/
257
    /*
258
     * DOCUMENT ME!
259
     *
260
     * @param idField DOCUMENT ME!
261
     * @param idRecord DOCUMENT ME!
262
     *
263
     * @return DOCUMENT ME!
264
     *
265
    public double getFieldValueAsDouble(int idField, int idRecord) throws IOException {
266
        Object[] tmpReg = getRecord(idRecord);
267

268
        return (double) Double.parseDouble(tmpReg[idField].toString());
269
    }
270
*/
271
    /**
272
     * Retrieve the location of the decimal point.
273
     *
274
     * @param inIndex DOCUMENT ME!
275
     *
276
     * @return DOCUMENT ME!
277
     */
278
    public int getFieldDecimalLength(int inIndex) {
279
        return myHeader.getFieldDecimalCount(inIndex);
280
    }
281

    
282
    /**
283
     * read the DBF file into memory.
284
     *
285
     * @throws Exception DOCUMENT ME!
286
     */
287
    public void open(File file) throws IOException {
288

    
289
        fin = new FileInputStream(file);
290
                channel = fin.getChannel();
291
                
292
                buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
293

    
294
        // create the header to contain the header information.
295
        myHeader = new DbaseFileHeader();
296
        myHeader.readHeader(buffer);
297
    }
298

    
299
    /**
300
     * Removes all data from the dataset
301
     */
302
    public void close() throws IOException {
303
            fin.close();
304
                channel.close();
305
    }
306
}