commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Jakarta-commons Wiki] Update of "Betwixt/TipsAndHints/XsdDateTime" by JesseSweetland
Date Tue, 05 Sep 2006 14:39:03 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Jakarta-commons Wiki" for change notification.

The following page has been changed by JesseSweetland:
http://wiki.apache.org/jakarta-commons/Betwixt/TipsAndHints/XsdDateTime

------------------------------------------------------------------------------
  
  Note that if no flavor is specified, it defaults to dateTime format.  Also, when writing
dates, the timezone is always specified (which is highly recommended).
  
+ Betwixt allows options to be specified on attributes, and those options are actually parsed
and stored on the attribute, but the context that is passed to ObjectStringConverters is the
element context, and so the options on that context apply to the element and not the attribute.
 If all of your attributes only use one flavor of date/time, then you can move the option
up to the element level.  If, however, you need to support multiple flavors across many attributes,
here is a hacked version of the ObjectStringConverter above that "guesses" the correct XSD
format.  The algorithms used to guess formats are as follows:
+ 
+ === objectToString ===
+ 
+  * If the object is not null and is an instance of java.sql.Time, then use xsd:time
+  * If the object is not null and is an instance of java.sql.Date, then use xsd:date
+  * If the object is not null and is an instance of java.util.Date, then use xsd:dateTime
+  * Otherwise, call super.objectToString
+ 
+ === stringToObject ===
+ 
+  * If the string is not null has a ':' in the third position, then use xsd:time
+  * If the string is not null and has a '-' in the fifth position and does not contain a
'T', then use xsd:date
+  * If the string is not null and has a 'T' in the tenth position, then use xsd:dateTime
+  * Otherwise, call super.stringToObject
+ 
+ {{{
+ import java.text.*;
+ import java.util.*;
+ import org.apache.commons.betwixt.expression.*;
+ import org.apache.commons.betwixt.strategy.*;
+ 
+ /**
+  *
+  * @author jessesw
+  */
+ public class DateObjectStringConverter extends DefaultObjectStringConverter {
+     private static final DateFormat PSUEDO_ISO8601_DATETIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
+     private static final DateFormat PSUEDO_ISO8601_DATETIME_FORMAT_TZ = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
+     private static final DateFormat PSUEDO_ISO8601_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
+     private static final DateFormat PSUEDO_ISO8601_DATE_FORMAT_TZ = new SimpleDateFormat("yyyy-MM-ddZ");
+     private static final DateFormat PSUEDO_ISO8601_TIME_FORMAT = new SimpleDateFormat("HH:mm:ss");
+     private static final DateFormat PSUEDO_ISO8601_TIME_FORMAT_TZ = new SimpleDateFormat("HH:mm:ssZ");
+     
+     public String objectToString(Object object, Class type, Context context) {
+         if(object == null) return null;
+         
+         if(object instanceof java.sql.Time) {
+             return toIso8601Time((Date)object);
+         } else if(object instanceof java.sql.Date) {
+             return toIso8601Date((Date)object);
+         } else if(object instanceof java.util.Date) {
+             return toIso8601DateTime((Date)object);
+         } else {
+             return super.objectToString(object, type, context);
+         }
+     }
+     
+     public Object stringToObject(String value, Class type, Context context) {
+         if(value == null) return null;
+ 
+         if(value.trim().indexOf(':') == 2) {
+             return fromIso8601Time(value);
+         } else if((value.trim().indexOf('-') == 4) && (value.trim().indexOf('T')
< 0)) {
+             return fromIso8601Date(value);
+         } else if(value.trim().indexOf('T') == 10) {
+             return fromIso8601DateTime(value);
+         } else {
+             return super.stringToObject(value, type, context);
+         }
+     }
+ 
+     private String toIso8601DateTime(Date dateTime) {
+         if(dateTime == null) return null;
+         StringBuilder psuedoIso8601DateTime = new StringBuilder(PSUEDO_ISO8601_DATETIME_FORMAT_TZ.format(dateTime));
+         String iso8601DateTime = psuedoIso8601DateTime.insert(psuedoIso8601DateTime.length()
- 2, ':').toString();
+         return iso8601DateTime;
+     }
+     
+     private String toIso8601Date(Date date) {
+         if(date == null) return null;
+         StringBuilder psuedoIso8601Date = new StringBuilder(PSUEDO_ISO8601_DATE_FORMAT_TZ.format(date));
+         String iso8601Date = psuedoIso8601Date.insert(psuedoIso8601Date.length() - 2, ':').toString();
+         return iso8601Date;
+     }
+  
+     private String toIso8601Time(Date time) {
+         if(time == null) return null;
+         StringBuilder psuedoIso8601Time = new StringBuilder(PSUEDO_ISO8601_TIME_FORMAT_TZ.format(time));
+         String iso8601Time = psuedoIso8601Time.insert(psuedoIso8601Time.length() - 2, ':').toString();
+         return iso8601Time;
+     }
+     
+     private Date fromIso8601DateTime(String iso8601DateTime) {
+         if(iso8601DateTime == null) return null;
+         try {
+             iso8601DateTime = iso8601DateTime.trim();
+             boolean tzPresent = iso8601DateTime.length() > 19;
+             if(tzPresent) {
+                 String psuedoIso8601DateTime;
+                 if(iso8601DateTime.charAt(19) == 'Z') {
+                     psuedoIso8601DateTime = new StringBuilder(iso8601DateTime.substring(0,
19)).append("-0000").toString();
+                 } else {
+                     psuedoIso8601DateTime = new StringBuilder(iso8601DateTime).deleteCharAt(22).toString();
+                 }
+                 return PSUEDO_ISO8601_DATETIME_FORMAT_TZ.parse(psuedoIso8601DateTime);
+             } else {
+                 return PSUEDO_ISO8601_DATETIME_FORMAT.parse(iso8601DateTime);
+             }
+         } catch(Throwable t) {
+             throw new IllegalArgumentException(t);
+         }
+     }
+     
+     private Date fromIso8601Date(String iso8601Date) {
+         if(iso8601Date == null) return null;
+         try {
+             iso8601Date = iso8601Date.trim();
+             boolean tzPresent = iso8601Date.length() > 10;
+             if(tzPresent) {
+                 String psuedoIso8601Date;
+                 if(iso8601Date.charAt(10) == 'Z') {
+                     psuedoIso8601Date = new StringBuilder(iso8601Date.substring(0, 10)).append("-0000").toString();
+                 } else {
+                     psuedoIso8601Date = new StringBuilder(iso8601Date).deleteCharAt(13).toString();
+                 }
+                 return PSUEDO_ISO8601_DATE_FORMAT_TZ.parse(psuedoIso8601Date);
+             } else {
+                 return PSUEDO_ISO8601_DATE_FORMAT.parse(iso8601Date);
+             }
+         } catch(Throwable t) {
+             throw new IllegalArgumentException(t);
+         }
+     }
+  
+     private Date fromIso8601Time(String iso8601Time) {
+         if(iso8601Time == null) return null;
+         try {
+             iso8601Time = iso8601Time.trim();
+             boolean tzPresent = iso8601Time.length() > 8;
+             if(tzPresent) {
+                 String psuedoIso8601Time;
+                 if(iso8601Time.charAt(8) == 'Z') {
+                     psuedoIso8601Time = new StringBuilder(iso8601Time.substring(0, 8)).append("-0000").toString();
+                 } else {
+                     psuedoIso8601Time = new StringBuilder(iso8601Time).deleteCharAt(11).toString();
+                 }
+                 return PSUEDO_ISO8601_TIME_FORMAT_TZ.parse(psuedoIso8601Time);
+             } else {
+                 return PSUEDO_ISO8601_TIME_FORMAT.parse(iso8601Time);
+             }
+         } catch(Throwable t) {
+             throw new IllegalArgumentException(t);
+         }
+     }
+ }
+ }}}
+ 

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message