Revision 45317 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

View differences:

DbaseFieldDescriptor.java
26 26
import org.gvsig.fmap.dal.feature.exception.AttributeFeatureTypeNotSuportedException;
27 27
import static org.gvsig.fmap.dal.store.dbf.utils.FieldFormatter.BOOLEAN_SIZE;
28 28
import static org.gvsig.fmap.dal.store.dbf.utils.FieldFormatter.DATE_SIZE;
29
import org.gvsig.tools.dataTypes.DataType;
29 30
import org.slf4j.Logger;
30 31
import org.slf4j.LoggerFactory;
31 32

  
......
34 35
 */
35 36
public class DbaseFieldDescriptor {
36 37

  
37
  private static final Logger LOGGER = LoggerFactory.getLogger(DbaseFieldDescriptor.class);
38
    private static final Logger LOGGER = LoggerFactory.getLogger(DbaseFieldDescriptor.class);
38 39

  
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';
40
    public static final char DBFTYPE_STRING = 'C';
41
    public static final char DBFTYPE_NUMBER = 'N';
42
    public static final char DBFTYPE_FLOAT = 'F';
43
    public static final char DBFTYPE_DATE = 'D';
44
    public static final char DBFTYPE_BOOLEAN = 'L';
44 45

  
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 = '+';
46
    public static final char DBFTYPE_LONG = 'l';
47
    public static final char DBFTYPE_DOUBLE = 'O';
48
    public static final char DBFTYPE_TIMESTAMP = '@';
49
    public static final char DBFTYPE_AUTOINCREMENT = '+';
49 50

  
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;
51
    public static final int MAX_SIZE = 254;
52
    public static final int MAX_NUMBER_SIZE = 20;
56 53

  
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;
54
    // Field Name
55
    private String name;
56
    private String name_trim;
63 57

  
64
  private int precision;
65
  private int scale;
58
    // Field Type (C N L D F or M)
59
    private char type;
60
    // Field Data Address offset from the start of the record.
61
    private int offsetInRecord;
62
    // Length of the data in bytes
63
    private int size;
66 64

  
67
  public String getName() {
68
    return this.name.trim();
69
  }
65
    private int scale;
70 66

  
71
  public int getSize() {
72
    return this.size;
73
  }
67
    public String getName() {
68
        return this.name.trim();
69
    }
74 70

  
75
  public int getScale() {
76
    return this.scale;
77
  }
71
    public int getSize() {
72
        return this.size;
73
    }
78 74

  
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)
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
                }
87 88
        }
89
        return 0;
88 90
    }
89
    return 0;
90
  }
91 91

  
92
  public char getType() {
93
    return this.type;
94
  }
92
    public char getType() {
93
        return this.type;
94
    }
95 95

  
96
  public int getOffsetInRecord() {
97
    return this.offsetInRecord;
98
  }
96
    public int getOffsetInRecord() {
97
        return this.offsetInRecord;
98
    }
99 99

  
100
  public String getName_trim() {
101
    return name_trim;
102
  }
100
    public String getName_trim() {
101
        return name_trim;
102
    }
103 103

  
104
  public void setName(String name) {
105
    this.name = name;
106
  }
104
    public void setName(String name) {
105
        this.name = name;
106
    }
107 107

  
108
  public void setName_trim(String name_trim) {
109
    this.name_trim = name_trim;
110
  }
108
    public void setName_trim(String name_trim) {
109
        this.name_trim = name_trim;
110
    }
111 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");
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
        }
143 144
    }
144
  }
145 145

  
146
  public void setOffsetInRecord(int offsetInRecord) {
147
    this.offsetInRecord = offsetInRecord;
148
  }
146
    public void setOffsetInRecord(int offsetInRecord) {
147
        this.offsetInRecord = offsetInRecord;
148
    }
149 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;
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 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");
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;
160 171
        }
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
    }
172
  }
173 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;
174
    public void setScale(int scale) {
175
        if (scale == 0) {
176
            this.scale = 0;
177
            return;
178
        }
179
        switch (this.getType()) {
180
            case DBFTYPE_FLOAT:
181
            case DBFTYPE_NUMBER:
182
                this.scale = this.getPrecision() - 1; //In the dbf's header do ignore scale
183
                break;
184
            default:
185
                this.scale = 0;
186
        }
182 187
    }
183
  }
184 188

  
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
  }
189
    private void warn(String msg) {
190
        LOGGER.warn(msg);
191
    }
220 192
}

Also available in: Unified diff