1 28 29 package com.caucho.xpath.pattern; 30 31 import org.w3c.dom.Node ; 32 33 36 public class SelectedNode { 37 protected Node _node; 38 39 protected int _depth; 40 protected int _level; 41 42 47 public SelectedNode(Node node) 48 { 49 _node = node; 50 51 _depth = 0; 52 for (Node ptr = node; ptr != null; ptr = ptr.getParentNode()) { 53 _depth++; 54 } 55 56 _level = 0; 57 for (Node ptr = node; ptr != null; ptr = ptr.getPreviousSibling()) { 58 _level++; 59 } 60 } 61 62 65 public Node getNode() 66 { 67 return _node; 68 } 69 70 73 public int compareTo(SelectedNode b) 74 { 75 int aDepth = _depth; 76 int bDepth = b._depth; 77 78 Node aPtr = getNode(); 79 Node bPtr = b.getNode(); 80 81 if (aDepth == bDepth && aPtr.getParentNode() == bPtr.getParentNode()) 82 return _level - b._level; 83 84 return compareTo(aPtr, aDepth, bPtr, bDepth); 85 } 86 87 90 static int compareTo(Node aPtr, int aDepth, Node bPtr, int bDepth) 91 { 92 for (int depth = aDepth; bDepth < depth; depth--) 93 aPtr = aPtr.getParentNode(); 94 95 for (int depth = bDepth; aDepth < depth; depth--) 96 bPtr = bPtr.getParentNode(); 97 98 Node aParent; 99 Node bParent; 100 while ((aParent = aPtr.getParentNode()) != 101 (bParent = bPtr.getParentNode())) { 102 aPtr = aParent; 103 bPtr = bParent; 104 } 105 106 if (aPtr == bPtr) 107 return aDepth - bDepth; 108 109 for (; aPtr != null; aPtr = aPtr.getPreviousSibling()) { 110 if (aPtr == bPtr) 111 return 1; 112 } 113 114 return -1; 115 } 116 } 117 118 | Popular Tags |