Exemple #1
0
  @Test
  public void nodeValue_Element() {
    // Arrange
    Node doucmentNode = Document.get().getDocumentElement();
    // Pre-Assert
    assertNull(doucmentNode.getNodeValue());

    // Act
    doucmentNode.setNodeValue("node");

    // Assert
    assertNull(doucmentNode.getNodeValue());
  }
  /**
   * 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();
      }
    }
  }
  private static void setupChildNodes(Node newNode, Node oldNode, boolean deep) {

    List<Node> childs = getChildNodeList(oldNode);
    if (deep) {
      // copy all child nodes
      for (Node child : childs) {
        appendChild(newNode, cloneNode(child, true));
      }
    } else {
      // only copy the TextNode if exists
      for (Node child : childs) {
        if (Node.TEXT_NODE == child.getNodeType()) {
          appendChild(newNode, Document.get().createTextNode(child.getNodeValue()));
          break;
        }
      }
    }
  }