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

History | View | Annotate | Download (10.4 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
      throw new BufferOverflowException();
201
    }
202
    return s;
203
  }
204

    
205
  public String format(double n, int size, int scale) {
206
    StringBuffer buf = this.getBuffer();
207
    NumberFormat nf = this.getNumberFormatter();
208

    
209
    buf.setLength(0); // Fast clear of buffer
210
    nf.setMaximumFractionDigits(scale);
211
    nf.format(n, buf, FIELDPOSITION);
212
    String s = buf.toString();
213
    if (s.length() > size) {
214
      throw new BufferOverflowException();
215
    }
216
    return s;
217
  }
218

    
219
  public String format(float n, int size, int scale) {
220
    StringBuffer buf = this.getBuffer();
221
    NumberFormat nf = this.getNumberFormatter();
222

    
223
    buf.setLength(0); // Fast clear of buffer
224
    nf.setMaximumFractionDigits(scale);
225
    nf.format(n, buf, FIELDPOSITION);
226
    String s = buf.toString();
227
    if (s.length() > size) {
228
      throw new BufferOverflowException();
229
    }
230
    return s;
231
  }
232

    
233
  public String parseString(String value, String defaultValue) {
234
    if (value == null || value.length() == 0) {
235
      return defaultValue;
236
    }
237
    return value;
238
  }
239

    
240
  public BigDecimal parseDecimal(String value, MathContext context, int scale, BigDecimal defaultValue) {
241
    if (StringUtils.isBlank(value)) {
242
      return defaultValue;
243
    }
244
    try {
245
      BigDecimal x = new BigDecimal(value, context);
246
      x.setScale(scale, scale);
247
      return x;
248
    } catch (Exception ex) {
249
      this.getLogger().warn("Can't parse value '" + value + "' as BigDecimal.", ex);
250
      return defaultValue;
251
    }
252
  }
253

    
254
  public Double parseDouble(String value, Double defaultValue) {
255
    if (StringUtils.isBlank(value)) {
256
      return defaultValue;
257
    }
258
    try {
259
      Double x = Double.parseDouble(value);
260
      return x;
261
    } catch (Exception ex) {
262
      this.getLogger().warn("Can't parse value '" + value + "' as double.", ex);
263
      return defaultValue;
264
    }
265
  }
266

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

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

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

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

    
319
  public Boolean parseBoolean(String value, Boolean defaultValue) {
320
    if (StringUtils.isBlank(value)) {
321
      return defaultValue;
322
    }
323
    switch (value.trim().charAt(0)) {
324
      case 'T':
325
      case 't':
326
        return true;
327
      case 'F':
328
      case 'f':
329
        return false;
330
      case ' ':
331
        return null;
332
      default:
333
        return defaultValue;
334
    }
335
  }
336

    
337
  public Date parseTimestamp(String value, Date defaultValue) {
338
    if (StringUtils.isBlank(value)) {
339
      return defaultValue;
340
    }
341
    try {
342
      Date x = YYYYMMDDHHMMSS_DATE_FORMAT.parse(value);
343
      return new java.sql.Timestamp(x.getTime());
344
    } catch (ParseException ex) {
345
      this.getLogger().warn("Can't parse value '" + value + "' as timestamp.", ex);
346
      return defaultValue;
347
    }
348
  }
349

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

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

    
376
}