001: /*
002: * Copyright (C) 2005 Joe Walnes.
003: * Copyright (C) 2006, 2007 XStream Committers.
004: * All rights reserved.
005: *
006: * The software in this package is published under the terms of the BSD
007: * style license a copy of which has been included with this distribution in
008: * the LICENSE.txt file.
009: *
010: * Created on 03. October 2005 by Joerg Schaible
011: */
012: package com.thoughtworks.xstream.converters.extended;
013:
014: import com.thoughtworks.xstream.converters.ConversionException;
015: import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter;
016:
017: import org.joda.time.DateTime;
018: import org.joda.time.DateTimeZone;
019: import org.joda.time.format.DateTimeFormatter;
020: import org.joda.time.format.ISODateTimeFormat;
021:
022: import java.util.Calendar;
023: import java.util.GregorianCalendar;
024: import java.util.Locale;
025: import java.util.TimeZone;
026:
027: /**
028: * A GregorianCalendarConverter conforming to the ISO8601 standard.
029: * http://www.iso.ch/iso/en/CatalogueDetailPage.CatalogueDetail?CSNUMBER=26780
030: *
031: * @author Mauro Talevi
032: * @author Jörg Schaible
033: * @since 1.1.3
034: */
035: public class ISO8601GregorianCalendarConverter extends
036: AbstractSingleValueConverter {
037: private static final DateTimeFormatter[] formattersUTC = new DateTimeFormatter[] {
038: ISODateTimeFormat.dateTime(),
039: ISODateTimeFormat.dateTimeNoMillis(),
040: ISODateTimeFormat.basicDateTime(),
041: ISODateTimeFormat.basicOrdinalDateTime(),
042: ISODateTimeFormat.basicOrdinalDateTimeNoMillis(),
043: ISODateTimeFormat.basicTime(),
044: ISODateTimeFormat.basicTimeNoMillis(),
045: ISODateTimeFormat.basicTTime(),
046: ISODateTimeFormat.basicTTimeNoMillis(),
047: ISODateTimeFormat.basicWeekDateTime(),
048: ISODateTimeFormat.basicWeekDateTimeNoMillis(),
049: ISODateTimeFormat.ordinalDateTime(),
050: ISODateTimeFormat.ordinalDateTimeNoMillis(),
051: ISODateTimeFormat.time(), ISODateTimeFormat.timeNoMillis(),
052: ISODateTimeFormat.tTime(),
053: ISODateTimeFormat.tTimeNoMillis(),
054: ISODateTimeFormat.weekDateTime(),
055: ISODateTimeFormat.weekDateTimeNoMillis(), };
056: private static final DateTimeFormatter[] formattersNoUTC = new DateTimeFormatter[] {
057: ISODateTimeFormat.basicDate(),
058: ISODateTimeFormat.basicOrdinalDate(),
059: ISODateTimeFormat.basicWeekDate(),
060: ISODateTimeFormat.date(), ISODateTimeFormat.dateHour(),
061: ISODateTimeFormat.dateHourMinute(),
062: ISODateTimeFormat.dateHourMinuteSecond(),
063: ISODateTimeFormat.dateHourMinuteSecondFraction(),
064: ISODateTimeFormat.dateHourMinuteSecondMillis(),
065: ISODateTimeFormat.hour(), ISODateTimeFormat.hourMinute(),
066: ISODateTimeFormat.hourMinuteSecond(),
067: ISODateTimeFormat.hourMinuteSecondFraction(),
068: ISODateTimeFormat.hourMinuteSecondMillis(),
069: ISODateTimeFormat.ordinalDate(),
070: ISODateTimeFormat.weekDate(), ISODateTimeFormat.year(),
071: ISODateTimeFormat.yearMonth(),
072: ISODateTimeFormat.yearMonthDay(),
073: ISODateTimeFormat.weekyear(),
074: ISODateTimeFormat.weekyearWeek(),
075: ISODateTimeFormat.weekyearWeekDay(), };
076:
077: public boolean canConvert(Class type) {
078: return type.equals(GregorianCalendar.class);
079: }
080:
081: public Object fromString(String str) {
082: for (int i = 0; i < formattersUTC.length; i++) {
083: DateTimeFormatter formatter = formattersUTC[i];
084: try {
085: DateTime dt = formatter.parseDateTime(str);
086: Calendar calendar = dt.toCalendar(Locale.getDefault());
087: calendar.setTimeZone(TimeZone.getDefault());
088: return calendar;
089: } catch (IllegalArgumentException e) {
090: // try with next formatter
091: }
092: }
093: String timeZoneID = TimeZone.getDefault().getID();
094: for (int i = 0; i < formattersNoUTC.length; i++) {
095: try {
096: DateTimeFormatter formatter = formattersNoUTC[i]
097: .withZone(DateTimeZone.forID(timeZoneID));
098: DateTime dt = formatter.parseDateTime(str);
099: Calendar calendar = dt.toCalendar(Locale.getDefault());
100: calendar.setTimeZone(TimeZone.getDefault());
101: return calendar;
102: } catch (IllegalArgumentException e) {
103: // try with next formatter
104: }
105: }
106: throw new ConversionException("Cannot parse date " + str);
107: }
108:
109: public String toString(Object obj) {
110: DateTime dt = new DateTime(obj);
111: return dt.toString(formattersUTC[0]);
112: }
113: }
|