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