for (Node kid = newChild.getFirstChild(); // Prescan
kid != null; kid = kid.getNextSibling()) {
if (!ownerDocument.isKidOK(this, kid)) {
String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null);
throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, msg);
}
}
}
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()) {
String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null);
throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, msg);
}
if (newChild.getOwnerDocument() != ownerDocument) {
String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null);
throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, msg);
}
if (!ownerDocument.isKidOK(this, newChild)) {
String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null);
throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, msg);
}
// refChild must be a child of this node (or null)
if (refChild != null && refChild.getParentNode() != this) {
String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null);
throw new DOMException(DOMException.NOT_FOUND_ERR, msg);
}
// 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) {
String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null);
throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, msg);
}
}
makeChildNode(); // make sure we have a node and not a string