commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From robert burrell donkin <robertburrelldon...@blueyonder.co.uk>
Subject Re: [betwixt] Making deserialise work like serialise (long)
Date Thu, 14 Jul 2005 18:55:22 GMT
On Thu, 2005-07-14 at 17:46 +0100, Ricardo Gladwell wrote:
> robert burrell donkin wrote:
> > On Tue, 2005-07-12 at 15:47 +0100, Ricardo Gladwell wrote:
> >> Hi All,
> > 
> > hi Ricardo
> > 
> >> How can I simulate this behaviour for serialisation? That is, how do I
> >> force betwixt to interpret the bank account and generate an appropriate
> >> country specific bank account, placing it into the bank account property
> >> of the shopper, without having to create an additional bean for generic
> >> bank account to hold the country-specific bank account.
> > 
> > i suggest that you being by getting hold of the betwixt source (either
> > from subversion see http://jakarta.apache.org/site/cvsindex.html or from
> > the latest release candidate
> > http://people.apache.org/~rdonkin/commons-betwixt/). you need to create
> > a custom ChainedBeanCreator which creates the right bean based on the
> > element type (take a look at ChainedBeanCreatorFactory for example of
> > creating a bean on the basis of an attribute value) and then add it to
> > the chain used to create new beans (see
> > http://jakarta.apache.org/commons/betwixt/guide/reading.html#Reading%
> > 20Beans%20-%20In%20Depth). though you'll have to add an entry for each
> > subclass, it should work.
> 
> Thanks for the above. Unfortunately, the dependencies for the project 
> I'm working on are quite strict and it wouldn't be possible to use 
> snapshot code as you suggest above (it is worth noting the Maven 
> repository on Ibiblio for this project is out of date, snapshots and 
> all). 

it's a bit complicated by the fact that it's a release candidate (not a
SNAPSHOT). release candidates should not be uploaded to ibiblio (or any
other mirror). but thank's for the warning: i will remember to try to
check that the maven repository is right when the full release is cut. 

> Otherwise, implementing your own ChainedBeanCreator and modifying 
> the bean creator chain is a very good idea.

i think i managed to confuse matters a little (in the rush).

the chain is pluggable: just implement the interface and plug it in. 

the reason why i suggested obtaining the source is that the code
contains better examples than the documentation. (no changes are needed
to the source to create a custom creation chain.)

FYI the release candidate is very close now to being accepted as betwixt
0.7. it is strongly recommended that all users upgrade to this new
version. 

> In the end, I cheated and hard-coded XPath to read specific bank 
> accounts using the BeanReader.registerBeanClass method:
> 
> reader.registerBeanClass("//bankAccount[bankAccount-AT]", 
> AustrianBankAccount.class);
> 
> And reading the file twice: once for the XML file, and again to extract 
> bank details and "manually" insert them into the appropriate bean. That 
> said, the bank accounts do not seem to be being read properly and the 
> parse() method returns null. Not sure what might be causing this.

it is possible to make betwixt work that way (you can integrate it with
digester rules) but it's pretty black belt...  

> > BTW any ideas about the best way to solve (or at least, chip away at)
> > the general problem would be gratefully received
> 
> Not sure this problems is resolvable: it means creating a reverse of the 
> betwixt file and applying it intelligently to XML reading. For example, 
> the following betwixt file:
> 
> <!-- AustrianBankAccount.betwixt -->
> <?xml version="1.0" encoding="UTF-8" ?>
> <info primitiveTypes="element">
>    <element name="bankAccount">
>      <element name="bankAccount-AT">...
> 
> Would have to be interpreted as being /bankAccount[bankAccount-AT] -> 
> AustrianBankAccount. How would BeanReader be able to grasp this is what 
> is wanted from the above than, for example, the bankAccount-AT refers to 
> a property of the AustrianBankAccount bean, for example? 

exactly :)

i've seen some mappers do things using reflection to determine
inheritance but in the end they still need to know the names of the
classes involved so i'm not sure how much is gained...

> Some sort of 
> intelligent comparison between the betwixt file and the bean object 
> model would be required to interpolate expected behaviour. Perhaps some 
> sort of additional scripting would be required?

scripting sounds very interesting (hadn't really thought about it
before). betwixt is generally declarative but maven's mix of declarative
data and scripting works very well. how do you see this working?

one way to solve your narrow problem would be to create a custom chain
bean creator using an option to pass element name -> class name mappings
through and add that to the default chain. if you need to use full
releases, then it'd probably be a little too late, though...

- robert


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message