Example #1
0
  /**
   * Inserts the node newChild before existing node refChild. If refChild is null, insert newChild
   * at end of the list of children.
   *
   * @param newChild The new child node to insert
   * @param refChild The reference node, i.e. the node before which the new node must be inserted.
   * @throws InvalidChildItemException Raised if <code>newChild</code> being added is not valid for
   *     this node type.
   * @throws NodeNotFound Raised if the <code>refChild</code> node is not found in the node's
   *     children.
   */
  public void insertBefore(DocItem newChild, DocItem refChild)
      throws InvalidChildItemException, NodeNotFound {
    if (!newChild.validChildAddition(this))
      throw new InvalidChildItemException(
          "Child item of type \'"
              + newChild.getName()
              + "\' says it is not allowed to belong to this (\'"
              + itemName
              + "\') doc item");

    int insertPos = docNodes.size();

    //
    // Does the refChild exist and exists in this node's children?
    if (refChild != null && docNodes.contains(refChild)) insertPos = docNodes.indexOf(refChild);
    else if (refChild != null) // Isn't null & doesn't belong to this node. Argh!
    throw new NodeNotFound("Cannot find node \'" + refChild.getName() + "\'");

    docNodes.add(insertPos, newChild);
  }