Hi Joe, Sorry for the confusion, I didn't notice right away the your getters throw an exception if the value is of the other type.
--Keith "Doyle, Joe" wrote: > > That was the first thing I tried. The first thing I ran into was that using > "int" for elem1's type caused a MappingException to get thrown: "Could not > find the class int". Not sure what's going there. So I changed that to > integer, and it worked slightly better. > > But, I think tougher to work around, is the next problem: > > java.lang.IllegalStateException: Field access error: elem2(java.lang.String) > access resulted in exception: java.lang.reflect.InvocationTargetException > > Though the error message doesn't say it, I think it has to do with the > generated IDL throwing a BAD_OPERATION exception when, for example, elem2() > is called when the union is set to ELEM1. > > I was hoping for some way to say in the mapping file, "don't write an > element if this condition isn't true" but wasn't able to figure one out. So > I wrote a FieldHandler that returns the appropiate union field from > getValue. > > -----Original Message----- > From: Keith Visco [mailto:[EMAIL PROTECTED]] > Sent: Friday, December 06, 2002 2:27 PM > To: [EMAIL PROTECTED] > Subject: Re: [castor-dev] marshalling objects from classes generated > from IDL unions > > Just use a mapping file: > > <?xml version="1.0"?> > <mapping> > <class name="TestUnion"> > <map-to xml="testUnion"/> > <field name="elem1" type="int" set-method="elem1" > get-method="elem1"/> > <field name="elem2" type="string" set-method="elem2" > get-method="elem2"/> > </class> > </mapping> > > --Keith > > "Doyle, Joe" wrote: > > > > I'm looking for advice about writing a mapping file to go from classes > > generated from IDL unions to XML. This is my second day of looking at > > Castor, and am ready to go down the FieldHandler road, but would like to > > avoid it if someone has done this before. > > > > Based on this IDL, > > > > enum TestUnionType { > > ELEM1, > > ELEM2 > > }; > > > > union TestUnion switch (TestUnionType) { > > case ELEM1: long elem1; > > case ELEM2: string elem2; > > }; > > > > I'd like to generate XML that looks like either > > <testUnion> > > <elem1>10000</elem1> > > </testUnion> > > > > or > > > > <testUnion> > > <elem2>test</elem2> > > </testUnion> > > > > based on which TestUnionType is specified. > > > > Here's the Java mapping for the IDL: > > > > /** * Generated from IDL definition of union "TestUnion" > > * @author JacORB IDL compiler > > */ > > public final class TestUnion > > implements org.omg.CORBA.portable.IDLEntity > > { > > private TestUnionType discriminator; > > private java.lang.String elem2; > > private int elem1; > > public TestUnion () > > { } > > public TestUnionType discriminator () > > { return discriminator; } > > > > public java.lang.String elem2 () > > { > > if( discriminator != TestUnionType.ELEM2) > > throw new org.omg.CORBA.BAD_OPERATION(); > > return elem2; > > } > > > > public void elem2 (java.lang.String _x) > > { > > discriminator = TestUnionType.ELEM2; > > elem2 = _x; > > } > > > > public int elem1 () > > { > > if( discriminator != TestUnionType.ELEM1) > > throw new org.omg.CORBA.BAD_OPERATION(); > > return elem1; > > } > > > > public void elem1 (int _x) > > { > > discriminator = TestUnionType.ELEM1; > > elem1 = _x; > > } > > } > > > > ----------------------------------------------------------- > > If you wish to unsubscribe from this mailing, send mail to > > [EMAIL PROTECTED] with a subject of: > > unsubscribe castor-dev > > ----------------------------------------------------------- > If you wish to unsubscribe from this mailing, send mail to > [EMAIL PROTECTED] with a subject of: > unsubscribe castor-dev > > ----------------------------------------------------------- > If you wish to unsubscribe from this mailing, send mail to > [EMAIL PROTECTED] with a subject of: > unsubscribe castor-dev ----------------------------------------------------------- If you wish to unsubscribe from this mailing, send mail to [EMAIL PROTECTED] with a subject of: unsubscribe castor-dev
