Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dataTypes / impl / coercion / CoerceToString.java @ 2297

History | View | Annotate | Download (9.47 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 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 2
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.tools.dataTypes.impl.coercion;
25

    
26
import java.math.BigDecimal;
27
import java.sql.Time;
28
import java.sql.Timestamp;
29
import java.text.NumberFormat;
30
import java.util.Date;
31
import java.util.Locale;
32
import java.text.DateFormat;
33
import java.text.DecimalFormatSymbols;
34
import java.text.SimpleDateFormat;
35
import java.time.format.DateTimeFormatter;
36
import java.time.format.FormatStyle;
37
import javax.swing.JFormattedTextField;
38
import org.gvsig.tools.dataTypes.AbstractCoercion;
39
import org.gvsig.tools.dataTypes.Coercion;
40
import org.gvsig.tools.dataTypes.CoercionContext;
41
import org.gvsig.tools.dataTypes.CoercionContextLocale;
42
import org.gvsig.tools.dataTypes.CoercionException;
43
import org.gvsig.tools.dataTypes.DataTypeUtils;
44

    
45
public class CoerceToString extends AbstractCoercion {
46

    
47
    @Override
48
    public Object coerce(Object value) throws CoercionException {
49
        if (value == null) {
50
            return null;
51
        }
52
        if (value instanceof CharSequence) {
53
            return value.toString();
54
        }
55
        if (value instanceof Number) {
56
            return coerce(value, DataTypeUtils.coerceContextDefaultDecimal());
57
        }
58
        return coerce(value, DataTypeUtils.coerceContextDefaultLocale());
59
    }
60

    
61
    @Override
62
    public Object coerce(Object value, CoercionContext context) throws CoercionException {
63
        if (value == null) {
64
            return null;
65
        }
66
        if (value instanceof CharSequence) {
67
            return value.toString();
68
        }
69
        if (value instanceof CharSequence) {
70
            return value.toString();
71
        }
72
        if (context == null) {
73
            if (value instanceof Number) {
74
                context = DataTypeUtils.coerceContextDefaultDecimal();
75
            } else {
76
                context = DataTypeUtils.coerceContextDefaultLocale();
77
            }
78
        }
79
        try {
80
            Locale locale;
81
            if (context instanceof CoercionContextLocale) {
82
                locale = ((CoercionContextLocale) context).locale();
83
            } else {
84
                locale = Locale.getDefault();
85
            }
86
            String s;
87
            if (value instanceof Time) {
88
                s = this.getTimeFormat(locale).format(value);
89

    
90
            } else if (value instanceof Timestamp) {
91
                s = this.getDateTimeFormat(locale).format(value);
92

    
93
            } else if (value instanceof java.sql.Date) {
94
                s = this.getDateFormat(locale).format(value);
95

    
96
            } else if (value instanceof Date) {
97
                s = this.getDateTimeFormat(locale).format(value);
98

    
99
            } else if (value instanceof Float || value instanceof Double) {
100
                double d = ((Number)value).doubleValue();
101
                NumberFormat nf = NumberFormat.getInstance(locale);
102
                if (d > 1.8014398509481984E16 || d < -1.8014398509481984E16) {
103
                    // Esto viene a ser la maxima precision de la mantisa, Math.pow(2, 54), 
104
                    // asi que para valores mas grandes utilizamos la notacion cientifica.
105
                    s = String.format("%E", d);
106
                } else {
107
                    nf.setMaximumFractionDigits(16);
108
                    nf.setGroupingUsed(false);
109
                    s = nf.format(((Number) value).doubleValue());
110
                }
111

    
112
            } else if (value instanceof BigDecimal) {
113
                DecimalFormatSymbols dfs = new DecimalFormatSymbols(locale);
114
                char decimalSeparator = dfs.getDecimalSeparator();
115
                s = ((BigDecimal) value).toPlainString();
116
                if (decimalSeparator!='.'){
117
                    s = s.replace('.', decimalSeparator);
118
                }
119
            } else if (value instanceof Locale) {
120
                s = CoerceToLocale.toString((Locale) value);
121

    
122
            } else {
123
                s = value.toString();
124
            }
125
            return s;
126
        } catch (Exception e) {
127
            throw new CoercionException(e);
128
        }
129

    
130
    }
131
    
132
    private DateFormat getTimeFormat(Locale locale) {
133
        DateFormat df = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
134
        try {
135
          SimpleDateFormat sdf = (SimpleDateFormat) df;
136
          // Forzamos a que las horas, minutos y segundos sean con dos digitos,
137
          // y los milisegundos con tres.
138
          String s = sdf.toPattern(); 
139
          if( !s.contains("HH") ) {
140
            if( s.contains("H") ) {
141
              s = s.replace("H", "HH");
142
            }
143
          }
144
          if( !s.contains("mm") ) {
145
            if( s.contains("m") ) {
146
              s = s.replace("m", "mm");
147
            }
148
          }
149
          if( !s.contains("ss") ) {
150
            if( s.contains("s") ) {
151
              s = s.replace("s", "ss");
152
            }
153
          }
154
          if( !s.contains("SSS") ) {
155
            if( s.contains("SS") ) {
156
              s = s.replace("SS", "SSS");
157
            } else if( s.contains("S") ) {
158
              s = s.replace("S", "SSS");
159
            }
160
          }
161
          sdf.applyPattern(s);
162
        } catch(Exception ex) {
163
            
164
        }
165
        return df;
166
    }
167
    
168
    private DateFormat getDateFormat(Locale locale) {
169
        DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
170
        try {
171
          SimpleDateFormat sdf = (SimpleDateFormat) df;
172
          String s = sdf.toPattern(); 
173
          // Forzamos a que el anyo sea con 4 digitos, dos el mes y dos el dia.
174
          if( !s.contains("yyyy") ) {
175
            if( s.contains("yy") ) {
176
              s = s.replace("yy", "yyyy");
177
            } else if( s.contains("y") ) {
178
              s = s.replace("y", "yyyy");
179
            }
180
          }
181
          if( !s.contains("dd") ) {
182
            if( s.contains("d") ) {
183
              s = s.replace("d", "dd");
184
            }
185
          }
186
          if( !s.contains("MM") ) {
187
            if( s.contains("M") ) {
188
              s = s.replace("M", "MM");
189
            }
190
          }
191
          sdf.applyPattern(s);
192
        } catch(Exception ex) {
193
            
194
        }
195
        return df;
196
    }
197

    
198
    private DateFormat getDateTimeFormat(Locale locale) {
199
        DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.MEDIUM, locale);
200
        try {
201
          SimpleDateFormat sdf = (SimpleDateFormat) df;
202
          String s = sdf.toPattern(); 
203
          // Forzamos a que el anyo sea con 4 digitos, dos el mes, dos el dia,
204
          // dos las horas, dos los minutos, dos los segundos y tres los milisegundos.
205
          if( !s.contains("yyyy") ) {
206
            if( s.contains("yy") ) {
207
              s = s.replace("yy", "yyyy");
208
            } else if( s.contains("y") ) {
209
              s = s.replace("y", "yyyy");
210
            }
211
          }
212
          if( !s.contains("dd") ) {
213
            if( s.contains("d") ) {
214
              s = s.replace("d", "dd");
215
            }
216
          }
217
          if( !s.contains("MM") ) {
218
            if( s.contains("M") ) {
219
              s = s.replace("M", "MM");
220
            }
221
          }
222
          if( !s.contains("HH") ) {
223
            if( s.contains("H") ) {
224
              s = s.replace("H", "HH");
225
            }
226
          }
227
          if( !s.contains("mm") ) {
228
            if( s.contains("m") ) {
229
              s = s.replace("m", "mm");
230
            }
231
          }
232
          if( !s.contains("ss") ) {
233
            if( s.contains("s") ) {
234
              s = s.replace("s", "ss");
235
            }
236
          }
237
          if( !s.contains("SSS") ) {
238
            if( s.contains("SS") ) {
239
              s = s.replace("SS", "SSS");
240
            } else if( s.contains("S") ) {
241
              s = s.replace("S", "SSS");
242
            }
243
          }
244
          sdf.applyPattern(s);
245
        } catch(Exception ex) {
246
            
247
        }
248
        return df;
249
    }
250

    
251
    public static void main(String[] args) throws CoercionException {
252
        Coercion toString = new CoerceToString();
253
        
254
        java.sql.Date d = new java.sql.Date(new java.util.Date().getTime());
255
        String x = (String) toString.coerce(d);
256
        System.out.println("Date");
257
        System.out.println(d);
258
        System.out.println(x);
259

    
260
        java.sql.Timestamp ts = new java.sql.Timestamp(new java.util.Date().getTime());
261
        x = (String) toString.coerce(ts);
262
        System.out.println("Timestamp");
263
        System.out.println(d);
264
        System.out.println(x);
265

    
266
        java.sql.Time t = new java.sql.Time(new java.util.Date().getTime());
267
        x = (String) toString.coerce(t);
268
        System.out.println("Time");
269
        System.out.println(d);
270
        System.out.println(x);
271

    
272
        java.util.Date d0 = new java.util.Date();
273
        x = (String) toString.coerce(d0);
274
        System.out.println("DateTime");
275
        System.out.println(d);
276
        System.out.println(x);
277

    
278
    }
279
}