Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / driver / dbf_Fernando / DBFDriver.java @ 4851

History | View | Annotate | Download (7.91 KB)

1
package com.hardcode.gdbms.driver.dbf_Fernando;
2

    
3
import com.hardcode.driverManager.Driver;
4

    
5
import com.hardcode.gdbms.engine.data.DataSourceFactory;
6
import com.hardcode.gdbms.engine.data.driver.DriverException;
7
import com.hardcode.gdbms.engine.data.driver.FileDriver;
8
import com.hardcode.gdbms.engine.data.file.FileDataWare;
9
import com.hardcode.gdbms.engine.values.Value;
10
import com.hardcode.gdbms.engine.values.ValueFactory;
11

    
12
import java.io.BufferedOutputStream;
13
import java.io.DataOutputStream;
14
import java.io.File;
15
import java.io.FileInputStream;
16
import java.io.FileOutputStream;
17
import java.io.IOException;
18
import java.nio.ByteBuffer;
19
import java.nio.ByteOrder;
20
import java.nio.channels.FileChannel;
21

    
22
import java.sql.Types;
23
import java.text.DateFormat;
24
import java.text.SimpleDateFormat;
25

    
26
import java.util.Calendar;
27
import java.util.Date;
28
import java.util.HashMap;
29

    
30

    
31
/**
32
 * DOCUMENT ME!
33
 *
34
 * @author Fernando Gonz?lez Cort?s
35
 */
36
public class DBFDriver implements Driver, FileDriver {
37
    private File file;
38
    private DbaseFile dbf = new DbaseFile();
39
    private char[] fieldTypes;
40

    
41
    /**
42
     * @see com.hardcode.driverManager.Driver#getName()
43
     */
44
    public String getName() {
45
        return "gdbms dbf driver";
46
    }
47

    
48
    /**
49
     * @see com.hardcode.gdbms.engine.data.GDBMSDriver#open(java.io.File)
50
     */
51
    public void open(File file) throws IOException {
52
        this.file = file;
53
        dbf.open(file);
54

    
55
        try {
56
            fieldTypes = new char[getFieldCount()];
57

    
58
            for (int i = 0; i < fieldTypes.length; i++) {
59
                fieldTypes[i] = dbf.getFieldType(i);
60
            }
61
        } catch (DriverException e) {
62
            throw new IOException(e.getMessage());
63
        }
64

    
65
        /*            memory = new Value[getRowCount()][getFieldCount()];
66
           for (int i = 0; i < getRowCount(); i++){
67
                   for (int j = 0; j < getFieldCount(); j++){
68
                           try {
69
                                       memory[i][j] = getFieldValueOff(i,j);
70
                               } catch (IOException e) {
71
                                       e.printStackTrace();
72
                               } catch (SemanticException e) {
73
                                       e.printStackTrace();
74
                               }
75
                   }
76
           }
77
         */
78
    }
79

    
80
    /**
81
     * @see com.hardcode.gdbms.engine.data.GDBMSDriver#close()
82
     */
83
    public void close() throws IOException {
84
        dbf.close();
85
    }
86

    
87
    /**
88
     * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldValue(long,
89
     *      int)
90
     */
91
    public Value getFieldValue(long rowIndex, int fieldId)
92
        throws DriverException {
93
        // Field Type (C  or M)
94
        char fieldType = fieldTypes[fieldId];
95

    
96
        if (fieldType == 'L') {
97
            return ValueFactory.createValue(dbf.getBooleanFieldValue(
98
                    (int) rowIndex, fieldId));
99

    
100
            /*                }else if (fieldType == 'N'){
101
               String strValue = dbf.getStringFieldValue(rowIndex, fieldId);
102
               long value = Long.parseLong(strValue);
103
               if ((value > Integer.MIN_VALUE) && (value < Integer.MAX_VALUE)){
104
                       return new IntValue((int) value);
105
               }else{
106
                       return new LongValue(value);
107
               }
108
             */
109
        } else if ((fieldType == 'F') || (fieldType == 'N')) {
110
            String strValue = dbf.getStringFieldValue((int) rowIndex, fieldId)
111
                                 .trim();
112

    
113
            if (strValue.length() == 0) {
114
                return null;
115
            }
116

    
117
            double value = Double.parseDouble(strValue);
118

    
119
            return ValueFactory.createValue(value);
120
        } else if (fieldType == 'C') {
121
            return ValueFactory.createValue(dbf.getStringFieldValue(
122
                    (int) rowIndex, fieldId).trim());
123
        } else if (fieldType == 'D') {
124
            String date = dbf.getStringFieldValue((int) rowIndex, fieldId).trim();
125

    
126
            if (date.length() == 0) {
127
                return null;
128
            }
129

    
130
            String year = date.substring(0, 4);
131
            String month = date.substring(4, 6);
132
            String day = date.substring(6, 8);
133
            Calendar c = Calendar.getInstance();
134
            c.set(Integer.parseInt(year), Integer.parseInt(month),
135
                Integer.parseInt(day), 0, 0, 0);
136
            c.set(Calendar.MILLISECOND, 0);
137

    
138
            return ValueFactory.createValue(c.getTime());
139
        } else {
140
            throw new DriverException("Unknown field type");
141
        }
142
    }
143

    
144
    /**
145
     * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldCount()
146
     */
147
    public int getFieldCount() throws DriverException {
148
        return dbf.getFieldCount();
149
    }
150

    
151
    /**
152
     * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldName(int)
153
     */
154
    public String getFieldName(int fieldId) throws DriverException {
155
        return dbf.getFieldName(fieldId);
156
    }
157

    
158
    /**
159
     * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getRowCount()
160
     */
161
    public long getRowCount() throws DriverException {
162
        return dbf.getRecordCount();
163
    }
164

    
165
    /**
166
     * @see com.hardcode.gdbms.engine.data.driver.FileDriver#fileAccepted(java.io.File)
167
     */
168
    public boolean fileAccepted(File f) {
169
        return f.getAbsolutePath().toUpperCase().endsWith("DBF");
170
    }
171

    
172
    /**
173
     * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldType(int)
174
     */
175
    public int getFieldType(int i) throws DriverException {
176
        char fieldType = fieldTypes[i];
177

    
178
        if (fieldType == 'L') {
179
            return Types.BOOLEAN;
180
        } else if ((fieldType == 'F') || (fieldType == 'N')) {
181
            return Types.DOUBLE;
182
        } else if (fieldType == 'C') {
183
            return Types.VARCHAR;
184
        } else if (fieldType == 'D') {
185
            return Types.DATE;
186
        } else {
187
            throw new DriverException("Unknown field type");
188
        }
189
    }
190

    
191
    /**
192
     * @see com.hardcode.gdbms.engine.data.driver.DriverCommons#getDriverProperties()
193
     */
194
    public HashMap getDriverProperties() {
195
        // TODO Auto-generated method stub
196
        return null;
197
    }
198

    
199
    /**
200
     * @see com.hardcode.gdbms.engine.data.driver.DriverCommons#setDataSourceFactory(com.hardcode.gdbms.engine.data.DataSourceFactory)
201
     */
202
    public void setDataSourceFactory(DataSourceFactory dsf) {
203
        // TODO Auto-generated method stub
204
    }
205

    
206
    /**
207
     * @see com.hardcode.gdbms.engine.data.driver.FileDriver#writeFile(com.hardcode.gdbms.engine.data.file.FileDataWare,
208
     *      java.io.File)
209
     */
210
    public void writeFile(FileDataWare dataWare)
211
        throws DriverException {
212
        try {
213
            File temp = File.createTempFile("gdbms", ".dbf");
214
            FileOutputStream fos = new FileOutputStream(temp);
215
            DataOutputStream dos = new DataOutputStream(fos);
216
            
217
            //version
218
            dos.writeByte(0x33);
219
            
220
            //date
221
            Date d = new Date();
222
            SimpleDateFormat sdf = new SimpleDateFormat("yy");
223
            byte year = Byte.parseByte(sdf.format(d));
224
            sdf = new SimpleDateFormat("MM");
225
            byte month = Byte.parseByte(sdf.format(d));
226
            sdf = new SimpleDateFormat("dd");
227
            byte day = Byte.parseByte(sdf.format(d));
228
            dos.writeByte(year);
229
            dos.writeByte(month);
230
            dos.writeByte(day);
231
            
232
            //record count
233
            int rowCount = (int) dataWare.getRowCount();
234
            dos.write(rowCount);
235
            
236
            //header length
237
            dos.write(296 + dataWare.getFieldCount() * 32);
238
            
239
        } catch (IOException e) {
240
            throw new DriverException(e);
241
        }
242
    }
243

    
244
    public void createSource(String path, String[] fieldNames, int[] fieldTypes) throws IOException {
245
        // TODO Auto-generated method stub
246
        
247
    }
248
        public int getFieldWidth(int i) throws DriverException {
249
                return dbf.getFieldLength(i);
250
        }
251
}