Howdy,
I'm writing an application that has a particularly rigid set of
requirements, including a fixed object model and a public
standards-project based DTD.
The object model uses a number of abstract classes and interfaces to
model the data, which would seem to be exactly what the xsi:type
attribute was developed to deal with. Unfortunately, since the XML
generated is for a globally-scoped standard, with no guarantees about
the processing system on the far end, I have to explicitly adhere to
the provided DTD, which doesn't include any xsi:type attributes in it.
Back in the 0.9.3.9 days, which is when the first pass of this
application was built, I modified a local copy of Castor to meet these
needs, and I'm looking for feedback as to whether there's a better way
to do this now that we're looking at moving to 0.9.4.
The main issue here is the structure of the object model. In a
simplified example, there are two classes: Bar and Baz, both of which
implement interface Foo, and a class FooContainer that simply gets a
list of Foo objects within it, and adds Foo objects to the list.
public interface Foo { }
public class Bar implements Foo { }
public class Baz implements Foo { }
public class FooContainer
{
ArrayList _foos = new ArrayList();
public ArrayList getFoos ( ) { return _foos; }
public void addFoo ( Foo foo ) { _foos.add(foo); }
}
Still with me? The problem lies in the fact that I want the XML
elements to say <bar> or <baz> depending, but I can only get the
interfaces <foo>
So, I added a class (and other supporting classes) to
org.exolab.castor.mapping.xml that introduced the possibility of a
bind-if element within a FieldHandler. What this allowed was the
ability to have <field> mappings for each of the concrete classes (Bar
and Baz), as well as one <field> mapping for the interface that
switched based on the type of the object. The interface field handler
is used on marshalling when the type of the Foos isn't known, and the
class field handler are used on unmarshalling when the XML element name
specifies the concrete class.
<class name="FooContainer">
<map-to xml="foocontainer"/>
<field name="_foos" type="Foo" collection="list"
get-method="getFoos">
<bind-if type="Bar">
<bind-xml name="bar" node="element"/>
</bind-if>
<bind-if type="Baz">
<bind-xml name="baz" node="element"/>
</bind-if>
</field>
<field name="_bars" type="Bar" collection="list"
set-method="addFoo">
<bind-xml name="bar" node="element"/>
</field>
<field name="_bazs" type="Baz" collection="list"
set-method="addFoo">
<bind-xml name="baz" node="element"/>
</field>
</class>
I know that seems really complicated, but it works, and if anyone wants
to know more, email me. What I'm looking for is A) a way to do this
without needing to specify that extra field handler, and B) some way of
getting some sort of functionality like this into the main tree so that
I don't have to keep patching one-off versions of Castor.
Anyone have any thoughts?
Thanks for anything in advance,
John
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev