1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 package org.jaxen.expr;
48
49 import java.util.Comparator;
50 import java.util.Iterator;
51
52 import org.jaxen.Navigator;
53 import org.jaxen.UnsupportedAxisException;
54
55
56 class NodeComparator implements Comparator {
57
58 private Navigator navigator;
59
60
61 NodeComparator(Navigator navigator) {
62 this.navigator = navigator;
63 }
64
65 public int compare(Object o1, Object o2) {
66
67 if (navigator == null) return 0;
68
69 if (isNonChild(o1) && isNonChild(o2)) {
70
71 try {
72 Object p1 = navigator.getParentNode(o1);
73 Object p2 = navigator.getParentNode(o2);
74
75 if (p1 == p2) {
76 if (navigator.isNamespace(o1) && navigator.isAttribute(o2)) {
77 return -1;
78 }
79 else if (navigator.isNamespace(o2) && navigator.isAttribute(o1)) {
80 return 1;
81 }
82 }
83
84 return compare(p1, p2);
85 }
86 catch (UnsupportedAxisException ex) {
87 return 0;
88 }
89
90 }
91
92 try {
93 int depth1 = getDepth(o1);
94 int depth2 = getDepth(o2);
95
96 Object a1 = o1;
97 Object a2 = o2;
98
99 while (depth1 > depth2) {
100 a1 = navigator.getParentNode(a1);
101 depth1--;
102 }
103 if (a1 == o2) return 1;
104
105 while (depth2 > depth1) {
106 a2 = navigator.getParentNode(a2);
107 depth2--;
108 }
109 if (a2 == o1) return -1;
110
111
112 while (true) {
113 Object p1 = navigator.getParentNode(a1);
114 Object p2 = navigator.getParentNode(a2);
115 if (p1 == p2) {
116 return compareSiblings(a1, a2);
117 }
118 a1 = p1;
119 a2 = p2;
120 }
121
122 }
123 catch (UnsupportedAxisException ex) {
124 return 0;
125 }
126 }
127
128
129 private boolean isNonChild(Object o) {
130 return navigator.isAttribute(o) || navigator.isNamespace(o);
131 }
132
133 private int compareSiblings(Object sib1, Object sib2)
134 throws UnsupportedAxisException {
135
136 Iterator following = navigator.getFollowingSiblingAxisIterator(sib1);
137 while (following.hasNext()) {
138 Object next = following.next();
139 if (next.equals(sib2)) return -1;
140 }
141 return 1;
142
143 }
144
145 private int getDepth(Object o) throws UnsupportedAxisException {
146
147 int depth = 0;
148 Object parent = o;
149
150 while ((parent = navigator.getParentNode(parent)) != null) {
151 depth++;
152 }
153 return depth;
154
155 }
156
157 }