gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dataTypes / impl / coercion / AbstractCoerceToDate.java @ 2078
History | View | Annotate | Download (4.34 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.text.DateFormat; |
27 |
import java.text.ParseException; |
28 |
import java.text.SimpleDateFormat; |
29 |
import java.util.Date; |
30 |
import java.util.Locale; |
31 |
|
32 |
import org.gvsig.tools.dataTypes.CoercionException; |
33 |
import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale; |
34 |
|
35 |
/**
|
36 |
* Abstract implementation for Date coercion classes. If the value is not a
|
37 |
* {@link Date}, it will use the {@link Object#toString()} method to convert the
|
38 |
* resulting {@link String} to a Date object.
|
39 |
*
|
40 |
* @author gvSIG Team
|
41 |
* @version $Id$
|
42 |
*/
|
43 |
public abstract class AbstractCoerceToDate implements CoercionWithLocale { |
44 |
|
45 |
@Override
|
46 |
public Object coerce(Object value) throws CoercionException { |
47 |
return coerce(value, Locale.getDefault()); |
48 |
} |
49 |
|
50 |
@Override
|
51 |
public Object coerce(Object value, Locale locale) throws CoercionException { |
52 |
return coerce(value, this.getFormatters(locale)); |
53 |
} |
54 |
|
55 |
protected Object coerce(Object value, DateFormat[] formatters) throws CoercionException { |
56 |
if( value == null ) { |
57 |
return null; |
58 |
} |
59 |
if ( value instanceof Date ) { |
60 |
return value;
|
61 |
} |
62 |
if (value instanceof Long) { |
63 |
return new Date((long)value); |
64 |
} |
65 |
|
66 |
String valueStr = value.toString();
|
67 |
if( valueStr == null ) { |
68 |
return null; |
69 |
} |
70 |
valueStr = valueStr.trim(); |
71 |
if( valueStr.length()==0 ) { |
72 |
return null; |
73 |
} |
74 |
if( valueStr.toLowerCase().equals("now") ) { |
75 |
return this.now(); |
76 |
} |
77 |
for (DateFormat formatter : formatters) { |
78 |
try {
|
79 |
Date d = formatter.parse(valueStr);
|
80 |
if( d!=null ) { |
81 |
return d;
|
82 |
} |
83 |
} catch (ParseException e) { |
84 |
// throw new CoercionException(getErrorMessage(formatters, valueStr),e);
|
85 |
} |
86 |
} |
87 |
throw new CoercionException(getErrorMessage(formatters, valueStr)); |
88 |
} |
89 |
|
90 |
private String getErrorMessage(DateFormat[] formatters, String value) throws CoercionException { |
91 |
StringBuilder builder = new StringBuilder(); |
92 |
builder.append("The value to coerce (\"");
|
93 |
builder.append(value); |
94 |
builder.append("\") has an invalid ");
|
95 |
builder.append(getDateType()); |
96 |
builder.append(" String format. The expected one is:");
|
97 |
for (DateFormat formatter : formatters) { |
98 |
String formatDesc = formatter instanceof SimpleDateFormat ? |
99 |
((SimpleDateFormat) formatter).toLocalizedPattern() :
|
100 |
formatter.toString(); |
101 |
builder.append(" '");
|
102 |
builder.append(formatDesc); |
103 |
builder.append("'");
|
104 |
} |
105 |
return builder.toString();
|
106 |
} |
107 |
|
108 |
/**
|
109 |
* Returns the {@link DateFormat} to apply when the value to coerce is not
|
110 |
* of Date type and it will be parsed as String.
|
111 |
*
|
112 |
* @param locale
|
113 |
* @return the {@link DateFormat} to apply to parse the value to coerce as
|
114 |
* {@link String}
|
115 |
*/
|
116 |
protected abstract DateFormat[] getFormatters(Locale locale); |
117 |
|
118 |
protected abstract Date now(); |
119 |
|
120 |
/**
|
121 |
* Returns the name of the Date type being coerced. Ex: Date, DateTime,
|
122 |
* Time. Only used for description when an error is produced when coercing
|
123 |
* the value.
|
124 |
*
|
125 |
* @return the name of the Date type being coerced
|
126 |
*/
|
127 |
protected abstract String getDateType(); |
128 |
|
129 |
} |