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 2005/07/14 23:31:12 UTC
svn commit: r219109 - in /jakarta/commons/proper/betwixt/trunk:
src/test/org/apache/commons/betwixt/io/read/TestPolymorphic.java
xdocs/guide/derived.xml
Author: rdonkin
Date: Thu Jul 14 14:31:10 2005
New Revision: 219109
URL: http://svn.apache.org/viewcvs?rev=219109&view=rev
Log:
Added new test case to remind myself about polymorphic multi-mappings and to convince myself that it does what I wanted already. Improved documentation: added a common use case for polymorphic multi-mappings.
Added:
jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/io/read/TestPolymorphic.java
Modified:
jakarta/commons/proper/betwixt/trunk/xdocs/guide/derived.xml
Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/io/read/TestPolymorphic.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/io/read/TestPolymorphic.java?rev=219109&view=auto
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/io/read/TestPolymorphic.java (added)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/io/read/TestPolymorphic.java Thu Jul 14 14:31:10 2005
@@ -0,0 +1,133 @@
+/*
+ * Copyright 2005 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.io.read;
+
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.util.Iterator;
+
+import org.apache.commons.betwixt.AbstractTestCase;
+import org.apache.commons.betwixt.io.BeanReader;
+import org.apache.commons.betwixt.io.BeanWriter;
+import org.xml.sax.InputSource;
+
+import junit.framework.TestCase;
+
+/**
+ * @author <a href='http://jakarta.apache.org/commons'>Jakarta Commons Team</a> for <a href='http://www.apache.org'>The Apache Software Foundation</a>
+ */
+public class TestPolymorphic extends AbstractTestCase {
+
+ public TestPolymorphic(String arg0) {
+ super(arg0);
+ }
+
+ private static final String MAPPING = "<?xml version='1.0'?>" +
+ "<betwixt-config>" +
+ " <class name='org.apache.commons.betwixt.io.read.Animals'>" +
+ " <element name='animals'>" +
+ " <element property='animals' updater='addAnimal'/>" +
+ " </element>" +
+ " </class>" +
+ " <class name='org.apache.commons.betwixt.io.read.FerretBean'>" +
+ " <element name='ferret'>" +
+ " <addDefaults/>" +
+ " </element>" +
+ " </class>" +
+ " <class name='org.apache.commons.betwixt.io.read.CatBean'>" +
+ " <element name='cat'>" +
+ " <addDefaults/>" +
+ " </element>" +
+ " </class>" +
+ " <class name='org.apache.commons.betwixt.io.read.DogBean'>" +
+ " <element name='dog'>" +
+ " <addDefaults/>" +
+ " </element>" +
+ " </class>" +
+ "</betwixt-config>";
+
+ private static final String XML = "<?xml version='1.0'?>" +
+ " <animals> " +
+ " <ferret>" +
+ " <call>Dook</call>" +
+ " <colour>albino</colour>" +
+ " <latinName>Mustela putoris furo</latinName>" +
+ " <name>Lector</name>" +
+ " </ferret>" +
+ " <cat>" +
+ " <call>Meow</call>" +
+ " <colour>black</colour>" +
+ " <latinName>Felis catus</latinName>" +
+ " <name>Sam</name>" +
+ " </cat>" +
+ " <dog>" +
+ " <breed>mongrol</breed>" +
+ " <call>Woof</call>" +
+ " <latinName>Canis familiaris</latinName>" +
+ " <name>Bobby</name>" +
+ " <pedigree>false</pedigree>" +
+ " </dog>" +
+ " </animals>";
+
+ public void testWrite() throws Exception
+ {
+ Animals animals = new Animals();
+ animals.addAnimal(new FerretBean("albino", "Lector"));
+ animals.addAnimal(new CatBean("Sam", "black"));
+ animals.addAnimal(new DogBean(false, "mongrol", "Bobby"));
+
+ StringWriter out = new StringWriter();
+ out.write("<?xml version='1.0'?>");
+ BeanWriter writer = new BeanWriter(out);
+ writer.getBindingConfiguration().setMapIDs(false);
+ writer.getXMLIntrospector().register(new InputSource(new StringReader(MAPPING)));
+ writer.write(animals);
+ xmlAssertIsomorphic(parseString(XML), parseString(out), true);
+ }
+
+
+ public void testRead() throws Exception
+ {
+ StringReader in = new StringReader(XML);
+ BeanReader reader = new BeanReader();
+ reader.getBindingConfiguration().setMapIDs(false);
+ reader.getXMLIntrospector().register(new InputSource(new StringReader(MAPPING)));
+ reader.registerBeanClass(Animals.class);
+ Animals animals = (Animals) reader.parse(in);
+
+ assertNotNull(animals);
+ assertEquals(3, animals.size());
+
+ Iterator it = animals.getAnimals();
+ Object firstAnimal = it.next();
+ assertTrue("First animal is a ferret", firstAnimal instanceof FerretBean);
+ FerretBean ferret = (FerretBean) firstAnimal;
+ assertEquals("Ferret name", "Lector", ferret.getName());
+ assertEquals("Ferret colour", "albino", ferret.getColour());
+
+ Object secondAnimal = it.next();
+ assertTrue(secondAnimal instanceof CatBean);
+ CatBean cat = (CatBean) secondAnimal;
+ assertEquals("Cat name", "Sam", cat.getName());
+ assertEquals("Cat colour", "black", cat.getColour());
+
+ Object thirdAnimal = it.next();
+ assertTrue(thirdAnimal instanceof DogBean);
+ DogBean dog = (DogBean) thirdAnimal;
+ assertEquals("Dog pedigree", false, dog.isPedigree());
+ assertEquals("Dog name", "Bobby", dog.getName());
+ }
+}
Modified: jakarta/commons/proper/betwixt/trunk/xdocs/guide/derived.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/xdocs/guide/derived.xml?rev=219109&r1=219108&r2=219109&view=diff
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/xdocs/guide/derived.xml (original)
+++ jakarta/commons/proper/betwixt/trunk/xdocs/guide/derived.xml Thu Jul 14 14:31:10 2005
@@ -78,6 +78,84 @@
feature is not required, a custom bean creation chain should be used.
</p>
</subsection>
+ <subsection name='Mapping An Element To A Class Name'>
+ <p>
+A common variation on the above occurs when the java and the xml should be decoupled.
+So, no attributes with class names permitted. A common use case requires that different
+elements are associated with different types of bean. For example:
+ </p>
+<source><![CDATA[
+ <animals>
+ <ferret>
+ <call>Dook</call>
+ <colour>albino</colour>
+ <latinName>Mustela putoris furo</latinName>
+ <name>Lector</name>
+ </ferret>
+ <cat>
+ <call>Meow</call>
+ <colour>black</colour>
+ <latinName>Felis catus</latinName>
+ <name>Sam</name>
+ </cat>
+ <dog>
+ <breed>mongrol</breed>
+ <call>Woof</call>
+ <latinName>Canis familiaris</latinName>
+ <name>Bobby</name>
+ <pedigree>false</pedigree>
+ </dog>
+ </animals>
+]]>
+</source>
+ <p>
+To read this xml, the class corresponding to each element must be known.
+Of course, this is a <a href='binding.html#Mixed%20Collections%20-%20Guessing%20Element%20Names'>mixed collection</a>
+and so needs to be mapped as such. Then a suitable <a href='reading.html#Reading%20Polymorphic%20Mappings'>polymophoric
+mapping</a> needs to be created. For example:
+ </p>
+<source><![CDATA[
+<betwixt-config>
+ <class name='org.apache.commons.betwixt.io.read.Animals'>
+ <element name='animals'>
+<!--
+Note that an adder must be set but addDefaults will add a name
+(and so prevent a polymorphic mapping) and so should be avoided.
+-->
+ <element property='animals' updater='addAnimal'/>
+ </element>
+ </class>
+ <class name='org.apache.commons.betwixt.io.read.FerretBean'>
+ <element name='ferret'>
+ <addDefaults/>
+ </element>
+ </class>
+ <class name='org.apache.commons.betwixt.io.read.CatBean'>
+ <element name='cat'>
+ <addDefaults/>
+ </element>
+ </class>
+ <class name='org.apache.commons.betwixt.io.read.DogBean'>
+ <element name='dog'>
+ <addDefaults/>
+ </element>
+ </class>
+</betwixt-config>
+]]>
+</source>
+ <p>
+This <a href='binding.html#Multi%20Mapping'>multi mapping</a> should be registered with
+the <code>XMLIntrospector</code> before the beans are written or read. For example (read):
+ </p>
+<source><![CDATA[
+ BeanReader reader = new BeanReader();
+ ...
+ reader.getXMLIntrospector().register(new InputSource(new StringReader(MAPPING)));
+ reader.registerBeanClass(Animals.class);
+ Animals animals = (Animals) reader.parse(in);
+]]>
+</source>
+ </subsection>
</section>
<section name='Mapping Derived Beans'>
<subsection name="Bind Time Type Verses Introspection Time Type">
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org