svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / dbf / DBFDriver.java @ 3300
History | View | Annotate | Download (9.12 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 |
|
15 |
import com.hardcode.gdbms.driver.DriverUtilities; |
16 |
import com.hardcode.gdbms.engine.data.DataSourceFactory; |
17 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
18 |
import com.hardcode.gdbms.engine.data.driver.FileDriver; |
19 |
import com.hardcode.gdbms.engine.data.edition.DataWare; |
20 |
import com.hardcode.gdbms.engine.data.file.FileDataWare; |
21 |
import com.hardcode.gdbms.engine.values.Value; |
22 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
23 |
import com.iver.cit.gvsig.fmap.drivers.shp.DbaseFileHeaderNIO; |
24 |
import com.iver.cit.gvsig.fmap.drivers.shp.DbaseFileWriterNIO; |
25 |
|
26 |
|
27 |
/**
|
28 |
* DOCUMENT ME!
|
29 |
*
|
30 |
* @author Fernando Gonz?lez Cort?s
|
31 |
*/
|
32 |
public class DBFDriver implements FileDriver { |
33 |
//private File file;
|
34 |
private static Locale ukLocale = new Locale("en", "UK"); // English, UK version |
35 |
private DbaseFile dbf = new DbaseFile(); |
36 |
private char[] fieldTypes; |
37 |
private DataSourceFactory dsf;
|
38 |
|
39 |
|
40 |
/**
|
41 |
* @see com.hardcode.driverManager.Driver#getName()
|
42 |
*/
|
43 |
public String getName() { |
44 |
return "gdbms dbf driver"; |
45 |
} |
46 |
|
47 |
/**
|
48 |
* @see com.hardcode.gdbms.engine.data.GDBMSDriver#open(java.io.File)
|
49 |
*/
|
50 |
public void open(File file) throws IOException { |
51 |
dbf.open(file); |
52 |
|
53 |
try {
|
54 |
fieldTypes = new char[getFieldCount()]; |
55 |
|
56 |
for (int i = 0; i < fieldTypes.length; i++) { |
57 |
fieldTypes[i] = dbf.getFieldType(i); |
58 |
} |
59 |
} catch (DriverException e) {
|
60 |
throw new IOException(e.getMessage()); |
61 |
} |
62 |
} |
63 |
|
64 |
/**
|
65 |
* @see com.hardcode.gdbms.engine.data.GDBMSDriver#close()
|
66 |
*/
|
67 |
public void close() throws IOException { |
68 |
dbf.close(); |
69 |
} |
70 |
|
71 |
/**
|
72 |
* @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldValue(long,
|
73 |
* int)
|
74 |
*/
|
75 |
public Value getFieldValue(long rowIndex, int fieldId) |
76 |
throws DriverException {
|
77 |
// Field Type (C or M)
|
78 |
char fieldType = fieldTypes[fieldId];
|
79 |
|
80 |
if (fieldType == 'L') { |
81 |
return ValueFactory.createValue(dbf.getBooleanFieldValue(
|
82 |
(int) rowIndex, fieldId));
|
83 |
|
84 |
/* }else if (fieldType == 'N'){
|
85 |
String strValue = dbf.getStringFieldValue(rowIndex, fieldId);
|
86 |
long value = Long.parseLong(strValue);
|
87 |
if ((value > Integer.MIN_VALUE) && (value < Integer.MAX_VALUE)){
|
88 |
return new IntValue((int) value);
|
89 |
}else{
|
90 |
return new LongValue(value);
|
91 |
}
|
92 |
*/
|
93 |
} else if ((fieldType == 'F') || (fieldType == 'N')) { |
94 |
String strValue = dbf.getStringFieldValue((int) rowIndex, fieldId) |
95 |
.trim(); |
96 |
|
97 |
if (strValue.length() == 0) { |
98 |
return null; |
99 |
} |
100 |
double value=0; |
101 |
try{
|
102 |
value = Double.parseDouble(strValue);
|
103 |
}catch (Exception e) { |
104 |
return ValueFactory.createValue(0D); |
105 |
} |
106 |
return ValueFactory.createValue(value);
|
107 |
} else if (fieldType == 'C') { |
108 |
return ValueFactory.createValue(dbf.getStringFieldValue(
|
109 |
(int) rowIndex, fieldId).trim());
|
110 |
} else if (fieldType == 'D') { |
111 |
String date = dbf.getStringFieldValue((int) rowIndex, fieldId).trim(); |
112 |
// System.out.println(rowIndex + " data=" + date);
|
113 |
if (date.length() == 0) { |
114 |
return null; |
115 |
} |
116 |
|
117 |
String year = date.substring(0, 4); |
118 |
String month = date.substring(4, 6); |
119 |
String day = date.substring(6, 8); |
120 |
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, ukLocale); |
121 |
/* Calendar c = Calendar.getInstance();
|
122 |
c.clear();
|
123 |
c.set(Integer.parseInt(year), Integer.parseInt(month),
|
124 |
Integer.parseInt(day));
|
125 |
c.set(Calendar.MILLISECOND, 0); */
|
126 |
String strAux = month + "/" + day + "/" + year; |
127 |
Date dat;
|
128 |
try {
|
129 |
dat = df.parse(strAux); |
130 |
} catch (ParseException e) { |
131 |
throw new DriverException("Bad Date Format"); |
132 |
} |
133 |
|
134 |
// System.out.println("numReg = " + rowIndex + " date:" + dat.getTime());
|
135 |
|
136 |
return ValueFactory.createValue(dat);
|
137 |
} else {
|
138 |
throw new DriverException("Unknown field type"); |
139 |
} |
140 |
} |
141 |
|
142 |
/**
|
143 |
* @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldCount()
|
144 |
*/
|
145 |
public int getFieldCount() throws DriverException { |
146 |
return dbf.getFieldCount();
|
147 |
} |
148 |
|
149 |
/**
|
150 |
* @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldName(int)
|
151 |
*/
|
152 |
public String getFieldName(int fieldId) throws DriverException { |
153 |
return dbf.getFieldName(fieldId);
|
154 |
} |
155 |
|
156 |
/**
|
157 |
* @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getRowCount()
|
158 |
*/
|
159 |
public long getRowCount() throws DriverException { |
160 |
return dbf.getRecordCount();
|
161 |
} |
162 |
|
163 |
/**
|
164 |
* @see com.hardcode.gdbms.engine.data.driver.FileDriver#fileAccepted(java.io.File)
|
165 |
*/
|
166 |
public boolean fileAccepted(File f) { |
167 |
return f.getAbsolutePath().toUpperCase().endsWith("DBF"); |
168 |
} |
169 |
|
170 |
/**
|
171 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldType(int)
|
172 |
*/
|
173 |
public int getFieldType(int i) throws DriverException { |
174 |
char fieldType = fieldTypes[i];
|
175 |
|
176 |
if (fieldType == 'L') { |
177 |
return Types.BOOLEAN; |
178 |
} else if ((fieldType == 'F') || (fieldType == 'N')) { |
179 |
return Types.DOUBLE; |
180 |
} else if (fieldType == 'C') { |
181 |
return Types.VARCHAR; |
182 |
} else if (fieldType == 'D') { |
183 |
return Types.DATE; |
184 |
} else {
|
185 |
throw new DriverException("Unknown field type"); |
186 |
} |
187 |
} |
188 |
|
189 |
/**
|
190 |
* @see com.hardcode.gdbms.engine.data.driver.DriverCommons#setDataSourceFactory(com.hardcode.gdbms.engine.data.DataSourceFactory)
|
191 |
*/
|
192 |
public void setDataSourceFactory(DataSourceFactory dsf) { |
193 |
this.dsf = dsf;
|
194 |
} |
195 |
|
196 |
private void writeToTemp(DataWare dataWare, File file) throws DriverException { |
197 |
DbaseFileWriterNIO dbfWrite = null;
|
198 |
DbaseFileHeaderNIO myHeader; |
199 |
Value[] record;
|
200 |
|
201 |
try {
|
202 |
myHeader = DbaseFileHeaderNIO.createDbaseHeader(dataWare); |
203 |
|
204 |
myHeader.setNumRecords((int) dataWare.getRowCount());
|
205 |
dbfWrite = new DbaseFileWriterNIO(myHeader,
|
206 |
(FileChannel) getWriteChannel(file.getPath()));
|
207 |
record = new Value[dataWare.getFieldCount()];
|
208 |
|
209 |
for (int j = 0; j < dataWare.getRowCount(); j++) { |
210 |
for (int r = 0; r < dataWare.getFieldCount(); r++) { |
211 |
record[r] = dataWare.getFieldValue(j, r); |
212 |
} |
213 |
|
214 |
dbfWrite.write(record); |
215 |
} |
216 |
|
217 |
dbfWrite.close(); |
218 |
} catch (IOException e) { |
219 |
throw new DriverException(e); |
220 |
} |
221 |
|
222 |
} |
223 |
|
224 |
/**
|
225 |
* @see com.hardcode.gdbms.engine.data.driver.FileDriver#writeFile(com.hardcode.gdbms.engine.data.file.FileDataWare,
|
226 |
* java.io.File)
|
227 |
*/
|
228 |
public void writeFile(FileDataWare dataWare) |
229 |
throws DriverException {
|
230 |
|
231 |
String temp = dsf.getTempFile();
|
232 |
|
233 |
writeToTemp(dataWare, new File(temp)); |
234 |
|
235 |
try {
|
236 |
FileChannel fcout = dbf.getWriteChannel();
|
237 |
FileChannel fcin = new FileInputStream(temp).getChannel(); |
238 |
|
239 |
DriverUtilities.copy(fcin, fcout); |
240 |
} catch (IOException e) { |
241 |
throw new DriverException(e); |
242 |
} |
243 |
} |
244 |
|
245 |
/**
|
246 |
* DOCUMENT ME!
|
247 |
*
|
248 |
* @param path DOCUMENT ME!
|
249 |
*
|
250 |
* @return DOCUMENT ME!
|
251 |
*
|
252 |
* @throws IOException DOCUMENT ME!
|
253 |
*/
|
254 |
private WritableByteChannel getWriteChannel(String path) |
255 |
throws IOException { |
256 |
WritableByteChannel channel;
|
257 |
|
258 |
File f = new File(path); |
259 |
|
260 |
if (!f.exists()) {
|
261 |
System.out.println("Creando fichero " + f.getAbsolutePath()); |
262 |
|
263 |
if (!f.createNewFile()) {
|
264 |
throw new IOException("Cannot create file " + f); |
265 |
} |
266 |
} |
267 |
|
268 |
RandomAccessFile raf = new RandomAccessFile(f, "rw"); |
269 |
channel = raf.getChannel(); |
270 |
|
271 |
return channel;
|
272 |
} |
273 |
|
274 |
public void createSource(String arg0, String[] arg1, int[] arg2) throws IOException { |
275 |
DbaseFileHeaderNIO myHeader; |
276 |
|
277 |
int[] lengths = new int[arg2.length]; |
278 |
for (int i = 0; i < arg2.length; i++) { |
279 |
lengths[i] = 100;
|
280 |
} |
281 |
myHeader = DbaseFileHeaderNIO.createDbaseHeader(arg1, arg2, lengths); |
282 |
myHeader.setNumRecords(0);
|
283 |
DbaseFileWriterNIO dbfWrite = new DbaseFileWriterNIO(myHeader,
|
284 |
(FileChannel) getWriteChannel(arg0));
|
285 |
dbfWrite = new DbaseFileWriterNIO(myHeader,
|
286 |
(FileChannel) getWriteChannel(arg0));
|
287 |
} |
288 |
} |