1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.util.xml;
16  
17  import java.util.Comparator;
18  import java.util.List;
19  
20  import org.dom4j.Attribute;
21  import org.dom4j.Element;
22  
23  /**
24   * <a href="ElementComparator.java.html"><b><i>View Source</i></b></a>
25   *
26   * @author Brian Wing Shun Chan
27   */
28  public class ElementComparator implements Comparator<Element> {
29  
30      public int compare(Element el1, Element el2) {
31          String el1Name = el1.getName();
32          String el2Name = el2.getName();
33  
34          if (!el1Name.equals(el2Name)) {
35              return el1Name.compareTo(el2Name);
36          }
37  
38          String el1Text = el1.getTextTrim();
39          String el2Text = el2.getTextTrim();
40  
41          if (!el1Text.equals(el2Text)) {
42              return el1Text.compareTo(el2Text);
43          }
44  
45          List<Attribute> el1Attributes = el1.attributes();
46          List<Attribute> el2Attributes = el2.attributes();
47  
48          if (el1Attributes.size() < el2Attributes.size()) {
49              return -1;
50          }
51          else if (el1Attributes.size() > el2Attributes.size()) {
52              return 1;
53          }
54  
55          for (Attribute attr : el1Attributes) {
56              int value = _compare(
57                  el2Attributes, attr, new AttributeComparator());
58  
59              if (value != 0) {
60                  return value;
61              }
62          }
63  
64          List<Element> el1Elements = el1.elements();
65          List<Element> el2Elements = el2.elements();
66  
67          if (el1Elements.size() < el2Elements.size()) {
68              return -1;
69          }
70          else if (el1Elements.size() > el2Elements.size()) {
71              return 1;
72          }
73  
74          for (Element el : el1Elements) {
75              int value = _compare(el2Elements, el, new ElementComparator());
76  
77              if (value != 0) {
78                  return value;
79              }
80          }
81  
82          return 0;
83      }
84  
85      private int _compare(
86          List<Attribute> list, Attribute obj, Comparator<Attribute> comparator) {
87  
88          int firstValue = -1;
89  
90          for (int i = 0; i < list.size(); i++) {
91              Attribute o = list.get(i);
92  
93              int value = comparator.compare(obj, o);
94  
95              if (i == 0) {
96                  firstValue = value;
97              }
98  
99              if (value == 0) {
100                 return 0;
101             }
102         }
103 
104         return firstValue;
105     }
106 
107     private int _compare(
108         List<Element> list, Element obj, Comparator<Element> comparator) {
109 
110         int firstValue = -1;
111 
112         for (int i = 0; i < list.size(); i++) {
113             Element o = list.get(i);
114 
115             int value = comparator.compare(obj, o);
116 
117             if (i == 0) {
118                 firstValue = value;
119             }
120 
121             if (value == 0) {
122                 return 0;
123             }
124         }
125 
126         return firstValue;
127     }
128 
129 }