It'd be something like this: <mapping abstract="true" type-name="mixed-set" type="java.util.Set" create-type="java.util.HashSet"> <collection ordered="false"> <structure name="option" marshaller="..." unmarshaller="..."/> <structure name="color" marshaller="..." unmarshaller="..."/> </collection> </mapping>
Then you'd reference this in the two places where you now have a <collection name="features" ...> with: <structure name="features" field="features" usage="optional" map-as="mixed-set"/> - Dennis Dennis M. Sosnoski SOA and Web Services in Java Training and Consulting http://www.sosnoski.com - http://www.sosnoski.co.nz Seattle, WA +1-425-939-0576 - Wellington, NZ +64-4-298-6117 Qazi, Karim wrote: > Thanks! > > Defining the extra <structure> element fixed my issue. > > However I did want to try to "normalize" the collection element so that > it could be reused using an "abstract" mapping which you eluded to. Can > you provide an example of this type of abstract mapping for a > java.util.Set which can then be reused with my two different type of > <structure> (color/option) elements? > > > > -----Original Message----- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf Of Dennis > Sosnoski > Sent: Sunday, February 03, 2008 5:10 PM > To: JiBX users > Subject: Re: [jibx-users] Un-Marshalling different object types in > collection using custom IdDefRefMapperBase not working > > Hi Karim, > > Your binding says that the <features> collections consists only of > <option> elements: > > <collection name="features" field="features" usage="optional" > create-type="java.util.HashSet"> > <structure name="option" > marshaller="com.edmunds.vehicle.jibx.VdmIdRefMapper" > unmarshaller="com.edmunds.vehicle.jibx.VdmIdRefMapper"/> > </collection> > > so when a different element start tag is found (<color>) you get an > exception. If you want to mix several different types of child elements > in the collections you're best off adding a <structure> definition for > each one, with a specific type="..." to show which Java type matches up > with each child element. If you do this, you'll probably also want to > add ordered="false" on the <collection> element, so that the child > elements can be in any order. > > I think you could also make your VdmIdRefMapper handle the element names > > for the items in the collection directly, and remove the name="option" > attribute from the <structure> definition. I won't guarantee this will > work properly (there are a lot of issues around the code generation not > knowing what element name to expect), but I think the current code > allows it. In this case you'd only have the single <structure> within > the <collection>, since the mapper would handle the element names as > appropriate to the actual object types. > > Since you've got the same collection information appearing in two > different places you may want to use an abstract <mapping> for > java.util.Set so you only define the <collection> details in one place. > You can then reference that abstract mapping where needed (replacing the > > <collection name="features" ...> elements in the current binding with > <struct name="features" ... map-as="setAbstractMapping">, for instance). > > - Dennis > > Dennis M. Sosnoski > SOA and Web Services in Java > Training and Consulting > http://www.sosnoski.com - http://www.sosnoski.co.nz > Seattle, WA +1-425-939-0576 - Wellington, NZ +64-4-298-6117 > > > > reemo wrote: > >> Anyone? I really need to figure out what I am doing wrong with this. >> >> Thanks. >> >> >> reemo wrote: >> >> >>> Hi, >>> >>> I have created my own custom IdDefRefMapperBase impl so that I can >>> have IDREF's on my collection. All the custom IdDefRefMapperBase is >>> doing is generating the correct "id" for the references by overriding >>> the getIdValue() . This is working fine for "marshalling" the >>> > objects > >>> out but when I try to read the same XML back in for "un-marshalling" >>> > I > >>> always get this error pasted below. You can see the marshalled xml >>> (below) that is having the error AS WELL as the xml that is NOT. You >>> will notice in the XML output that is NOT having the issue, that the >>> "<color/>" element is not present on the "<style/>" element. There >>> are ONLY <option> elements. >>> >>> What it seems like is happening as that the collection's can't >>> > support > >>> IDREF's with mulitple object types in the collection? Is this the >>> case? >>> >>> I have tried everyting and searched the mailing lists, JIRA, etc with >>> no luck on this issue. Please help! >>> >>> Thanks in advance! >>> >>> >>> ----------------- >>> Mapping file: >>> ------------------ >>> <mapping class="test.TestModelYear" name="modelYear"> >>> <collection create-type="java.util.HashSet" field="styles" >>> item-type="test.TestStyle"/> >>> >>> <collection name="features" field="features" usage="optional" >>> create-type="java.util.HashSet" >>> > >>> <structure name="option" >>> marshaller="com.edmunds.vehicle.jibx.VdmIdRefMapper" >>> >>> unmarshaller="com.edmunds.vehicle.jibx.VdmIdRefMapper"/> >>> </collection> >>> </mapping> >>> >>> <mapping class="test.TestStyle" name="style"> >>> <collection name="features" field="features" usage="optional" >>> create-type="java.util.HashSet" >>> > >>> <structure name="option" >>> marshaller="com.edmunds.vehicle.jibx.VdmIdRefMapper" >>> >>> unmarshaller="com.edmunds.vehicle.jibx.VdmIdRefMapper"/> >>> </collection> >>> </mapping> >>> >>> <mapping class="test.TestOption" name="option" > >>> <value style="attribute" name="path" field="path" >>> > ident="def"/> > >>> <value style="attribute" name="optionName" field="optionName" >>> usage="optional"/> >>> >>> </mapping> >>> >>> <mapping class="test.TestColor" name="color"> >>> <value style="attribute" name="path" field="path" >>> > ident="def"/> > >>> <value style="attribute" name="color" field="color" >>> usage="optional"/> >>> >>> </mapping> >>> >>> ------------- >>> Test Case: >>> -------------- >>> package com.edmunds.jibx; >>> >>> import junit.framework.TestCase; >>> import org.jibx.runtime.BindingDirectory; >>> import org.jibx.runtime.IBindingFactory; >>> import org.jibx.runtime.IMarshallingContext; >>> import org.jibx.runtime.IUnmarshallingContext; >>> import test.TestColor; >>> import test.TestModelYear; >>> import test.TestOption; >>> import test.TestStyle; >>> >>> import java.io.FileInputStream; >>> import java.io.FileOutputStream; >>> import java.util.HashSet; >>> import java.util.Set; >>> >>> >>> public class TestJibxMappings extends TestCase { >>> >>> private static final String TMP_SAMPLE_OUTPUT_XML_FILE = >>> "/tmp/sample-output.xml"; >>> >>> public TestModelYear generateObject() throws Exception { >>> >>> >>> TestModelYear modelYear = new TestModelYear(); >>> TestStyle style = new TestStyle(); >>> >>> TestOption option = new TestOption(); >>> option.setPath("/feature/100"); >>> option.setOptionName("Option 1"); >>> TestOption option2 = new TestOption(); >>> option2.setPath("/feature/101"); >>> option2.setOptionName("Option 2"); >>> TestColor color = new TestColor(); >>> color.setPath("/feature/102"); >>> color.setColor("Blue"); >>> >>> Set features = new HashSet(); >>> features.add(option); >>> features.add(option2); >>> features.add(color); >>> >>> modelYear.setFeatures(features); >>> >>> Set styles = new HashSet(); >>> style.setFeatures(features); >>> >>> styles.add(style); >>> modelYear.setStyles(styles); >>> >>> return modelYear; >>> } >>> >>> >>> private void marshallToFile() throws Exception { >>> TestModelYear modelYear = generateObject(); >>> IBindingFactory bindingFactory = >>> BindingDirectory.getFactory(TestModelYear.class); >>> IMarshallingContext mctx = >>> bindingFactory.createMarshallingContext(); >>> mctx.setIndent(2); >>> >>> mctx.marshalDocument(modelYear, "UTF-8", null, new >>> FileOutputStream(TMP_SAMPLE_OUTPUT_XML_FILE)); >>> } >>> >>> >>> public void testUnmarshallObjectsFromFile() { >>> try { >>> marshallToFile(); >>> IBindingFactory bindingFactory = >>> BindingDirectory.getFactory(TestModelYear.class); >>> IUnmarshallingContext unmarshallCtx = >>> bindingFactory.createUnmarshallingContext(); >>> TestModelYear modelYear = (TestModelYear) >>> unmarshallCtx.unmarshalDocument(new >>> FileInputStream(TMP_SAMPLE_OUTPUT_XML_FILE), "UTF-8"); >>> assert modelYear != null; >>> } catch (Exception e) { >>> e.printStackTrace(); >>> assert false; >>> } >>> >>> } >>> } >>> >>> >>> ---------------------------- >>> XML WITH-OUT ERROR: >>> ---------------------------- >>> <?xml version="1.0" encoding="UTF-8"?> >>> <modelYear> >>> <style> >>> <features> >>> <option path="/feature/100" optionName="Option 1"/> >>> <option path="/feature/101" optionName="Option 2"/> >>> </features> >>> </style> >>> <features> >>> <option ref="/feature/100"/> >>> <option ref="/feature/101"/> >>> </features> >>> </modelYear> >>> >>> >>> ---------------------------- >>> XML WITH ERROR: >>> ---------------------------- >>> <?xml version="1.0" encoding="UTF-8"?> >>> <modelYear> >>> <style> >>> <features> >>> <option path="/feature/100" optionName="Option 1"/> >>> <option path="/feature/101" optionName="Option 2"/> >>> <color path="/feature/102" color="Blue"/> <!-- this is the >>> > color > >>> element causing issue that I want --> >>> </features> >>> </style> >>> <features> >>> <option ref="/feature/100"/> >>> <option ref="/feature/101"/> >>> <option ref="/feature/102"/> <!-- ref to color element above >>> causing issue --> >>> </features> >>> </modelYear> >>> >>> --------- >>> Error: >>> --------- >>> org.jibx.runtime.JiBXException: Expected "features" end tag, found >>> "color" start tag (line 5, col 48) >>> at >>> >>> > org.jibx.runtime.impl.UnmarshallingContext.parsePastCurrentEndTag(Unmars > hallingContext.java:792) > >>> at test.TestStyle.JiBX_test_jibx_unmarshal_1_0(TestStyle.java) >>> at test.JiBX_test_jibxTestStyle_access2.unmarshal() >>> at test.JiBX_MungeAdapter.JiBX_test_jibx_unmarshal_1_0() >>> at >>> > test.TestModelYear.JiBX_test_jibx_unmarshal_1_2(TestModelYear.java) > >>> at test.JiBX_test_jibxTestModelYear_access2.unmarshal() >>> at >>> >>> > org.jibx.runtime.impl.UnmarshallingContext.unmarshalElement(Unmarshallin > gContext.java:2537) > >>> at >>> >>> > org.jibx.runtime.impl.UnmarshallingContext.unmarshalDocument(Unmarshalli > ngContext.java:2680) > >>> at >>> >>> > com.edmunds.jibx.TestJibxMappings.testUnmarshallObjectsFromFile(TestJibx > Mappings.java:98) > >>> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) >>> at >>> >>> > sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.jav > a:39) > >>> at >>> >>> > sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessor > Impl.java:25) > >>> at java.lang.reflect.Method.invoke(Method.java:324) >>> at >>> > org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:580) > >>> at org.testng.internal.Invoker.invokeMethod(Invoker.java:473) >>> at >>> > org.testng.internal.Invoker.invokeTestMethod(Invoker.java:567) > >>> at >>> > org.testng.internal.Invoker.invokeTestMethods(Invoker.java:834) > >>> at >>> >>> > org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker. > java:125) > >>> at >>> > org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) > >>> at org.testng.TestRunner.runWorkers(TestRunner.java:689) >>> at org.testng.TestRunner.privateRun(TestRunner.java:566) >>> at org.testng.TestRunner.run(TestRunner.java:466) >>> at org.testng.SuiteRunner.runTest(SuiteRunner.java:301) >>> at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:296) >>> at org.testng.SuiteRunner.privateRun(SuiteRunner.java:276) >>> at org.testng.SuiteRunner.run(SuiteRunner.java:191) >>> at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:808) >>> at org.testng.TestNG.runSuitesLocally(TestNG.java:776) >>> at org.testng.TestNG.run(TestNG.java:701) >>> at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:73) >>> at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:124) >>> >>> >>> > ------------------------------------------------------------------------ > - > >>> This SF.net email is sponsored by: Microsoft >>> Defy all challenges. Microsoft(R) Visual Studio 2008. >>> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ >>> _______________________________________________ >>> jibx-users mailing list >>> jibx-users@lists.sourceforge.net >>> https://lists.sourceforge.net/lists/listinfo/jibx-users >>> >>> >>> >>> >> >> > > ------------------------------------------------------------------------ > - > This SF.net email is sponsored by: Microsoft > Defy all challenges. Microsoft(R) Visual Studio 2008. > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > _______________________________________________ > jibx-users mailing list > jibx-users@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/jibx-users > > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Microsoft > Defy all challenges. Microsoft(R) Visual Studio 2008. > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > _______________________________________________ > jibx-users mailing list > jibx-users@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/jibx-users > > ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ _______________________________________________ jibx-users mailing list jibx-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jibx-users