svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.dbf / src / main / java / org / gvsig / fmap / dal / store / dbf / utils / DbaseFieldDescriptor.java @ 44926
History | View | Annotate | Download (5.74 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2020 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.dal.store.dbf.utils; |
25 |
|
26 |
import org.gvsig.fmap.dal.feature.exception.AttributeFeatureTypeNotSuportedException; |
27 |
import static org.gvsig.fmap.dal.store.dbf.utils.FieldFormatter.BOOLEAN_SIZE; |
28 |
import static org.gvsig.fmap.dal.store.dbf.utils.FieldFormatter.DATE_SIZE; |
29 |
import org.slf4j.Logger; |
30 |
import org.slf4j.LoggerFactory; |
31 |
|
32 |
/**
|
33 |
* Class for holding the information assicated with a record.
|
34 |
*/
|
35 |
public class DbaseFieldDescriptor { |
36 |
|
37 |
private static final Logger LOGGER = LoggerFactory.getLogger(DbaseFieldDescriptor.class); |
38 |
|
39 |
public static final char DBFTYPE_STRING = 'C'; |
40 |
public static final char DBFTYPE_NUMBER = 'N'; |
41 |
public static final char DBFTYPE_FLOAT = 'F'; |
42 |
public static final char DBFTYPE_DATE = 'D'; |
43 |
public static final char DBFTYPE_BOOLEAN = 'L'; |
44 |
|
45 |
public static final char DBFTYPE_LONG = 'l'; |
46 |
public static final char DBFTYPE_DOUBLE = 'O'; |
47 |
public static final char DBFTYPE_TIMESTAMP = '@'; |
48 |
public static final char DBFTYPE_AUTOINCREMENT = '+'; |
49 |
|
50 |
public static final int MAX_SIZE = 254; |
51 |
public static final int MAX_NUMBER_SIZE = 20; |
52 |
|
53 |
// Field Name
|
54 |
private String name; |
55 |
private String name_trim; |
56 |
|
57 |
// Field Type (C N L D F or M)
|
58 |
private char type; |
59 |
// Field Data Address offset from the start of the record.
|
60 |
private int offsetInRecord; |
61 |
// Length of the data in bytes
|
62 |
private int size; |
63 |
|
64 |
private int precision; |
65 |
private int scale; |
66 |
|
67 |
public String getName() { |
68 |
return this.name.trim(); |
69 |
} |
70 |
|
71 |
public int getSize() { |
72 |
return this.size; |
73 |
} |
74 |
|
75 |
public int getScale() { |
76 |
return this.scale; |
77 |
} |
78 |
|
79 |
public int getPrecision() { |
80 |
switch (this.getType()) { |
81 |
case DBFTYPE_FLOAT:
|
82 |
case DBFTYPE_NUMBER:
|
83 |
if (this.scale < 1) { |
84 |
return this.size; // -XXX (ignoramos el signo) |
85 |
} else {
|
86 |
return this.size - 2; // -0.XXX (ignoramos el signo) |
87 |
} |
88 |
} |
89 |
return 0; |
90 |
} |
91 |
|
92 |
public char getType() { |
93 |
return this.type; |
94 |
} |
95 |
|
96 |
public int getOffsetInRecord() { |
97 |
return this.offsetInRecord; |
98 |
} |
99 |
|
100 |
public String getName_trim() { |
101 |
return name_trim;
|
102 |
} |
103 |
|
104 |
public void setName(String name) { |
105 |
this.name = name;
|
106 |
} |
107 |
|
108 |
public void setName_trim(String name_trim) { |
109 |
this.name_trim = name_trim;
|
110 |
} |
111 |
|
112 |
public void setType(char type) throws AttributeFeatureTypeNotSuportedException { |
113 |
switch (type) {
|
114 |
case DBFTYPE_NUMBER:
|
115 |
case DBFTYPE_FLOAT:
|
116 |
case DBFTYPE_STRING:
|
117 |
case DBFTYPE_DATE:
|
118 |
case DBFTYPE_BOOLEAN:
|
119 |
this.type = type;
|
120 |
break;
|
121 |
case 'c': |
122 |
this.type = DBFTYPE_STRING;
|
123 |
break;
|
124 |
case 'S': |
125 |
case 's': |
126 |
warn("Field type for " + name + " set to S which is flat out wrong people!, I am setting this to C, in the hopes you meant character."); |
127 |
this.type = DBFTYPE_STRING;
|
128 |
break;
|
129 |
case 'd': |
130 |
this.type = DBFTYPE_DATE;
|
131 |
break;
|
132 |
case 'f': |
133 |
this.type = DBFTYPE_FLOAT;
|
134 |
break;
|
135 |
case 'n': |
136 |
this.type = DBFTYPE_NUMBER;
|
137 |
break;
|
138 |
case 'l': |
139 |
this.type = DBFTYPE_BOOLEAN;
|
140 |
break;
|
141 |
default:
|
142 |
throw new AttributeFeatureTypeNotSuportedException(name, type, "unknown", "DBF"); |
143 |
} |
144 |
} |
145 |
|
146 |
public void setOffsetInRecord(int offsetInRecord) { |
147 |
this.offsetInRecord = offsetInRecord;
|
148 |
} |
149 |
|
150 |
public void setSize(int size) { |
151 |
switch (this.getType()) { |
152 |
case DBFTYPE_FLOAT:
|
153 |
case DBFTYPE_NUMBER:
|
154 |
this.size = size;
|
155 |
break;
|
156 |
|
157 |
case DBFTYPE_STRING:
|
158 |
if (size > MAX_SIZE) {
|
159 |
warn("Field Length for " + this.name + " set to " + size + " Which is longer than "+MAX_SIZE+" not consistent with dbase III"); |
160 |
} |
161 |
this.size = size;
|
162 |
break;
|
163 |
case DBFTYPE_DATE:
|
164 |
this.size = DATE_SIZE;
|
165 |
break;
|
166 |
case DBFTYPE_BOOLEAN:
|
167 |
this.size = BOOLEAN_SIZE;
|
168 |
break;
|
169 |
default:
|
170 |
this.size = size;
|
171 |
} |
172 |
} |
173 |
|
174 |
public void setScale(int scale) { |
175 |
switch (this.getType()) { |
176 |
case DBFTYPE_FLOAT:
|
177 |
case DBFTYPE_NUMBER:
|
178 |
this.scale = scale;
|
179 |
break;
|
180 |
default:
|
181 |
this.scale = 0; |
182 |
} |
183 |
} |
184 |
|
185 |
// public void calculateScaleAndPrecission() {
|
186 |
// switch (this.getType()) {
|
187 |
// case DBFTYPE_FLOAT:
|
188 |
// case DBFTYPE_NUMBER:
|
189 |
// if (this.size > 0) {
|
190 |
// if (this.scale < 1) {
|
191 |
// this.precision = this.size - 1;
|
192 |
// } else {
|
193 |
// this.precision = this.size - 3;
|
194 |
// }
|
195 |
// }
|
196 |
// break;
|
197 |
// default:
|
198 |
// this.scale = 0;
|
199 |
// }
|
200 |
// }
|
201 |
//
|
202 |
// public void setPrecision(int precision) {
|
203 |
// switch (this.getType()) {
|
204 |
// case DBFTYPE_FLOAT:
|
205 |
// case DBFTYPE_NUMBER:
|
206 |
// if (precision > 250) {
|
207 |
// this.precision = 250;
|
208 |
// } else {
|
209 |
// this.precision = precision;
|
210 |
// }
|
211 |
// break;
|
212 |
// default:
|
213 |
// this.precision = 0;
|
214 |
// }
|
215 |
// }
|
216 |
|
217 |
private void warn(String msg) { |
218 |
LOGGER.warn(msg); |
219 |
} |
220 |
} |