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 01:29:44 UTC

svn commit: r774135 - in /incubator/pivot/trunk/wtk/src/pivot/wtkx: BindProcessor.java WTKXSerializer.java

Author: tvolkert
Date: Tue May 12 23:29:44 2009
New Revision: 774135

URL: http://svn.apache.org/viewvc?rev=774135&view=rev
Log:
Updated WTKXSerializer#resolveSource() to work with URL resolution

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

Modified: incubator/pivot/trunk/wtk/src/pivot/wtkx/BindProcessor.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtkx/BindProcessor.java?rev=774135&r1=774134&r2=774135&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtkx/BindProcessor.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtkx/BindProcessor.java Tue May 12 23:29:44 2009
@@ -262,7 +262,6 @@
             List<JCVariableDecl> strandedBindFields = annotationDossier.getStrandedBindFields();
 
             if (loadGroups != null || strandedBindFields != null) {
-                boolean foo = false;
                 // There is some bind work to be done in this class; start by
                 // creating the source code buffer
                 StringBuilder buf = new StringBuilder("class _A {");
@@ -305,7 +304,6 @@
                         }
 
                         if (compile != null && compile.booleanValue()) {
-                            foo = true;
                             FileObject sourceFile = null;
                             if (classDeclaration.sym != null) {
                                 sourceFile = classDeclaration.sym.sourcefile;
@@ -366,12 +364,10 @@
                                                 buf.append(String.format
                                                     ("object = __namedObjects.get(\"%s\");", bindName));
                                                 buf.append
-                                                    ("if (object == null) {");
+                                                    ("if (object == null) ");
                                                 buf.append(String.format
                                                     ("throw new pivot.wtkx.BindException(\"Element not found: %s.\");",
                                                     bindName));
-                                                buf.append
-                                                    ("}");
                                                 buf.append(String.format
                                                     ("%s = (%s)object;", bindFieldName, bindField.vartype.toString()));
                                             }
@@ -459,11 +455,9 @@
                                     buf.append(String.format
                                         ("object = wtkxSerializer.getObjectByName(\"%s\");", bindName));
                                     buf.append
-                                        ("if (object == null) {");
+                                        ("if (object == null) ");
                                     buf.append(String.format
                                         ("throw new pivot.wtkx.BindException(\"Element not found: %s.\");", bindName));
-                                    buf.append
-                                        ("}");
                                     buf.append(String.format
                                         ("%s = (%s)object;", bindFieldName, bindField.vartype.toString()));
                                 }
@@ -500,11 +494,9 @@
                         buf.append(String.format
                             ("object = namedObjects.get(\"%s\");", bindName));
                         buf.append
-                            ("if (object == null) {");
+                            ("if (object == null) ");
                         buf.append(String.format
                             ("throw new pivot.wtkx.BindException(\"Element not found: %s.\");", bindName));
-                        buf.append
-                            ("}");
                         buf.append(String.format
                             ("%s = (%s)object;", bindFieldName, bindField.vartype.toString()));
                     }
@@ -522,9 +514,6 @@
 
                 // Add the AST method declaration to our class
                 classDeclaration.defs = classDeclaration.defs.prepend(parsedMethodDeclaration);
-                if (foo) {
-                    System.out.println(parsedMethodDeclaration);
-                }
             }
         }
 

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=774135&r1=774134&r2=774135&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtkx/WTKXSerializer.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtkx/WTKXSerializer.java Tue May 12 23:29:44 2009
@@ -968,8 +968,10 @@
                                             attribute.localName.substring(1);
                                         String setterMethodName = BeanDictionary.SET_PREFIX + key;
 
-                                        buf.append(String.format("__%d.%s(%s);", element.ref,
-                                            setterMethodName, resolveSource(attribute.value, attributeType)));
+                                        buf.append(resolveSource(attribute.value, attributeType,
+                                            ++Element.counter));
+                                        buf.append(String.format("__%d.%s(__%d);", element.ref,
+                                            setterMethodName, Element.counter));
                                     }
                                 }
                             }
@@ -1210,101 +1212,117 @@
         return resolvedValue;
     }
 
-    private String resolveSource(String attributeValue, Class<?> propertyType)
+    private String resolveSource(String attributeValue, Class<?> propertyType, int ref)
         throws MalformedURLException {
-        String resolvedSource = null;
+        StringBuilder buf = new StringBuilder();
 
         if (propertyType == Boolean.class
             || propertyType == Boolean.TYPE) {
-            try {
-                resolvedSource = String.valueOf(Boolean.parseBoolean(attributeValue));
-            } catch(NumberFormatException exception) {
-                resolvedSource = "\"" + attributeValue + "\"";
-            }
+            buf.append(String.format("boolean __%d = %b;",
+                ref, Boolean.parseBoolean(attributeValue)));
         } else if (propertyType == Character.class
             || propertyType == Character.TYPE) {
             if (attributeValue.length() > 0) {
-                resolvedSource = "'" + attributeValue.charAt(0) + "'";
+                buf.append(String.format("char __%d = '%c';",
+                    ref, attributeValue.charAt(0)));
             }
         } else if (propertyType == Byte.class
             || propertyType == Byte.TYPE) {
             try {
-                resolvedSource = String.valueOf(Byte.parseByte(attributeValue));
+                buf.append(String.format("byte __%d = %d;",
+                    ref, Byte.parseByte(attributeValue)));
             } catch(NumberFormatException exception) {
-                resolvedSource = "\"" + attributeValue + "\"";
+                buf.append(String.format("String __%d = \"%s\";", ref, attributeValue));
             }
         } else if (propertyType == Short.class
             || propertyType == Short.TYPE) {
             try {
-                resolvedSource = String.valueOf(Short.parseShort(attributeValue));
+                buf.append(String.format("short __%d = %d;",
+                    ref, Short.parseShort(attributeValue)));
             } catch(NumberFormatException exception) {
-                resolvedSource = "\"" + attributeValue + "\"";
+                buf.append(String.format("String __%d = \"%s\";", ref, attributeValue));
             }
         } else if (propertyType == Integer.class
             || propertyType == Integer.TYPE) {
             try {
-                resolvedSource = String.valueOf(Integer.parseInt(attributeValue));
+                buf.append(String.format("int __%d = %d;",
+                    ref, Integer.parseInt(attributeValue)));
             } catch(NumberFormatException exception) {
-                resolvedSource = "\"" + attributeValue + "\"";
+                buf.append(String.format("String __%d = \"%s\";", ref, attributeValue));
             }
         } else if (propertyType == Long.class
             || propertyType == Long.TYPE) {
             try {
-                resolvedSource = String.valueOf(Long.parseLong(attributeValue));
+                buf.append(String.format("long __%d = %d;",
+                    ref, Long.parseLong(attributeValue)));
             } catch(NumberFormatException exception) {
-                resolvedSource = "\"" + attributeValue + "\"";
+                buf.append(String.format("String __%d = \"%s\";", ref, attributeValue));
             }
         } else if (propertyType == Float.class
             || propertyType == Float.TYPE) {
             try {
-                resolvedSource = String.valueOf(Float.parseFloat(attributeValue));
+                buf.append(String.format("float __%d = %f;",
+                    ref, Float.parseFloat(attributeValue)));
             } catch(NumberFormatException exception) {
-                resolvedSource = "\"" + attributeValue + "\"";
+                buf.append(String.format("String __%d = \"%s\";", ref, attributeValue));
             }
         } else if (propertyType == Double.class
             || propertyType == Double.TYPE) {
             try {
-                resolvedSource = String.valueOf(Double.parseDouble(attributeValue));
+                buf.append(String.format("double __%d = %f;",
+                    ref, Double.parseDouble(attributeValue)));
             } catch(NumberFormatException exception) {
-                resolvedSource = "\"" + attributeValue + "\"";
+                buf.append(String.format("String __%d = \"%s\";", ref, 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) + "\"";
+                            buf.append(String.format("String __%d = \"%s\";",
+                                ref, attributeValue.substring(1)));
                         } else {
-                            // TODO
-                            resolvedSource = "\"" + attributeValue + "\"";
+                            buf.append(String.format
+                                ("java.net.URL __%d = getClass().getResource(\"%s\");",
+                                ref, attributeValue.substring(1)));
                         }
                     }
                 } else if (attributeValue.charAt(0) == RESOURCE_KEY_PREFIX) {
                     if (attributeValue.length() > 1) {
                         if (attributeValue.charAt(1) == RESOURCE_KEY_PREFIX) {
-                            resolvedSource = "\"" + attributeValue.substring(1) + "\"";
+                            buf.append(String.format("String __%d = \"%s\";",
+                                ref, attributeValue.substring(1)));
                         } else {
                             // TODO
-                            resolvedSource = "\"" + attributeValue + "\"";
+                            buf.append(String.format("String __%d = \"%s\";", ref, attributeValue));
                         }
                     }
                 } else if (attributeValue.charAt(0) == OBJECT_REFERENCE_PREFIX) {
                     if (attributeValue.length() > 1) {
                         if (attributeValue.charAt(1) == OBJECT_REFERENCE_PREFIX) {
-                            resolvedSource = "\"" + attributeValue.substring(1) + "\"";
+                            buf.append(String.format("String __%d = \"%s\";",
+                                ref, attributeValue.substring(1)));
                         } else {
-                            resolvedSource = "__namedObjects.get(\"" + attributeValue.substring(1) + "\")";
+                            buf.append(String.format("%s __%d = (%s)__namedObjects.get(\"%s\");",
+                                propertyType.getName(), ref, propertyType.getName(),
+                                attributeValue.substring(1)));
                         }
                     }
                 } else {
-                    resolvedSource = "\"" + attributeValue + "\"";
+                    buf.append(String.format("String __%d = \"%s\";",
+                        ref, attributeValue));
                 }
             } else {
-                resolvedSource = "\"\"";
+                buf.append(String.format("String __%d = \"\";", ref));
             }
         }
 
-        return resolvedSource;
+        if (buf.length() == 0) {
+            // Fall-through case
+            buf.append(String.format("String __%d = null;", ref));
+        }
+
+        return buf.toString();
     }
 
     /**