You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by tv...@apache.org on 2009/05/13 00:10:03 UTC

svn commit: r774113 - /incubator/pivot/trunk/wtk/src/pivot/wtkx/WTKXSerializer.java

Author: tvolkert
Date: Tue May 12 22:10:03 2009
New Revision: 774113

URL: http://svn.apache.org/viewvc?rev=774113&view=rev
Log:
Added resolveSource() method to WTKXSerializer

Modified:
    incubator/pivot/trunk/wtk/src/pivot/wtkx/WTKXSerializer.java

Modified: incubator/pivot/trunk/wtk/src/pivot/wtkx/WTKXSerializer.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtkx/WTKXSerializer.java?rev=774113&r1=774112&r2=774113&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtkx/WTKXSerializer.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtkx/WTKXSerializer.java Tue May 12 22:10:03 2009
@@ -820,7 +820,7 @@
 
                             try {
                                 // TODO Pass ClassLoader here
-                                Class<?> type = Class.forName(className);
+                                Class<?> type = Class.forName(className, false, getClass().getClassLoader());
                                 element = new Element(element, localName, Element.Type.INSTANCE,
                                     attributes, type, Element.counter);
                             } catch(Exception exception) {
@@ -966,11 +966,12 @@
                                         }
                                         */
                                     } else {
-                                        // TODO What about primitive setters?
-                                        Method setterMethod = BeanDictionary.getSetterMethod
-                                            (type, attribute.localName, String.class);
-                                        buf.append(String.format("__%d.%s(\"%s\");",
-                                            element.ref, setterMethod.getName(), attribute.value));
+                                        String key = Character.toUpperCase(attribute.localName.charAt(0)) +
+                                            attribute.localName.substring(1);
+                                        String setterMethodName = BeanDictionary.SET_PREFIX + key;
+
+                                        buf.append(String.format("__%d.%s(%s);", element.ref,
+                                            setterMethodName, resolveSource(attribute.value, attributeType)));
                                     }
                                 }
                             }
@@ -1211,6 +1212,103 @@
         return resolvedValue;
     }
 
+    private String resolveSource(String attributeValue, Class<?> propertyType)
+        throws MalformedURLException {
+        String resolvedSource = null;
+
+        if (propertyType == Boolean.class
+            || propertyType == Boolean.TYPE) {
+            try {
+                resolvedSource = String.valueOf(Boolean.parseBoolean(attributeValue));
+            } catch(NumberFormatException exception) {
+                resolvedSource = "\"" + attributeValue + "\"";
+            }
+        } else if (propertyType == Character.class
+            || propertyType == Character.TYPE) {
+            if (attributeValue.length() > 0) {
+                resolvedSource = "'" + attributeValue.charAt(0) + "'";
+            }
+        } else if (propertyType == Byte.class
+            || propertyType == Byte.TYPE) {
+            try {
+                resolvedSource = String.valueOf(Byte.parseByte(attributeValue));
+            } catch(NumberFormatException exception) {
+                resolvedSource = "\"" + attributeValue + "\"";
+            }
+        } else if (propertyType == Short.class
+            || propertyType == Short.TYPE) {
+            try {
+                resolvedSource = String.valueOf(Short.parseShort(attributeValue));
+            } catch(NumberFormatException exception) {
+                resolvedSource = "\"" + attributeValue + "\"";
+            }
+        } else if (propertyType == Integer.class
+            || propertyType == Integer.TYPE) {
+            try {
+                resolvedSource = String.valueOf(Integer.parseInt(attributeValue));
+            } catch(NumberFormatException exception) {
+                resolvedSource = "\"" + attributeValue + "\"";
+            }
+        } else if (propertyType == Long.class
+            || propertyType == Long.TYPE) {
+            try {
+                resolvedSource = String.valueOf(Long.parseLong(attributeValue));
+            } catch(NumberFormatException exception) {
+                resolvedSource = "\"" + attributeValue + "\"";
+            }
+        } else if (propertyType == Float.class
+            || propertyType == Float.TYPE) {
+            try {
+                resolvedSource = String.valueOf(Float.parseFloat(attributeValue));
+            } catch(NumberFormatException exception) {
+                resolvedSource = "\"" + attributeValue + "\"";
+            }
+        } else if (propertyType == Double.class
+            || propertyType == Double.TYPE) {
+            try {
+                resolvedSource = String.valueOf(Double.parseDouble(attributeValue));
+            } catch(NumberFormatException exception) {
+                resolvedSource = "\"" + attributeValue + "\"";
+            }
+        } else {
+            if (attributeValue.length() > 0) {
+                if (attributeValue.charAt(0) == URL_PREFIX) {
+                    if (attributeValue.length() > 1) {
+                        if (attributeValue.charAt(1) == URL_PREFIX) {
+                            resolvedSource = "\"" + attributeValue.substring(1) + "\"";
+                        } else {
+                            // TODO
+                            resolvedSource = "\"" + attributeValue + "\"";
+                        }
+                    }
+                } else if (attributeValue.charAt(0) == RESOURCE_KEY_PREFIX) {
+                    if (attributeValue.length() > 1) {
+                        if (attributeValue.charAt(1) == RESOURCE_KEY_PREFIX) {
+                            resolvedSource = "\"" + attributeValue.substring(1) + "\"";
+                        } else {
+                            // TODO
+                            resolvedSource = "\"" + attributeValue + "\"";
+                        }
+                    }
+                } else if (attributeValue.charAt(0) == OBJECT_REFERENCE_PREFIX) {
+                    if (attributeValue.length() > 1) {
+                        if (attributeValue.charAt(1) == OBJECT_REFERENCE_PREFIX) {
+                            resolvedSource = "\"" + attributeValue.substring(1) + "\"";
+                        } else {
+                            resolvedSource = "__namedObjects.get(\"" + attributeValue.substring(1) + "\")";
+                        }
+                    }
+                } else {
+                    resolvedSource = "\"" + attributeValue + "\"";
+                }
+            } else {
+                resolvedSource = "\"\"";
+            }
+        }
+
+        return resolvedSource;
+    }
+
     /**
      * Invokes a static property setter.
      *