private void checkPreConditions(final Content child, final int index,
                                  final boolean replace) {
    if (child == null) {
      throw new NullPointerException("Cannot add null object");
    }

    checkIndex(index, replace);

    if (child.getParent() != null) {
      // the content to be added already has a parent.
      final Parent p = child.getParent();
      if (p instanceof Document) {
        throw new IllegalAddException((Element)child,
                                      "The Content already has an existing parent document");
      }
      throw new IllegalAddException(
        "The Content already has an existing parent \"" +
        ((Element)p).getQualifiedName() + "\"");
    }

    if (child == parent) {
      throw new IllegalAddException(
        "The Element cannot be added to itself");
    }

    // Detect if we have <a><b><c/></b></a> and c.add(a)
    if ((parent instanceof Element && child instanceof Element) &&
        ((Element)child).isAncestor((Element)parent)) {
      throw new IllegalAddException(
        "The Element cannot be added as a descendent of itself");
    }
  }
private void checkPreConditions(final Content child, final int index,
                                  final boolean replace)