if ( log.isTraceEnabled() ) {
log.trace( "Writing bean graph (qualified name '" + qualifiedName + "'" );
}
// introspect to obtain bean info
XMLBeanInfo beanInfo = introspector.introspect( bean );
if ( beanInfo != null ) {
ElementDescriptor elementDescriptor = beanInfo.getElementDescriptor();
if ( elementDescriptor != null ) {
context = context.newContext( bean );
if ( qualifiedName == null ) {
qualifiedName = elementDescriptor.getQualifiedName();
}
if ( namespaceUri == null ) {
namespaceUri = elementDescriptor.getURI();
}
if ( localName == null ) {
localName = elementDescriptor.getLocalName();
}
String ref = null;
String id = null;
// only give id's to non-primatives
if ( elementDescriptor.isPrimitiveType() ) {
// write without an id
writeElement(
namespaceUri,
localName,
qualifiedName,
elementDescriptor,
context );
} else {
pushBean ( context.getBean() );
if ( getBindingConfiguration().getMapIDs() ) {
ref = (String) idMap.get( context.getBean() );
}
if ( ref == null ) {
// this is the first time that this bean has be written
AttributeDescriptor idAttribute = beanInfo.getIDAttribute();
if (idAttribute == null) {
// use a generated id
id = idGenerator.nextId();
idMap.put( bean, id );
if ( getBindingConfiguration().getMapIDs() ) {
// write element with id
writeElement(
namespaceUri,
localName,
qualifiedName,
elementDescriptor,
context ,
beanInfo.getIDAttributeName(),
id);
} else {
// write element without ID
writeElement(
namespaceUri,
localName,
qualifiedName,
elementDescriptor,
context );
}
} else {
// use id from bean property
// it's up to the user to ensure uniqueness
// XXX should we trap nulls?
Object exp = idAttribute.getTextExpression().evaluate( context );
if (exp == null) {
// we'll use a random id
log.debug("Using random id");
id = idGenerator.nextId();
} else {
// convert to string
id = exp.toString();
}
idMap.put( bean, id);
// the ID attribute should be written automatically
writeElement(
namespaceUri,
localName,
qualifiedName,
elementDescriptor,
context );
}
} else {
if ( !ignoreElement( elementDescriptor, context )) {
// we've already written this bean so write an IDREF
writeIDREFElement(
elementDescriptor,
namespaceUri,
localName,
qualifiedName,
beanInfo.getIDREFAttributeName(),
ref);
}
}
popBean();
}