001: package org.gomba.utils.convert;
002:
003: import java.util.Date;
004:
005: import org.apache.commons.beanutils.ConversionException;
006: import org.apache.commons.beanutils.Converter;
007: import org.w3c.util.DateParser;
008: import org.w3c.util.InvalidDateException;
009:
010: /**
011: * <p>
012: * {@link Converter}implementation that converts an incoming String into a
013: * <code>java.util.Date</code> object according to the ISO 8601 standard,
014: * optionally using a default value or throwing a {@link ConversionException}if
015: * a conversion error occurs.
016: * </p>
017: *
018: * <p>
019: * Supported date formats:
020: * <ul>
021: * <li>1997-07-16T19:20:30.45-02:00</li>
022: * <li>1997-07-16T19:20:30+01:00</li>
023: * <li>1997-07-16T19:20:30</li>
024: * <li>1997-07-16T19:20</li>
025: * <li>1997-07-16</li>
026: * <li>1997-07</li>
027: * <li>1997</li>
028: * </ul>
029: * </p>
030: *
031: * <p>
032: * This class makes use of <code>org.w3c.util.DateParser</code> to parse
033: * strings.
034: * </p>
035: *
036: * @author Flavio Tordini
037: * @version $Id: ISO8601DateConverter.java,v 1.1.1.1 2004/06/16 13:15:12
038: * flaviotordini Exp $
039: * @see <a
040: * href="http://www.w3.org/TR/NOTE-datetime">http://www.w3.org/TR/NOTE-datetime
041: * </a>
042: * @see <a
043: * href="http://dev.w3.org/cvsweb/java/classes/org/w3c/util/">http://dev.w3.org/cvsweb/java/classes/org/w3c/util/
044: * </a>
045: */
046:
047: public final class ISO8601DateConverter implements Converter {
048:
049: /**
050: * Create a {@link Converter}that will throw a {@link ConversionException}
051: * if a conversion error occurs.
052: */
053: public ISO8601DateConverter() {
054:
055: this .defaultValue = null;
056: this .useDefault = false;
057:
058: }
059:
060: /**
061: * Create a {@link Converter}that will return the specified default value
062: * if a conversion error occurs.
063: *
064: * @param defaultValue
065: * The default value to be returned
066: */
067: public ISO8601DateConverter(Object defaultValue) {
068:
069: this .defaultValue = defaultValue;
070: this .useDefault = true;
071:
072: }
073:
074: /**
075: * The default value specified to our Constructor, if any.
076: */
077: private Object defaultValue = null;
078:
079: /**
080: * Should we return the default value on conversion errors?
081: */
082: private boolean useDefault = true;
083:
084: /**
085: * Convert the specified input object into an output object of the specified
086: * type.
087: *
088: * @param type
089: * Data type to which this value should be converted
090: * @param value
091: * The input value to be converted
092: *
093: * @exception ConversionException
094: * if conversion cannot be performed successfully
095: */
096: public Object convert(Class type, Object value) {
097:
098: if (value == null) {
099: if (this .useDefault) {
100: return (this .defaultValue);
101: }
102: throw new ConversionException("No value specified");
103: }
104:
105: if (value instanceof Date) {
106: return (value);
107: } else if (value instanceof java.lang.String) {
108: String valueStr = (String) value;
109: // w3c DateParser defaults to 1970 for an empty string
110: if (valueStr.length() == 0) {
111: throw new ConversionException(
112: "Cannot convert an empty string to "
113: + Date.class);
114: }
115: try {
116: return DateParser.parse(valueStr);
117: } catch (InvalidDateException ide) {
118: throw new ConversionException(ide);
119: }
120: }
121:
122: if (this .useDefault) {
123: return (this .defaultValue);
124: }
125: throw new ConversionException("Cannot convert.");
126:
127: }
128:
129: }
|