Java Code Examples for org.jsoup.nodes.Node#parent()

The following examples show how to use org.jsoup.nodes.Node#parent() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: dkpro-c4corpus   File: NodeHelper.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns true if node1 is ancestor of node2 or node1 == node2
 *
 * @param node1 node 1
 * @param node2 node 2
 * @return boolean value
 */
public static boolean isAncestor(Node node1, Node node2)
{
    if (node1 == node2) {
        return true;
    }
    Node ancestor = node2;

    while (ancestor != null) {
        if (ancestor == node1) {
            return true;
        }
        ancestor = ancestor.parent();
    }

    return false;
}
 
Example 2
Source Project: dkpro-c4corpus   File: Paragraph.java    License: Apache License 2.0 6 votes vote down vote up
public String getPath(Node n)
{
    String nodePath = "";
    while (n != null) {
        if (n instanceof TextNode) {
            n = n.parent();
        }
        if (NodeHelper.isInnerText(n)) {
            n = n.parent();
        }
        String parentNodeName = n.nodeName();
        nodePath = parentNodeName + "." + nodePath;

        if (!parentNodeName.equalsIgnoreCase("html")) {
            n = n.parent();
        }
        else {
            break;
        }
    }

    return nodePath;
}
 
Example 3
Source Project: jsoup-learning   File: NodeTraversor.java    License: MIT License 6 votes vote down vote up
/**
 * Start a depth-first traverse of the root and all of its descendants.
 * @param root the root node point to traverse.
 */
public void traverse(Node root) {
    Node node = root;
    int depth = 0;
    
    while (node != null) {
        visitor.head(node, depth);
        if (node.childNodeSize() > 0) {
            node = node.childNode(0);
            depth++;
        } else {
            while (node.nextSibling() == null && depth > 0) {
                visitor.tail(node, depth);
                node = node.parent();
                depth--;
            }
            visitor.tail(node, depth);
            if (node == root)
                break;
            node = node.nextSibling();
        }
    }
}
 
Example 4
Source Project: dkpro-c4corpus   File: NodeHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns the nearest common ancestor of node1 and node2
 *
 * @param node1 node 1
 * @param node2 node 2
 * @return nearest common ancestor node
 * @throws IllegalStateException if node1 and node2 has no common ancestor
 *                               to make sure that node1 and node2 should inside the same document
 */
public static Node nearestCommonAncestor(Node node1, Node node2)
{
    Node ancestor = node1;
    while (ancestor != null) {
        if (isAncestor(ancestor, node2)) {
            return ancestor;
        }
        ancestor = ancestor.parent();
    }
    throw new IllegalStateException("node1 and node2 do not have common ancestor");
}
 
Example 5
Source Project: dkpro-c4corpus   File: NodeHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns true if node has a link ancestor
 *
 * @param node node
 * @return boolean value
 */
public static boolean isLink(Node node)
{
    Node ancestor = node;

    while (ancestor != null) {
        if (isLinkTag(ancestor)) {
            return true;
        }
        ancestor = ancestor.parent();
    }

    return false;
}
 
Example 6
/**
 * Visit from node to the ancestor - if all the visited ancestors are
 * {@link NodeHelper.TagType#INNER_TEXT} returns
 * {@link ParagraphsExplorer.AncestorState#INNERTEXT_ONLY} - if one of the
 * visited ancestors is {@link NodeHelper#isBlockTag(Node)}
 * returns {@link ParagraphsExplorer.AncestorState#BLOCKLEVEL} - otherwise
 * returns {@link ParagraphsExplorer.AncestorState#UNKNOW}
 */
private static AncestorState getAncestorStateOfBranch(Node ancestor, Node node)
{
    if (!NodeHelper.isAncestor(ancestor, node)) {
        throw new InvalidParameterException("ancestor pre-condition violation");
    }
    if (node == ancestor) {
        if (NodeHelper.isBlockTag(node)) {
            return AncestorState.BLOCKLEVEL;
        }
        if (NodeHelper.isInlineTag(node)) {
            return AncestorState.INNERTEXT_ONLY;
        }
        return AncestorState.UNKNOW;
    }
    Node n = node.parent();
    boolean innerTextOnly = true;
    while (n != ancestor && n != null) {
        if (NodeHelper.isBlockTag(n)) {
            return AncestorState.BLOCKLEVEL;
        }
        if (!NodeHelper.isInlineTag(n)) {
            innerTextOnly = false;
        }
        n = n.parent();
    }
    return innerTextOnly ? AncestorState.INNERTEXT_ONLY : AncestorState.UNKNOW;
}
 
Example 7
public static String getXpath(Node node) {
    String result = "";
    Node temp = node;
    while (temp != null) {
        String name = getNodeName(temp);
        result = "," + name + result;
        temp = temp.parent();
    }
    return result;
    
}
 
Example 8
public static String getXpath(Node node) {
    String result = "";
    Node temp = node;
    while (temp != null) {
        String name = getNodeName(temp);
        result = "," + name + result;
        temp = temp.parent();
    }
    return result;
    
}