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 {
38  
39      public int compare(Object obj1, Object obj2) {
40          Element el1 = (Element)obj1;
41          Element el2 = (Element)obj2;
42  
43          String el1Name = el1.getName();
44          String el2Name = el2.getName();
45  
46          if (!el1Name.equals(el2Name)) {
47              return el1Name.compareTo(el2Name);
48          }
49  
50          String el1Text = el1.getTextTrim();
51          String el2Text = el2.getTextTrim();
52  
53          if (!el1Text.equals(el2Text)) {
54              return el1Text.compareTo(el2Text);
55          }
56  
57          List el1Attrs = el1.attributes();
58          List el2Attrs = el2.attributes();
59  
60          if (el1Attrs.size() < el2Attrs.size()) {
61              return -1;
62          }
63          else if (el1Attrs.size() > el2Attrs.size()) {
64              return 1;
65          }
66  
67          for (int i = 0; i < el1Attrs.size(); i++) {
68              Attribute attr = (Attribute)el1Attrs.get(i);
69  
70              int value = _compare(el2Attrs, attr, new AttributeComparator());
71  
72              if (value != 0) {
73                  return value;
74              }
75          }
76  
77          List el1Elements = el1.elements();
78          List el2Elements = el2.elements();
79  
80          if (el1Elements.size() < el2Elements.size()) {
81              return -1;
82          }
83          else if (el1Elements.size() > el2Elements.size()) {
84              return 1;
85          }
86  
87          for (int i = 0; i < el1Elements.size(); i++) {
88              Element el = (Element)el1Elements.get(i);
89  
90              int value = _compare(el2Elements, el, new ElementComparator());
91  
92              if (value != 0) {
93                  return value;
94              }
95          }
96  
97          return 0;
98      }
99  
100     private int _compare(List list, Object obj, Comparator comparator) {
101         int firstValue = -1;
102 
103         for (int i = 0; i < list.size(); i++) {
104             Object o = list.get(i);
105 
106             int value = comparator.compare(obj, o);
107 
108             if (i == 0) {
109                 firstValue = value;
110             }
111 
112             if (value == 0) {
113                 return 0;
114             }
115         }
116 
117         return firstValue;
118     }
119 
120 }