1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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   */
37  public class ElementComparator implements Comparator<Element> {
38  
39      public int compare(Element el1, Element el2) {
40          String el1Name = el1.getName();
41          String el2Name = el2.getName();
42  
43          if (!el1Name.equals(el2Name)) {
44              return el1Name.compareTo(el2Name);
45          }
46  
47          String el1Text = el1.getTextTrim();
48          String el2Text = el2.getTextTrim();
49  
50          if (!el1Text.equals(el2Text)) {
51              return el1Text.compareTo(el2Text);
52          }
53  
54          List<Attribute> el1Attrs = el1.attributes();
55          List<Attribute> el2Attrs = el2.attributes();
56  
57          if (el1Attrs.size() < el2Attrs.size()) {
58              return -1;
59          }
60          else if (el1Attrs.size() > el2Attrs.size()) {
61              return 1;
62          }
63  
64          for (Attribute attr : el1Attrs) {
65              int value = _compare(el2Attrs, 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(List<?> list, Object obj, Comparator comparator) {
94          int firstValue = -1;
95  
96          for (int i = 0; i < list.size(); i++) {
97              Object o = list.get(i);
98  
99              int value = comparator.compare(obj, o);
100 
101             if (i == 0) {
102                 firstValue = value;
103             }
104 
105             if (value == 0) {
106                 return 0;
107             }
108         }
109 
110         return firstValue;
111     }
112 
113 }