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