001 //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/io/dbaseapi/DBFDataSection.java $ 002 003 /*---------------------------------------------------------------------------- 004 This file is part of deegree, http://deegree.org/ 005 Copyright (C) 2001-2009 by: 006 Department of Geography, University of Bonn 007 and 008 lat/lon GmbH 009 010 This library is free software; you can redistribute it and/or modify it under 011 the terms of the GNU Lesser General Public License as published by the Free 012 Software Foundation; either version 2.1 of the License, or (at your option) 013 any later version. 014 This library is distributed in the hope that it will be useful, but WITHOUT 015 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 016 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 017 details. 018 You should have received a copy of the GNU Lesser General Public License 019 along with this library; if not, write to the Free Software Foundation, Inc., 020 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 021 022 Contact information: 023 024 lat/lon GmbH 025 Aennchenstr. 19, 53177 Bonn 026 Germany 027 http://lat-lon.de/ 028 029 Department of Geography, University of Bonn 030 Prof. Dr. Klaus Greve 031 Postfach 1147, 53001 Bonn 032 Germany 033 http://www.geographie.uni-bonn.de/deegree/ 034 035 e-mail: info@deegree.org 036 ----------------------------------------------------------------------------*/ 037 038 package org.deegree.io.dbaseapi; 039 040 import java.util.ArrayList; 041 import java.util.List; 042 043 /** 044 * Class representing a record of the data section of a dBase III/IV file<BR> 045 * at the moment only the daata types character ("C") and numeric ("N") are supported 046 * 047 * 048 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a> 049 * @author last edited by: $Author: mschneider $ 050 * 051 * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $ 052 */ 053 public class DBFDataSection { 054 055 /** 056 * length of one record in bytes 057 */ 058 private int recordlength = 0; 059 060 private FieldDescriptor[] fieldDesc = null; 061 062 private List<ByteContainer> data = new ArrayList<ByteContainer>(); 063 064 /** 065 * constructor 066 */ 067 public DBFDataSection( FieldDescriptor[] fieldDesc ) { 068 069 this.fieldDesc = fieldDesc; 070 071 // calculate length of the data section 072 recordlength = 0; 073 for ( int i = 0; i < this.fieldDesc.length; i++ ) { 074 075 byte[] fddata = this.fieldDesc[i].getFieldDescriptor(); 076 077 recordlength += fddata[16]; 078 079 fddata = null; 080 081 } 082 083 recordlength++; 084 085 } 086 087 /** 088 * method: public setRecord(ArrayList recData) writes a data record to byte array representing 089 * the data section of the dBase file. The method gets the data type of each field in recData 090 * from fieldDesc wich has been set at the constructor. 091 */ 092 public void setRecord( ArrayList recData ) 093 throws DBaseException { 094 095 setRecord( data.size(), recData ); 096 097 } 098 099 /** 100 * method: public setRecord(int index, ArrayList recData) writes a data record to byte array 101 * representing the data section of the dBase file. The method gets the data type of each field 102 * in recData from fieldDesc wich has been set at the constructor. index specifies the location 103 * of the retrieved record in the datasection. if an invalid index is used an exception will be 104 * thrown 105 */ 106 public void setRecord( int index, ArrayList recData ) 107 throws DBaseException { 108 109 ByteContainer datasec = new ByteContainer( recordlength ); 110 111 if ( ( index < 0 ) || ( index > data.size() ) ) 112 throw new DBaseException( "invalid index: " + index ); 113 114 if ( recData.size() != this.fieldDesc.length ) 115 throw new DBaseException( "invalid size of recData" ); 116 117 int offset = 0; 118 119 datasec.data[offset] = 0x20; 120 121 offset++; 122 123 byte[] b = null; 124 125 // write every field on the ArrayList to the data byte array 126 for ( int i = 0; i < recData.size(); i++ ) { 127 byte[] fddata = this.fieldDesc[i].getFieldDescriptor(); 128 switch ( fddata[11] ) { 129 130 // if data type is character 131 case (byte) 'C': 132 if ( recData.get( i ) != null && !( recData.get( i ) instanceof String ) ) { 133 throw new DBaseException( "invalid data type at field: " + i ); 134 } 135 if ( recData.get( i ) == null ) { 136 b = new byte[0]; 137 } else { 138 b = ( (String) recData.get( i ) ).getBytes(); 139 } 140 if ( b.length > fddata[16] ) 141 throw new DBaseException( "string contains too many characters " + (String) recData.get( i ) ); 142 for ( int j = 0; j < b.length; j++ ) 143 datasec.data[offset + j] = b[j]; 144 for ( int j = b.length; j < fddata[16]; j++ ) 145 datasec.data[offset + j] = 0x20; 146 break; 147 case (byte) 'N': 148 if ( recData.get( i ) != null && !( recData.get( i ) instanceof Number ) ) 149 throw new DBaseException( "invalid data type at field: " + i ); 150 if ( recData.get( i ) == null ) { 151 b = new byte[0]; 152 } else { 153 b = ( (Number) recData.get( i ) ).toString().getBytes(); 154 } 155 if ( b.length > fddata[16] ) 156 throw new DBaseException( "string contains too many characters " + (String) recData.get( i ) ); 157 for ( int j = 0; j < b.length; j++ ) 158 datasec.data[offset + j] = b[j]; 159 for ( int j = b.length; j < fddata[16]; j++ ) 160 datasec.data[offset + j] = 0x0; 161 break; 162 default: 163 throw new DBaseException( "data type not supported" ); 164 165 } 166 167 offset += fddata[16]; 168 169 } 170 171 // puts the record to the ArrayList (container) 172 data.add( index, datasec ); 173 174 } 175 176 /** 177 * method: public byte[] getDataSection() returns the data section as a byte array. 178 */ 179 public byte[] getDataSection() { 180 181 // allocate memory for all datarecords on one array + 1 byte 182 byte[] outdata = new byte[data.size() * recordlength + 1]; 183 184 // set the file terminating byte 185 outdata[outdata.length - 1] = 0x1A; 186 187 // get all records from the ArrayList and put it 188 // on a single array 189 int j = 0; 190 for ( int i = 0; i < data.size(); i++ ) { 191 192 ByteContainer bc = data.get( i ); 193 194 for ( int k = 0; k < recordlength; k++ ) { 195 outdata[j++] = bc.data[k]; 196 } 197 198 } 199 200 return outdata; 201 202 } 203 204 /** 205 * method: public int getNoOfRecords() returns the number of records within the container 206 */ 207 public int getNoOfRecords() { 208 209 return data.size(); 210 211 } 212 213 } 214 215 class ByteContainer { 216 217 public byte[] data = null; 218 219 public ByteContainer( int size ) { 220 221 data = new byte[size]; 222 223 } 224 225 }