private Object buildObjectValuesFromDOMRecord(DOMRecord record, AbstractSession session, ObjectBuildingQuery query, JoinedAttributeManager joinManager) {
//This DOMRecord represents the root node of the AnyType instance
//Grab ALL children to populate the collection.
Node root = record.getDOM();
NodeList children = root.getChildNodes();
ContainerPolicy cp = getContainerPolicy();
Object container = cp.containerInstance();
int length = children.getLength();
Node next = null;
if(length > 0) {
next = record.getDOM().getFirstChild();
}
while(next != null) {
Object objectValue = null;
if (isUnmappedContent(next)) {
if ((next.getNodeType() == Node.TEXT_NODE) && this.isMixedContent()) {
if (next.getNodeValue().trim().length() > 0) {
objectValue = next.getNodeValue();
if(getConverter() != null) {
objectValue = getConverter().convertDataValueToObjectValue(objectValue, session, record.getUnmarshaller());
}
cp.addInto(objectValue, container, session);
}
} else if (next.getNodeType() == Node.ELEMENT_NODE) {
ClassDescriptor referenceDescriptor = null;
//In this case it must be an element so we need to dig up the descriptor
//make a nested record and build an object from it.
DOMRecord nestedRecord = (DOMRecord) record.buildNestedRow((Element) next);
if (!useXMLRoot) {
referenceDescriptor = getDescriptor(nestedRecord, session, null);
if ((referenceDescriptor != null) && (keepAsElementPolicy != UnmarshalKeepAsElementPolicy.KEEP_ALL_AS_ELEMENT)) {
ObjectBuilder builder = referenceDescriptor.getObjectBuilder();
objectValue = builder.buildObject(query, nestedRecord, joinManager);
if(getConverter() != null) {
objectValue = getConverter().convertDataValueToObjectValue(objectValue, session, record.getUnmarshaller());
}
cp.addInto(objectValue, container, session);
} else {
if ((keepAsElementPolicy == UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT) || (keepAsElementPolicy == UnmarshalKeepAsElementPolicy.KEEP_ALL_AS_ELEMENT)) {
XMLPlatformFactory.getInstance().getXMLPlatform().namespaceQualifyFragment((Element) next);
objectValue = next;
if(getConverter() != null) {
objectValue = getConverter().convertDataValueToObjectValue(objectValue, session, record.getUnmarshaller());
}
cp.addInto(objectValue, container, session);
}
}
} else {
String schemaType = ((Element) next).getAttributeNS(XMLConstants.SCHEMA_INSTANCE_URL, XMLConstants.SCHEMA_TYPE_ATTRIBUTE);
QName schemaTypeQName = null;
XPathFragment frag = new XPathFragment();
if ((null != schemaType) && (!schemaType.equals(""))) {
frag.setXPath(schemaType);
if (frag.hasNamespace()) {
String prefix = frag.getPrefix();
XMLPlatform xmlPlatform = XMLPlatformFactory.getInstance().getXMLPlatform();
String url = xmlPlatform.resolveNamespacePrefix(next, prefix);
frag.setNamespaceURI(url);
schemaTypeQName = new QName(url, frag.getLocalName());
}
XMLContext xmlContext = nestedRecord.getUnmarshaller().getXMLContext();
referenceDescriptor = xmlContext.getDescriptorByGlobalType(frag);
}
if (referenceDescriptor == null) {
try {
QName qname = new QName(nestedRecord.getNamespaceURI(), nestedRecord.getLocalName());
referenceDescriptor = getDescriptor(nestedRecord, session, qname);
} catch (XMLMarshalException e) {
referenceDescriptor = null;
}
// Check if descriptor is for a wrapper, if it is null it out and let continue
XMLDescriptor xmlReferenceDescriptor = (XMLDescriptor) referenceDescriptor;
if (referenceDescriptor != null && xmlReferenceDescriptor.isWrapper()) {
referenceDescriptor = null;
}
}
if ((referenceDescriptor != null) && (getKeepAsElementPolicy() != UnmarshalKeepAsElementPolicy.KEEP_ALL_AS_ELEMENT)) {
ObjectBuilder builder = referenceDescriptor.getObjectBuilder();
objectValue = builder.buildObject(query, nestedRecord, joinManager);
Object updated = ((XMLDescriptor) referenceDescriptor).wrapObjectInXMLRoot(objectValue, next.getNamespaceURI(), next.getLocalName(), next.getPrefix(), false);
if(getConverter() != null) {
updated = getConverter().convertDataValueToObjectValue(updated, session, record.getUnmarshaller());
}
cp.addInto(updated, container, session);
} else if ((referenceDescriptor == null) && (keepAsElementPolicy == UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT)) {
XMLPlatformFactory.getInstance().getXMLPlatform().namespaceQualifyFragment((Element) next);
objectValue = next;
if(getConverter() != null) {
objectValue = getConverter().convertDataValueToObjectValue(objectValue, session, record.getUnmarshaller());
}
cp.addInto(objectValue, container, session);
} else {
Object value = null;
Node textchild = ((Element) next).getFirstChild();
if ((textchild != null) && (textchild.getNodeType() == Node.TEXT_NODE)) {
value = ((Text) textchild).getNodeValue();
}
if ((value != null) && !value.equals("")) {
if (schemaTypeQName != null) {
Class theClass = (Class) XMLConversionManager.getDefaultXMLTypes().get(schemaTypeQName);
if (theClass != null) {
value = ((XMLConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, theClass, schemaTypeQName);
}
}
if(getConverter() != null) {
value = getConverter().convertDataValueToObjectValue(value, session, record.getUnmarshaller());
}
XMLRoot rootValue = new XMLRoot();
rootValue.setLocalName(next.getLocalName());
rootValue.setSchemaType(schemaTypeQName);
rootValue.setNamespaceURI(next.getNamespaceURI());
rootValue.setObject(value);
cp.addInto(rootValue, container, session);
}
}
}
}
}