Anonymous avatar Anonymous committed 2ee6900

WW-673: WebWork type convert base class

git-svn-id: http://svn.opensymphony.com/svn/webwork/trunk@601 573baa09-0c28-0410-bef9-dab3c582ae83

Comments (0)

Files changed (1)

src/java/com/opensymphony/webwork/util/WebWorkTypeConverter.java

+package com.opensymphony.webwork.util;
+
+import ognl.DefaultTypeConverter;
+
+import java.util.Map;
+
+/**
+ * Base class for type converters used in WebWork. This class provides two abstract
+ * methods that are used to convert both to and from strings -- the critical
+ * functionality that is core to WebWork's type coversion system.
+ * <p/>
+ * Type converts do not have to use this class. It is merely a helper
+ * base class.
+ */
+public abstract class WebWorkTypeConverter extends DefaultTypeConverter {
+    public Object convertValue(Map context, Object o, Class toClass) {
+        if (toClass.equals(String.class)) {
+            return convertToString(context, o);
+        } else if (o instanceof String[]) {
+            return convertFromString(context, (String[]) o, toClass);
+        } else if (o instanceof String) {
+            return convertFromString(context, new String[]{(String) o}, toClass);
+        } else {
+            return super.convertValue(context, o, toClass);
+        }
+    }
+
+    /**
+     * Converts one or more String values to the specified class.
+     *
+     * @param context the action context
+     * @param values  the String values to be converted, such as those submitted from an HTML form
+     * @param toClass the class to convert to
+     * @return the converted object
+     */
+    public abstract Object convertFromString(Map context, String[] values, Class toClass);
+
+    /**
+     * Converts the specified object to a String.
+     *
+     * @param context the action context
+     * @param o       the object to be converted
+     * @return the converted String
+     */
+    public abstract String convertToString(Map context, Object o);
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.