Commits

Anonymous committed 13944b5

iterator and enumeration property accessors that fit better with webwork -- other stuff i can't remember

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@105e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (8)

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

 
                         //Ognl.getValue(OgnlUtil.compile((String) name), context, o);
                         Evaluation currentEvaluation = ognlContext.getCurrentEvaluation();
+
                         SimpleNode node = currentEvaluation.getNode();
                         currentEvaluation.setSource(o);
                         ognlContext.pushEvaluation(new Evaluation(node, o));

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

 import java.io.ObjectStreamException;
 import java.io.Serializable;
 import java.util.Map;
+import java.util.Iterator;
+import java.util.Enumeration;
 
 
 /**
     static {
         accessor = new CompoundRootAccessor();
         OgnlRuntime.setPropertyAccessor(CompoundRoot.class, accessor);
+        OgnlRuntime.setPropertyAccessor(Iterator.class, new XWorkIteratorPropertyAccessor());
+        OgnlRuntime.setPropertyAccessor(Enumeration.class, new XWorkEnumerationAcccessor());
         OgnlRuntime.setMethodAccessor(CompoundRoot.class, accessor);
         OgnlRuntime.setNullHandler(Object.class, new InstantiatingNullHandler());
     }

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

 import com.opensymphony.xwork.ActionContext;
 
 import ognl.DefaultTypeConverter;
+import ognl.OgnlRuntime;
 
 import java.lang.reflect.Member;
 
  * @version $Revision$
  */
 public class XWorkBasicConverter extends DefaultTypeConverter {
-    //~ Static fields/initializers /////////////////////////////////////////////
-
-    private static final Object OK_NULL_RESULT = new Object();
-
     //~ Methods ////////////////////////////////////////////////////////////////
 
     public Object convertValue(Map context, Object o, Member member, String s, Object value, Class toType) {
         Object result = null;
 
-        //short circuit conversion for empty strings... these are not conversion errors
-        if ("".equals(value)) {
-            result = (String.class.equals(toType)) ? "" : OK_NULL_RESULT;
-
-            return result;
-        }
-
         if (toType == String.class) {
             result = doConvertToString(context, value);
         } else if (toType == boolean.class) {
             }
         }
 
-        if (result == null) {
-            throw new TypeConversionException("Unable to convert value '" + value + "' to type " + toType.getName());
-        }
-
-        if (result == OK_NULL_RESULT) {
-            return null;
-        }
-
         return result;
     }
 
         return clazz;
     }
 
-    private Date doConvertToDate(Map context, Object value) {
+    private Object doConvertToDate(Map context, Object value) {
         Date result = null;
 
         if (value instanceof String) {
             try {
                 result = df.parse(sa);
             } catch (ParseException e) {
-                e.printStackTrace();
+                throw new RuntimeException("Could not parse date");
             }
         } else if (Date.class.isAssignableFrom(value.getClass())) {
             result = (Date) value;

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

 
 import com.opensymphony.xwork.ActionContext;
 
-import ognl.DefaultTypeConverter;
-import ognl.Evaluation;
-import ognl.OgnlContext;
-import ognl.TypeConverter;
+import ognl.*;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;

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

+package com.opensymphony.xwork.util;
+
+import ognl.EnumerationPropertyAccessor;
+import ognl.OgnlException;
+import ognl.ObjectPropertyAccessor;
+
+import java.util.Map;
+
+/**
+ * User: plightbo
+ * Date: Nov 13, 2003
+ * Time: 7:13:37 AM
+ */
+public class XWorkEnumerationAcccessor extends EnumerationPropertyAccessor {
+    ObjectPropertyAccessor opa = new ObjectPropertyAccessor();
+
+    public void setProperty(Map context, Object target, Object name, Object value) throws OgnlException {
+        opa.setProperty(context, target, name, value);
+    }
+}

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

+package com.opensymphony.xwork.util;
+
+import ognl.IteratorPropertyAccessor;
+import ognl.OgnlException;
+import ognl.ObjectPropertyAccessor;
+
+import java.util.Map;
+
+/**
+ * User: plightbo
+ * Date: Nov 13, 2003
+ * Time: 7:12:22 AM
+ */
+public class XWorkIteratorPropertyAccessor extends IteratorPropertyAccessor {
+    ObjectPropertyAccessor opa = new ObjectPropertyAccessor();
+
+    public void setProperty(Map context, Object target, Object name, Object value) throws OgnlException {
+        opa.setProperty(context, target, name, value);
+    }
+}

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

 		props.put("id", "");
 
 		OgnlUtil.setProperties(props, bar, context);
-		assertNull(bar.getId());
+        assertNotNull(bar.getId());
+        assertEquals(0, bar.getId().longValue());
 
 		props.put("id", new String[] {""});
 
 		bar.setId(null);
 		OgnlUtil.setProperties(props, bar, context);
-		assertNull(bar.getId());
+        assertNotNull(bar.getId());
+        assertEquals(0, bar.getId().longValue());
 	}
 
 	public void testValueStackSetValueEmptyStringAsLong() {
 		vs.push(bar);
 
 		vs.setValue("id", "");
-		assertNull(bar.getId());
+        assertNotNull(bar.getId());
+        assertEquals(0, bar.getId().longValue());
+
+        bar.setId(null);
 
 		vs.setValue("id", new String[]{""});
-		assertNull(bar.getId());
+        assertNotNull(bar.getId());
+        assertEquals(0, bar.getId().longValue());
 	}
 
 }

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

 
 import ognl.Ognl;
 import ognl.OgnlException;
+import ognl.OgnlRuntime;
 
 import java.math.BigDecimal;
 import java.math.BigInteger;
         stack.push(action);
         ActionContext.setContext(new ActionContext(stack.getContext()));
         ActionContext.getContext().put("report.conversion.errors", Boolean.TRUE);
-        assertNull("Conversion should have failed.", converter.convertValue(context, action, null, "date", new String[] {
+        assertEquals("Conversion should have failed.",
+                null,
+                converter.convertValue(context, action, null, "date", new String[]{
                     "invalid date"
                 }, Date.class));
         stack.pop();
         stack.push(action.getModel());
         ActionContext.setContext(new ActionContext(stack.getContext()));
         ActionContext.getContext().put("report.conversion.errors", Boolean.TRUE);
-        assertNull("Conversion should have failed.", converter.convertValue(context, action, null, "birth", new String[] {
+        assertEquals("Conversion should have failed.",
+                null,
+                converter.convertValue(context, action, null, "birth", new String[]{
                     "invalid date"
                 }, Date.class));
         stack.pop();
         assertEquals("Invalid date for birth.", ((List) action.getFieldErrors().get("birth")).get(0));
     }
 
-    public void testNoFieldErrorAddedForEmptyStringParameter() {
-        SimpleAction action = new SimpleAction();
-
-        OgnlValueStack stack = new OgnlValueStack();
-        stack.push(action);
-        ActionContext.setContext(new ActionContext(stack.getContext()));
-        assertNull("Conversion should have failed.", converter.convertValue(context, action, null, "bar", new String[] {
-                    ""
-                }, Integer.class));
-        stack.pop();
-        assertFalse(action.hasFieldErrors());
-    }
-
     public void testStringArrayToCollection() {
         ArrayList list = new ArrayList();
         list.add("foo");
         list.add("bar");
         list.add("baz");
-        assertEquals(list, converter.convertValue(context, null, null, null, new String[] {
-                    "foo", "bar", "baz"
-                }, Collection.class));
+        assertEquals(list, converter.convertValue(context, null, null, null, new String[]{
+            "foo", "bar", "baz"
+        }, Collection.class));
     }
 
     public void testStringArrayToList() {
         list.add("foo");
         list.add("bar");
         list.add("baz");
-        assertEquals(list, converter.convertValue(context, null, null, null, new String[] {
-                    "foo", "bar", "baz"
-                }, List.class));
+        assertEquals(list, converter.convertValue(context, null, null, null, new String[]{
+            "foo", "bar", "baz"
+        }, List.class));
     }
 
     public void testStringArrayToPrimitiveWrappers() {
-        Long[] longs = (Long[]) converter.convertValue(context, null, null, null, new String[] {
-                "123", "456"
-            }, Long[].class);
+        Long[] longs = (Long[]) converter.convertValue(context, null, null, null, new String[]{
+            "123", "456"
+        }, Long[].class);
         assertNotNull(longs);
-        assertTrue(Arrays.equals(new Long[] {new Long(123), new Long(456)}, longs));
+        assertTrue(Arrays.equals(new Long[]{new Long(123), new Long(456)}, longs));
 
-        Integer[] ints = (Integer[]) converter.convertValue(context, null, null, null, new String[] {
-                "123", "456"
-            }, Integer[].class);
+        Integer[] ints = (Integer[]) converter.convertValue(context, null, null, null, new String[]{
+            "123", "456"
+        }, Integer[].class);
         assertNotNull(ints);
-        assertTrue(Arrays.equals(new Integer[] {
-                    new Integer(123), new Integer(456)
-                }, ints));
+        assertTrue(Arrays.equals(new Integer[]{
+            new Integer(123), new Integer(456)
+        }, ints));
 
-        Double[] doubles = (Double[]) converter.convertValue(context, null, null, null, new String[] {
-                "123", "456"
-            }, Double[].class);
+        Double[] doubles = (Double[]) converter.convertValue(context, null, null, null, new String[]{
+            "123", "456"
+        }, Double[].class);
         assertNotNull(doubles);
-        assertTrue(Arrays.equals(new Double[] {new Double(123), new Double(456)}, doubles));
+        assertTrue(Arrays.equals(new Double[]{new Double(123), new Double(456)}, doubles));
 
-        Float[] floats = (Float[]) converter.convertValue(context, null, null, null, new String[] {
-                "123", "456"
-            }, Float[].class);
+        Float[] floats = (Float[]) converter.convertValue(context, null, null, null, new String[]{
+            "123", "456"
+        }, Float[].class);
         assertNotNull(floats);
-        assertTrue(Arrays.equals(new Float[] {new Float(123), new Float(456)}, floats));
+        assertTrue(Arrays.equals(new Float[]{new Float(123), new Float(456)}, floats));
 
-        Boolean[] booleans = (Boolean[]) converter.convertValue(context, null, null, null, new String[] {
-                "true", "false"
-            }, Boolean[].class);
+        Boolean[] booleans = (Boolean[]) converter.convertValue(context, null, null, null, new String[]{
+            "true", "false"
+        }, Boolean[].class);
         assertNotNull(booleans);
-        assertTrue(Arrays.equals(new Boolean[] {Boolean.TRUE, Boolean.FALSE}, booleans));
+        assertTrue(Arrays.equals(new Boolean[]{Boolean.TRUE, Boolean.FALSE}, booleans));
     }
 
     public void testStringArrayToPrimitives() throws OgnlException {
-        long[] longs = (long[]) converter.convertValue(context, null, null, null, new String[] {
-                "123", "456"
-            }, long[].class);
+        long[] longs = (long[]) converter.convertValue(context, null, null, null, new String[]{
+            "123", "456"
+        }, long[].class);
         assertNotNull(longs);
-        assertTrue(Arrays.equals(new long[] {123, 456}, longs));
+        assertTrue(Arrays.equals(new long[]{123, 456}, longs));
 
-        int[] ints = (int[]) converter.convertValue(context, null, null, null, new String[] {
-                "123", "456"
-            }, int[].class);
+        int[] ints = (int[]) converter.convertValue(context, null, null, null, new String[]{
+            "123", "456"
+        }, int[].class);
         assertNotNull(ints);
-        assertTrue(Arrays.equals(new int[] {123, 456}, ints));
+        assertTrue(Arrays.equals(new int[]{123, 456}, ints));
 
-        double[] doubles = (double[]) converter.convertValue(context, null, null, null, new String[] {
-                "123", "456"
-            }, double[].class);
+        double[] doubles = (double[]) converter.convertValue(context, null, null, null, new String[]{
+            "123", "456"
+        }, double[].class);
         assertNotNull(doubles);
-        assertTrue(Arrays.equals(new double[] {123, 456}, doubles));
+        assertTrue(Arrays.equals(new double[]{123, 456}, doubles));
 
-        float[] floats = (float[]) converter.convertValue(context, null, null, null, new String[] {
-                "123", "456"
-            }, float[].class);
+        float[] floats = (float[]) converter.convertValue(context, null, null, null, new String[]{
+            "123", "456"
+        }, float[].class);
         assertNotNull(floats);
-        assertTrue(Arrays.equals(new float[] {123, 456}, floats));
+        assertTrue(Arrays.equals(new float[]{123, 456}, floats));
 
-        boolean[] booleans = (boolean[]) converter.convertValue(context, null, null, null, new String[] {
-                "true", "false"
-            }, boolean[].class);
+        boolean[] booleans = (boolean[]) converter.convertValue(context, null, null, null, new String[]{
+            "true", "false"
+        }, boolean[].class);
         assertNotNull(booleans);
-        assertTrue(Arrays.equals(new boolean[] {true, false}, booleans));
+        assertTrue(Arrays.equals(new boolean[]{true, false}, booleans));
     }
 
     public void testStringArrayToSet() {
         list.add("foo");
         list.add("bar");
         list.add("baz");
-        assertEquals(list, converter.convertValue(context, null, null, null, new String[] {
-                    "foo", "bar", "bar", "baz"
-                }, Set.class));
+        assertEquals(list, converter.convertValue(context, null, null, null, new String[]{
+            "foo", "bar", "bar", "baz"
+        }, Set.class));
     }
 
     public void testStringToCustomTypeUsingCustomConverter() {