NodeValue v;
for (int i = 0; i <= size; i++) {
v = (NodeValue)values[i];
if(v.getImplementationType() != NodeValue.PERSISTENT_NODE) {
// found an in-memory document
final DocumentImpl doc = ((NodeImpl)v).getDocument();
if (doc==null) {
continue;
}
// make this document persistent: doc.makePersistent()
// returns a map of all root node ids mapped to the corresponding
// persistent node. We scan the current sequence and replace all
// in-memory nodes with their new persistent node objects.
final DocumentImpl expandedDoc = doc.expandRefs(null);
final org.exist.dom.DocumentImpl newDoc = expandedDoc.makePersistent();
if (newDoc != null) {
NodeId rootId = newDoc.getBrokerPool().getNodeFactory().createInstance();
for (int j = i; j <= size; j++) {
v = (NodeValue) values[j];
if(v.getImplementationType() != NodeValue.PERSISTENT_NODE) {
NodeImpl node = (NodeImpl) v;
if (node.getDocument() == doc) {
if (node.getNodeType() == Node.ATTRIBUTE_NODE)
{node = expandedDoc.getAttribute(node.getNodeNumber());}
else
{node = expandedDoc.getNode(node.getNodeNumber());}
NodeId nodeId = node.getNodeId();
if (nodeId == null)
{throw new XPathException("Internal error: nodeId == null");}
if (node.getNodeType() == Node.DOCUMENT_NODE)
{nodeId = rootId;}