if (size == 1 && node == results.get(0)) {
continue; // nothing to do (replace X with X)
}
ParentNode parent = node.getParent();
StringBuffer atomics = null;
boolean isInitialized = false;
int position = 0;
if (size > 1) {
if (identities == null) {
identities = new HashSet();
} else {
identities.clear();
}
}
for (int j=0; j < size; j++) {
Node result = results.get(j);
if (DefaultResultSequence.isAtomicValue(result)) { // concat atomic values
String value = result.getValue();
if (atomics == null) {
atomics = new StringBuffer(value.length());
} else {
atomics.append(' ');
}
atomics.append(value);
} else if (parent != null) {
if (size > 1 && !identities.add(result)) {
result = result.copy(); // multiple identical nodes in results
// throw new MultipleParentException(
// "XQuery morpher result sequence must not contain multiple identical nodes");
}
boolean isRoot = parent instanceof Document && node instanceof Element;
if (!isInitialized) {
if (!(node instanceof Attribute)) position = parent.indexOf(node);
if (!isRoot) node.detach();
isInitialized = true;
}
if (result instanceof Attribute) {
result.detach();
((Element) parent).addAttribute((Attribute)result);
} else {
if (isRoot && result instanceof Element) {
parent.replaceChild(node, result);
} else {
result.detach();
parent.insertChild(result, position);
}
position++;
}
}
}