On Thu, 2006-01-05 at 22:29 -0600, Keith Visco wrote:
> You can create a custom field handler to be your GUID generator.
>
> And then use the following mapping:
>
> <class name="...ClientDcl" identity="clientGUID">
> <map-to xml="ClientDcl"/>
> ...
> <field name="clientGUID" handler="MyGUIDHandler"/>
> <bind-xml name="ClientGUID"/>
> </field>
> ...
> </class>
>
> Once you have your GUID generator working you can then add the following
> to your mapping file:
>
> <class name="...AddressRoleDcl">
> <map-to xml="AddressRoleDcl"/>
> ...
> <field name="clientGUID" reference="true">
> <bind-xml name="ClientGUID" node="element"/>
> </field>
> ...
> </class>
After doing like the above suggested, I get the following exception:
[DEBUG]: (TestXMLBindingFramework.testCastor:75) - Unmarshalling AsXML
to ClientDcl...
[DEBUG]: (MyGuidHandler.convertUponSet:38) - original setValue value =
[DEBUG]: (MyGuidHandler.convertUponSet:40) - new setValue value =
48EBBAAC-F551-9937-D4C4-0D80407FA9B4
[DEBUG]: (TestXMLBindingFramework.testCastor:79) - Unmarshalling
successful in 202 milliseconds.
[DEBUG]: (TestXMLBindingFramework.testCastor:84) - Marshalling ClientDcl
to AsXML ...
Unable to resolve ID for instance of class 'java.lang.String' due to the
following error: Unable to resolve ClassDescriptor.
at org.exolab.castor.xml.Marshaller.getObjectID(Marshaller.java:2009)
at org.exolab.castor.xml.Marshaller.marshal(Marshaller.java:1649)
at org.exolab.castor.xml.Marshaller.marshal(Marshaller.java:1852)
at org.exolab.castor.xml.Marshaller.marshal(Marshaller.java:1852)
at org.exolab.castor.xml.Marshaller.marshal(Marshaller.java:1835)
at org.exolab.castor.xml.Marshaller.marshal(Marshaller.java:842)
at TestXMLBindingFramework.testCastor(TestXMLBindingFramework.java:86)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
As u can see, the GUID gets generated & also prints out while
marshalling without the reference="true" attribute in the Castor
mapping. But, as soon as u put in reference="true" on the bind-xml
element, it all blows up!
import org.apache.log4j.Logger;
import org.exolab.castor.mapping.GeneralizedFieldHandler;
public class MyGuidHandler extends GeneralizedFieldHandler {
Logger logger = Logger.getRootLogger();
/**
* This method is used to convert the value when the
* getValue method is called. The getValue method will
* obtain the actual field value from given 'parent' object.
* This convert method is then invoked with the field's
* value. The value returned from this method will be
* the actual value returned by getValue method.
*
* @param value the object value to convert after
* performing a get operation
* @return the converted value.
*/
public Object convertUponGet(Object value) {
return value;
}
/**
* This method is used to convert the value when the
* setValue method is called. The setValue method will
* call this method to obtain the converted value.
* The converted value will then be used as the value to
* set for the field.
*
* @param value the object value to convert before
* performing a set operation
* @return the converted value.
*/
public Object convertUponSet(Object value) {
logger.debug( "original setValue value = " + value );
value = new GuidGeneratorUtl().toString();
logger.debug( "new setValue value = " + value );
return value;
}
/**
* Returns the class type for the field that this
* GeneralizedFieldHandler converts to and from. This
* should be the type that is used in the
* object model.
*
* @return the class type of of the field
*/
public Class getFieldType() {
return String.class;
}
}
>
>
> Keep in mind that your handler (your GUID generator) will need to keep
> track of GUIDs during the marshalling for each object that it generates
> a GUID for so that when the handler is called again for the same object
> it will return the correct GUID for that object as your handler will get
> called each time it needs to output the GUID. This will be once for the
> object itself and then once for each reference to the object.
>
> For help on creating a custom FieldHandler please see the website.
>
> --Keith
>
>
>
>
>
> Sandeep Khanna wrote:
> > Hi All,
> >
> > Given the following visual representation of Object relationships & the
> > relevant Castor mapping:
> >
> > ClientDcl
> > |-ClientGUID
> > |-FirstName
> > |-LastName
> > |-...
> > |-AddressRoleDcl
> > |-AddressRoleGUID
> > |-ClientGUID
> > |-AddressGUID
> > |-...
> > |-AddressDcl
> > |-AddressGUID
> > |-...
> >
> > I would like to unmarshall the following XML:
> >
> > <?xml version="1.0" encoding="UTF-8"?>
> > <AsClient>
> > <ClientGUID/>
> > <FirstName>Sandeep</FirstName>
> > <LastName>Khanna</LastName>
> > <Fields>
> > <AsClientField>
> > <ClientGUID/>
> > <FieldName>ExtraFieldName</FieldName>
> > <FieldTypeCode>02</FieldTypeCode>
> > <TextValue>ExtraFieldValue</TextValue>
> > </AsClientField>
> > </Fields>
> > <AddressRoles> <!-- Map -->
> > <AsAddressRole> <!-- value -->
> > <AddressRoleGUID/>
> > <AddressGUID/>
> > <ClientGUID/>
> > <AddressRoleCode>2</AddressRoleCode> <!-- key -->
> > <AsAddress>
> > <AddressGUID/>
> > <AddressLine1>81 ALEXANDRA DRIVE</AddressLine1>
> > <AddressLine2/>
> > <AddressLine3/>
> > <addressLine4/>
> > <City>NEW BRITIAN</City>
> > <StateCode>07</StateCode>
> > <CountryCode/>
> > <PostalID>06789</PostalID>
> > <Fields>
> > <AsAddressField>
> > <ClientGUID/>
> > <FieldName/>
> > <FieldTypeCode>02</FieldTypeCode>
> > <TextValue/>
> > </AsAddressField>
> > </Fields>
> > </AsAddress>
> > </AsAddressRole>
> > </AddressRoles>
> > </AsClient>
> >
> > We have a GUIDGeneratorUtl class that creates unique GUIDs. We would
> > like to use it from within the Castor framework to generate the GUID and
> > set it in the above Object relationships.
> >
> > Questions:
> > 1) How do I setup/use a GUID generator class from within the Castor
> > mapping file.
> > 2) As per the above visual Object relationship, we would require the
> > ClientGUID that was generated and set on the ClientDcl object to be used
> > while setting the ClientGUID attribute on the AddressRoleDcl object
> > contained in the ClientDcl instance.
> > 3) As per the above visual Object relationship,
> > AddressRoleDcl.AddressGUID needs to be set from the
> > AddressDcl.AddressGUID which would be generated only after we unmarshall
> > or parse the AddressDcl element/node. One way would be to unmarshall the
> > AddressDcl element/node first to have the AddressGUID ready before
> > unmarshalling AddressRoleDcl element/node.
> >
> > Any comments, suggestions, pointers would be helpful.
> >
> > --Sandeep Khanna
> >
> >
> > -------------------------------------------------
> > If you wish to unsubscribe from this list, please
> > send an empty message to the following address:
> >
> > [EMAIL PROTECTED]
> > -------------------------------------------------
> >
> >
>
>
> -------------------------------------------------
> If you wish to unsubscribe from this list, please
> send an empty message to the following address:
>
> [EMAIL PROTECTED]
> -------------------------------------------------
>
-------------------------------------------------
If you wish to unsubscribe from this list, please
send an empty message to the following address:
[EMAIL PROTECTED]
-------------------------------------------------