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 2010/07/03 16:44:31 UTC
svn commit: r960225 - in /pivot/trunk: core/src/org/apache/pivot/beans/
core/test/org/apache/pivot/beans/ tests/src/org/apache/pivot/tests/
Author: gbrown
Date: Sat Jul 3 14:44:31 2010
New Revision: 960225
URL: http://svn.apache.org/viewvc?rev=960225&view=rev
Log:
Remove nested serializer map from BXMLSerializer. This feature is no longer necessary since Bindable is better suited to this use case. Additionally, the path syntax used by BXMLSerializer#get() was not consistent with other aspects of the platform (it did not use the complete JSON path syntax supported by JSON.get()). Also, the behavior of BXMLSerializer#get() could be confusing. For example, given the following:
window.bxml:
<Window>
<bxml:include bxml:id="content" src="content.bxml"/>
</Window>
content.bxml:
<Label text="foo">
<bxml:script>
var text = "bar";
</bxml:script>
</Label>
A caller might expect get("content.text") to return "foo"; however, it would return "bar". A call to get("content['text']") would fail. With this change, "foo" would be returned, and the bracket-based path syntax will now work.
Lastly, this change will help facilitate support for serializing arbitrary objects via <bxml:include>. If the include tag is updated to support arbitrary serializers, there is no guarantee that a serializer will provide a namespace that can be accessed via path (in fact, most serializers will not).
Added:
pivot/trunk/core/test/org/apache/pivot/beans/
pivot/trunk/tests/src/org/apache/pivot/tests/NamespaceTest.java
pivot/trunk/tests/src/org/apache/pivot/tests/namespace_test.bxml
pivot/trunk/tests/src/org/apache/pivot/tests/namespace_test_content.bxml
Modified:
pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java
Modified: pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java?rev=960225&r1=960224&r2=960225&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java Sat Jul 3 14:44:31 2010
@@ -53,6 +53,7 @@ import org.apache.pivot.collections.Dict
import org.apache.pivot.collections.HashMap;
import org.apache.pivot.collections.List;
import org.apache.pivot.collections.Sequence;
+import org.apache.pivot.json.JSON;
import org.apache.pivot.serialization.SerializationException;
import org.apache.pivot.serialization.Serializer;
import org.apache.pivot.util.ListenerList;
@@ -64,15 +65,15 @@ import org.apache.pivot.util.Vote;
* Loads an object hierarchy from an XML document.
*/
public class BXMLSerializer implements Serializer<Object>, Dictionary<String, Object> {
- private class NamedObjectBindings implements Bindings {
+ private class NamespaceBindings implements Bindings {
@Override
public Object get(Object key) {
- return namedObjects.get(key.toString());
+ return namespace.get(key.toString());
}
@Override
public Object put(String key, Object value) {
- return namedObjects.put(key, value);
+ return namespace.put(key, value);
}
@Override
@@ -84,24 +85,24 @@ public class BXMLSerializer implements S
@Override
public Object remove(Object key) {
- return namedObjects.remove(key.toString());
+ return namespace.remove(key.toString());
}
@Override
public void clear() {
- namedObjects.clear();
+ namespace.clear();
}
@Override
public boolean containsKey(Object key) {
- return namedObjects.containsKey(key.toString());
+ return namespace.containsKey(key.toString());
}
@Override
public boolean containsValue(Object value) {
boolean contains = false;
- for (String key : namedObjects) {
- if (namedObjects.get(key).equals(value)) {
+ for (String key : namespace) {
+ if (namespace.get(key).equals(value)) {
contains = true;
break;
}
@@ -112,13 +113,13 @@ public class BXMLSerializer implements S
@Override
public boolean isEmpty() {
- return namedObjects.isEmpty();
+ return namespace.isEmpty();
}
@Override
public Set<String> keySet() {
java.util.HashSet<String> keySet = new java.util.HashSet<String>();
- for (String key : namedObjects) {
+ for (String key : namespace) {
keySet.add(key);
}
@@ -128,8 +129,8 @@ public class BXMLSerializer implements S
@Override
public Set<Entry<String, Object>> entrySet() {
java.util.HashMap<String, Object> hashMap = new java.util.HashMap<String, Object>();
- for (String key : namedObjects) {
- hashMap.put(key, namedObjects.get(key));
+ for (String key : namespace) {
+ hashMap.put(key, namespace.get(key));
}
return hashMap.entrySet();
@@ -137,14 +138,14 @@ public class BXMLSerializer implements S
@Override
public int size() {
- return namedObjects.getCount();
+ return namespace.getCount();
}
@Override
public Collection<Object> values() {
java.util.ArrayList<Object> values = new java.util.ArrayList<Object>();
- for (String key : namedObjects) {
- values.add(namedObjects.get(key));
+ for (String key : namespace) {
+ values.add(namespace.get(key));
}
return values;
@@ -281,8 +282,7 @@ public class BXMLSerializer implements S
private String internalNamespacePrefix;
private Class<? extends Annotation> bindingAnnotationClass;
- private HashMap<String, Object> namedObjects;
- private HashMap<String, BXMLSerializer> namedSerializers;
+ private HashMap<String, Object> namespace;
private XMLInputFactory xmlInputFactory;
private ScriptEngineManager scriptEngineManager;
@@ -335,12 +335,10 @@ public class BXMLSerializer implements S
if (owner == null) {
inline = false;
- namedObjects = new HashMap<String, Object>();
- namedSerializers = new HashMap<String, BXMLSerializer>();
+ namespace = new HashMap<String, Object>();
} else {
inline = true;
- namedObjects = owner.namedObjects;
- namedSerializers = owner.namedSerializers;
+ namespace = owner.namespace;
}
clearNamespaceOnRead = !inline;
@@ -349,7 +347,7 @@ public class BXMLSerializer implements S
xmlInputFactory.setProperty("javax.xml.stream.isCoalescing", true);
scriptEngineManager = new javax.script.ScriptEngineManager();
- scriptEngineManager.setBindings(new NamedObjectBindings());
+ scriptEngineManager.setBindings(new NamespaceBindings());
}
public Resources getResources() {
@@ -430,8 +428,7 @@ public class BXMLSerializer implements S
// Reset the serializer
if (clearNamespaceOnRead) {
- namedObjects.clear();
- namedSerializers.clear();
+ namespace.clear();
}
root = null;
@@ -728,15 +725,6 @@ public class BXMLSerializer implements S
// Read the object
BXMLSerializer serializer = createSerializer(resources, inline ? this : null);
- if (element.id != null) {
- if (namedSerializers.containsKey(element.id)) {
- throw new SerializationException("Namespace ID " + element.id
- + " is already in use.");
- }
-
- namedSerializers.put(element.id, serializer);
- }
-
if (src.charAt(0) == '/') {
element.value = serializer.readObject(src.substring(1));
} else {
@@ -753,14 +741,14 @@ public class BXMLSerializer implements S
}
}
- // Add the value to the named objects map
+ // Add the value to the namespace
if (element.id != null) {
- if (namedObjects.containsKey(element.id)) {
+ if (namespace.containsKey(element.id)) {
throw new SerializationException("Element ID " + element.id
+ " is already in use.");
}
- namedObjects.put(element.id, element.value);
+ namespace.put(element.id, element.value);
// If the type has an ID property, use it
Class<?> type = element.value.getClass();
@@ -1157,112 +1145,62 @@ public class BXMLSerializer implements S
}
/**
- * Retrieves a named object.
+ * Gets a value from this serializer's namespace.
*
- * @param name
- * The name of the object, relative to this loader. The object's name is
- * the concatenation of its parent IDs and its ID, separated by periods
- * (e.g. "foo.bar.baz").
- *
- * @return The named object, or <tt>null</tt> if an object with the given
- * name does not exist. Use {@link #containsKey(String)} to distinguish
- * between the two cases.
+ * @param id
*/
@Override
- public Object get(String name) {
- if (name == null) {
- throw new IllegalArgumentException("name is null.");
- }
-
- Object value = null;
-
- int i = name.lastIndexOf('.');
- if (i == -1) {
- value = namedObjects.get(name);
- } else {
- String serializerName = name.substring(0, name.lastIndexOf('.'));
- String id = name.substring(serializerName.length() + 1);
- BXMLSerializer serializer = getSerializer(serializerName);
-
- if (serializer != null) {
- value = serializer.get(id);
- }
+ public Object get(String id) {
+ if (id == null) {
+ throw new IllegalArgumentException();
}
- return value;
+ return JSON.get(namespace, id);
}
+ /**
+ * Puts a value into this serializer's namespace.
+ *
+ * @param id
+ * @param value
+ */
@Override
- public Object put(String name, Object value) {
- if (name == null) {
- throw new IllegalArgumentException("name is null.");
- }
-
- Object previousValue;
-
- int i = name.lastIndexOf('.');
- if (i == -1) {
- previousValue = namedObjects.put(name, value);
- } else {
- String serializerName = name.substring(0, name.lastIndexOf('.'));
- String id = name.substring(serializerName.length() + 1);
- BXMLSerializer serializer = getSerializer(serializerName);
- previousValue = serializer.put(id, value);
+ public Object put(String id, Object value) {
+ if (id == null) {
+ throw new IllegalArgumentException();
}
clearNamespaceOnRead = false;
- return previousValue;
+ return JSON.put(namespace, id, value);
}
+ /**
+ * Removes a value from this serializer's namespace.
+ *
+ * @param id
+ */
@Override
- public Object remove(String name) {
- if (name == null) {
- throw new IllegalArgumentException("name is null.");
- }
-
- Object previousValue;
-
- int i = name.lastIndexOf('.');
- if (i == -1) {
- previousValue = namedObjects.remove(name);
- } else {
- String serializerName = name.substring(0, name.lastIndexOf('.'));
- String id = name.substring(serializerName.length() + 1);
- BXMLSerializer serializer = getSerializer(serializerName);
- previousValue = serializer.remove(id);
+ public Object remove(String id) {
+ if (id == null) {
+ throw new IllegalArgumentException();
}
- return previousValue;
+ return JSON.remove(namespace, id);
}
+ /**
+ * Tests this serializer's namespace for the existence of a given ID.
+ *
+ * @param id
+ */
@Override
- public boolean containsKey(String name) {
- if (name == null) {
- throw new IllegalArgumentException("name is null.");
- }
-
- boolean containsKey = false;
-
- int i = name.lastIndexOf('.');
- if (i == -1) {
- containsKey = namedObjects.containsKey(name);
- } else {
- String serializerName = name.substring(0, name.lastIndexOf('.'));
- String id = name.substring(serializerName.length() + 1);
- BXMLSerializer serializer = getSerializer(serializerName);
-
- if (serializer != null) {
- containsKey = serializer.containsKey(id);
- }
+ public boolean containsKey(String id) {
+ if (id == null) {
+ throw new IllegalArgumentException();
}
- return containsKey;
- }
-
- public boolean isEmpty() {
- return namedObjects.isEmpty()
- && namedSerializers.isEmpty();
+ return JSON.containsKey(namespace, id);
}
/**
@@ -1278,35 +1216,6 @@ public class BXMLSerializer implements S
}
/**
- * Retrieves a nested serializer.
- *
- * @param name
- * The name of the serializer, relative to this loader. The serializer's name
- * is the concatentation of its parent IDs and its ID, separated by periods
- * (e.g. "foo.bar.baz").
- *
- * @return The named serializer, or <tt>null</tt> if a serializer with the
- * given name does not exist.
- */
- public BXMLSerializer getSerializer(String name) {
- if (name == null) {
- throw new IllegalArgumentException("name is null.");
- }
-
- BXMLSerializer serializer = this;
- String[] path = name.split("\\.");
-
- int i = 0;
- int n = path.length;
- while (i < n && serializer != null) {
- String id = path[i++];
- serializer = serializer.namedSerializers.get(id);
- }
-
- return serializer;
- }
-
- /**
* Returns the location of the BXML most recently processed by this
* serializer.
*
Added: pivot/trunk/tests/src/org/apache/pivot/tests/NamespaceTest.java
URL: http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/NamespaceTest.java?rev=960225&view=auto
==============================================================================
--- pivot/trunk/tests/src/org/apache/pivot/tests/NamespaceTest.java (added)
+++ pivot/trunk/tests/src/org/apache/pivot/tests/NamespaceTest.java Sat Jul 3 14:44:31 2010
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pivot.tests;
+
+import org.apache.pivot.beans.BXMLSerializer;
+import org.apache.pivot.collections.Map;
+import org.apache.pivot.wtk.Application;
+import org.apache.pivot.wtk.DesktopApplicationContext;
+import org.apache.pivot.wtk.Display;
+import org.apache.pivot.wtk.Window;
+
+public class NamespaceTest implements Application {
+ private Window window = null;
+
+ @Override
+ public void startup(Display display, Map<String, String> properties)
+ throws Exception {
+ BXMLSerializer bxmlSerializer = new BXMLSerializer();
+ window = (Window)bxmlSerializer.readObject(this, "namespace_test.bxml");
+ System.out.println(bxmlSerializer.get("content.text"));
+ System.out.println(bxmlSerializer.get("content['text']"));
+ window.open(display);
+ }
+
+ @Override
+ public boolean shutdown(boolean optional) {
+ if (window != null) {
+ window.close();
+ }
+
+ return false;
+ }
+
+ @Override
+ public void resume() {
+ }
+
+
+ @Override
+ public void suspend() {
+ }
+
+ public static void main(String[] args) {
+ DesktopApplicationContext.main(NamespaceTest.class, args);
+ }
+}
Added: pivot/trunk/tests/src/org/apache/pivot/tests/namespace_test.bxml
URL: http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/namespace_test.bxml?rev=960225&view=auto
==============================================================================
--- pivot/trunk/tests/src/org/apache/pivot/tests/namespace_test.bxml (added)
+++ pivot/trunk/tests/src/org/apache/pivot/tests/namespace_test.bxml Sat Jul 3 14:44:31 2010
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements. See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to you under the Apache License,
+Version 2.0 (the "License"); you may not use this file except in
+compliance with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
+
+<Window title="Namespace Test" maximized="true"
+ xmlns:bxml="http://pivot.apache.org/bxml"
+ xmlns="org.apache.pivot.wtk">
+ <bxml:include bxml:id="content" src="namespace_test_content.bxml"/>
+</Window>
Added: pivot/trunk/tests/src/org/apache/pivot/tests/namespace_test_content.bxml
URL: http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/namespace_test_content.bxml?rev=960225&view=auto
==============================================================================
--- pivot/trunk/tests/src/org/apache/pivot/tests/namespace_test_content.bxml (added)
+++ pivot/trunk/tests/src/org/apache/pivot/tests/namespace_test_content.bxml Sat Jul 3 14:44:31 2010
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements. See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to you under the Apache License,
+Version 2.0 (the "License"); you may not use this file except in
+compliance with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
+
+<Label text="Foo"
+ xmlns:bxml="http://pivot.apache.org/bxml"
+ xmlns="org.apache.pivot.wtk">
+ <bxml:script>
+ var text="Bar";
+ </bxml:script>
+</Label>