You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rd...@apache.org on 2006/05/19 00:00:08 UTC

svn commit: r407644 - in /jakarta/commons/proper/betwixt/trunk: src/java/org/apache/commons/betwixt/ src/java/org/apache/commons/betwixt/expression/ src/test/org/apache/commons/betwixt/expression/ src/test/org/apache/commons/betwixt/poly/ xdocs/ xdocs/...

Author: rdonkin
Date: Thu May 18 15:00:07 2006
New Revision: 407644

URL: http://svn.apache.org/viewvc?rev=407644&view=rev
Log:
Improved default mapping for collection subclasses.

Added:
    jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/expression/CollectionUpdater.java
    jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/expression/TestCollectionUpdater.java
    jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/
    jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaList.java
    jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaOneImpl.java
    jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaTwoImpl.java
    jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/IAlpha.java
    jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/TestPolyList.java
Modified:
    jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/XMLIntrospector.java
    jakarta/commons/proper/betwixt/trunk/xdocs/guide/derived.xml
    jakarta/commons/proper/betwixt/trunk/xdocs/tasks.xml

Modified: jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/XMLIntrospector.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/XMLIntrospector.java?rev=407644&r1=407643&r2=407644&view=diff
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/XMLIntrospector.java (original)
+++ jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/XMLIntrospector.java Thu May 18 15:00:07 2006
@@ -37,6 +37,7 @@
 import org.apache.commons.betwixt.digester.MultiMappingBeanInfoDigester;
 import org.apache.commons.betwixt.digester.XMLBeanInfoDigester;
 import org.apache.commons.betwixt.digester.XMLIntrospectorHelper;
+import org.apache.commons.betwixt.expression.CollectionUpdater;
 import org.apache.commons.betwixt.expression.EmptyExpression;
 import org.apache.commons.betwixt.expression.IteratorExpression;
 import org.apache.commons.betwixt.expression.MapEntryAdder;
@@ -727,9 +728,12 @@
                 getLog().trace("Bean is loop");
                 ElementDescriptor loopDescriptor = new ElementDescriptor();
                 loopDescriptor.setCollective(true);
+                loopDescriptor.setHollow(true);
+                loopDescriptor.setSingularPropertyType(Object.class);
                 loopDescriptor.setContextExpression(
                     new IteratorExpression( EmptyExpression.getInstance() )
                 );
+                loopDescriptor.setUpdater(CollectionUpdater.getInstance());
                 if ( bean.isMapType() ) {
                     loopDescriptor.setQualifiedName( "entry" );
                 }

Added: jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/expression/CollectionUpdater.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/expression/CollectionUpdater.java?rev=407644&view=auto
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/expression/CollectionUpdater.java (added)
+++ jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/expression/CollectionUpdater.java Thu May 18 15:00:07 2006
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ * 
+ * Licensed 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.commons.betwixt.expression;
+
+import java.util.Collection;
+
+/**
+ * Updates a Collection by adding the new value to it.
+ */
+public class CollectionUpdater implements Updater {
+
+    private static CollectionUpdater INSTANCE;
+    
+    /**
+     * Gets singleton instance.
+     * @return <code>CollectionUpdater</code>, not null
+     */
+    public static synchronized CollectionUpdater getInstance() {
+         if (INSTANCE == null) {
+             INSTANCE = new CollectionUpdater();
+         }
+         return INSTANCE;
+    }
+    
+    /**
+     * Updates collection contained by the context by adding the new value.
+     * @param context <code>Context</code>, not null
+     * @param newValue value to be added, possibly null 
+     */
+    public void update(Context context, Object newValue) {
+            if (newValue != null) {
+                Object subject = context.getBean();
+                if (subject != null && subject instanceof Collection) {
+                    Collection collection = (Collection) subject;
+                    collection.add(newValue);
+                }
+            }
+    }
+}

Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/expression/TestCollectionUpdater.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/expression/TestCollectionUpdater.java?rev=407644&view=auto
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/expression/TestCollectionUpdater.java (added)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/expression/TestCollectionUpdater.java Thu May 18 15:00:07 2006
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ * 
+ * Licensed 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.commons.betwixt.expression;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests for {@link CollectionUpdater}.
+ */
+public class TestCollectionUpdater extends TestCase {
+    
+    private CollectionUpdater updater = CollectionUpdater.getInstance();
+    private Context context = new Context();
+   
+    protected void setUp() throws Exception {
+        super.setUp();
+        updater = CollectionUpdater.getInstance();
+        context = new Context();
+    }
+    
+    public void testUpdateNull() throws Exception {
+        context.setBean(null);
+        updater.update(context, null);
+        updater.update(context, "Whatever");
+    }
+
+    public void testUpdateNotCollection() throws Exception {
+        context.setBean("Whatever");
+        updater.update(context, null);
+        updater.update(context, "Whatever");
+    }
+    
+    public void testUpdateCollection() throws Exception {
+        List list = new ArrayList();
+        context.setBean(list);
+        updater.update(context, null);
+        updater.update(context, "Whatever");
+        assertEquals("Updater updates the list with the value", 1, list.size());
+        updater.update(context, "Thus");
+        assertEquals("Updater updates the list with the value",  2, list.size());
+        assertEquals("Updater updates the list in order", "Whatever", list.get(0));
+        assertEquals("Updater updates the list in order", "Thus", list.get(1));
+    }
+}

Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaList.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaList.java?rev=407644&view=auto
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaList.java (added)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaList.java Thu May 18 15:00:07 2006
@@ -0,0 +1,23 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ * 
+ * Licensed 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.commons.betwixt.poly;
+
+import java.util.*;
+
+public class AlphaList extends ArrayList {
+
+}

Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaOneImpl.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaOneImpl.java?rev=407644&view=auto
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaOneImpl.java (added)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaOneImpl.java Thu May 18 15:00:07 2006
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ * 
+ * Licensed 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.commons.betwixt.poly;
+
+public class AlphaOneImpl implements IAlpha {
+
+	public void alpha() {
+	}
+
+}

Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaTwoImpl.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaTwoImpl.java?rev=407644&view=auto
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaTwoImpl.java (added)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/AlphaTwoImpl.java Thu May 18 15:00:07 2006
@@ -0,0 +1,23 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ * 
+ * Licensed 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.commons.betwixt.poly;
+
+public class AlphaTwoImpl implements IAlpha {
+
+	public void alpha() {
+	}
+
+}

Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/IAlpha.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/IAlpha.java?rev=407644&view=auto
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/IAlpha.java (added)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/IAlpha.java Thu May 18 15:00:07 2006
@@ -0,0 +1,21 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ * 
+ * Licensed 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.commons.betwixt.poly;
+
+public interface IAlpha {
+
+	public void alpha();
+}

Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/TestPolyList.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/TestPolyList.java?rev=407644&view=auto
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/TestPolyList.java (added)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/poly/TestPolyList.java Thu May 18 15:00:07 2006
@@ -0,0 +1,97 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ * 
+ * Licensed 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.commons.betwixt.poly;
+
+import java.io.StringReader;
+import java.io.StringWriter;
+
+import org.apache.commons.betwixt.AbstractTestCase;
+import org.apache.commons.betwixt.BindingConfiguration;
+import org.apache.commons.betwixt.ElementDescriptor;
+import org.apache.commons.betwixt.XMLBeanInfo;
+import org.apache.commons.betwixt.XMLIntrospector;
+import org.apache.commons.betwixt.io.BeanReader;
+import org.apache.commons.betwixt.io.BeanWriter;
+import org.xml.sax.InputSource;
+
+public class TestPolyList extends AbstractTestCase {
+
+	public TestPolyList(String testName) {
+		super(testName);
+	}
+
+	private static final String XML = "<AlphaList>" + "<AlphaOneImpl/>"
+			+ "<AlphaTwoImpl/>" + "</AlphaList>";
+
+	private static final String MAPPING = "<?xml version='1.0'?>"
+			+ "<betwixt-config>" 
+			+ "  <class name='org.apache.commons.betwixt.poly.AlphaOneImpl'>"
+			+ "   <element name='AlphaOneImpl'/>" 
+			+ "  </class>"
+			+ "  <class name='org.apache.commons.betwixt.poly.AlphaTwoImpl'>"
+			+ "    <element name='AlphaTwoImpl'/>" 
+			+ "  </class>"
+			+ "</betwixt-config>";
+	
+	public void testWrite() throws Exception {
+		AlphaList bean = new AlphaList();
+		AlphaOneImpl one = new AlphaOneImpl();
+		bean.add(one);
+		AlphaTwoImpl two = new AlphaTwoImpl();
+		bean.add(two);
+
+		StringWriter out = new StringWriter();
+		BeanWriter writer = new BeanWriter(out);
+		StringReader mapping = new StringReader(MAPPING);
+		writer.getXMLIntrospector().register(new InputSource(mapping));
+		configure(writer.getBindingConfiguration());
+		writer.write(bean);
+	
+        xmlAssertIsomorphicContent(
+                parseString(XML),
+                parseString(out.getBuffer().toString()),
+                true);
+	}
+
+	public void testRead() throws Exception {
+		StringReader in = new StringReader(XML);
+		BeanReader reader = new BeanReader();
+		StringReader mapping = new StringReader(MAPPING);
+		reader.registerMultiMapping(new InputSource(mapping));
+        reader.registerBeanClass(AlphaList.class);
+		configure(reader.getBindingConfiguration());
+		Object bean = reader.parse(in);
+		assertTrue(bean instanceof AlphaList);
+		AlphaList list = (AlphaList) bean;
+		assertEquals(2, list.size());
+	}
+
+	public void testIntrospection() throws Exception	 {
+		XMLIntrospector introspector = new XMLIntrospector();
+		XMLBeanInfo beanInfo = introspector.introspect(AlphaList.class);
+		ElementDescriptor[] descriptors = beanInfo.getElementDescriptor().getElementDescriptors();
+		assertEquals("One descriptor", 1, descriptors.length);
+        assertTrue(descriptors[0].isHollow());
+        assertNotNull(descriptors[0].getContextExpression());
+        assertNotNull(descriptors[0].getUpdater());
+        assertEquals("A list can contain any object", Object.class, descriptors[0].getSingularPropertyType());
+	}
+
+	private void configure(BindingConfiguration configuration) {
+		configuration.setMapIDs(false);
+	}
+}

Modified: jakarta/commons/proper/betwixt/trunk/xdocs/guide/derived.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/xdocs/guide/derived.xml?rev=407644&r1=407643&r2=407644&view=diff
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/xdocs/guide/derived.xml (original)
+++ jakarta/commons/proper/betwixt/trunk/xdocs/guide/derived.xml Thu May 18 15:00:07 2006
@@ -237,5 +237,30 @@
         </p>
     </subsection>
 </section>
+<section name='Default Mapping For Collections Implementations'>
+	<p>
+The default mapping for a collection is the one created automatically
+by Betwixt. This is controlled by a number of general configuration settings.
+For some classes (including <code>Collection</code> implementations)
+special rules are applied. For collection implementations, these special
+rules ensure that the contents are written and polymophism is supported
+for reading.
+	</p>
+	<p>
+For example, suppose <code>AlphaList extends ArrayList</code>. It usually
+contains <code>BetaBean</code>'s and <code>GammaBean</code>'s. Use a 
+<a href='binding.html#Multi Mapping Document Format'>multi-mapping</a> contain
+mappings for just <code>BetaBean</code> and <code>GammaBean</code> and not
+<code>AlphaList</code>. This will force the default mapping to be used. 
+When reading, <code>AlphaList</code> should be registered after the 
+multimapping thus:
+	</p>
+<source><![CDATA[
+	BeanReader reader = ...
+	...
+	reader.registerMultiMapping(...);
+    reader.registerBeanClass(AlphaList.class);
+]]></source>
+</section>
 </body>
 </document>

Modified: jakarta/commons/proper/betwixt/trunk/xdocs/tasks.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/xdocs/tasks.xml?rev=407644&r1=407643&r2=407644&view=diff
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/xdocs/tasks.xml (original)
+++ jakarta/commons/proper/betwixt/trunk/xdocs/tasks.xml Thu May 18 15:00:07 2006
@@ -200,6 +200,9 @@
     <subsection name='Since 0.7'>            
         <ul>
           <li>
+Improved support for natural polymorphic mappings of collection subclasses.
+          </li>
+          <li>
 Added support for option inheritance between parent and target mappings. Issue #37542.
           </li>
         	<li>



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org