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