throws XmlException, IllegalAccessException, InvocationTargetException
{
List properties = oMap.getPropertyMap();
for (Iterator i = properties.iterator(); i.hasNext();)
{
PropertyMap pMap = (PropertyMap)i.next();
// If there is a property map for this attribute at the 'global'
// level, use that one instead
if (config.getPropertyMapByName(pMap.getPropertyName()) != null)
pMap = config.getPropertyMapByName(pMap.getPropertyName());
if (pMap.getWriteAsAttribute() != attributes)
continue;
logger.fine("Writing object property " + pMap.getPropertyName() +
" as xml element " + pMap.getPropertyXmlName() + ".");
Method method = pMap.getGetter();
if (method == null)
{
String name = pMap.getPropertyName();
// If the setter method is undefined for this property map, then the
// property map was probably added to config programmatically or through
// a config file, rather than being generated from a class.
if (method == null && pMap != null)
{
// Guess what the setter might be
method = pMap.guessGetter(object);
// And cache the method if it's not a global property map
if (method != null)
pMap.setGetter(method);
}
if (pMap == null)
throw new XmlException("Lost property definition for " +
name);
if (method == null)
throw new XmlException("Cannot find getter method for " + name +
" in the object " + object.getClass().getName());
}
Object value = method.invoke(object, (Object[])null);
String type = "unknown";
Class typeClass = null;
if (value == null)
{
typeClass = pMap.getPropertyType();
type = typeClass.getName();
}
if (value != null)
{
type = value.getClass().getName();
typeClass = value.getClass();
// 'Correct' the type if we are dealing with a List or Set
if ((value instanceof List || value instanceof Set) &&
((Collection)value).size() > 0 &&
((Collection)value).iterator().hasNext() == true)
{
if (((Collection)value).iterator().next() != null)
{
type =
(((Collection)value).iterator().next()).getClass().getName();
typeClass = (((Collection)value).iterator().next()).getClass();
}
// Yikes, we have a collection with at least one element, but the
// first element is a NULL! We should just skip this whole
// property.
else
continue;
}
// 'Correct' the type if we are dealing with an array
else if (value.getClass().isArray() == true &&
((Object[])value).length > 0)
{
type = ((Object[])value)[0].getClass().getName();
typeClass = ((Object[])value)[0].getClass();
}
// If the properties 'actual' type is a primative, our getter will
// actually return the Object wrapper for the primative so we must
// update our PropertyMap to reflect this.
if (pMap.getPropertyType() == null ||
!type.equals(pMap.getPropertyType().getName()))
pMap.setPropertyType(typeClass);
type = pMap.getPropertyType().getName();
}
boolean isAttribute = (config.getConverter(type) != null);
if (pMap.getConverter() != null)
isAttribute = true;
if (isAttribute == false && attributes == true)
{
pMap.setWriteAsAttribute(false);
continue;
}
// If it's an object and it's null, don't write it out.
if (value == null && isAttribute == false)