KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > util > Nodes


1 package org.lobobrowser.util;
2
3 import org.w3c.dom.*;
4
5 public class Nodes {
6     public static Node getCommonAncestor(Node node1, Node node2) {
7         if(node1 == null || node2 == null) {
8             return null;
9         }
10         Node checkNode = node1;
11         while(!isSameOrAncestorOf(checkNode, node2)) {
12             checkNode = checkNode.getParentNode();
13             if(checkNode == null) {
14                 return null;
15             }
16         }
17         return checkNode;
18     }
19     
20     public static boolean isSameOrAncestorOf(Node node, Node child) {
21         if(child.isSameNode(node)) {
22             return true;
23         }
24         Node parent = child.getParentNode();
25         if(parent == null) {
26             return false;
27         }
28         return isSameOrAncestorOf(node, parent);
29     }
30 }
31
Popular Tags