Commits

Anonymous committed 93b523b

Backported Date handling in XWorkBasicConvertor from XWork2 to XWork 1_2 branch.
Updated Foo.java for the updated unit test.

git-svn-id: http://svn.opensymphony.com/svn/xwork/branches/xwork_1-2@1322e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (3)

src/java/com/opensymphony/xwork/util/XWorkBasicConverter.java

 public class XWorkBasicConverter extends DefaultTypeConverter {
 
     private static String MILLISECOND_FORMAT = ".SSS";
-
+    final private static SimpleDateFormat RFC3399_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
+    
     public Object convertValue(Map context, Object o, Member member, String s, Object value, Class toType) {
         Object result = null;
 
                 SimpleDateFormat dfmt = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT,
                         locale);
 
-                SimpleDateFormat[] fmts = {fullfmt, dtfmt, dfmt};
+                SimpleDateFormat[] fmts = {fullfmt, dtfmt, dfmt, RFC3399_FORMAT};
                 for (int i = 0; i < fmts.length; i++) {
                     try {
                         check = fmts[i].parse(sa);
                     } catch (ParseException ignore) {
                     }
                 }
-            } else {
-                df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
+            }
+            else if(java.util.Date.class == toType) {
+            	Date check = null;
+            	SimpleDateFormat d1 = (SimpleDateFormat)DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, locale);
+            	SimpleDateFormat d2 = (SimpleDateFormat)DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, locale);
+            	SimpleDateFormat d3 = (SimpleDateFormat)DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale);
+            	SimpleDateFormat[] dfs = {d1, d2, d3, RFC3399_FORMAT}; //added RFC 3339 date format (XW-473)
+            	for (int i = 0; i < dfs.length; i++) {
+            		try {
+            			check = dfs[i].parse(sa);
+            			df = dfs[i];
+            			if (check != null) {
+            				break;
+            			}
+            		}
+            		catch (ParseException ignore) {
+            		}
+            	}
+            }
+            //final fallback for dates without time
+            if (df == null){
+            	df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
             }
 
             try {

src/test/com/opensymphony/xwork/util/Foo.java

 
     Bar bar;
     Date birthday;
+    Date event;
+    Date meeting;
     Foo child;
     List cats;
     List moreCats;
     public boolean isUseful() {
         return useful;
     }
+
+	public Date getEvent()
+	{
+		return event;
+	}
+
+	public void setEvent(Date event)
+	{
+		this.event = event;
+	}
+
+	public Date getMeeting()
+	{
+		return meeting;
+	}
+
+	public void setMeeting(Date meeting)
+	{
+		this.meeting = meeting;
+	}
 }

src/test/com/opensymphony/xwork/util/OgnlUtilTest.java

 
         Map props = new HashMap();
         props.put("birthday", "02/12/1982");
+        // US style test
         OgnlUtil.setProperties(props, foo, context);
 
         Calendar cal = Calendar.getInstance();
         cal.set(Calendar.YEAR, 1982);
 
         assertEquals(cal.getTime(), foo.getBirthday());
+        
+        Locale.setDefault(Locale.UK);
+        //UK style test
+        props.put("event", "18/10/2006 14:23:45");
+        props.put("meeting", "09/09/2006 14:30");
+        OgnlUtil.setProperties(props, foo, context);
+        
+        cal = Calendar.getInstance();
+        cal.clear();
+        cal.set(Calendar.MONTH, Calendar.OCTOBER);
+        cal.set(Calendar.DAY_OF_MONTH, 18);
+        cal.set(Calendar.YEAR, 2006);
+        cal.set(Calendar.HOUR_OF_DAY, 14);
+        cal.set(Calendar.MINUTE, 23);
+        cal.set(Calendar.SECOND, 45);
+        
+        assertEquals(cal.getTime(), foo.getEvent());
+        
+        cal = Calendar.getInstance();
+        cal.clear();
+        cal.set(Calendar.MONTH, Calendar.SEPTEMBER);
+        cal.set(Calendar.DAY_OF_MONTH, 9);
+        cal.set(Calendar.YEAR, 2006);
+        cal.set(Calendar.HOUR_OF_DAY, 14);
+        cal.set(Calendar.MINUTE, 30);
+        
+        assertEquals(cal.getTime(), foo.getMeeting());
+        
+        Locale.setDefault(orig);
+         
         Locale.setDefault(orig);
+        
+        //test RFC 3339 date format for JSON
+        props.put("event", "1996-12-19T16:39:57Z");
+        OgnlUtil.setProperties(props, foo, context);
+        
+        cal = Calendar.getInstance();
+        cal.clear();
+        cal.set(Calendar.MONTH, Calendar.DECEMBER);
+        cal.set(Calendar.DAY_OF_MONTH, 19);
+        cal.set(Calendar.YEAR, 1996);
+        cal.set(Calendar.HOUR_OF_DAY, 16);
+        cal.set(Calendar.MINUTE, 39);
+        cal.set(Calendar.SECOND, 57);
+        
+        assertEquals(cal.getTime(), foo.getEvent());
     }
 
     public void testSetPropertiesInt() {
         // just do some of the 15 tests
         Map beans = OgnlUtil.getBeanMap(foo);
         assertNotNull(beans);
-        assertEquals(15, beans.size());
+        assertEquals(17, beans.size());
         assertEquals("Hello Santa", beans.get("title"));
         assertEquals(new Long("123"), beans.get("ALong"));
         assertEquals(new Integer("44"), beans.get("number"));