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 / FieldFormatter.java @ 45316

History | View | Annotate | Download (10.7 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 java.math.BigDecimal;
27
import java.math.MathContext;
28
import java.nio.BufferOverflowException;
29
import java.sql.Timestamp;
30
import java.text.FieldPosition;
31
import java.text.NumberFormat;
32
import java.text.ParseException;
33
import java.text.SimpleDateFormat;
34
import java.util.Date;
35
import java.util.Locale;
36
import org.apache.commons.lang3.StringUtils;
37
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
38
import org.gvsig.tools.logger.FilteredLogger;
39
import org.slf4j.Logger;
40
import org.slf4j.LoggerFactory;
41

    
42
/**
43
 *
44
 * @author jjdelcerro
45
 */
46
@SuppressWarnings("UseSpecificCatch")
47
public class FieldFormatter {
48

    
49
  private static final Logger LOGGER = LoggerFactory.getLogger(FieldFormatter.class);
50

    
51
  public static final int TIME_SIZE = 6;
52
  public static final int TIMESTAMP_SIZE = 14;
53
  public static final int DATE_SIZE = 8;
54
  public static final int BOOLEAN_SIZE = 1;
55

    
56
  private static final SimpleDateFormat YYYYMMDD_DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
57
  private static final SimpleDateFormat HHMMSS_DATE_FORMAT = new SimpleDateFormat("HHmmss");
58
  private static final SimpleDateFormat YYYYMMDDHHMMSS_DATE_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");
59

    
60
  private static final FieldPosition FIELDPOSITION = new FieldPosition(NumberFormat.INTEGER_FIELD);
61

    
62
  private static final int MAXCHARS = 255;
63

    
64
  private StringBuffer buffer;
65
  private NumberFormat numberFormatter;
66
  private FilteredLogger logger;
67

    
68
  public FieldFormatter() {
69

    
70
  }
71

    
72
  private StringBuffer getBuffer() {
73
    if (this.buffer == null) {
74
      this.buffer = new StringBuffer(MAXCHARS);
75
    }
76
    return this.buffer;
77
  }
78

    
79
  private NumberFormat getNumberFormatter() {
80
    if (this.numberFormatter == null) {
81
      this.numberFormatter = NumberFormat.getNumberInstance(Locale.US);
82
      // Avoid grouping on number format
83
      this.numberFormatter.setGroupingUsed(false);
84
    }
85
    return this.numberFormatter;
86
  }
87

    
88
  private FilteredLogger getLogger() {
89
    if (this.logger == null) {
90
      this.logger = new FilteredLogger(LOGGER, "DBFFiledFormatter", -1);
91
      this.logger.setInterval(2000);
92
    }
93
    return this.logger;
94
  }
95

    
96
  public String format(String s, int size) {
97
    if (s.length() > size) {
98
      return s.substring(0, size);
99
    } else {
100
      return s;
101
    }
102
  }
103

    
104
  public String format(boolean b) {
105
    return b ? "T" : "F";
106
  }
107

    
108
  public String formatDate(Date date) {
109
    if (date == null) {
110
      //      yyyyMMdd
111
      return "        ";
112
    }
113
    StringBuffer buf = this.getBuffer();
114
    buf.setLength(0); // Fast clear of buffer
115
    YYYYMMDD_DATE_FORMAT.format(date, buf, FIELDPOSITION);
116
    return buf.toString();
117
  }
118

    
119
  public String formatTime(Date date) {
120
    if (date == null) {
121
      //      HHMMSS
122
      return "      ";
123
    }
124
    StringBuffer buf = this.getBuffer();
125
    buf.setLength(0); // Fast clear of buffer
126
    HHMMSS_DATE_FORMAT.format(date, buf, FIELDPOSITION);
127
    return buf.toString();
128
  }
129

    
130
  public String formatTimestamp(Date date) {
131
    if (date == null) {
132
      //      YYYYMMDDHHMMSS
133
      return "              ";
134
    }
135
    StringBuffer buf = this.getBuffer();
136
    buf.setLength(0); // Fast clear of buffer
137
    YYYYMMDDHHMMSS_DATE_FORMAT.format(date, buf, FIELDPOSITION);
138
    return buf.toString();
139
  }
140

    
141
  public String formatTimestamp(Timestamp timestamp) {
142
    if (timestamp == null) {
143
      //      YYYYMMDDHHMMSS
144
      return "              ";
145
    }
146
    StringBuffer buf = this.getBuffer();
147
    buf.setLength(0); // Fast clear of buffer
148
    YYYYMMDDHHMMSS_DATE_FORMAT.format(timestamp, buf, FIELDPOSITION);
149
    return buf.toString();
150
  }
151

    
152
  public String format(byte n) {
153
    StringBuffer buf = this.getBuffer();
154
    NumberFormat nf = this.getNumberFormatter();
155

    
156
    buf.setLength(0); // Fast clear of buffer
157
    nf.setMaximumFractionDigits(0);
158
    nf.setMinimumFractionDigits(0);
159
    nf.format(n, buf, FIELDPOSITION);
160
    String s = buf.toString();
161
    if (s.length() > 2) {
162
      throw new BufferOverflowException();
163
    }
164
    return s;
165
  }
166

    
167
  public String format(int n, int size) {
168
    StringBuffer buf = this.getBuffer();
169
    NumberFormat nf = this.getNumberFormatter();
170

    
171
    buf.setLength(0); // Fast clear of buffer
172
    nf.setMaximumFractionDigits(0);
173
    nf.setMinimumFractionDigits(0);
174
    nf.format(n, buf, FIELDPOSITION);
175
    String s = buf.toString();
176
    if (s.length() > size) {
177
      throw new BufferOverflowException();
178
    }
179
    return s;
180
  }
181

    
182
  public String format(long n, int size) {
183
    StringBuffer buf = this.getBuffer();
184
    NumberFormat nf = this.getNumberFormatter();
185

    
186
    buf.setLength(0); // Fast clear of buffer
187
    nf.setMaximumFractionDigits(0);
188
    nf.setMinimumFractionDigits(0);
189
    nf.format(n, buf, FIELDPOSITION);
190
    String s = buf.toString();
191
    if (s.length() > size) {
192
      throw new BufferOverflowException();
193
    }
194
    return s;
195
  }
196

    
197
  public String format(BigDecimal n, int size) {
198
    String s = n.toPlainString();
199
    if (s.length() > size) {
200
        int index = s.indexOf('.');
201
        if(index < 0 || index >= size ) {
202
          throw new BufferOverflowException();
203
        }
204
      s = s.substring(0, size);
205
    }
206
    return s;
207
  }
208

    
209
  public String format(double n, int size, int scale) {
210
    String s = Double.toString(n);
211
    if (s.length() > size) {
212
        if(s.indexOf('E') >= 0){
213
          //FIXME: 
214
          throw new BufferOverflowException();
215
        }
216
        int index = s.indexOf('.');
217
        if(index < 0 || index >= size ) {
218
          throw new BufferOverflowException();
219
        }
220
        s = s.substring(0, size);
221
        
222
    }
223
    return s;
224
  }
225

    
226
  public String format(float n, int size, int scale) {
227
    String s = Float.toString(n);
228
    if (s.length() > size) {
229
        if(s.indexOf('E') >= 0){
230
          //FIXME: 
231
          throw new BufferOverflowException();
232
        }
233
        int index = s.indexOf('.');
234
        if(index < 0 || index >= size ) {
235
          throw new BufferOverflowException();
236
        }
237

    
238
        s = s.substring(0, size);
239
    }
240
    return s;
241
  }
242

    
243
  public String parseString(String value, String defaultValue) {
244
    if (value == null || value.length() == 0) {
245
      return defaultValue;
246
    }
247
    return value;
248
  }
249

    
250
  public BigDecimal parseDecimal(String value, MathContext context, int scale, BigDecimal defaultValue) {
251
    if (StringUtils.isBlank(value)) {
252
      return defaultValue;
253
    }
254
    try {
255
      BigDecimal x = new BigDecimal(value, context);
256
      if( scale>=0) {
257
        x = x.setScale(scale, context.getRoundingMode());
258
      }
259
      return x;
260
    } catch (Exception ex) {
261
      if (!StringUtils.containsOnly(value, "*")) {
262
        this.getLogger().warn("Can't parse value '" + value + "' as BigDecimal.", ex);
263
      }
264
      return defaultValue;
265
    }
266
  }
267

    
268
  public Double parseDouble(String value, Double defaultValue) {
269
    if (StringUtils.isBlank(value)) {
270
      return defaultValue;
271
    }
272
    try {
273
      Double x = Double.parseDouble(value);
274
      return x;
275
    } catch (Exception ex) {
276
      this.getLogger().warn("Can't parse value '" + value + "' as double.", ex);
277
      return defaultValue;
278
    }
279
  }
280

    
281
  public Float parseFloat(String value, Float defaultValue) {
282
    if (StringUtils.isBlank(value)) {
283
      return defaultValue;
284
    }
285
    try {
286
      Float x = Float.parseFloat(value);
287
      return x;
288
    } catch (Exception ex) {
289
      this.getLogger().warn("Can't parse value '" + value + "' as float.", ex);
290
      return defaultValue;
291
    }
292
  }
293

    
294
  public Integer parseInt(String value, Integer defaultValue) {
295
    if (StringUtils.isBlank(value)) {
296
      return defaultValue;
297
    }
298
    try {
299
      int x = Integer.parseInt(value);
300
      return x;
301
    } catch (Exception ex) {
302
      this.getLogger().warn("Can't parse value '" + value + "' as int.", ex);
303
      return defaultValue;
304
    }
305
  }
306

    
307
  public Long parseLong(String value, Long defaultValue) {
308
    if (StringUtils.isBlank(value)) {
309
      return defaultValue;
310
    }
311
    try {
312
      Long x = new Long(value);
313
      return x;
314
    } catch (Exception ex) {
315
      this.getLogger().warn("Can't parse value '" + value + "' as long.", ex);
316
      return defaultValue;
317
    }
318
  }
319

    
320
  public Byte parseByte(String value, Byte defaultValue) {
321
    if (StringUtils.isBlank(value)) {
322
      return defaultValue;
323
    }
324
    try {
325
      byte x = Byte.parseByte(value);
326
      return x;
327
    } catch (Exception ex) {
328
      this.getLogger().warn("Can't parse value '" + value + "' as byte.", ex);
329
      return defaultValue;
330
    }
331
  }
332

    
333
  public Boolean parseBoolean(String value, Boolean defaultValue) {
334
    if (StringUtils.isBlank(value)) {
335
      return defaultValue;
336
    }
337
    switch (value.trim().charAt(0)) {
338
      case 'T':
339
      case 't':
340
        return true;
341
      case 'F':
342
      case 'f':
343
        return false;
344
      case ' ':
345
        return null;
346
      default:
347
        return defaultValue;
348
    }
349
  }
350

    
351
  public Date parseTimestamp(String value, Date defaultValue) {
352
    if (StringUtils.isBlank(value)) {
353
      return defaultValue;
354
    }
355
    try {
356
      Date x = YYYYMMDDHHMMSS_DATE_FORMAT.parse(value);
357
      return new java.sql.Timestamp(x.getTime());
358
    } catch (ParseException ex) {
359
      this.getLogger().warn("Can't parse value '" + value + "' as timestamp.", ex);
360
      return defaultValue;
361
    }
362
  }
363

    
364
  public Date parseTime(String value, Date defaultValue) {
365
    if (StringUtils.isBlank(value)) {
366
      return defaultValue;
367
    }
368
    try {
369
      Date x = HHMMSS_DATE_FORMAT.parse(value);
370
      return new java.sql.Time(x.getTime());
371
    } catch (ParseException ex) {
372
      this.getLogger().warn("Can't parse value '" + value + "' as time.", ex);
373
      return defaultValue;
374
    }
375
  }
376

    
377
  public Date parseDate(String value, Date defaultValue) {
378
    if (StringUtils.isBlank(value)) {
379
      return defaultValue;
380
    }
381
    try {
382
      Date x = YYYYMMDD_DATE_FORMAT.parse(value);
383
      return new java.sql.Date(x.getTime());
384
    } catch (ParseException ex) {
385
      this.getLogger().warn("Can't parse value '" + value + "' as date.", ex);
386
      return defaultValue;
387
    }
388
  }
389

    
390
}