You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2009/09/28 14:25:08 UTC

svn commit: r819508 - in /incubator/pivot/trunk/wtk/src/org/apache/pivot: wtk/ButtonGroup.java wtkx/WTKXSerializer.java

Author: gbrown
Date: Mon Sep 28 12:25:07 2009
New Revision: 819508

URL: http://svn.apache.org/viewvc?rev=819508&view=rev
Log:
Process wtkx:id as soon as the associated instance is available (don't wait until closing tag for instance elements); fix NPE in ButtonGroup.

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

Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/ButtonGroup.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/ButtonGroup.java?rev=819508&r1=819507&r2=819508&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/ButtonGroup.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/ButtonGroup.java Mon Sep 28 12:25:07 2009
@@ -112,14 +112,18 @@
     }
 
     public void setSelection(Button selection) {
-        if (!contains(selection)) {
+        if (selection != null
+            && !contains(selection)) {
             throw new IllegalArgumentException();
         }
 
         Button previousSelection = this.selection;
         if (previousSelection != selection) {
             this.selection = selection;
-            selection.setSelected(true);
+
+            if (selection != null) {
+                selection.setSelected(true);
+            }
 
             buttonGroupListeners.selectionChanged(this, previousSelection);
         }

Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtkx/WTKXSerializer.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtkx/WTKXSerializer.java?rev=819508&r1=819507&r2=819508&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtkx/WTKXSerializer.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtkx/WTKXSerializer.java Mon Sep 28 12:25:07 2009
@@ -163,16 +163,18 @@
 
         public final Element parent;
         public final Type type;
+        public final String id;
         public final String tagName;
         public final int lineNumber;
         public final List<Attribute> attributes;
 
         public Object value;
 
-        public Element(Element parent, Type type, String tagName, int lineNumber,
+        public Element(Element parent, Type type, String id, String tagName, int lineNumber,
             List<Attribute> attributes, Object value) {
             this.parent = parent;
             this.type = type;
+            this.id = id;
             this.tagName = tagName;
             this.lineNumber = lineNumber;
             this.attributes = attributes;
@@ -182,23 +184,25 @@
 
     private static class Attribute {
         public final String namespaceURI;
-        public final String prefix;
         public final String localName;
         public final String value;
 
-        public Attribute(String namespaceURI, String prefix, String localName, String value) {
+        public Attribute(String namespaceURI, String localName, String value) {
             this.namespaceURI = namespaceURI;
-            this.prefix = prefix;
             this.localName = localName;
             this.value = value;
         }
     }
 
-    private class ElementInvocationHandler implements InvocationHandler {
+    private class AttributeInvocationHandler implements InvocationHandler {
         private ScriptEngine scriptEngine;
+        private String event;
+        private String script;
 
-        public ElementInvocationHandler(ScriptEngine scriptEngine) {
+        public AttributeInvocationHandler(ScriptEngine scriptEngine, String event, String script) {
             this.scriptEngine = scriptEngine;
+            this.event = event;
+            this.script = script;
         }
 
         @Override
@@ -207,16 +211,13 @@
             Object result = null;
 
             String methodName = method.getName();
-            Bindings bindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
-            if (bindings.containsKey(methodName)) {
-                Invocable invocable;
+            if (methodName.equals(event)) {
                 try {
-                    invocable = (Invocable)scriptEngine;
-                } catch (ClassCastException exception) {
-                    throw new SerializationException(exception);
+                    scriptEngine.eval(script);
+                } catch (ScriptException exception) {
+                    System.err.println(exception);
+                    System.err.println(script);
                 }
-
-                result = invocable.invokeFunction(methodName, args);
             }
 
             // If the function didn't return a value, return the default
@@ -231,17 +232,13 @@
 
             return result;
         }
-    };
+    }
 
-    private class AttributeInvocationHandler implements InvocationHandler {
+    private class ElementInvocationHandler implements InvocationHandler {
         private ScriptEngine scriptEngine;
-        private String event;
-        private String script;
 
-        public AttributeInvocationHandler(ScriptEngine scriptEngine, String event, String script) {
+        public ElementInvocationHandler(ScriptEngine scriptEngine) {
             this.scriptEngine = scriptEngine;
-            this.event = event;
-            this.script = script;
         }
 
         @Override
@@ -250,13 +247,16 @@
             Object result = null;
 
             String methodName = method.getName();
-            if (methodName.equals(event)) {
+            Bindings bindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
+            if (bindings.containsKey(methodName)) {
+                Invocable invocable;
                 try {
-                    scriptEngine.eval(script);
-                } catch (ScriptException exception) {
-                    System.err.println(exception);
-                    System.err.println(script);
+                    invocable = (Invocable)scriptEngine;
+                } catch (ClassCastException exception) {
+                    throw new SerializationException(exception);
                 }
+
+                result = invocable.invokeFunction(methodName, args);
             }
 
             // If the function didn't return a value, return the default
@@ -271,7 +271,7 @@
 
             return result;
         }
-    }
+    };
 
     private URL location = null;
     private Resources resources = null;
@@ -510,20 +510,31 @@
         String localName = reader.getLocalName();
 
         // Build attribute list; these will be processed in the close tag
+        String id = null;
         ArrayList<Attribute> attributes = new ArrayList<Attribute>();
 
         for (int i = 0, n = reader.getAttributeCount(); i < n; i++) {
-            String attributeNamespaceURI = reader.getAttributeNamespace(i);
-            if (attributeNamespaceURI == null) {
-                attributeNamespaceURI = reader.getNamespaceURI("");
-            }
-
             String attributePrefix = reader.getAttributePrefix(i);
             String attributeLocalName = reader.getAttributeLocalName(i);
             String attributeValue = reader.getAttributeValue(i);
 
-            attributes.add(new Attribute(attributeNamespaceURI,
-                attributePrefix, attributeLocalName, attributeValue));
+            if (attributePrefix != null
+                && attributePrefix.equals(WTKX_PREFIX)) {
+                if (attributeLocalName.equals(ID_ATTRIBUTE)) {
+                    id = attributeValue;
+                } else {
+                    throw new SerializationException(attributePrefix + ":" + attributeLocalName
+                        + " is not a valid attribute.");
+                }
+            } else {
+                String attributeNamespaceURI = reader.getAttributeNamespace(i);
+                if (attributeNamespaceURI == null) {
+                    attributeNamespaceURI = reader.getNamespaceURI("");
+                }
+
+                attributes.add(new Attribute(attributeNamespaceURI, attributeLocalName,
+                    attributeValue));
+            }
         }
 
         // Determine the type and value of this element
@@ -567,6 +578,12 @@
                     Class<?> type = Class.forName(className);
                     elementType = Element.Type.INSTANCE;
                     value = type.newInstance();
+
+                    // Add the value to the named objects map here so it is available to
+                    // sub-elements (rather than waiting until the close tag)
+                    if (id != null) {
+                        namedObjects.put(id, value);
+                    }
                 } catch (Exception exception) {
                     throw new SerializationException(exception);
                 }
@@ -611,7 +628,7 @@
         }
 
         Location xmlStreamLocation = reader.getLocation();
-        element = new Element(element, elementType, tagName, xmlStreamLocation.getLineNumber(),
+        element = new Element(element, elementType, id, tagName, xmlStreamLocation.getLineNumber(),
             attributes, value);
 
         // If this is the root, set it
@@ -628,7 +645,6 @@
         switch (element.type) {
             case INSTANCE:
             case INCLUDE: {
-                String id = null;
                 ArrayList<Attribute> instancePropertyAttributes = new ArrayList<Attribute>();
                 ArrayList<Attribute> staticPropertyAttributes = new ArrayList<Attribute>();
 
@@ -639,28 +655,18 @@
                     Resources resources = this.resources;
 
                     for (Attribute attribute : element.attributes) {
-                        if (attribute.prefix != null
-                            && attribute.prefix.equals(WTKX_PREFIX)) {
-                            if (attribute.localName.equals(ID_ATTRIBUTE)) {
-                                id = attribute.value;
-                            } else {
-                                throw new SerializationException(WTKX_PREFIX + ":" + attribute.localName
-                                    + " is not a valid attribute.");
-                            }
+                        if (attribute.localName.equals(INCLUDE_SRC_ATTRIBUTE)) {
+                            src = attribute.value;
+                        } else if (attribute.localName.equals(INCLUDE_RESOURCES_ATTRIBUTE)) {
+                            resources = new Resources(resources, attribute.value);
                         } else {
-                            if (attribute.localName.equals(INCLUDE_SRC_ATTRIBUTE)) {
-                                src = attribute.value;
-                            } else if (attribute.localName.equals(INCLUDE_RESOURCES_ATTRIBUTE)) {
-                                resources = new Resources(resources, attribute.value);
-                            } else {
-                                if (!Character.isUpperCase(attribute.localName.charAt(0))) {
-                                    throw new SerializationException("Instance property setters are not"
-                                        + " supported for " + WTKX_PREFIX + ":" + INCLUDE_TAG
-                                        + " " + " tag.");
-                                }
-
-                                staticPropertyAttributes.add(attribute);
+                            if (!Character.isUpperCase(attribute.localName.charAt(0))) {
+                                throw new SerializationException("Instance property setters are not"
+                                    + " supported for " + WTKX_PREFIX + ":" + INCLUDE_TAG
+                                    + " " + " tag.");
                             }
+
+                            staticPropertyAttributes.add(attribute);
                         }
                     }
 
@@ -672,8 +678,8 @@
 
                     // Read the object
                     WTKXSerializer serializer = new WTKXSerializer(resources);
-                    if (id != null) {
-                        includeSerializers.put(id, serializer);
+                    if (element.id != null) {
+                        includeSerializers.put(element.id, serializer);
                     }
 
                     if (src.charAt(0) == '/') {
@@ -682,7 +688,7 @@
                         element.value = serializer.readObject(new URL(location, src));
                     }
 
-                    if (id == null
+                    if (element.id == null
                         && !serializer.isEmpty()
                         && serializer.scriptEngineManager == null) {
                         System.err.println("Include \"" + src + "\" defines unreachable objects.");
@@ -690,34 +696,14 @@
                 } else {
                     // Process attributes looking for wtkx:id and all property setters
                     for (Attribute attribute : element.attributes) {
-                        if (attribute.prefix != null
-                            && attribute.prefix.equals(WTKX_PREFIX)) {
-                            if (attribute.localName.equals(ID_ATTRIBUTE)) {
-                                id = attribute.value;
-                            } else {
-                                throw new SerializationException(WTKX_PREFIX + ":" + attribute.localName
-                                    + " is not a valid attribute.");
-                            }
+                        if (Character.isUpperCase(attribute.localName.charAt(0))) {
+                            staticPropertyAttributes.add(attribute);
                         } else {
-                            if (Character.isUpperCase(attribute.localName.charAt(0))) {
-                                staticPropertyAttributes.add(attribute);
-                            } else {
-                                instancePropertyAttributes.add(attribute);
-                            }
+                            instancePropertyAttributes.add(attribute);
                         }
                     }
                 }
 
-                // If an ID was specified, add the value to the named object map
-                if (id != null) {
-                    if (id.length() == 0) {
-                        throw new IllegalArgumentException(WTKX_PREFIX + ":" + ID_ATTRIBUTE
-                            + " must not be null.");
-                    }
-
-                    namedObjects.put(id, element.value);
-                }
-
                 // Apply instance attributes
                 Dictionary<String, Object> dictionary;
                 if (element.value instanceof Dictionary<?, ?>) {
@@ -899,13 +885,6 @@
                 String src = null;
                 String language = this.language;
                 for (Attribute attribute : element.attributes) {
-                    if (attribute.prefix != null
-                        && attribute.prefix.length() > 0) {
-                        throw new SerializationException(attribute.prefix + ":" +
-                            attribute.localName + " is not a valid" + " attribute for the "
-                            + WTKX_PREFIX + ":" + SCRIPT_TAG + " tag.");
-                    }
-
                     if (attribute.localName.equals(SCRIPT_SRC_ATTRIBUTE)) {
                         src = attribute.value;
                     } else if (attribute.localName.equals(SCRIPT_LANGUAGE_ATTRIBUTE)) {