|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| package org.dom4j.util; |
|
11 |
| |
|
12 |
| import java.util.Comparator; |
|
13 |
| |
|
14 |
| import org.dom4j.Attribute; |
|
15 |
| import org.dom4j.Branch; |
|
16 |
| import org.dom4j.CDATA; |
|
17 |
| import org.dom4j.CharacterData; |
|
18 |
| import org.dom4j.Comment; |
|
19 |
| import org.dom4j.Document; |
|
20 |
| import org.dom4j.DocumentType; |
|
21 |
| import org.dom4j.Element; |
|
22 |
| import org.dom4j.Entity; |
|
23 |
| import org.dom4j.Namespace; |
|
24 |
| import org.dom4j.Node; |
|
25 |
| import org.dom4j.ProcessingInstruction; |
|
26 |
| import org.dom4j.QName; |
|
27 |
| import org.dom4j.Text; |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| public class NodeComparator implements Comparator { |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
14
| public int compare(Object o1, Object o2) {
|
|
70 |
14
| if ( o1 == o2 ) {
|
|
71 |
0
| return 0;
|
|
72 |
| } |
|
73 |
14
| else if ( o1 == null ) {
|
|
74 |
| |
|
75 |
0
| return -1;
|
|
76 |
| } |
|
77 |
14
| else if ( o2 == null ) {
|
|
78 |
0
| return 1;
|
|
79 |
| } |
|
80 |
14
| if ( o1 instanceof Node ) {
|
|
81 |
14
| if ( o2 instanceof Node ) {
|
|
82 |
14
| return compare( (Node) o1, (Node) o2 );
|
|
83 |
| } |
|
84 |
| else { |
|
85 |
| |
|
86 |
0
| return 1;
|
|
87 |
| } |
|
88 |
| } |
|
89 |
| else { |
|
90 |
0
| if ( o2 instanceof Node ) {
|
|
91 |
| |
|
92 |
0
| return -1;
|
|
93 |
| } |
|
94 |
| else { |
|
95 |
0
| if ( o1 instanceof Comparable ) {
|
|
96 |
0
| Comparable c1 = (Comparable) o1;
|
|
97 |
0
| return c1.compareTo( o2 );
|
|
98 |
| } |
|
99 |
| else { |
|
100 |
0
| int answer = o1.getClass().getName().compareTo(
|
|
101 |
| o2.getClass().getName() |
|
102 |
| ); |
|
103 |
0
| if ( answer == 0 ) {
|
|
104 |
0
| answer = o1.hashCode() - o2.hashCode();
|
|
105 |
| } |
|
106 |
0
| return answer;
|
|
107 |
| } |
|
108 |
| } |
|
109 |
| } |
|
110 |
| } |
|
111 |
| |
|
112 |
108518
| public int compare( Node n1, Node n2 ) {
|
|
113 |
108518
| int nodeType1 = n1.getNodeType();
|
|
114 |
108518
| int nodeType2 = n2.getNodeType();
|
|
115 |
108518
| int answer = nodeType1 - nodeType2;
|
|
116 |
108518
| if ( answer != 0 ) {
|
|
117 |
0
| return answer;
|
|
118 |
| } |
|
119 |
| else { |
|
120 |
108518
| switch (nodeType1) {
|
|
121 |
38140
| case Node.ELEMENT_NODE:
|
|
122 |
38140
| return compare((Element) n1, (Element) n2);
|
|
123 |
10
| case Node.DOCUMENT_NODE:
|
|
124 |
10
| return compare((Document) n1, (Document) n2);
|
|
125 |
0
| case Node.ATTRIBUTE_NODE:
|
|
126 |
0
| return compare((Attribute) n1, (Attribute) n2);
|
|
127 |
69244
| case Node.TEXT_NODE:
|
|
128 |
69244
| return compare((Text) n1, (Text) n2);
|
|
129 |
150
| case Node.CDATA_SECTION_NODE:
|
|
130 |
150
| return compare((CDATA) n1, (CDATA) n2);
|
|
131 |
0
| case Node.ENTITY_REFERENCE_NODE:
|
|
132 |
0
| return compare((Entity) n1, (Entity) n2);
|
|
133 |
10
| case Node.PROCESSING_INSTRUCTION_NODE:
|
|
134 |
10
| return compare((ProcessingInstruction) n1, (ProcessingInstruction) n2);
|
|
135 |
748
| case Node.COMMENT_NODE:
|
|
136 |
748
| return compare((Comment) n1, (Comment) n2);
|
|
137 |
0
| case Node.DOCUMENT_TYPE_NODE:
|
|
138 |
0
| return compare((DocumentType) n1, (DocumentType) n2);
|
|
139 |
216
| case Node.NAMESPACE_NODE:
|
|
140 |
216
| return compare((Namespace) n1, (Namespace) n2);
|
|
141 |
0
| default:
|
|
142 |
0
| throw new RuntimeException(
|
|
143 |
| "Invalid node types. node1: " + n1 + " and node2: " + n2 |
|
144 |
| ); |
|
145 |
| } |
|
146 |
| } |
|
147 |
| } |
|
148 |
| |
|
149 |
112
| public int compare( Document n1, Document n2 ) {
|
|
150 |
112
| int answer = compare( n1.getDocType(), n2.getDocType() );
|
|
151 |
112
| if ( answer == 0 ) {
|
|
152 |
112
| answer = compareContent( n1, n2 );
|
|
153 |
| } |
|
154 |
112
| return answer;
|
|
155 |
| } |
|
156 |
| |
|
157 |
38140
| public int compare( Element n1, Element n2 ) {
|
|
158 |
38140
| int answer = compare( n1.getQName(), n2.getQName() );
|
|
159 |
38140
| if ( answer == 0 ) {
|
|
160 |
| |
|
161 |
38140
| int c1 = n1.attributeCount();
|
|
162 |
38140
| int c2 = n2.attributeCount();
|
|
163 |
38140
| answer = c1 - c2;
|
|
164 |
38140
| if ( answer == 0 ) {
|
|
165 |
38138
| for ( int i = 0; i < c1; i++ ) {
|
|
166 |
15192
| Attribute a1 = n1.attribute(i);
|
|
167 |
15192
| Attribute a2 = n2.attribute(a1.getQName());
|
|
168 |
15192
| answer = compare( a1, a2 );
|
|
169 |
15192
| if ( answer != 0 ) {
|
|
170 |
2
| return answer;
|
|
171 |
| } |
|
172 |
| } |
|
173 |
38136
| answer = compareContent( n1, n2 );
|
|
174 |
| } |
|
175 |
| } |
|
176 |
38138
| return answer;
|
|
177 |
| } |
|
178 |
| |
|
179 |
15192
| public int compare( Attribute n1, Attribute n2 ) {
|
|
180 |
15192
| int answer = compare( n1.getQName(), n2.getQName() );
|
|
181 |
15192
| if ( answer == 0 ) {
|
|
182 |
15192
| answer = compare( n1.getValue(), n2.getValue() );
|
|
183 |
| } |
|
184 |
15192
| return answer;
|
|
185 |
| } |
|
186 |
| |
|
187 |
53332
| public int compare( QName n1, QName n2 ) {
|
|
188 |
53332
| int answer = compare( n1.getNamespaceURI(), n2.getNamespaceURI() );
|
|
189 |
53332
| if ( answer == 0 ) {
|
|
190 |
53332
| answer = compare( n1.getQualifiedName(), n2.getQualifiedName() );
|
|
191 |
| } |
|
192 |
53332
| return answer;
|
|
193 |
| } |
|
194 |
| |
|
195 |
216
| public int compare( Namespace n1, Namespace n2 ) {
|
|
196 |
216
| int answer = compare( n1.getURI(), n2.getURI() );
|
|
197 |
216
| if ( answer == 0 ) {
|
|
198 |
216
| answer = compare( n1.getPrefix(), n2.getPrefix() );
|
|
199 |
| } |
|
200 |
216
| return answer;
|
|
201 |
| } |
|
202 |
| |
|
203 |
70142
| public int compare( CharacterData t1, CharacterData t2 ) {
|
|
204 |
70142
| return compare( t1.getText(), t2.getText() );
|
|
205 |
| } |
|
206 |
| |
|
207 |
112
| public int compare( DocumentType o1, DocumentType o2 ) {
|
|
208 |
112
| if ( o1 == o2 ) {
|
|
209 |
112
| return 0;
|
|
210 |
| } |
|
211 |
0
| else if ( o1 == null ) {
|
|
212 |
| |
|
213 |
0
| return -1;
|
|
214 |
| } |
|
215 |
0
| else if ( o2 == null ) {
|
|
216 |
0
| return 1;
|
|
217 |
| } |
|
218 |
0
| int answer = compare( o1.getPublicID(), o2.getPublicID() );
|
|
219 |
0
| if ( answer == 0 ) {
|
|
220 |
0
| answer = compare( o1.getSystemID(), o2.getSystemID() );
|
|
221 |
0
| if ( answer == 0 ) {
|
|
222 |
0
| answer = compare( o1.getName(), o2.getName() );
|
|
223 |
| } |
|
224 |
| } |
|
225 |
0
| return answer;
|
|
226 |
| } |
|
227 |
| |
|
228 |
0
| public int compare( Entity n1, Entity n2 ) {
|
|
229 |
0
| int answer = compare( n1.getName(), n2.getName() );
|
|
230 |
0
| if ( answer == 0 ) {
|
|
231 |
0
| answer = compare( n1.getText(), n2.getText() );
|
|
232 |
| } |
|
233 |
0
| return answer;
|
|
234 |
| } |
|
235 |
| |
|
236 |
10
| public int compare( ProcessingInstruction n1, ProcessingInstruction n2 ) {
|
|
237 |
10
| int answer = compare( n1.getTarget(), n2.getTarget() );
|
|
238 |
10
| if ( answer == 0 ) {
|
|
239 |
10
| answer = compare( n1.getText(), n2.getText() );
|
|
240 |
| } |
|
241 |
10
| return answer;
|
|
242 |
| } |
|
243 |
| |
|
244 |
38248
| public int compareContent( Branch b1, Branch b2 ) {
|
|
245 |
38248
| int c1 = b1.nodeCount();
|
|
246 |
38248
| int c2 = b2.nodeCount();
|
|
247 |
38248
| int answer = c1 - c2;
|
|
248 |
38248
| if ( answer == 0 ) {
|
|
249 |
38244
| for ( int i = 0; i < c1; i++ ) {
|
|
250 |
108504
| Node n1 = b1.node(i);
|
|
251 |
108504
| Node n2 = b2.node(i);
|
|
252 |
108504
| answer = compare( n1, n2 );
|
|
253 |
108504
| if ( answer != 0 ) {
|
|
254 |
12
| break;
|
|
255 |
| } |
|
256 |
| } |
|
257 |
| } |
|
258 |
38248
| return answer;
|
|
259 |
| } |
|
260 |
| |
|
261 |
192450
| public int compare( String o1, String o2 ) {
|
|
262 |
192450
| if ( o1 == o2 ) {
|
|
263 |
120728
| return 0;
|
|
264 |
| } |
|
265 |
71722
| else if ( o1 == null ) {
|
|
266 |
| |
|
267 |
0
| return -1;
|
|
268 |
| } |
|
269 |
71722
| else if ( o2 == null ) {
|
|
270 |
0
| return 1;
|
|
271 |
| } |
|
272 |
71722
| return o1.compareTo( o2 );
|
|
273 |
| } |
|
274 |
| } |
|
275 |
| |
|
276 |
| |
|
277 |
| |
|
278 |
| |
|
279 |
| |
|
280 |
| |
|
281 |
| |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
| |
|
286 |
| |
|
287 |
| |
|
288 |
| |
|
289 |
| |
|
290 |
| |
|
291 |
| |
|
292 |
| |
|
293 |
| |
|
294 |
| |
|
295 |
| |
|
296 |
| |
|
297 |
| |
|
298 |
| |
|
299 |
| |
|
300 |
| |
|
301 |
| |
|
302 |
| |
|
303 |
| |
|
304 |
| |
|
305 |
| |
|
306 |
| |
|
307 |
| |
|
308 |
| |
|
309 |
| |
|
310 |
| |
|
311 |
| |
|
312 |
| |
|
313 |
| |
|
314 |
| |
|
315 |
| |
|
316 |
| |
|
317 |
| |
|
318 |
| |
|
319 |
| |
|
320 |
| |
|
321 |
| |
|
322 |
| |