1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util.xml;
24  
25  import java.util.Comparator;
26  import java.util.List;
27  
28  import org.dom4j.Attribute;
29  import org.dom4j.Element;
30  
31  /**
32   * <a href="ElementComparator.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class ElementComparator implements Comparator<Element> {
37  
38      public int compare(Element el1, Element el2) {
39          String el1Name = el1.getName();
40          String el2Name = el2.getName();
41  
42          if (!el1Name.equals(el2Name)) {
43              return el1Name.compareTo(el2Name);
44          }
45  
46          String el1Text = el1.getTextTrim();
47          String el2Text = el2.getTextTrim();
48  
49          if (!el1Text.equals(el2Text)) {
50              return el1Text.compareTo(el2Text);
51          }
52  
53          List<Attribute> el1Attributes = el1.attributes();
54          List<Attribute> el2Attributes = el2.attributes();
55  
56          if (el1Attributes.size() < el2Attributes.size()) {
57              return -1;
58          }
59          else if (el1Attributes.size() > el2Attributes.size()) {
60              return 1;
61          }
62  
63          for (Attribute attr : el1Attributes) {
64              int value = _compare(
65                  el2Attributes, attr, new AttributeComparator());
66  
67              if (value != 0) {
68                  return value;
69              }
70          }
71  
72          List<Element> el1Elements = el1.elements();
73          List<Element> el2Elements = el2.elements();
74  
75          if (el1Elements.size() < el2Elements.size()) {
76              return -1;
77          }
78          else if (el1Elements.size() > el2Elements.size()) {
79              return 1;
80          }
81  
82          for (Element el : el1Elements) {
83              int value = _compare(el2Elements, el, new ElementComparator());
84  
85              if (value != 0) {
86                  return value;
87              }
88          }
89  
90          return 0;
91      }
92  
93      private int _compare(
94          List<Attribute> list, Attribute obj, Comparator<Attribute> comparator) {
95  
96          int firstValue = -1;
97  
98          for (int i = 0; i < list.size(); i++) {
99              Attribute o = list.get(i);
100 
101             int value = comparator.compare(obj, o);
102 
103             if (i == 0) {
104                 firstValue = value;
105             }
106 
107             if (value == 0) {
108                 return 0;
109             }
110         }
111 
112         return firstValue;
113     }
114 
115     private int _compare(
116         List<Element> list, Element obj, Comparator<Element> comparator) {
117 
118         int firstValue = -1;
119 
120         for (int i = 0; i < list.size(); i++) {
121             Element o = list.get(i);
122 
123             int value = comparator.compare(obj, o);
124 
125             if (i == 0) {
126                 firstValue = value;
127             }
128 
129             if (value == 0) {
130                 return 0;
131             }
132         }
133 
134         return firstValue;
135     }
136 
137 }