In principle I am guessing one should be able to start with an xsd file Generate the Code using the Source Code generator with the option of not creating the descriptor files. Use the MappingTool using the Top level Class or Element to generate a mapping file and then in turn use this as the mapping file during the unmarshalling and marshalling process.
Well what I am noticing is that the Mapping tool does include all the "valid=true" and <array>-total attributes however it does not add any of the "extends= " attributes in the mapping file. So I am guessing this is a Bug I should file against the Mapping Tool. What I am kind of almost doing is a circular self reference check. Finally, I find out that by adding the extends attribute to the class element and properly adding all the elements did solve the problem. It is though a bit tricky cause I am now perplexed as to the case of my own more complicated project. Here I am not able to get this to work properly. I would say that it would really help if the Mapping tool could add in the proper extend statements. Thanks -----Original Message----- From: Keith Visco [mailto:[EMAIL PROTECTED] Sent: Tuesday, June 17, 2003 3:13 PM To: [EMAIL PROTECTED] Subject: Re: [castor-dev] SrcCode Generator Marshaller adding extra fields when using a Mapping File Gerald, If you're getting the extra attributes, that means Castor was unable to find the proper mapping for the respective object in the mapping file and reverting to default introspection of the object's class to automatically create a mapping. Normally when this happens it's because the user was using one of the static marshal methods and therefore the non-static mapping was inaccessible. But this is not what's happening in your case as your code is not using the static marshal method. Without having run your example, I can only venture a guess. It's most likely due to inheritence. For example, your concrete class appears to be com.u2d.ship.Shipper, but your mapped class appears to be com.u2d.shipgen.Shipper. This is why Castor adds the "xsi:type" attributes in your output, so that it can know the proper instance during unmarshalling. Castor is probably getting hung up on the inheritence and introspecting the com.u2d.ship.Shipper class , and unfortunately including superclass attributes that it shouldn't be including. This would definately be a bug if that's the case. A simple workaround would be the following to prevent this additional introspection: Add the following to your mapping file: <class name="com.u2d.ship.Shipper" extends="com.u2d.shipgen.Shipper"/> That should solve the problem. You might also want to file this as a bug in our bugzilla database (http://bugzilla.exolab.org), if that's indeed what is happening. Thanks, --Keith > "Gerald E. Bennett" wrote: > > This is meant as a response to the question I have placed earlier that > might have not been clear. > > I am having problems with the marshaller who's output has extra fields > and attributes such as "valid", "array-count" which are not specified > at all in the mapping file. > I have tried to use the sample code that Eitan provided Castor > example. > > Below is what I have done and an example of the xml Output I get: > > 1) Modified the build.xml to include generation of Mapping Files > 2) Deleted Descriptor Java files in the src: > com.u2d.shipgen.*Descriptor.java > 3) Modified tests to load the mapping file in the marshaller and > unmarshaller correctly. > 4) Added a Mapping file to the root: ModelMappingFile.xml > This file was produced by copying the Model.xml file which was > generated using the org.exolab.castor.tools.MappingTool > However this file was modified replacing all "cst:int" with > "cst:integer" and "cst:" with "" and all valid elements were > removed. Using the mapping file as is caused a > MappingException be thrown. > > Output: > basictest: > [java] junit.framework.AssertionFailedError: exception: Nested > error: org.exolab.castor.mapping.MappingException: Could not find the > class int > [java] at junit.framework.Assert.fail(Assert.java:47) > [java] at com.u2d.shipgen.BasicTest.testMarshalModel(Unknown > Source) > [java] at com.u2d.shipgen.BasicTest.main(Unknown Source) > [java] Exception in thread "main" > [java] Java Result: 1 > > I can't seem to figure out why the output for out/ship.xml when > running the BasicTest does not contain these additional extra elements > and attributes that are not specified in the binding file that are > procduced when running the test called BindingTest. > > This is the exact same problem I am having in my own code!! > > Thanks > > Sample Output > ship.xml > <shipper shipment-count="3" valid="true" > xsi:type="java:com.u2d.ship.Shipper"> > <name>Eitan</name> > <shipment price="4.7" valid="true" > xsi:type="java:com.u2d.ship.Shipment"> > <service valid="true" xsi:type="java:com.u2d.ship.Service"> > <name>Express 1 Day</name> > <content/> > </service> > <ship-spec valid="true" weight="10.0" > xsi:type="java:com.u2d.ship.ShipSpec"> > <to valid="true" xsi:type="java:com.u2d.ship.USAddr"> > <addr2>Apt. 234</addr2> > <zip>02345</zip> > <addr1>1232 81st St.</addr1> > <state>NY</state> > <city>New York</city> > </to> > <from valid="true" xsi:type="java:com.u2d.ship.USAddr"> > <addr2/> > <zip>78749</zip> > <addr1>2300 Roland Ct.</addr1> > <state>TX</state> > <city>Austin</city> > </from> > </ship-spec> > <carrier valid="true" service-count="4" > xsi:type="java:com.u2d.ship.Carrier"> > <service valid="true" xsi:type="java:com.u2d.ship.Service"> > <name>FedEx Ground</name> > <content/> > </service> > <service valid="true" xsi:type="java:com.u2d.ship.Service"> > <name>Express 1 Day</name> > <content/> > </service> > <service valid="true" xsi:type="java:com.u2d.ship.Service"> > <name>Express 2 Day</name> > <content/> > </service> > <service valid="true" xsi:type="java:com.u2d.ship.Service"> > <name>Express Saver (3 day)</name> > <content/> > </service> > <name>FedEx</name> > </carrier> > <tracknum>tbseabboaoon</tracknum> > </shipment> > <shipment price="11.61" valid="true" > xsi:type="java:com.u2d.ship.Shipment"> > <service valid="true" xsi:type="java:com.u2d.ship.Service"> > <name>UPS Ground</name> > <content/> > </service> > <ship-spec valid="true" weight="3.0" > xsi:type="java:com.u2d.ship.ShipSpec"> > <to valid="true" xsi:type="java:com.u2d.ship.USAddr"> > <addr2/> > <zip>74012</zip> > <addr1>1103 Lochness Dr.</addr1> > <state>TX</state> > <city>Tyler</city> > </to> > <from valid="true" xsi:type="java:com.u2d.ship.USAddr"> > <addr2/> > <zip>78749</zip> > <addr1>2300 Roland Ct.</addr1> > <state>TX</state> > <city>Austin</city> > </from> > </ship-spec> > <carrier valid="true" service-count="3" > xsi:type="java:com.u2d.ship.Carrier"> > <service valid="true" xsi:type="java:com.u2d.ship.Service"> > <name>UPS Ground</name> > <content/> > </service> > <service valid="true" xsi:type="java:com.u2d.ship.Service"> > <name>UPS 1 day</name> > <content/> > </service> > <service valid="true" xsi:type="java:com.u2d.ship.Service"> > <name>UPS 2 day</name> > <content/> > </service> > <name>UPS</name> > </carrier> > <tracknum>qzjjerrnqmbu</tracknum> > </shipment> > <shipment price="13.89" valid="true" > xsi:type="java:com.u2d.ship.Shipment"> > <service valid="true" xsi:type="java:com.u2d.ship.Service"> > <name>Express Saver (3 day)</name> > <content/> > </service> > <ship-spec valid="true" weight="15.0" > xsi:type="java:com.u2d.ship.ShipSpec"> > <to valid="true" xsi:type="java:com.u2d.ship.USAddr"> > <addr2>Apt. 234</addr2> > <zip>02345</zip> > <addr1>1232 81st St.</addr1> > <state>NY</state> > <city>New York</city> > </to> > <from valid="true" xsi:type="java:com.u2d.ship.USAddr"> > <addr2/> > <zip>74012</zip> > <addr1>1103 Lochness Dr.</addr1> > <state>TX</state> > <city>Tyler</city> > </from> > </ship-spec> > <carrier valid="true" service-count="4" > xsi:type="java:com.u2d.ship.Carrier"> > <service valid="true" xsi:type="java:com.u2d.ship.Service"> > <name>FedEx Ground</name> > <content/> > </service> > <service valid="true" xsi:type="java:com.u2d.ship.Service"> > <name>Express 1 Day</name> > <content/> > </service> > <service valid="true" xsi:type="java:com.u2d.ship.Service"> > <name>Express 2 Day</name> > <content/> > </service> > <service valid="true" xsi:type="java:com.u2d.ship.Service"> > <name>Express Saver (3 day)</name> > <content/> > </service> > <name>FedEx</name> > </carrier> > <tracknum>dytulnaqywmj</tracknum> > </shipment> > </shipper> > <carrier valid="true" service-count="4" > xsi:type="java:com.u2d.ship.Carrier"> > <service valid="true" xsi:type="java:com.u2d.ship.Service"> > <name>FedEx Ground</name> > <content/> > </service> > <service valid="true" xsi:type="java:com.u2d.ship.Service"> > <name>Express 1 Day</name> > <content/> > </service> > <service valid="true" xsi:type="java:com.u2d.ship.Service"> > <name>Express 2 Day</name> > <content/> > </service> > <service valid="true" xsi:type="java:com.u2d.ship.Service"> > <name>Express Saver (3 day)</name> > <content/> > </service> > <name>FedEx</name> > </carrier> > <carrier valid="true" service-count="3" > xsi:type="java:com.u2d.ship.Carrier"> > <service valid="true" xsi:type="java:com.u2d.ship.Service"> > <name>UPS Ground</name> > <content/> > </service> > <service valid="true" xsi:type="java:com.u2d.ship.Service"> > <name>UPS 1 day</name> > <content/> > </service> > <service valid="true" xsi:type="java:com.u2d.ship.Service"> > <name>UPS 2 day</name> > <content/> > </service> > <name>UPS</name> > </carrier> > </model> > > > > > The Mapping File: > > <mapping xmlns="http://castor.exolab.org/" > xmlns:cst="http://castor.exolab.org/"> > <description>Castor generated mapping file</description> <class > name="com.u2d.shipgen.Carrier" access="shared"> <description>Default > mapping for class com.u2d.shipgen.Carrier</description> > <map-to xml="carrier"/> > <field name="serviceCount" type="integer"> > <bind-xml name="service-count" node="attribute"/> > </field> > <field name="service" type="com.u2d.shipgen.Service" > collection="array"> > <bind-xml name="service" node="element"/> > </field> > <field name="name" type="java.lang.String"> > <bind-xml name="name" node="element"/> > </field> > </class> > <class name="com.u2d.shipgen.UsAddressType" access="shared"> > <description>Default mapping for class > com.u2d.shipgen.UsAddressType</description> > <map-to xml="us-address-type"/> > <field name="addr2" type="java.lang.String"> > <bind-xml name="addr2" node="element"/> > </field> > <field name="zip" type="java.lang.String"> > <bind-xml name="zip" node="element"/> > </field> > <field name="addr1" type="java.lang.String"> > <bind-xml name="addr1" node="element"/> > </field> > <field name="state" type="java.lang.String"> > <bind-xml name="state" node="element"/> > </field> > <field name="city" type="java.lang.String"> > <bind-xml name="city" node="element"/> > </field> > </class> > <class name="com.u2d.shipgen.Model" access="shared"> > <description>Default mapping for class > com.u2d.shipgen.Model</description> > <map-to xml="model"/> > <field name="carrierCount" type="integer"> > <bind-xml name="carrier-count" node="attribute"/> > </field> > <field name="shipperCount" type="integer"> > <bind-xml name="shipper-count" node="attribute"/> > </field> > <field name="shipper" type="com.u2d.shipgen.Shipper" > collection="array"> > <bind-xml name="shipper" node="element"/> > </field> > <field name="carrier" type="com.u2d.shipgen.Carrier" > collection="array"> > <bind-xml name="carrier" node="element"/> > </field> > </class> > <class name="com.u2d.shipgen.Shipper" access="shared"> > <description>Default mapping for class > com.u2d.shipgen.Shipper</description> > <map-to xml="shipper"/> > <field name="shipmentCount" type="integer"> > <bind-xml name="shipment-count" node="attribute"/> > </field> > <field name="name" type="java.lang.String"> > <bind-xml name="name" node="element"/> > </field> > <field name="shipment" type="com.u2d.shipgen.Shipment" > collection="array"> > <bind-xml name="shipment" node="element"/> > </field> > </class> > <class name="com.u2d.shipgen.Shipment" access="shared"> > <description>Default mapping for class > com.u2d.shipgen.Shipment</description> > <map-to xml="shipment"/> > <field name="price" type="float"> > <bind-xml name="price" node="attribute"/> > </field> > <field name="service" type="java.lang.Object"> > <bind-xml name="service" node="element"/> > </field> > <field name="shipSpec" type="com.u2d.shipgen.ShipSpec"> > <bind-xml name="ship-spec" node="element"/> > </field> > <field name="carrier" type="java.lang.Object"> > <bind-xml name="carrier" node="element"/> > </field> > <field name="tracknum" type="java.lang.String"> > <bind-xml name="tracknum" node="element"/> > </field> > </class> > <class name="com.u2d.shipgen.Service" access="shared"> > <description>Default mapping for class > com.u2d.shipgen.Service</description> > <map-to xml="service"/> > <field name="name" type="java.lang.String"> > <bind-xml name="name" node="element"/> > </field> > <field name="content" type="java.lang.String"> > <bind-xml name="content" node="element"/> > </field> > </class> > <class name="com.u2d.shipgen.ShipSpec" access="shared"> > <description>Default mapping for class > com.u2d.shipgen.ShipSpec</description> > <map-to xml="ship-spec"/> > <field name="weight" type="float"> > <bind-xml name="weight" node="attribute"/> > </field> > <field name="to" type="com.u2d.shipgen.UsAddressType"> > <bind-xml name="to" node="element"/> > </field> > <field name="from" type="com.u2d.shipgen.UsAddressType"> > <bind-xml name="from" node="element"/> > </field> > </class> > </mapping> > > The Modified BindingTest Java Code: > > package com.u2d.ship; > import java.util.*; > import java.io.*; > import org.exolab.castor.xml.*; > import org.exolab.castor.mapping.*; > import org.xml.sax.InputSource; > import junit.framework.*; > /** > * @author Eitan Suez > */ > public class BindingTest extends TestCase { > public BindingTest(String name) { > super(name); > } > private Carrier fedex, ups; > private Service fgnd, f1day, f2day, f3day; > private Service ugnd, u1day, u2day; > private Shipper me; > private USAddr mine, lyn, pop; > private ShipSpec spec1, spec2, spec3; > private Model model; > protected void setUp() { > fedex = new Carrier("FedEx"); > fgnd = new Service("FedEx Ground"); > f1day = new Service("Express 1 Day"); > f2day = new Service("Express 2 Day"); > f3day = new Service("Express Saver (3 day)"); fedex.addService(fgnd); > fedex.addService(f1day); fedex.addService(f2day); > fedex.addService(f3day); > ups = new Carrier("UPS"); > ugnd = new Service("UPS Ground"); > u1day = new Service("UPS 1 day"); > u2day = new Service("UPS 2 day"); > ups.addService(ugnd); > ups.addService(u1day); > ups.addService(u2day); > me = new Shipper("Eitan"); > mine = new USAddr("2300 Roland Ct.", "", "Austin", "TX", "78749"); > lyn = new USAddr("1232 81st St.", "Apt. 234", "New York", "NY", > "02345"); > pop = new USAddr("1103 Lochness Dr.", "", "Tyler", "TX", "74012"); > spec1 = new ShipSpec(mine, lyn, 10); > spec2 = new ShipSpec(mine, pop, 3); > spec3 = new ShipSpec(pop, lyn, 15); > me.ship(fedex, f1day, spec1); > me.ship(ups, ugnd, spec2); > me.ship(fedex, f3day, spec3); > model = new Model(); > model.addShipper(me); > model.addCarrier(fedex); > model.addCarrier(ups); > } > > protected void tearDown() { > fgnd = f1day = f2day = f3day = null; > ugnd = u1day = u2day = null; > fedex = ups = null; > mine = lyn = pop = null; > spec1 = spec2 = spec3 = null; > me = null; > model = null; > } > /** > * this test is good but incomplete -- it needs to verify > * that marshaled xml is valid according to schema. can > * use xerces 2 to validate doc against schema. > */ > public void testSrcGenMarshalling() { > Mapping mapping = new Mapping(); > try { > Marshaller mrs = new Marshaller(new FileWriter("out/ship.xml")); > mrs.setNoNamespaceSchemaLocation("../ship.xsd"); > File file = new File("ModelMappingFile.xml"); > FileReader fileReader = new FileReader(file); > InputSource source = new InputSource(fileReader); > mapping.loadMapping(source); //mrs.setMarshalExtendedType(false); > mrs.setMapping(mapping); > mrs.marshal(model); > } catch (Exception ex) { > fail("exception: " + ex.getMessage()); > } > com.u2d.shipgen.Model mod2 = null; > try { > Reader reader = new FileReader("out/ship.xml"); > Unmarshaller umrs = new Unmarshaller(Model.class); > umrs.setMapping(mapping); > mod2 = (Model) umrs.unmarshal(reader); > } catch (Exception ex) { > fail("exception: " + ex.getMessage()); > } > verifyModel(mod2); > } > private void verifyModel(com.u2d.shipgen.Model mod2) { > com.u2d.shipgen.Shipper me2 = (com.u2d.shipgen.Shipper) > mod2.getShipper(0); > assertEquals("Eitan", me2.getName()); > assertEquals(3, me2.getShipmentCount()); > com.u2d.shipgen.Shipment sh2 = (com.u2d.shipgen.Shipment) > me2.getShipment(0); > com.u2d.shipgen.UsAddressType from2 = sh2.getShipSpec().getFrom(); > assertEquals(from2.getAddr1(), spec1.getFrom().getAddr1()); > } > public static void main(String[] args) throws Exception { > BindingTest test = new BindingTest("a binding test"); > test.setUp(); > test.testSrcGenMarshalling(); > test.tearDown(); > } > } > > The Modified Build file: > > > <!-- edited with XMLSPY v5 rel. 4 U (http://www.xmlspy.com) by mraruba > (mraruba) --> > <project name="srcgen sample" default="build"> <description>sample src > generator app</description> <property name="build.dir" value="build"/> > <property name="classes.dir" value="${build.dir}/classes"/> > <property name="map.dir" value="${build.dir}/mapping"/> > <property name="src.dir" value="src"/> > <property name="test.dir" value="test"/> > <path id="base.path"> > <fileset dir="lib"> > <include name="*.jar"/> > </fileset> > </path> > <path id="class.path"> > <path refid="base.path"/> > <pathelement path="${classes.dir}"/> > </path> > <target name="init"> > <mkdir dir="${classes.dir}"/> > <mkdir dir="${map.dir}"/> > </target> > <target name="clean"> > <delete dir="${build.dir}"/> > <delete dir="src/com/u2d/shipgen"/> > </target> > <target name="srcgen" depends="clean" description="srcgen"> > <java classpathref="base.path" > classname="org.exolab.castor.builder.SourceGenerator" fork="true"> > <arg value="-i"/> > <arg value="ship.xsd"/> > <arg value="-package"/> > <arg value="com.u2d.shipgen"/> > <arg value="-types"/> > <arg value="j2"/> > <arg value="-dest"/> > <arg value="src"/> > <arg value="-nodesc"/> > </java> > </target> > <target name="compile" depends="srcgen,init"> > <javac destdir="${classes.dir}" srcdir="src/com/u2d/shipgen" > classpathref="class.path"/> > </target> > <target name="genmap" depends="compile" description="compile only the > generated code"> > <antcall target="createcastormappingfile"> > <param name="name" value="Carrier"/> > </antcall> > <antcall target="createcastormappingfile"> > <param name="name" value="Model"/> > </antcall> > <antcall target="createcastormappingfile"> > <param name="name" value="Service"/> > </antcall> > <antcall target="createcastormappingfile"> > <param name="name" value="Shipment"/> > </antcall> > <antcall target="createcastormappingfile"> > <param name="name" value="Shipper"/> > </antcall> > <antcall target="createcastormappingfile"> > <param name="name" value="ShipSpec"/> > </antcall> > <antcall target="createcastormappingfile"> > <param name="name" value="UsAddressType"/> > </antcall> > </target> > <target name="createcastormappingfile"> > <java classname="org.exolab.castor.tools.MappingTool" fork="yes"> > <classpath refid="class.path"/> > <arg value="-i"/> > <arg value="com.u2d.shipgen.${name}"/> > <arg value="-o"/> > <arg value="${map.dir}\${name}.xml"/> > </java> > </target> > <target name="build" depends="init" description="compile code"> > <javac destdir="${classes.dir}" srcdir="src" > classpathref="class.path"/> > <javac classpathref="class.path" srcdir="${test.dir}" > destdir="${classes.dir}"/> > </target> > <target name="junitui" depends="build" description="launch junit swing > gui"> > <java classpathref="class.path" classname="junit.swingui.TestRunner" > fork="true"/> > </target> > <target name="basictest" depends="build" description="basic > marshalling test"> > <java classpathref="class.path" classname="com.u2d.shipgen.BasicTest" > fork="true"/> > </target> > <target name="bindingtest" depends="build" description="basic > marshalling test"> > <java classpathref="class.path" classname="com.u2d.ship.BindingTest" > fork="true"/> > </target> > </project> > > ----------------------------------------------------------- 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
