Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / dbf / DBFDriver.java @ 5595

History | View | Annotate | Download (9.76 KB)

1
package com.iver.cit.gvsig.fmap.drivers.dbf;
2

    
3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.IOException;
6
import java.io.RandomAccessFile;
7
import java.nio.channels.FileChannel;
8
import java.nio.channels.WritableByteChannel;
9
import java.sql.Types;
10
import java.text.DateFormat;
11
import java.text.ParseException;
12
import java.util.Date;
13
import java.util.Locale;
14
import java.util.Properties;
15

    
16
import com.hardcode.gdbms.driver.DriverUtilities;
17
import com.hardcode.gdbms.engine.data.DataSourceFactory;
18
import com.hardcode.gdbms.engine.data.driver.DriverException;
19
import com.hardcode.gdbms.engine.data.driver.FileDriver;
20
import com.hardcode.gdbms.engine.data.edition.DataWare;
21
import com.hardcode.gdbms.engine.data.file.FileDataWare;
22
import com.hardcode.gdbms.engine.values.Value;
23
import com.hardcode.gdbms.engine.values.ValueFactory;
24
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition;
25
import com.iver.cit.gvsig.fmap.drivers.shp.DbaseFileHeaderNIO;
26
import com.iver.cit.gvsig.fmap.drivers.shp.DbaseFileWriterNIO;
27
import com.iver.cit.gvsig.fmap.edition.EditionException;
28
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
29
import com.iver.cit.gvsig.fmap.edition.IWriteable;
30
import com.iver.cit.gvsig.fmap.edition.IWriter;
31
import com.iver.cit.gvsig.fmap.edition.writers.dbf.DbfWriter;
32

    
33

    
34
/**
35
 * DOCUMENT ME!
36
 *
37
 * @author Fernando Gonz?lez Cort?s
38
 */
39
public class DBFDriver implements FileDriver, IWriteable {
40
    //private File file;
41
    private static Locale ukLocale = new Locale("en", "UK"); // English, UK version
42
    private DbaseFile dbf = new DbaseFile();
43
    private char[] fieldTypes;
44
    private DataSourceFactory dsf;
45
    private DbfWriter dbfWriter = new DbfWriter();
46
        private File file = null;
47

    
48
    /**
49
     * @see com.hardcode.driverManager.Driver#getName()
50
     */
51
    public String getName() {
52
        return "gdbms dbf driver";
53
    }
54

    
55
    /**
56
     * @see com.hardcode.gdbms.engine.data.GDBMSDriver#open(java.io.File)
57
     */
58
    public void open(File file) throws IOException {
59
            this.file  = file;
60
        dbf.open(file);
61

    
62
        try {
63
            fieldTypes = new char[getFieldCount()];
64

    
65
            for (int i = 0; i < fieldTypes.length; i++) {
66
                fieldTypes[i] = dbf.getFieldType(i);
67
            }
68
            dbfWriter.setFile(file);
69
        } catch (DriverException e) {
70
            throw new IOException(e.getMessage());
71
        }
72
        
73
    }
74

    
75
    /**
76
     * @see com.hardcode.gdbms.engine.data.GDBMSDriver#close()
77
     */
78
    public void close() throws IOException {
79
        dbf.close();
80
    }
81

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

    
91
        if (fieldType == 'L') {
92
            return ValueFactory.createValue(dbf.getBooleanFieldValue(
93
                    (int) rowIndex, fieldId));
94

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

    
108
            if (strValue.length() == 0) {
109
                return null;
110
            }
111
            double value=0;
112
            try{
113
            value = Double.parseDouble(strValue);
114
            }catch (Exception e) {
115
                                return ValueFactory.createValue(0D);
116
                        }
117
            return ValueFactory.createValue(value);
118
        } else if (fieldType == 'C') {
119
            return ValueFactory.createValue(dbf.getStringFieldValue(
120
                    (int) rowIndex, fieldId).trim());
121
        } else if (fieldType == 'D') {
122
            String date = dbf.getStringFieldValue((int) rowIndex, fieldId).trim();
123
            // System.out.println(rowIndex + " data=" + date);
124
            if (date.length() == 0) {
125
                return null;
126
            }
127

    
128
            String year = date.substring(0, 4);
129
            String month = date.substring(4, 6);
130
            String day = date.substring(6, 8);
131
            DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, ukLocale);
132
            /* Calendar c = Calendar.getInstance();
133
            c.clear();
134
            c.set(Integer.parseInt(year), Integer.parseInt(month),
135
                Integer.parseInt(day));
136
            c.set(Calendar.MILLISECOND, 0); */
137
            String strAux = month + "/" + day + "/" + year;
138
            Date dat;
139
            try {
140
                dat = df.parse(strAux);
141
            } catch (ParseException e) {
142
                throw new DriverException("Bad Date Format");            
143
            } 
144
            
145
            // System.out.println("numReg = " + rowIndex + " date:" + dat.getTime());
146

    
147
            return ValueFactory.createValue(dat);
148
        } else {
149
            throw new DriverException("Unknown field type");
150
        }
151
    }
152

    
153
    /**
154
     * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldCount()
155
     */
156
    public int getFieldCount() throws DriverException {
157
        return dbf.getFieldCount();
158
    }
159

    
160
    /**
161
     * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldName(int)
162
     */
163
    public String getFieldName(int fieldId) throws DriverException {
164
        return dbf.getFieldName(fieldId);
165
    }
166

    
167
    /**
168
     * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getRowCount()
169
     */
170
    public long getRowCount() throws DriverException {
171
        return dbf.getRecordCount();
172
    }
173

    
174
    /**
175
     * @see com.hardcode.gdbms.engine.data.driver.FileDriver#fileAccepted(java.io.File)
176
     */
177
    public boolean fileAccepted(File f) {
178
        return f.getAbsolutePath().toUpperCase().endsWith("DBF");
179
    }
180

    
181
    /**
182
     * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldType(int)
183
     */
184
    public int getFieldType(int i) throws DriverException {
185
        char fieldType = fieldTypes[i];
186

    
187
        if (fieldType == 'L') {
188
            return Types.BOOLEAN;
189
        } else if ((fieldType == 'F') || (fieldType == 'N')) {
190
            return Types.DOUBLE;
191
        } else if (fieldType == 'C') {
192
            return Types.VARCHAR;
193
        } else if (fieldType == 'D') {
194
            return Types.DATE;
195
        } else {
196
            throw new DriverException("Unknown field type");
197
        }
198
    }
199

    
200
    /**
201
     * @see com.hardcode.gdbms.engine.data.driver.DriverCommons#setDataSourceFactory(com.hardcode.gdbms.engine.data.DataSourceFactory)
202
     */
203
    public void setDataSourceFactory(DataSourceFactory dsf) {
204
        this.dsf = dsf;
205
    }
206

    
207
    private void writeToTemp(DataWare dataWare, File file) throws DriverException {
208
        DbaseFileWriterNIO dbfWrite = null;
209
        DbaseFileHeaderNIO myHeader;
210
        Value[] record;
211

    
212
        try {
213
            myHeader = DbaseFileHeaderNIO.createDbaseHeader(dataWare);
214

    
215
            myHeader.setNumRecords((int) dataWare.getRowCount());
216
            dbfWrite = new DbaseFileWriterNIO(myHeader,
217
                    (FileChannel) getWriteChannel(file.getPath()));
218
            record = new Value[dataWare.getFieldCount()];
219

    
220
            for (int j = 0; j < dataWare.getRowCount(); j++) {
221
                for (int r = 0; r < dataWare.getFieldCount(); r++) {
222
                    record[r] = dataWare.getFieldValue(j, r);
223
                }
224

    
225
                dbfWrite.write(record);
226
            }
227
            
228
            dbfWrite.close();
229
        } catch (IOException e) {
230
            throw new DriverException(e);
231
        }
232
        
233
    }
234
    
235
    /**
236
     * @see com.hardcode.gdbms.engine.data.driver.FileDriver#writeFile(com.hardcode.gdbms.engine.data.file.FileDataWare,
237
     *      java.io.File)
238
     */
239
    public void writeFile(FileDataWare dataWare)
240
        throws DriverException {
241
        
242
        String temp = dsf.getTempFile();
243
        
244
        writeToTemp(dataWare, new File(temp));
245

    
246
        try {
247
            FileChannel fcout = dbf.getWriteChannel();
248
            FileChannel fcin = new FileInputStream(temp).getChannel();
249

    
250
            DriverUtilities.copy(fcin, fcout);
251
        } catch (IOException e) {
252
            throw new DriverException(e);
253
        }
254
    }
255

    
256
    /**
257
     * DOCUMENT ME!
258
     *
259
     * @param path DOCUMENT ME!
260
     *
261
     * @return DOCUMENT ME!
262
     *
263
     * @throws IOException DOCUMENT ME!
264
     */
265
    private WritableByteChannel getWriteChannel(String path)
266
        throws IOException {
267
        WritableByteChannel channel;
268

    
269
        File f = new File(path);
270

    
271
        if (!f.exists()) {
272
            System.out.println("Creando fichero " + f.getAbsolutePath());
273

    
274
            if (!f.createNewFile()) {
275
                throw new IOException("Cannot create file " + f);
276
            }
277
        }
278

    
279
        RandomAccessFile raf = new RandomAccessFile(f, "rw");
280
        channel = raf.getChannel();
281

    
282
        return channel;
283
    }
284

    
285
    public void createSource(String arg0, String[] arg1, int[] arg2) throws IOException {
286
        DbaseFileHeaderNIO myHeader;
287

    
288
        int[] lengths = new int[arg2.length];
289
        for (int i = 0; i < arg2.length; i++) {
290
            lengths[i] = 100;
291
        }
292
        myHeader = DbaseFileHeaderNIO.createDbaseHeader(arg1, arg2, lengths);
293
        myHeader.setNumRecords(0);
294
        DbaseFileWriterNIO dbfWrite = new DbaseFileWriterNIO(myHeader,
295
                (FileChannel) getWriteChannel(arg0));
296
        dbfWrite = new DbaseFileWriterNIO(myHeader,
297
                (FileChannel) getWriteChannel(arg0));
298
    }
299

    
300
        public int getFieldWidth(int i) throws DriverException {
301
                return dbf.getFieldLength(i);
302
        }
303

    
304
        public IWriter getWriter() {
305
                return dbfWriter;
306
        }
307

    
308
}