Statistics
| Revision:

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 @ 44844

History | View | Annotate | Download (5.64 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
  // Field Name
51
  private String name;
52
  private String name_trim;
53

    
54
  // Field Type (C N L D F or M)
55
  private char type;
56
  // Field Data Address offset from the start of the record.
57
  private int offsetInRecord;
58
  // Length of the data in bytes
59
  private int size;
60

    
61
  private int precision;
62
  private int scale;
63

    
64
  public String getName() {
65
    return this.name.trim();
66
  }
67

    
68
  public int getSize() {
69
    return this.size;
70
  }
71

    
72
  public int getScale() {
73
    return this.scale;
74
  }
75

    
76
  public int getPrecision() {
77
    switch (this.getType()) {
78
      case DBFTYPE_FLOAT:
79
      case DBFTYPE_NUMBER:
80
        if (this.scale < 1) {
81
          return this.size; // -XXX (ignoramos el signo)
82
        } else {
83
          return this.size - 2; // -0.XXX (ignoramos el signo)
84
        }
85
    }
86
    return 0;
87
  }
88

    
89
  public char getType() {
90
    return this.type;
91
  }
92

    
93
  public int getOffsetInRecord() {
94
    return this.offsetInRecord;
95
  }
96

    
97
  public String getName_trim() {
98
    return name_trim;
99
  }
100

    
101
  public void setName(String name) {
102
    this.name = name;
103
  }
104

    
105
  public void setName_trim(String name_trim) {
106
    this.name_trim = name_trim;
107
  }
108

    
109
  public void setType(char type) throws AttributeFeatureTypeNotSuportedException {
110
    switch (type) {
111
      case DBFTYPE_NUMBER:
112
      case DBFTYPE_FLOAT:
113
      case DBFTYPE_STRING:
114
      case DBFTYPE_DATE:
115
      case DBFTYPE_BOOLEAN:
116
        this.type = type;
117
        break;
118
      case 'c':
119
        this.type = DBFTYPE_STRING;
120
        break;
121
      case 'S':
122
      case 's':
123
        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.");
124
        this.type = DBFTYPE_STRING;
125
        break;
126
      case 'd':
127
        this.type = DBFTYPE_DATE;
128
        break;
129
      case 'f':
130
        this.type = DBFTYPE_FLOAT;
131
        break;
132
      case 'n':
133
        this.type = DBFTYPE_NUMBER;
134
        break;
135
      case 'l':
136
        this.type = DBFTYPE_BOOLEAN;
137
        break;
138
      default:
139
        throw new AttributeFeatureTypeNotSuportedException(name, type, "unknown", "DBF");
140
    }
141
  }
142

    
143
  public void setOffsetInRecord(int offsetInRecord) {
144
    this.offsetInRecord = offsetInRecord;
145
  }
146

    
147
  public void setSize(int size) {
148
    switch (this.getType()) {
149
      case DBFTYPE_FLOAT:
150
      case DBFTYPE_NUMBER:
151
        this.size = size;
152
        break;
153

    
154
      case DBFTYPE_STRING:
155
        if (size > 254) {
156
          warn("Field Length for " + this.name + " set to " + size + " Which is longer than 254, not consistent with dbase III");
157
        }
158
        this.size = size;
159
        break;
160
      case DBFTYPE_DATE:
161
        this.size = DATE_SIZE;
162
        break;
163
      case DBFTYPE_BOOLEAN:
164
        this.size = BOOLEAN_SIZE;
165
        break;
166
      default:
167
        this.size = size;
168
    }
169
  }
170

    
171
  public void setScale(int scale) {
172
    switch (this.getType()) {
173
      case DBFTYPE_FLOAT:
174
      case DBFTYPE_NUMBER:
175
        this.scale = scale;
176
        break;
177
      default:
178
        this.scale = 0;
179
    }
180
  }
181

    
182
//  public void calculateScaleAndPrecission() {
183
//    switch (this.getType()) {
184
//      case DBFTYPE_FLOAT:
185
//      case DBFTYPE_NUMBER:
186
//        if (this.size > 0) {
187
//          if (this.scale < 1) {
188
//            this.precision = this.size - 1;
189
//          } else {
190
//            this.precision = this.size - 3;
191
//          }
192
//        }
193
//        break;
194
//      default:
195
//        this.scale = 0;
196
//    }
197
//  }
198
//
199
//  public void setPrecision(int precision) {
200
//    switch (this.getType()) {
201
//      case DBFTYPE_FLOAT:
202
//      case DBFTYPE_NUMBER:
203
//        if (precision > 250) {
204
//          this.precision = 250;
205
//        } else {
206
//          this.precision = precision;
207
//        }
208
//        break;
209
//      default:
210
//        this.precision = 0;
211
//    }
212
//  }
213

    
214
  private void warn(String msg) {
215
    LOGGER.warn(msg);
216
  }
217
}