Statistics
| Revision:

root / trunk / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / driver / csvstring / CSVStringDriver.java @ 10627

History | View | Annotate | Download (8.72 KB)

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

    
3
import java.io.BufferedReader;
4
import java.io.File;
5
import java.io.FileNotFoundException;
6
import java.io.FileOutputStream;
7
import java.io.FileReader;
8
import java.io.IOException;
9
import java.io.PrintWriter;
10
import java.sql.Date;
11
import java.sql.Time;
12
import java.sql.Timestamp;
13
import java.sql.Types;
14
import java.util.ArrayList;
15

    
16
import com.hardcode.driverManager.Driver;
17
import com.hardcode.gdbms.driver.exceptions.CloseDriverException;
18
import com.hardcode.gdbms.driver.exceptions.FileNotFoundDriverException;
19
import com.hardcode.gdbms.driver.exceptions.OpenDriverException;
20
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
21
import com.hardcode.gdbms.driver.exceptions.WriteDriverException;
22
import com.hardcode.gdbms.engine.data.DataSourceFactory;
23
import com.hardcode.gdbms.engine.data.driver.FileDriver;
24
import com.hardcode.gdbms.engine.data.file.FileDataWare;
25
import com.hardcode.gdbms.engine.values.Value;
26
import com.hardcode.gdbms.engine.values.ValueFactory;
27
import com.hardcode.gdbms.engine.values.ValueWriter;
28

    
29

    
30
/**
31
 * Driver para ficheros csv, en el que la primera fila se toma como la que
32
 * define los nombres de los campos
33
 *
34
 * @author Fernando Gonz?lez Cort?s
35
 */
36
public class CSVStringDriver implements Driver, FileDriver, ValueWriter {
37
    private File file;
38
    private BufferedReader reader;
39
    private ArrayList lineas;
40
    private ValueWriter vWriter = ValueWriter.internalValueWriter;
41
    public static String DEFAULT_SEPARATOR=",";
42
    private String separator=DEFAULT_SEPARATOR;
43

    
44
    /**
45
     * @see com.hardcode.gdbms.driver.Driver#getName()
46
     */
47
    public String getName() {
48
        return "csv string";
49
    }
50

    
51
    /**
52
     * @see com.hardcode.gdbms.data.DataSource#getFieldName(int)
53
     */
54
    public String getFieldName(int fieldId) throws ReadDriverException {
55
        String[] campos = (String[]) lineas.get(0);
56

    
57
        return campos[fieldId];
58
    }
59

    
60
    /**
61
     * @see com.hardcode.gdbms.data.DataSource#getIntFieldValue(int, int)
62
     */
63
    public Value getFieldValue(long rowIndex, int fieldId)
64
        throws ReadDriverException {
65
        String[] campos = (String[]) lineas.get((int) (rowIndex + 1));
66
        if (campos.length<fieldId+1 || campos[fieldId].equals("null")) {
67
            return null;
68
        }
69

    
70
        Value value = ValueFactory.createValue(campos[fieldId]);
71

    
72
        return value;
73
    }
74

    
75
    /**
76
     * @see com.hardcode.gdbms.data.DataSource#getFieldCount()
77
     */
78
    public int getFieldCount() throws ReadDriverException {
79
        String[] campos = (String[]) lineas.get(0);
80

    
81
        return campos.length;
82
    }
83

    
84
    /**
85
     * @see com.hardcode.gdbms.data.DataSource#open(java.io.File)
86
     */
87
    public void open(File file) throws OpenDriverException {
88
        this.file = file;
89
        try {
90
                        reader = new BufferedReader(new FileReader(file));
91
                } catch (FileNotFoundException e) {
92
                        throw new FileNotFoundDriverException(getName(),e,file.getAbsolutePath());
93
                }
94

    
95
        lineas = new ArrayList();
96

    
97
        String aux;
98

    
99
        int colCount;
100
        String[] campos;
101
        try {
102
                        if ((aux = reader.readLine()) != null) {
103
                                campos = aux.split(DEFAULT_SEPARATOR);
104
                            lineas.add(campos);
105
                                colCount = campos.length;
106
                                while ((aux = reader.readLine()) != null) {
107
                                        campos = aux.split(DEFAULT_SEPARATOR);
108
                                        if (campos.length < colCount) {
109
                                                for (int i=campos.length;i < colCount;i++){
110
                                                        aux = aux +" ;";
111
                                                }
112
                                                campos = aux.split(DEFAULT_SEPARATOR);
113
                                        }
114
                                    lineas.add(campos);
115
                                }
116
                        }
117
                } catch (IOException e) {
118
                        throw new OpenDriverException(getName(),e);
119
                }
120
    }
121

    
122
    /**
123
     * @see com.hardcode.gdbms.data.DataSource#close()
124
     */
125
    public void close() throws CloseDriverException {
126
        try {
127
                        reader.close();
128
                } catch (IOException e) {
129
                        throw new CloseDriverException(getName(),e);
130
                }
131
    }
132

    
133
    /**
134
     * @see com.hardcode.gdbms.data.DataSource#getRowCount()
135
     */
136
    public long getRowCount() throws ReadDriverException {
137
        return lineas.size() - 1;
138
    }
139

    
140
    /**
141
     * @see com.hardcode.gdbms.engine.data.driver.FileDriver#fileAccepted(java.io.File)
142
     */
143
    public boolean fileAccepted(File f) {
144
        return f.getAbsolutePath().toUpperCase().endsWith("CSV");
145
    }
146

    
147
    /**
148
     * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldType(int)
149
     */
150
    public int getFieldType(int i) throws ReadDriverException {
151
        return Types.VARCHAR;
152
    }
153

    
154
    /**
155
     * @throws ReadDriverException
156
     * @see com.hardcode.gdbms.engine.data.driver.FileDriver#writeFile(com.hardcode.gdbms.engine.data.edition.DataWare,
157
     *      java.io.File)
158
     */
159
    public void writeFile(FileDataWare dataWare)
160
        throws WriteDriverException, ReadDriverException {
161
        PrintWriter out;
162

    
163
        try {
164
            out = new PrintWriter(new FileOutputStream(file));
165

    
166
            String fieldRow = dataWare.getFieldName(0);
167

    
168
            for (int i = 1; i < (dataWare.getFieldCount() - 1); i++) {
169
                fieldRow += (DEFAULT_SEPARATOR + dataWare.getFieldName(i));
170
            }
171

    
172
            out.println(fieldRow);
173

    
174
            for (int i = 0; i < dataWare.getRowCount(); i++) {
175
                String row = dataWare.getFieldValue(i, 0).getStringValue(this);
176

    
177
                for (int j = 1; j < (dataWare.getFieldCount() - 1); j++) {
178
                    row += (DEFAULT_SEPARATOR +
179
                    dataWare.getFieldValue(i, j).getStringValue(this));
180
                }
181

    
182
                out.println(row);
183
            }
184

    
185
            out.close();
186
        } catch (FileNotFoundException e) {
187
            throw new FileNotFoundDriverException(getName(),e,file.getAbsolutePath());
188
        }
189
    }
190

    
191
    public void createSource(String path, String[] fieldNames, int[] fieldTypes) throws ReadDriverException {
192
        File file = new File(path);
193

    
194
        file.getParentFile().mkdirs();
195
        try {
196
                        file.createNewFile();
197
                } catch (IOException e) {
198
                        throw new ReadDriverException(getName(),e);
199
                }
200

    
201
        PrintWriter out;
202
                try {
203
                        out = new PrintWriter(new FileOutputStream(file));
204
                } catch (FileNotFoundException e) {
205
                        throw new FileNotFoundDriverException(getName(),e,file.getAbsolutePath());
206
                }
207

    
208
        String header = fieldNames[0];
209
        for (int i = 1; i < fieldNames.length; i++) {
210
            header += DEFAULT_SEPARATOR + fieldNames[i];
211
        }
212
        out.println(header);
213

    
214
        out.close();
215
    }
216

    
217
    /**
218
     * DOCUMENT ME!
219
     *
220
     * @return DOCUMENT ME!
221
     */
222
    public String getNullStatementString() {
223
        return vWriter.getNullStatementString();
224
    }
225

    
226
    /**
227
     * DOCUMENT ME!
228
     *
229
     * @param b DOCUMENT ME!
230
     *
231
     * @return DOCUMENT ME!
232
     */
233
    public String getStatementString(boolean b) {
234
        return vWriter.getStatementString(b);
235
    }
236

    
237
    /**
238
     * DOCUMENT ME!
239
     *
240
     * @param binary DOCUMENT ME!
241
     *
242
     * @return DOCUMENT ME!
243
     */
244
    public String getStatementString(byte[] binary) {
245
        return vWriter.getStatementString(binary);
246
    }
247

    
248
    /**
249
     * DOCUMENT ME!
250
     *
251
     * @param d DOCUMENT ME!
252
     *
253
     * @return DOCUMENT ME!
254
     */
255
    public String getStatementString(Date d) {
256
        return d.toString();
257
    }
258

    
259
    /**
260
     * DOCUMENT ME!
261
     *
262
     * @param d DOCUMENT ME!
263
     * @param sqlType DOCUMENT ME!
264
     *
265
     * @return DOCUMENT ME!
266
     */
267
    public String getStatementString(double d, int sqlType) {
268
        return vWriter.getStatementString(d, sqlType);
269
    }
270

    
271
    /**
272
     * DOCUMENT ME!
273
     *
274
     * @param i DOCUMENT ME!
275
     * @param sqlType DOCUMENT ME!
276
     *
277
     * @return DOCUMENT ME!
278
     */
279
    public String getStatementString(int i, int sqlType) {
280
        return vWriter.getStatementString(i, sqlType);
281
    }
282

    
283
    /**
284
     * DOCUMENT ME!
285
     *
286
     * @param i DOCUMENT ME!
287
     *
288
     * @return DOCUMENT ME!
289
     */
290
    public String getStatementString(long i) {
291
        return vWriter.getStatementString(i);
292
    }
293

    
294
    /**
295
     * DOCUMENT ME!
296
     *
297
     * @param str DOCUMENT ME!
298
     * @param sqlType DOCUMENT ME!
299
     *
300
     * @return DOCUMENT ME!
301
     */
302
    public String getStatementString(String str, int sqlType) {
303
        return str;
304
    }
305

    
306
    /**
307
     * DOCUMENT ME!
308
     *
309
     * @param t DOCUMENT ME!
310
     *
311
     * @return DOCUMENT ME!
312
     */
313
    public String getStatementString(Time t) {
314
        return t.toString();
315
    }
316

    
317
    /**
318
     * DOCUMENT ME!
319
     *
320
     * @param ts DOCUMENT ME!
321
     *
322
     * @return DOCUMENT ME!
323
     */
324
    public String getStatementString(Timestamp ts) {
325
        return ts.toString();
326
    }
327

    
328
    public void setDataSourceFactory(DataSourceFactory dsf) {
329
    }
330

    
331
        public int getFieldWidth(int i) throws ReadDriverException {
332
                return 30;
333
        }
334

    
335
        public String getSeparator() {
336
                return separator;
337
        }
338

    
339
        public void setSeparator(String separator) {
340
                this.separator = separator;
341
                DEFAULT_SEPARATOR=separator;
342
        }
343
}