Hi All
When I load a bean from an xml file using castor,
the association between the child and parent elements in the xml file
are not reflected in the object structure. Here is an example:
XML:
<country>
<city>
<street/>
</city>
</country>
OBJ:
country.getYity(); // exists :-)
city.getStreet(); // exists :-)
street.getParentCity(); //<-- not possible :-(
publi class Country{ private City city; }
public class City { private Strret street;}
public Street { }
As you can see, in the XML file, the street knows its parent because
of the XML structure.
As objects, a city knows its streets, but a street does not know its
city directly.
My idea to implement the missing association consists of the following steps:
1) add a property "parent" to each bean.
2) write an AssociationManager that sets the association child2parent
directly after the objects have been unmarshalled from the xml file.
Implementation approach:
1)
Source generation :
all castor generated beans inherit from a super class, that
contains a property parent
public abstract class AbstractObject{
private Object parent;
}
2) Use in an application :
- unmarshall the xml file to an object tree that has the root country
- add the missing associations child2parent by calling the
AssoiationManager
AssosiationManger.associate(country)
public class AssosiationManger{
public static assoicate(Object root){
// root is a Country object
// step 1: find fileds in Country.class using reflection
// if the field inherits from AbstractObject
// filed.setParent(root)
// associate(filed) // recursive call
//
}
}
I would be very gald to hear feedback!
Cheers
Aziz Hammadi