if (errorChecking) {
for (Node kid = newChild.getFirstChild(); // Prescan
kid != null; kid = kid.getNextSibling()) {
if (!ownerDocument.isKidOK(this, kid)) {
throw new DOMException(
DOMException.HIERARCHY_REQUEST_ERR,
DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null));
}
}
}
while (newChild.hasChildNodes()) {
insertBefore(newChild.getFirstChild(), refChild);
}
return newChild;
}
if (newChild == refChild) {
// stupid case that must be handled as a no-op triggering events...
refChild = refChild.getNextSibling();
removeChild(newChild);
insertBefore(newChild, refChild);
return newChild;
}
if (needsSyncChildren()) {
synchronizeChildren();
}
if (errorChecking) {
if (isReadOnly()) {
throw new DOMException(
DOMException.NO_MODIFICATION_ALLOWED_ERR,
DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null));
}
if (newChild.getOwnerDocument() != ownerDocument && newChild != ownerDocument) {
throw new DOMException(DOMException.WRONG_DOCUMENT_ERR,
DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null));
}
if (!ownerDocument.isKidOK(this, newChild)) {
throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null));
}
// refChild must be a child of this node (or null)
if (refChild != null && refChild.getParentNode() != this) {
throw new DOMException(DOMException.NOT_FOUND_ERR,
DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null));
}
// Prevent cycles in the tree
// newChild cannot be ancestor of this Node,
// and actually cannot be this
boolean treeSafe = true;
for (NodeImpl a = this; treeSafe && a != null; a = a.parentNode())
{
treeSafe = newChild != a;
}
if(!treeSafe) {
throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null));
}
}
// notify document