in grails-converters/src/main/groovy/org/grails/web/converters/marshaller/xml/DomainClassMarshaller.java [96:209]
public void marshalObject(Object value, XML xml) throws ConverterException {
Class clazz = value.getClass();
List<String> excludes = xml.getExcludes(clazz);
List<String> includes = xml.getIncludes(clazz);
IncludeExcludeSupport<String> includeExcludeSupport = new IncludeExcludeSupport<String>();
PersistentEntity domainClass = findDomainClass(value);
if ( domainClass == null ) {
throw new GrailsConfigurationException("Could not retrieve the respective entity for domain " + value.getClass().getName() + " in the mapping context API");
}
BeanWrapper beanWrapper = new BeanWrapperImpl(value);
PersistentProperty id = domainClass.getIdentity();
if(shouldInclude(includeExcludeSupport, includes, excludes,value, id.getName())) {
Object idValue = beanWrapper.getPropertyValue(id.getName());
if (idValue != null) xml.attribute("id", String.valueOf(idValue));
}
if (shouldInclude(includeExcludeSupport, includes, excludes, value, GormProperties.VERSION) && includeVersion) {
Object versionValue = beanWrapper.getPropertyValue(domainClass.getVersion().getName());
if (versionValue != null) {
final String str = String.valueOf(versionValue);
if (StringUtils.hasText(str)) {
xml.attribute("version", str);
}
}
}
if(includeClass && shouldInclude(includeExcludeSupport, includes, excludes, value, "class")) {
xml.attribute("class",domainClass.getJavaClass().getName());
}
List<PersistentProperty> properties = domainClass.getPersistentProperties();
for (PersistentProperty property : properties) {
String propertyName = property.getName();
if (property.equals(domainClass.getVersion())) {
continue;
}
if(!shouldInclude(includeExcludeSupport, includes, excludes, value, property.getName())) continue;
xml.startNode(propertyName);
if (!(property instanceof Association)) {
// Write non-relation property
Object val = beanWrapper.getPropertyValue(propertyName);
xml.convertAnother(val);
}
else {
if (isRenderDomainClassRelations()) {
Object referenceObject = beanWrapper.getPropertyValue(propertyName);
if (referenceObject != null && shouldInitializeProxy(referenceObject)) {
referenceObject = proxyHandler.unwrapIfProxy(referenceObject);
if (referenceObject instanceof SortedMap) {
referenceObject = new TreeMap((SortedMap) referenceObject);
}
else if (referenceObject instanceof SortedSet) {
referenceObject = new TreeSet((SortedSet) referenceObject);
}
else if (referenceObject instanceof Set) {
referenceObject = new LinkedHashSet((Set) referenceObject);
}
else if (referenceObject instanceof Map) {
referenceObject = new LinkedHashMap((Map) referenceObject);
}
else if (referenceObject instanceof Collection) {
referenceObject = new ArrayList((Collection) referenceObject);
}
xml.convertAnother(referenceObject);
}
}
else {
Object referenceObject = beanWrapper.getPropertyValue(propertyName);
if (referenceObject != null) {
PersistentEntity referencedDomainClass = ((Association) property).getAssociatedEntity();
// Embedded are now always fully rendered
if (referencedDomainClass == null || ((Association)property).isEmbedded() || property.getType().isEnum()) {
xml.convertAnother(referenceObject);
}
else if ((property instanceof OneToOne) || (property instanceof ManyToOne)|| ((Association)property).isEmbedded()) {
asShortObject(referenceObject, xml, referencedDomainClass.getIdentity(), referencedDomainClass);
}
else {
PersistentProperty referencedIdProperty = referencedDomainClass.getIdentity();
@SuppressWarnings("unused")
String refPropertyName = ((Association) property).getReferencedPropertyName();
if (referenceObject instanceof Collection) {
Collection o = (Collection) referenceObject;
for (Object el : o) {
xml.startNode(xml.getElementName(el));
asShortObject(el, xml, referencedIdProperty, referencedDomainClass);
xml.end();
}
}
else if (referenceObject instanceof Map) {
Map<Object, Object> map = (Map<Object, Object>) referenceObject;
for (Map.Entry<Object, Object> entry : map.entrySet()) {
String key = String.valueOf(entry.getKey());
Object o = entry.getValue();
xml.startNode("entry").attribute("key", key);
asShortObject(o, xml, referencedIdProperty, referencedDomainClass);
xml.end();
}
}
}
}
}
}
xml.end();
}
}