/**
  * @param parent the parent node
  * @param child the child node
  * @return the position of the child node within the array of children or -1 if there is no parent
  *     child relationship
  */
 public static int indexOf(Node parent, Node child) {
   for (int i = 0; i < parent.getChildCount(); i++) {
     if (child.equals(parent.getChild(i))) {
       return i;
     }
   }
   return -1;
 }
  /**
   * @param root the root node
   * @param node the node to start from
   * @return a list of nodes starting with the parent closest to the root and ending with the given
   *     node
   */
  private List<Node> getParents(Element root, Node node) {
    ArrayList<Node> result = new ArrayList<Node>();
    while ((node != null) && !node.equals(root)) {
      result.add(node);

      node = node.getParentNode();
    }
    Collections.reverse(result);
    return result;
  }
  /**
   * Recursively walks the tree depth first and creates a list of affected nodes.
   *
   * @param curNode the (relative) root node to start with
   */
  private void walk(Node curNode) {

    // check if we're still within the affected subtree
    // and the current node has any taggable content
    if (!this.outOfAffectedSubtree
        && ((curNode.getNodeValue() == null) || !curNode.getNodeValue().trim().isEmpty())) {

      // all text nodes gets added, in case we get into the affected subtree with this
      // node or one of its children
      if (curNode.getNodeType() == Node.TEXT_NODE) {
        affectedNodes.push(curNode);
      }

      // we check for children and go down the subtrees
      if (curNode.hasChildNodes()) {
        for (int i = 0; i < curNode.getChildCount(); i++) {
          walk(curNode.getChild(i));
        }
      }
      // if we reach the outer left node
      // we're in the affacted subtree -> all parent nodes can stay on the stack
      else if (curNode.equals(outerLeftNode)) {
        this.inAffectedSubtree = true;
      }
      // if we reach the outer right node
      // we reject all the rest of the upcoming nodes
      else if (curNode.equals(outerRightNode)) {
        this.outOfAffectedSubtree = true;
      }
      // if the current node is a text node it has already been pushed onto the stack
      // and if we're not within the affected subtree, we're removing the current node from the
      // stack
      // (not being in the affected subtree means neither the current node nor one of its
      //  children is the outer left node)
      if (!inAffectedSubtree && (curNode.getNodeType() == Node.TEXT_NODE)) {
        affectedNodes.pop();
      }
    }
  }
Esempio n. 4
0
  @PatchMethod
  static Node getNextSibling(Node node) {
    Node parent = node.getParentNode();
    if (parent == null) return null;

    List<Node> list = getChildNodeList(parent);

    for (int i = 0; i < list.size(); i++) {
      Node current = list.get(i);
      if (current.equals(node) && i < list.size() - 1) {
        return list.get(i + 1);
      }
    }

    return null;
  }