001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util.xml;
016    
017    import java.util.Comparator;
018    import java.util.List;
019    
020    import org.dom4j.Attribute;
021    import org.dom4j.Element;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     */
026    public class ElementComparator implements Comparator<Element> {
027    
028            public int compare(Element el1, Element el2) {
029                    String el1Name = el1.getName();
030                    String el2Name = el2.getName();
031    
032                    if (!el1Name.equals(el2Name)) {
033                            return el1Name.compareTo(el2Name);
034                    }
035    
036                    String el1Text = el1.getTextTrim();
037                    String el2Text = el2.getTextTrim();
038    
039                    if (!el1Text.equals(el2Text)) {
040                            return el1Text.compareTo(el2Text);
041                    }
042    
043                    List<Attribute> el1Attributes = el1.attributes();
044                    List<Attribute> el2Attributes = el2.attributes();
045    
046                    if (el1Attributes.size() < el2Attributes.size()) {
047                            return -1;
048                    }
049                    else if (el1Attributes.size() > el2Attributes.size()) {
050                            return 1;
051                    }
052    
053                    for (Attribute attr : el1Attributes) {
054                            int value = _compare(
055                                    el2Attributes, attr, new AttributeComparator());
056    
057                            if (value != 0) {
058                                    return value;
059                            }
060                    }
061    
062                    List<Element> el1Elements = el1.elements();
063                    List<Element> el2Elements = el2.elements();
064    
065                    if (el1Elements.size() < el2Elements.size()) {
066                            return -1;
067                    }
068                    else if (el1Elements.size() > el2Elements.size()) {
069                            return 1;
070                    }
071    
072                    for (Element el : el1Elements) {
073                            int value = _compare(el2Elements, el, new ElementComparator());
074    
075                            if (value != 0) {
076                                    return value;
077                            }
078                    }
079    
080                    return 0;
081            }
082    
083            private int _compare(
084                    List<Attribute> list, Attribute obj, Comparator<Attribute> comparator) {
085    
086                    int firstValue = -1;
087    
088                    for (int i = 0; i < list.size(); i++) {
089                            Attribute o = list.get(i);
090    
091                            int value = comparator.compare(obj, o);
092    
093                            if (i == 0) {
094                                    firstValue = value;
095                            }
096    
097                            if (value == 0) {
098                                    return 0;
099                            }
100                    }
101    
102                    return firstValue;
103            }
104    
105            private int _compare(
106                    List<Element> list, Element obj, Comparator<Element> comparator) {
107    
108                    int firstValue = -1;
109    
110                    for (int i = 0; i < list.size(); i++) {
111                            Element o = list.get(i);
112    
113                            int value = comparator.compare(obj, o);
114    
115                            if (i == 0) {
116                                    firstValue = value;
117                            }
118    
119                            if (value == 0) {
120                                    return 0;
121                            }
122                    }
123    
124                    return firstValue;
125            }
126    
127    }