root / trunk / libraries / libGDBMS / src / com / hardcode / gdbms / driver / csv / CSVDriver.java @ 466
History | View | Annotate | Download (2.72 KB)
1 |
package com.hardcode.gdbms.driver.csv; |
---|---|
2 |
|
3 |
import java.io.BufferedReader; |
4 |
import java.io.File; |
5 |
import java.io.FileReader; |
6 |
import java.io.IOException; |
7 |
import java.util.ArrayList; |
8 |
|
9 |
import com.hardcode.driverManager.Driver; |
10 |
import com.hardcode.gdbms.engine.data.DriverException; |
11 |
import com.hardcode.gdbms.engine.data.FileDriver; |
12 |
import com.hardcode.gdbms.engine.values.IntValue; |
13 |
import com.hardcode.gdbms.engine.values.StringValue; |
14 |
import com.hardcode.gdbms.engine.values.Value; |
15 |
|
16 |
|
17 |
/**
|
18 |
* Driver para ficheros csv, en el que la primera fila se toma como la que
|
19 |
* define los nombres de los campos
|
20 |
*
|
21 |
* @author Fernando Gonz?lez Cort?s
|
22 |
*/
|
23 |
public class CSVDriver implements Driver, FileDriver { |
24 |
private BufferedReader reader; |
25 |
private ArrayList lineas; |
26 |
|
27 |
/**
|
28 |
* @see com.hardcode.gdbms.driver.Driver#getName()
|
29 |
*/
|
30 |
public String getName() { |
31 |
return "csv"; |
32 |
} |
33 |
|
34 |
/**
|
35 |
* @see com.hardcode.gdbms.data.DataSource#getFieldName(int)
|
36 |
*/
|
37 |
public String getFieldName(int fieldId) throws DriverException { |
38 |
String[] campos = (String[]) lineas.get(0); |
39 |
|
40 |
return campos[fieldId];
|
41 |
} |
42 |
|
43 |
/**
|
44 |
* @see com.hardcode.gdbms.data.DataSource#getIntFieldValue(int, int)
|
45 |
*/
|
46 |
public Value getFieldValue(long rowIndex, int fieldId) |
47 |
throws DriverException {
|
48 |
String[] campos = (String[]) lineas.get((int) (rowIndex + 1)); |
49 |
|
50 |
if (fieldId == 0) { |
51 |
IntValue value = new IntValue();
|
52 |
value.setValue(Integer.parseInt(campos[fieldId]));
|
53 |
|
54 |
return value;
|
55 |
} |
56 |
|
57 |
StringValue value = new StringValue();
|
58 |
value.setValue(campos[fieldId]); |
59 |
|
60 |
return value;
|
61 |
} |
62 |
|
63 |
/**
|
64 |
* @see com.hardcode.gdbms.data.DataSource#getFieldCount()
|
65 |
*/
|
66 |
public int getFieldCount() throws DriverException { |
67 |
String[] campos = (String[]) lineas.get(0); |
68 |
|
69 |
return campos.length;
|
70 |
} |
71 |
|
72 |
/**
|
73 |
* @see com.hardcode.gdbms.data.DataSource#open(java.io.File)
|
74 |
*/
|
75 |
public void open(File file) throws IOException { |
76 |
reader = new BufferedReader(new FileReader(file)); |
77 |
|
78 |
lineas = new ArrayList(); |
79 |
|
80 |
String aux;
|
81 |
|
82 |
while ((aux = reader.readLine()) != null) { |
83 |
String[] campos = aux.split(";"); |
84 |
lineas.add(campos); |
85 |
} |
86 |
} |
87 |
|
88 |
/**
|
89 |
* @see com.hardcode.gdbms.data.DataSource#close()
|
90 |
*/
|
91 |
public void close() throws IOException { |
92 |
reader.close(); |
93 |
} |
94 |
|
95 |
/**
|
96 |
* @see com.hardcode.gdbms.data.DataSource#getRowCount()
|
97 |
*/
|
98 |
public long getRowCount() { |
99 |
return lineas.size() - 1; |
100 |
} |
101 |
|
102 |
/**
|
103 |
* @see com.hardcode.gdbms.engine.data.FileDriver#fileAccepted(java.io.File)
|
104 |
*/
|
105 |
public boolean fileAccepted(File f) { |
106 |
return f.getAbsolutePath().toUpperCase().endsWith("CSV"); |
107 |
} |
108 |
} |