* @return The last <code>XMLNode</code> in the path
*
* @exception org.eclipse.persistence.oxm.exceptions.XMLMarshalException Thrown if passed an invalid XPath string
*/
private NodeList createCollection(XMLField xmlField, Node element, Object value, XMLField lastUpdated, DocumentPreservationPolicy docPresPolicy, AbstractSession session) throws XMLMarshalException {
XMLNodeList createdElements = new XMLNodeList();
//CR:### If the value is null, then the node(s) must not be created.
if ((value == null) || (value instanceof Collection && (((Collection)value).size() == 0))) {
return createdElements;
}
Node nextElement = element;
Element sibling = null;
XPathFragment siblingFragment = null;
if(lastUpdated != null) {
siblingFragment = lastUpdated.getXPathFragment();
}
if ((lastUpdated != null) && !siblingFragment.isAttribute() && !siblingFragment.nameIsText()) {
//find the sibling element.
NodeList nodes = unmarshalXPathEngine.selectElementNodes(element, siblingFragment, getNamespaceResolverForField(lastUpdated));
if (nodes.getLength() > 0) {
sibling = (Element)nodes.item(nodes.getLength() - 1);
}
}
NodeList elements;
XPathFragment next = xmlField.getXPathFragment();
while (next != null) {
if (next.isAttribute()) {
addAttribute(next, xmlField, nextElement, value, session);
} else if (next.containsIndex()) {
// If we are creating multiple nodes from this XPath, assume the value is for the last node.
boolean hasMore = !(next.getHasText() || (next.getNextFragment() == null));
if (hasMore) {
nextElement = addIndexedElement(next, xmlField, nextElement, this, !hasMore, session);
} else {
Object valueToWrite = getValueToWrite(value, xmlField, session);
nextElement = addIndexedElement(next, xmlField, nextElement, valueToWrite, !hasMore, session);
createdElements.add(nextElement);
}
} else {
boolean hasMore = !(next.getHasText() || (next.getNextFragment() == null));
if (hasMore) {
elements = addElements(next, xmlField, nextElement, this, !hasMore, sibling, docPresPolicy, session);
} else {
XPathFragment nextFragment = next.getNextFragment();
if ((nextFragment != null) && nextFragment.isAttribute() && !(value instanceof List)) {
elements = addElements(next, xmlField, nextElement, this, hasMore, sibling, docPresPolicy, session);
} else {
Object valueToWrite = getValueToWrite(value, xmlField, session);
elements = addElements(next, xmlField, nextElement, valueToWrite, !hasMore, sibling, docPresPolicy, session);
createdElements.addAll(elements);
}
}
nextElement = elements.item(elements.getLength() - 1);
}