1   /**
2    * Copyright (c) 2000-2009 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> el1Attributes = el1.attributes();
55          List<Attribute> el2Attributes = el2.attributes();
56  
57          if (el1Attributes.size() < el2Attributes.size()) {
58              return -1;
59          }
60          else if (el1Attributes.size() > el2Attributes.size()) {
61              return 1;
62          }
63  
64          for (Attribute attr : el1Attributes) {
65              int value = _compare(
66                  el2Attributes, attr, new AttributeComparator());
67  
68              if (value != 0) {
69                  return value;
70              }
71          }
72  
73          List<Element> el1Elements = el1.elements();
74          List<Element> el2Elements = el2.elements();
75  
76          if (el1Elements.size() < el2Elements.size()) {
77              return -1;
78          }
79          else if (el1Elements.size() > el2Elements.size()) {
80              return 1;
81          }
82  
83          for (Element el : el1Elements) {
84              int value = _compare(el2Elements, el, new ElementComparator());
85  
86              if (value != 0) {
87                  return value;
88              }
89          }
90  
91          return 0;
92      }
93  
94      private int _compare(
95          List<Attribute> list, Attribute obj, Comparator<Attribute> comparator) {
96  
97          int firstValue = -1;
98  
99          for (int i = 0; i < list.size(); i++) {
100             Attribute o = list.get(i);
101 
102             int value = comparator.compare(obj, o);
103 
104             if (i == 0) {
105                 firstValue = value;
106             }
107 
108             if (value == 0) {
109                 return 0;
110             }
111         }
112 
113         return firstValue;
114     }
115 
116     private int _compare(
117         List<Element> list, Element obj, Comparator<Element> comparator) {
118 
119         int firstValue = -1;
120 
121         for (int i = 0; i < list.size(); i++) {
122             Element o = list.get(i);
123 
124             int value = comparator.compare(obj, o);
125 
126             if (i == 0) {
127                 firstValue = value;
128             }
129 
130             if (value == 0) {
131                 return 0;
132             }
133         }
134 
135         return firstValue;
136     }
137 
138 }