root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / shp / DbaseFileHeaderNIO.java @ 1132
History | View | Annotate | Download (23.5 KB)
1 |
/*
|
---|---|
2 |
* Geotools - OpenSource mapping toolkit
|
3 |
* (C) 2002, Centre for Computational Geography
|
4 |
*
|
5 |
* This library is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU Lesser General Public
|
7 |
* License as published by the Free Software Foundation;
|
8 |
* version 2.1 of the License.
|
9 |
*
|
10 |
* This library is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13 |
* Lesser General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU Lesser General Public
|
16 |
* License along with this library; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
18 |
*
|
19 |
* This file is based on an origional contained in the GISToolkit project:
|
20 |
* http://gistoolkit.sourceforge.net/
|
21 |
*
|
22 |
*/
|
23 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
24 |
*
|
25 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
26 |
*
|
27 |
* This program is free software; you can redistribute it and/or
|
28 |
* modify it under the terms of the GNU General Public License
|
29 |
* as published by the Free Software Foundation; either version 2
|
30 |
* of the License, or (at your option) any later version.
|
31 |
*
|
32 |
* This program is distributed in the hope that it will be useful,
|
33 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
34 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
35 |
* GNU General Public License for more details.
|
36 |
*
|
37 |
* You should have received a copy of the GNU General Public License
|
38 |
* along with this program; if not, write to the Free Software
|
39 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
40 |
*
|
41 |
* For more information, contact:
|
42 |
*
|
43 |
* Generalitat Valenciana
|
44 |
* Conselleria d'Infraestructures i Transport
|
45 |
* Av. Blasco Ib??ez, 50
|
46 |
* 46010 VALENCIA
|
47 |
* SPAIN
|
48 |
*
|
49 |
* +34 963862235
|
50 |
* gvsig@gva.es
|
51 |
* www.gvsig.gva.es
|
52 |
*
|
53 |
* or
|
54 |
*
|
55 |
* IVER T.I. S.A
|
56 |
* Salamanca 50
|
57 |
* 46005 Valencia
|
58 |
* Spain
|
59 |
*
|
60 |
* +34 963163400
|
61 |
* dac@iver.es
|
62 |
*/
|
63 |
package com.iver.cit.gvsig.fmap.drivers.shp; |
64 |
|
65 |
import com.hardcode.gdbms.engine.data.DriverException; |
66 |
import com.hardcode.gdbms.engine.instruction.SemanticException; |
67 |
import com.hardcode.gdbms.engine.values.Value; |
68 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
69 |
import com.vividsolutions.jts.geom.Geometry; |
70 |
|
71 |
|
72 |
import java.io.EOFException; |
73 |
import java.io.IOException; |
74 |
|
75 |
import java.nio.ByteBuffer; |
76 |
import java.nio.ByteOrder; |
77 |
import java.nio.channels.ReadableByteChannel; |
78 |
import java.nio.channels.WritableByteChannel; |
79 |
|
80 |
import java.util.Calendar; |
81 |
import java.util.Date; |
82 |
import java.util.logging.Level; |
83 |
import java.util.logging.Logger; |
84 |
|
85 |
|
86 |
/**
|
87 |
* Class to represent the header of a Dbase III file. Creation date: (5/15/2001
|
88 |
* 5:15:30 PM)
|
89 |
*/
|
90 |
public class DbaseFileHeaderNIO { |
91 |
// Constant for the size of a record
|
92 |
private static final int FILE_DESCRIPTOR_SIZE = 32; |
93 |
|
94 |
// type of the file, must be 03h
|
95 |
private static final byte MAGIC = 0x03; |
96 |
private static final int MINIMUM_HEADER = 33; |
97 |
|
98 |
// Date the file was last updated.
|
99 |
private Date date = new Date(); |
100 |
private int recordCnt = 0; |
101 |
private int fieldCnt = 0; |
102 |
private int myFileType = 0; |
103 |
|
104 |
// set this to a default length of 1, which is enough for one "space"
|
105 |
// character which signifies an empty record
|
106 |
private int recordLength = 1; |
107 |
|
108 |
// set this to a flagged value so if no fields are added before the write,
|
109 |
// we know to adjust the headerLength to MINIMUM_HEADER
|
110 |
private int headerLength = -1; |
111 |
private int largestFieldSize = 0; |
112 |
private Logger logger = Logger.getLogger("org.geotools.data.shapefile"); |
113 |
|
114 |
// collection of header records.
|
115 |
// lets start out with a zero-length array, just in case
|
116 |
private DbaseField[] fields = null; // new DbaseField[0]; |
117 |
|
118 |
/**
|
119 |
* Lee del buffer.
|
120 |
*
|
121 |
* @param buffer .
|
122 |
* @param channel .
|
123 |
*
|
124 |
* @throws IOException .
|
125 |
* @throws EOFException .
|
126 |
*/
|
127 |
private void read(ByteBuffer buffer, ReadableByteChannel channel) |
128 |
throws IOException { |
129 |
while (buffer.remaining() > 0) { |
130 |
if (channel.read(buffer) == -1) { |
131 |
throw new EOFException("Premature end of file"); |
132 |
} |
133 |
} |
134 |
} |
135 |
|
136 |
/**
|
137 |
* Determine the most appropriate Java Class for representing the data in
|
138 |
* the field.
|
139 |
* <PRE>
|
140 |
* All packages are java.lang unless otherwise specified.
|
141 |
* C (Character) -> String
|
142 |
* N (Numeric) -> Integer or Double (depends on field's decimal count)
|
143 |
* F (Floating) -> Double
|
144 |
* L (Logical) -> Boolean
|
145 |
* D (Date) -> java.util.Date
|
146 |
* Unknown -> String
|
147 |
* </PRE>
|
148 |
*
|
149 |
* @param i The index of the field, from 0 to <CODE>getNumFields() -
|
150 |
* 1</CODE> .
|
151 |
*
|
152 |
* @return A Class which closely represents the dbase field type.
|
153 |
*/
|
154 |
public Class getFieldClass(int i) { |
155 |
Class typeClass = null; |
156 |
|
157 |
switch (fields[i].fieldType) {
|
158 |
case 'C': |
159 |
typeClass = String.class;
|
160 |
|
161 |
break;
|
162 |
|
163 |
case 'N': |
164 |
|
165 |
if (fields[i].decimalCount == 0) { |
166 |
typeClass = Integer.class;
|
167 |
} else {
|
168 |
typeClass = Double.class;
|
169 |
} |
170 |
|
171 |
break;
|
172 |
|
173 |
case 'F': |
174 |
typeClass = Double.class;
|
175 |
|
176 |
break;
|
177 |
|
178 |
case 'L': |
179 |
typeClass = Boolean.class;
|
180 |
|
181 |
break;
|
182 |
|
183 |
case 'D': |
184 |
typeClass = Date.class;
|
185 |
|
186 |
break;
|
187 |
|
188 |
default:
|
189 |
typeClass = String.class;
|
190 |
|
191 |
break;
|
192 |
} |
193 |
|
194 |
return typeClass;
|
195 |
} |
196 |
|
197 |
/**
|
198 |
* Add a column to this DbaseFileHeader. The type is one of (C N L or D)
|
199 |
* character, number, logical(true/false), or date. The Field length is
|
200 |
* the total length in bytes reserved for this column. The decimal count
|
201 |
* only applies to numbers(N), and floating point values (F), and refers
|
202 |
* to the number of characters to reserve after the decimal point.
|
203 |
* <B>Don't expect miracles from this...</B>
|
204 |
* <PRE>
|
205 |
* Field Type MaxLength
|
206 |
* ---------- ---------
|
207 |
* C 254
|
208 |
* D 8
|
209 |
* F 20
|
210 |
* N 18
|
211 |
* </PRE>
|
212 |
*
|
213 |
* @param inFieldName The name of the new field, must be less than 10
|
214 |
* characters or it gets truncated.
|
215 |
* @param inFieldType A character representing the dBase field, ( see above
|
216 |
* ). Case insensitive.
|
217 |
* @param inFieldLength The length of the field, in bytes ( see above )
|
218 |
* @param inDecimalCount For numeric fields, the number of decimal places
|
219 |
* to track.
|
220 |
*/
|
221 |
public void addColumn(String inFieldName, char inFieldType, |
222 |
int inFieldLength, int inDecimalCount) { |
223 |
/*if (inFieldLength <=0) {
|
224 |
throw new DbaseFileException("field length <= 0");
|
225 |
}
|
226 |
*/
|
227 |
if (fields == null) { |
228 |
fields = new DbaseField[0]; |
229 |
} |
230 |
|
231 |
int tempLength = 1; // the length is used for the offset, and there is a * for deleted as the first byte |
232 |
DbaseField[] tempFieldDescriptors = new DbaseField[fields.length + 1]; |
233 |
|
234 |
for (int i = 0; i < fields.length; i++) { |
235 |
fields[i].fieldDataAddress = tempLength; |
236 |
tempLength = tempLength + fields[i].fieldLength; |
237 |
tempFieldDescriptors[i] = fields[i]; |
238 |
} |
239 |
|
240 |
tempFieldDescriptors[fields.length] = new DbaseField();
|
241 |
tempFieldDescriptors[fields.length].fieldLength = inFieldLength; |
242 |
tempFieldDescriptors[fields.length].decimalCount = inDecimalCount; |
243 |
tempFieldDescriptors[fields.length].fieldDataAddress = tempLength; |
244 |
|
245 |
// set the field name
|
246 |
String tempFieldName = inFieldName;
|
247 |
|
248 |
if (tempFieldName == null) { |
249 |
tempFieldName = "NoName";
|
250 |
} |
251 |
|
252 |
// Fix for GEOT-42, ArcExplorer will not handle field names > 10 chars
|
253 |
// Sorry folks.
|
254 |
if (tempFieldName.length() > 10) { |
255 |
tempFieldName = tempFieldName.substring(0, 10); |
256 |
warn("FieldName " + inFieldName +
|
257 |
" is longer than 10 characters, truncating to " +
|
258 |
tempFieldName); |
259 |
} |
260 |
|
261 |
tempFieldDescriptors[fields.length].fieldName = tempFieldName; |
262 |
|
263 |
// the field type
|
264 |
if ((inFieldType == 'C') || (inFieldType == 'c')) { |
265 |
tempFieldDescriptors[fields.length].fieldType = 'C';
|
266 |
|
267 |
if (inFieldLength > 254) { |
268 |
warn("Field Length for " + inFieldName + " set to " + |
269 |
inFieldLength + |
270 |
" Which is longer than 254, not consistent with dbase III");
|
271 |
} |
272 |
} else if ((inFieldType == 'S') || (inFieldType == 's')) { |
273 |
tempFieldDescriptors[fields.length].fieldType = 'C';
|
274 |
warn("Field type for " + inFieldName +
|
275 |
" set to S which is flat out wrong people!, I am setting this to C, in the hopes you meant character.");
|
276 |
|
277 |
if (inFieldLength > 254) { |
278 |
warn("Field Length for " + inFieldName + " set to " + |
279 |
inFieldLength + |
280 |
" Which is longer than 254, not consistent with dbase III");
|
281 |
} |
282 |
|
283 |
tempFieldDescriptors[fields.length].fieldLength = 8;
|
284 |
} else if ((inFieldType == 'D') || (inFieldType == 'd')) { |
285 |
tempFieldDescriptors[fields.length].fieldType = 'D';
|
286 |
|
287 |
if (inFieldLength != 8) { |
288 |
warn("Field Length for " + inFieldName + " set to " + |
289 |
inFieldLength + " Setting to 8 digets YYYYMMDD");
|
290 |
} |
291 |
|
292 |
tempFieldDescriptors[fields.length].fieldLength = 8;
|
293 |
} else if ((inFieldType == 'F') || (inFieldType == 'f')) { |
294 |
tempFieldDescriptors[fields.length].fieldType = 'F';
|
295 |
|
296 |
if (inFieldLength > 20) { |
297 |
warn("Field Length for " + inFieldName + " set to " + |
298 |
inFieldLength + |
299 |
" Preserving length, but should be set to Max of 20 not valid for dbase IV, and UP specification, not present in dbaseIII.");
|
300 |
} |
301 |
} else if ((inFieldType == 'N') || (inFieldType == 'n')) { |
302 |
tempFieldDescriptors[fields.length].fieldType = 'N';
|
303 |
|
304 |
if (inFieldLength > 18) { |
305 |
warn("Field Length for " + inFieldName + " set to " + |
306 |
inFieldLength + |
307 |
" Preserving length, but should be set to Max of 18 for dbase III specification.");
|
308 |
} |
309 |
|
310 |
if (inDecimalCount < 0) { |
311 |
warn("Field Decimal Position for " + inFieldName + " set to " + |
312 |
inDecimalCount + |
313 |
" Setting to 0 no decimal data will be saved.");
|
314 |
tempFieldDescriptors[fields.length].decimalCount = 0;
|
315 |
} |
316 |
|
317 |
if (inDecimalCount > (inFieldLength - 1)) { |
318 |
warn("Field Decimal Position for " + inFieldName + " set to " + |
319 |
inDecimalCount + " Setting to " + (inFieldLength - 1) + |
320 |
" no non decimal data will be saved.");
|
321 |
tempFieldDescriptors[fields.length].decimalCount = inFieldLength - |
322 |
1;
|
323 |
} |
324 |
} else if ((inFieldType == 'L') || (inFieldType == 'l')) { |
325 |
tempFieldDescriptors[fields.length].fieldType = 'L';
|
326 |
|
327 |
if (inFieldLength != 1) { |
328 |
warn("Field Length for " + inFieldName + " set to " + |
329 |
inFieldLength + |
330 |
" Setting to length of 1 for logical fields.");
|
331 |
} |
332 |
|
333 |
tempFieldDescriptors[fields.length].fieldLength = 1;
|
334 |
} else {
|
335 |
//throw new DbaseFileException("Undefined field type "+inFieldType + " For column "+inFieldName);
|
336 |
} |
337 |
|
338 |
// the length of a record
|
339 |
tempLength = tempLength + |
340 |
tempFieldDescriptors[fields.length].fieldLength; |
341 |
|
342 |
// set the new fields.
|
343 |
fields = tempFieldDescriptors; |
344 |
fieldCnt = fields.length; |
345 |
headerLength = MINIMUM_HEADER + (32 * fields.length);
|
346 |
recordLength = tempLength; |
347 |
} |
348 |
|
349 |
/**
|
350 |
* Remove a column from this DbaseFileHeader.
|
351 |
*
|
352 |
* @param inFieldName The name of the field, will ignore case and trim.
|
353 |
*
|
354 |
* @return index of the removed column, -1 if no found
|
355 |
*
|
356 |
* @todo This is really ugly, don't know who wrote it, but it needs
|
357 |
* fixin...
|
358 |
*/
|
359 |
public int removeColumn(String inFieldName) { |
360 |
int retCol = -1; |
361 |
int tempLength = 1; |
362 |
DbaseField[] tempFieldDescriptors = new DbaseField[fields.length - 1]; |
363 |
|
364 |
for (int i = 0, j = 0; i < fields.length; i++) { |
365 |
if (!inFieldName.equalsIgnoreCase(fields[i].fieldName.trim())) {
|
366 |
// if this is the last field and we still haven't found the
|
367 |
// named field
|
368 |
if ((i == j) && (i == (fields.length - 1))) { |
369 |
System.err.println("Could not find a field named '" + |
370 |
inFieldName + "' for removal");
|
371 |
|
372 |
return retCol;
|
373 |
} |
374 |
|
375 |
tempFieldDescriptors[j] = fields[i]; |
376 |
tempFieldDescriptors[j].fieldDataAddress = tempLength; |
377 |
tempLength += tempFieldDescriptors[j].fieldLength; |
378 |
|
379 |
// only increment j on non-matching fields
|
380 |
j++; |
381 |
} else {
|
382 |
retCol = i; |
383 |
} |
384 |
} |
385 |
|
386 |
// set the new fields.
|
387 |
fields = tempFieldDescriptors; |
388 |
headerLength = 33 + (32 * fields.length); |
389 |
recordLength = tempLength; |
390 |
|
391 |
return retCol;
|
392 |
} |
393 |
|
394 |
/**
|
395 |
* DOCUMENT ME!
|
396 |
*
|
397 |
* @param inWarn DOCUMENT ME!
|
398 |
*
|
399 |
* @todo addProgessListener handling
|
400 |
*/
|
401 |
private void warn(String inWarn) { |
402 |
if (logger.isLoggable(Level.WARNING)) { |
403 |
logger.warning(inWarn); |
404 |
} |
405 |
} |
406 |
|
407 |
// Retrieve the length of the field at the given index
|
408 |
|
409 |
/**
|
410 |
* Returns the field length in bytes.
|
411 |
*
|
412 |
* @param inIndex The field index.
|
413 |
*
|
414 |
* @return The length in bytes.
|
415 |
*/
|
416 |
public int getFieldLength(int inIndex) { |
417 |
return fields[inIndex].fieldLength;
|
418 |
} |
419 |
|
420 |
// Retrieve the location of the decimal point within the field.
|
421 |
|
422 |
/**
|
423 |
* Get the decimal count of this field.
|
424 |
*
|
425 |
* @param inIndex The field index.
|
426 |
*
|
427 |
* @return The decimal count.
|
428 |
*/
|
429 |
public int getFieldDecimalCount(int inIndex) { |
430 |
return fields[inIndex].decimalCount;
|
431 |
} |
432 |
|
433 |
// Retrieve the Name of the field at the given index
|
434 |
|
435 |
/**
|
436 |
* Get the field name.
|
437 |
*
|
438 |
* @param inIndex The field index.
|
439 |
*
|
440 |
* @return The name of the field.
|
441 |
*/
|
442 |
public String getFieldName(int inIndex) { |
443 |
return fields[inIndex].fieldName;
|
444 |
} |
445 |
|
446 |
// Retrieve the type of field at the given index
|
447 |
|
448 |
/**
|
449 |
* Get the character class of the field.
|
450 |
*
|
451 |
* @param inIndex The field index.
|
452 |
*
|
453 |
* @return The dbase character representing this field.
|
454 |
*/
|
455 |
public char getFieldType(int inIndex) { |
456 |
return fields[inIndex].fieldType;
|
457 |
} |
458 |
|
459 |
/**
|
460 |
* Get the date this file was last updated.
|
461 |
*
|
462 |
* @return The Date last modified.
|
463 |
*/
|
464 |
public Date getLastUpdateDate() { |
465 |
return date;
|
466 |
} |
467 |
|
468 |
/**
|
469 |
* Return the number of fields in the records.
|
470 |
*
|
471 |
* @return The number of fields in this table.
|
472 |
*/
|
473 |
public int getNumFields() { |
474 |
return fields.length;
|
475 |
} |
476 |
|
477 |
/**
|
478 |
* Return the number of records in the file
|
479 |
*
|
480 |
* @return The number of records in this table.
|
481 |
*/
|
482 |
public int getNumRecords() { |
483 |
return recordCnt;
|
484 |
} |
485 |
|
486 |
/**
|
487 |
* Get the length of the records in bytes.
|
488 |
*
|
489 |
* @return The number of bytes per record.
|
490 |
*/
|
491 |
public int getRecordLength() { |
492 |
return recordLength;
|
493 |
} |
494 |
|
495 |
/**
|
496 |
* Get the length of the header
|
497 |
*
|
498 |
* @return The length of the header in bytes.
|
499 |
*/
|
500 |
public int getHeaderLength() { |
501 |
return headerLength;
|
502 |
} |
503 |
|
504 |
/**
|
505 |
* Read the header data from the DBF file.
|
506 |
*
|
507 |
* @param in DOCUMENT ME!
|
508 |
*
|
509 |
* @throws IOException DOCUMENT ME!
|
510 |
*/
|
511 |
public void readHeader(ByteBuffer in) throws IOException { |
512 |
// type of file.
|
513 |
myFileType = in.get(); |
514 |
|
515 |
if (myFileType != 0x03) { |
516 |
throw new IOException("Unsupported DBF file Type " + |
517 |
Integer.toHexString(myFileType));
|
518 |
} |
519 |
|
520 |
// parse the update date information.
|
521 |
int tempUpdateYear = (int) in.get(); |
522 |
int tempUpdateMonth = (int) in.get(); |
523 |
int tempUpdateDay = (int) in.get(); |
524 |
tempUpdateYear = tempUpdateYear + 1900;
|
525 |
|
526 |
Calendar c = Calendar.getInstance(); |
527 |
c.set(Calendar.YEAR, tempUpdateYear);
|
528 |
c.set(Calendar.MONTH, tempUpdateMonth - 1); |
529 |
c.set(Calendar.DATE, tempUpdateDay);
|
530 |
date = c.getTime(); |
531 |
|
532 |
// read the number of records.
|
533 |
in.order(ByteOrder.LITTLE_ENDIAN);
|
534 |
recordCnt = in.getInt(); |
535 |
|
536 |
// read the length of the header structure.
|
537 |
headerLength = in.getShort(); |
538 |
|
539 |
// read the length of a record
|
540 |
recordLength = in.getShort(); |
541 |
|
542 |
in.order(ByteOrder.BIG_ENDIAN);
|
543 |
|
544 |
// skip the reserved bytes in the header.
|
545 |
in.position(in.position() + 20);
|
546 |
|
547 |
// calculate the number of Fields in the header
|
548 |
fieldCnt = (headerLength - FILE_DESCRIPTOR_SIZE - 1) / FILE_DESCRIPTOR_SIZE;
|
549 |
|
550 |
// read all of the header records
|
551 |
fields = new DbaseField[fieldCnt];
|
552 |
|
553 |
for (int i = 0; i < fieldCnt; i++) { |
554 |
fields[i] = new DbaseField();
|
555 |
|
556 |
// read the field name
|
557 |
byte[] buffer = new byte[11]; |
558 |
in.get(buffer); |
559 |
fields[i].fieldName = new String(buffer); |
560 |
|
561 |
// read the field type
|
562 |
fields[i].fieldType = (char) in.get();
|
563 |
|
564 |
// read the field data address, offset from the start of the record.
|
565 |
fields[i].fieldDataAddress = in.getInt(); |
566 |
|
567 |
// read the field length in bytes
|
568 |
int tempLength = (int) in.get(); |
569 |
|
570 |
if (tempLength < 0) { |
571 |
tempLength = tempLength + 256;
|
572 |
} |
573 |
|
574 |
fields[i].fieldLength = tempLength; |
575 |
|
576 |
// read the field decimal count in bytes
|
577 |
fields[i].decimalCount = (int) in.get();
|
578 |
|
579 |
// read the reserved bytes.
|
580 |
in.position(in.position() + 14);
|
581 |
} |
582 |
|
583 |
// Last byte is a marker for the end of the field definitions.
|
584 |
in.get(); |
585 |
} |
586 |
|
587 |
/**
|
588 |
* Get the largest field size of this table.
|
589 |
*
|
590 |
* @return The largt field size iiin bytes.
|
591 |
*/
|
592 |
public int getLargestFieldSize() { |
593 |
return largestFieldSize;
|
594 |
} |
595 |
|
596 |
/**
|
597 |
* Set the number of records in the file
|
598 |
*
|
599 |
* @param inNumRecords The number of records.
|
600 |
*/
|
601 |
public void setNumRecords(int inNumRecords) { |
602 |
recordCnt = inNumRecords; |
603 |
} |
604 |
|
605 |
/**
|
606 |
* Write the header data to the DBF file.
|
607 |
*
|
608 |
* @param out A channel to write to. If you have an OutputStream you can
|
609 |
* obtain the correct channel by using
|
610 |
* java.nio.Channels.newChannel(OutputStream out).
|
611 |
*
|
612 |
* @throws IOException If errors occur.
|
613 |
*/
|
614 |
public void writeHeader(WritableByteChannel out) throws IOException { |
615 |
// take care of the annoying case where no records have been added...
|
616 |
if (headerLength == -1) { |
617 |
headerLength = MINIMUM_HEADER; |
618 |
} |
619 |
|
620 |
ByteBuffer buffer = ByteBuffer.allocateDirect(headerLength); |
621 |
buffer.order(ByteOrder.LITTLE_ENDIAN);
|
622 |
|
623 |
// write the output file type.
|
624 |
buffer.put((byte) MAGIC);
|
625 |
|
626 |
// write the date stuff
|
627 |
Calendar c = Calendar.getInstance(); |
628 |
c.setTime(new Date()); |
629 |
buffer.put((byte) (c.get(Calendar.YEAR) % 100)); |
630 |
buffer.put((byte) (c.get(Calendar.MONTH) + 1)); |
631 |
buffer.put((byte) (c.get(Calendar.DAY_OF_MONTH))); |
632 |
|
633 |
// write the number of records in the datafile.
|
634 |
buffer.putInt(recordCnt); |
635 |
|
636 |
// write the length of the header structure.
|
637 |
buffer.putShort((short) headerLength);
|
638 |
|
639 |
// write the length of a record
|
640 |
buffer.putShort((short) recordLength);
|
641 |
|
642 |
// // write the reserved bytes in the header
|
643 |
// for (int i=0; i<20; i++) out.writeByteLE(0);
|
644 |
buffer.position(buffer.position() + 20);
|
645 |
|
646 |
// write all of the header records
|
647 |
int tempOffset = 0; |
648 |
|
649 |
for (int i = 0; i < fields.length; i++) { |
650 |
// write the field name
|
651 |
for (int j = 0; j < 11; j++) { |
652 |
if (fields[i].fieldName.length() > j) {
|
653 |
buffer.put((byte) fields[i].fieldName.charAt(j));
|
654 |
} else {
|
655 |
buffer.put((byte) 0); |
656 |
} |
657 |
} |
658 |
|
659 |
// write the field type
|
660 |
buffer.put((byte) fields[i].fieldType);
|
661 |
|
662 |
// // write the field data address, offset from the start of the record.
|
663 |
buffer.putInt(tempOffset); |
664 |
tempOffset += fields[i].fieldLength; |
665 |
|
666 |
// write the length of the field.
|
667 |
buffer.put((byte) fields[i].fieldLength);
|
668 |
|
669 |
// write the decimal count.
|
670 |
buffer.put((byte) fields[i].decimalCount);
|
671 |
|
672 |
// write the reserved bytes.
|
673 |
//for (in j=0; jj<14; j++) out.writeByteLE(0);
|
674 |
buffer.position(buffer.position() + 14);
|
675 |
} |
676 |
|
677 |
// write the end of the field definitions marker
|
678 |
buffer.put((byte) 0x0D); |
679 |
|
680 |
buffer.position(0);
|
681 |
|
682 |
int r = buffer.remaining();
|
683 |
|
684 |
while ((r -= out.write(buffer)) > 0) { |
685 |
; // do nothing
|
686 |
} |
687 |
} |
688 |
|
689 |
/**
|
690 |
* Get a simple representation of this header.
|
691 |
*
|
692 |
* @return A String representing the state of the header.
|
693 |
*/
|
694 |
public String toString() { |
695 |
StringBuffer fs = new StringBuffer(); |
696 |
|
697 |
for (int i = 0, ii = fields.length; i < ii; i++) { |
698 |
DbaseField f = fields[i]; |
699 |
fs.append(f.fieldName + " " + f.fieldType + " " + f.fieldLength + |
700 |
" " + f.decimalCount + " " + f.fieldDataAddress + "\n"); |
701 |
} |
702 |
|
703 |
return "DB3 Header\n" + "Date : " + date + "\n" + "Records : " + |
704 |
recordCnt + "\n" + "Fields : " + fieldCnt + "\n" + fs; |
705 |
} |
706 |
|
707 |
/**
|
708 |
* Crea un DbaseFile.
|
709 |
*
|
710 |
* @return DbaseFileHeaderNIO
|
711 |
*
|
712 |
* @throws IOException .
|
713 |
*/
|
714 |
public static DbaseFileHeaderNIO createNewDbaseHeader() |
715 |
throws IOException { |
716 |
DbaseFileHeaderNIO header = new DbaseFileHeaderNIO();
|
717 |
|
718 |
for (int i = 0, ii = 1; i < ii; i++) { |
719 |
//AttributeType type = featureType.getAttributeType(i);
|
720 |
Class colType = Integer.class; |
721 |
String colName = "ID"; |
722 |
int fieldLen = 10; |
723 |
|
724 |
if (fieldLen <= 0) { |
725 |
fieldLen = 255;
|
726 |
} |
727 |
|
728 |
// @todo respect field length
|
729 |
if ((colType == Integer.class) || (colType == Short.class) || |
730 |
(colType == Byte.class)) {
|
731 |
header.addColumn(colName, 'N', Math.min(fieldLen, 10), 0); |
732 |
} else if (colType == Long.class) { |
733 |
header.addColumn(colName, 'N', Math.min(fieldLen, 19), 0); |
734 |
} else if ((colType == Double.class) || (colType == Float.class) || |
735 |
(colType == Number.class)) {
|
736 |
int l = Math.min(fieldLen, 33); |
737 |
int d = Math.max(l - 2, 0); |
738 |
header.addColumn(colName, 'N', l, d);
|
739 |
} else if (java.util.Date.class.isAssignableFrom(colType)) { |
740 |
header.addColumn(colName, 'D', fieldLen, 0); |
741 |
} else if (colType == Boolean.class) { |
742 |
header.addColumn(colName, 'L', 1, 0); |
743 |
} else if (CharSequence.class.isAssignableFrom(colType)) { |
744 |
// Possible fix for GEOT-42 : ArcExplorer doesn't like 0 length
|
745 |
// ensure that maxLength is at least 1
|
746 |
header.addColumn(colName, 'C', Math.min(254, fieldLen), 0); |
747 |
} else if (Geometry.class.isAssignableFrom(colType)) { |
748 |
continue;
|
749 |
} else {
|
750 |
throw new IOException("Unable to write : " + colType.getName()); |
751 |
} |
752 |
} |
753 |
|
754 |
return header;
|
755 |
} |
756 |
|
757 |
/**
|
758 |
* DOCUMENT ME!
|
759 |
*/
|
760 |
|
761 |
public static DbaseFileHeaderNIO createDbaseHeader(SelectableDataSource sds) |
762 |
throws IOException { |
763 |
DbaseFileHeaderNIO header = new DbaseFileHeaderNIO();
|
764 |
try {
|
765 |
for (int i = 0, ii = sds.getFieldCount(); i < ii; i++) { |
766 |
//AttributeType type = featureType.getAttributeType(i);
|
767 |
Value value=sds.getFieldValue(0,i);
|
768 |
//int type = sds.getFieldType(i);
|
769 |
int type=getTypeValue(value);
|
770 |
String colName = sds.getFieldName(i);
|
771 |
///int fieldLen = ((DBFDriver)sds.getDriver()).getFieldLength(i);
|
772 |
int fieldLen=100; //TODO aqu? el tama?o no es correcto hay que calcularlo, ahora mismo est? puesto a pi??n. |
773 |
int decimales=5; |
774 |
// if (fieldLen <= 0) {
|
775 |
// fieldLen = 255;
|
776 |
// }
|
777 |
// @todo respect field length
|
778 |
switch (type) {
|
779 |
case (Value.DOUBLE):
|
780 |
case (Value.INTEGER):
|
781 |
case (Value.DECIMAL):
|
782 |
header.addColumn(colName, 'N', Math.min(fieldLen, 10), decimales); |
783 |
break;
|
784 |
case (Value.DATE):
|
785 |
header.addColumn(colName, 'D', fieldLen, 0); |
786 |
break;
|
787 |
case (Value.BOOLEAN):
|
788 |
header.addColumn(colName, 'L', 1, 0); |
789 |
break;
|
790 |
case (Value.STRING):
|
791 |
header.addColumn(colName, 'C', Math.min(254, fieldLen), 0); |
792 |
} |
793 |
} |
794 |
} catch (DriverException e) {
|
795 |
e.printStackTrace(); |
796 |
} catch (SemanticException e) {
|
797 |
e.printStackTrace(); |
798 |
} |
799 |
return header;
|
800 |
} |
801 |
private static int getTypeValue(Value value) throws SemanticException{ |
802 |
if (value.getClass().getName().equals("com.hardcode.gdbms.engine.values.BooleanValue")) |
803 |
return Value.BOOLEAN;
|
804 |
if (value.getClass().getName().equals("com.hardcode.gdbms.engine.values.DateValue")) |
805 |
return Value.DATE;
|
806 |
if (value.getClass().getName().equals("com.hardcode.gdbms.engine.values.DoubleValue")) |
807 |
return Value.DOUBLE;
|
808 |
if (value.getClass().getName().equals("com.hardcode.gdbms.engine.values.FloatValue")) |
809 |
return Value.FLOAT;
|
810 |
if (value.getClass().getName().equals("com.hardcode.gdbms.engine.values.IntValue")) |
811 |
return Value.INTEGER;
|
812 |
if (value.getClass().getName().equals("com.hardcode.gdbms.engine.values.LongValue")) |
813 |
return Value.LONG;
|
814 |
if (value.getClass().getName().equals("com.hardcode.gdbms.engine.values.StringValue")) |
815 |
return Value.STRING;
|
816 |
// default:
|
817 |
throw new SemanticException("Unexpected className in getTypeValue (GDBMS)"); |
818 |
} |
819 |
/**
|
820 |
* Class for holding the information assicated with a record.
|
821 |
*/
|
822 |
class DbaseField { |
823 |
// Field Name
|
824 |
String fieldName;
|
825 |
|
826 |
// Field Type (C N L D or M)
|
827 |
char fieldType;
|
828 |
|
829 |
// Field Data Address offset from the start of the record.
|
830 |
int fieldDataAddress;
|
831 |
|
832 |
// Length of the data in bytes
|
833 |
int fieldLength;
|
834 |
|
835 |
// Field decimal count in Binary, indicating where the decimal is
|
836 |
int decimalCount;
|
837 |
} |
838 |
} |