Revision 2080 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/AbstractCoerceToDate.java

View differences:

AbstractCoerceToDate.java
28 28
import java.text.SimpleDateFormat;
29 29
import java.util.Date;
30 30
import java.util.Locale;
31
import org.gvsig.tools.dataTypes.AbstractCoercion;
31 32

  
32 33
import org.gvsig.tools.dataTypes.CoercionException;
33
import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale;
34
import org.gvsig.tools.dataTypes.DataTypeUtils;
35
import org.gvsig.tools.dataTypes.DataTypesManager;
36
import org.gvsig.tools.dataTypes.CoercionContext;
37
import org.gvsig.tools.dataTypes.CoercionContextLocale;
34 38

  
35 39
/**
36 40
 * Abstract implementation for Date coercion classes. If the value is not a
......
40 44
 * @author gvSIG Team
41 45
 * @version $Id$
42 46
 */
43
public abstract class AbstractCoerceToDate implements CoercionWithLocale {
47
public abstract class AbstractCoerceToDate extends AbstractCoercion {
44 48

  
45
    @Override
46
    public Object coerce(Object value) throws CoercionException {
47
        return coerce(value, Locale.getDefault());
49
  @Override
50
  public Object coerce(Object value) throws CoercionException {
51
    return coerce(value, DataTypeUtils.coerceContextDefaultLocale());
52
  }
53

  
54
  @Override
55
  public Object coerce(Object value, CoercionContext context) throws CoercionException {
56
    Locale locale;
57
    if (context instanceof CoercionContextLocale) {
58
      locale = ((CoercionContextLocale) context).locale();
59
    } else {
60
      locale = Locale.getDefault();
48 61
    }
62
    return coerce(value, this.getFormatters(locale));
63
  }
49 64

  
50
    @Override
51
    public Object coerce(Object value, Locale locale) throws CoercionException {
52
        return coerce(value, this.getFormatters(locale));
65
  protected Object coerce(Object value, DateFormat[] formatters) throws CoercionException {
66
    if (value instanceof Number) {
67
      return new Date(((Number)value).longValue());
53 68
    }
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;
69
    @SuppressWarnings("null")
70
    String valueStr = value.toString();
71
    if (valueStr == null) {
72
      return null;
73
    }
74
    valueStr = valueStr.trim();
75
    if (valueStr.length() == 0) {
76
      return null;
77
    }
78
    if (valueStr.toLowerCase().equals("now")) {
79
      return this.now();
80
    }
81
    try {
82
      Date d = this.valueOf(valueStr);
83
      if (d != null) {
84
        return d;
85
      }
86
    } catch (Exception ex) {
87
      // Continue
88
    }
89
    for (DateFormat formatter : formatters) {
90
      try {
91
        Date d = formatter.parse(valueStr);
92
        if (d != null) {
93
          return d;
61 94
        }
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));
95
      } catch (ParseException e) {
96
        // Continue
97
      }
88 98
    }
99
    throw new CoercionException(getErrorMessage(formatters, valueStr));
100
  }
89 101

  
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();
102
  private String getErrorMessage(DateFormat[] formatters, String value) throws CoercionException {
103
    StringBuilder builder = new StringBuilder();
104
    builder.append("The value to coerce (\"");
105
    builder.append(value);
106
    builder.append("\") has an invalid ");
107
    builder.append(getDateType());
108
    builder.append(" String format. The expected one is:");
109
    for (DateFormat formatter : formatters) {
110
      String formatDesc = formatter instanceof SimpleDateFormat
111
              ? ((SimpleDateFormat) formatter).toLocalizedPattern()
112
              : formatter.toString();
113
      builder.append(" '");
114
      builder.append(formatDesc);
115
      builder.append("'");
106 116
    }
117
    return builder.toString();
118
  }
107 119

  
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);
120
  /**
121
   * Returns the {@link DateFormat} to apply when the value to coerce is not of
122
   * Date type and it will be parsed as String.
123
   *
124
   * @param locale
125
   * @return the {@link DateFormat} to apply to parse the value to coerce as
126
   * {@link String}
127
   */
128
  protected abstract DateFormat[] getFormatters(Locale locale);
117 129

  
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();
130
  protected Date now() {
131
    Date d = new Date();
132
    return d;
133
  }
128 134

  
135
  protected Date valueOf(String value) {
136
    return null;
137
  }
138

  
139
  /**
140
   * Returns the name of the Date type being coerced. Ex: Date, DateTime, Time.
141
   * Only used for description when an error is produced when coercing the
142
   * value.
143
   *
144
   * @return the name of the Date type being coerced
145
   */
146
  protected abstract String getDateType();
147

  
129 148
}

Also available in: Unified diff