svn commit: r529716 - in /ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util: ObjectType.java StringUtil.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r529716 - in /ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util: ObjectType.java StringUtil.java

jaz-3
Author: jaz
Date: Tue Apr 17 12:06:45 2007
New Revision: 529716

URL: http://svn.apache.org/viewvc?view=rev&rev=529716
Log:
added methods to StringUtil to convert results of Map.toString() and List.toString() back to Map/List (assuming the contents are Strings for now); changed the ObjectType for Arrays->List conversion to only run when the type is List (per discussion on Dev list)

Modified:
    ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/ObjectType.java
    ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/StringUtil.java

Modified: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/ObjectType.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/ObjectType.java?view=diff&rev=529716&r1=529715&r2=529716
==============================================================================
--- ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/ObjectType.java (original)
+++ ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/ObjectType.java Tue Apr 17 12:06:45 2007
@@ -443,16 +443,6 @@
             return null;
         }
 
-        // do simple array to list conversion first (so that other checks can run against the updated object)
-        if (obj.getClass().isArray()) {
-            List newObj = FastList.newInstance();
-            int len = Array.getLength(obj);
-            for (int i = 0; i < len; i++) {
-                newObj.add(Array.get(obj, i));
-            }
-            obj = newObj;
-        }
-
         if (obj.getClass().getName().equals(type)) {
             return obj;
         }
@@ -465,7 +455,14 @@
 
         String fromType = null;
 
-        if (obj instanceof java.lang.String) {
+        if ((type.equals("List") || type.equals("java.util.List")) && obj.getClass().isArray()) {
+            List newObj = FastList.newInstance();
+            int len = Array.getLength(obj);
+            for (int i = 0; i < len; i++) {
+                newObj.add(Array.get(obj, i));
+            }
+            return newObj;        
+        } else if (obj instanceof java.lang.String) {
             fromType = "String";
             String str = (String) obj;
             if ("String".equals(type) || "java.lang.String".equals(type)) {
@@ -651,13 +648,24 @@
                     }
                 }
             } else if ("List".equals(type) || "java.util.List".equals(type)) {
-                List tempList = FastList.newInstance();
-                tempList.add(str);
-                return tempList;
+                if (str.startsWith("[") && str.endsWith("]")) {
+                    return StringUtil.toList(str);
+                } else {
+                    List tempList = FastList.newInstance();
+                    tempList.add(str);
+                    return tempList;
+                }
             } else if ("Set".equals(type) || "java.util.Set".equals(type)) {
-                Set tempSet = FastSet.newInstance();
-                tempSet.add(str);
-                return tempSet;
+                if (str.startsWith("[") && str.endsWith("]")) {
+                    return StringUtil.toSet(str);
+                } else {
+                    Set tempSet = FastSet.newInstance();
+                    tempSet.add(str);
+                    return tempSet;
+                }
+            } else if (("Map".equals(type) || "java.util.Map".equals(type)) &&
+                    (str.startsWith("{") && str.endsWith("}"))) {
+                return StringUtil.toMap(str);
             } else {
                 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
             }

Modified: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/StringUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/StringUtil.java?view=diff&rev=529716&r1=529715&r2=529716
==============================================================================
--- ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/StringUtil.java (original)
+++ ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/StringUtil.java Tue Apr 17 12:06:45 2007
@@ -20,6 +20,7 @@
 
 import javolution.util.FastList;
 import javolution.util.FastMap;
+import javolution.util.FastSet;
 
 import java.io.UnsupportedEncodingException;
 import java.net.URLDecoder;
@@ -226,6 +227,70 @@
             buf.append(encodedValue);
         }
         return buf.toString();
+    }
+
+    /**
+     * Reads a String version of a Map (should contain only strings) and creates a new Map
+     *
+     * @param s String value of a Map ({n1=v1, n2=v2})
+     * @return new Map
+     */
+    public static Map toMap(String s) {
+        Map newMap = FastMap.newInstance();
+        if (s.startsWith("{") && s.endsWith("}")) {
+            s = s.substring(1, s.length() - 1);
+            String[] entry = s.split("\\,\\s");
+            for (int i = 0; i < entry.length; i++) {
+                String[] nv = entry[i].split("\\=");
+                newMap.put(nv[0], nv[1]);
+            }
+        } else {
+            throw new IllegalArgumentException("String is not from Map.toString()");
+        }
+
+        return newMap;
+    }
+
+    /**
+     * Reads a String version of a List (should contain only strings) and creates a new List
+     *
+     * @param s String value of a Map ({n1=v1, n2=v2})
+     * @return new List
+     */
+    public static List toList(String s) {
+        List newList = FastList.newInstance();
+        if (s.startsWith("[") && s.endsWith("]")) {
+            s = s.substring(1, s.length() - 1);
+            String[] entry = s.split("\\,\\s");
+            for (int i = 0; i < entry.length; i++) {
+                newList.add(entry[i]);
+            }
+        } else {
+            throw new IllegalArgumentException("String is not from List.toString()");
+        }
+
+        return newList;
+    }
+
+    /**
+     * Reads a String version of a Set (should contain only strings) and creates a new Set
+     *
+     * @param s String value of a Map ({n1=v1, n2=v2})
+     * @return new List
+     */
+    public static Set toSet(String s) {
+        Set newSet = FastSet.newInstance();
+        if (s.startsWith("[") && s.endsWith("]")) {
+            s = s.substring(1, s.length() - 1);
+            String[] entry = s.split("\\,\\s");
+            for (int i = 0; i < entry.length; i++) {
+                newSet.add(entry[i]);
+            }
+        } else {
+            throw new IllegalArgumentException("String is not from Set.toString()");
+        }
+
+        return newSet;    
     }
 
     /**