001    /**
002     * Copyright (c) 2000-2013 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            @Override
029            public int compare(Element el1, Element el2) {
030                    String el1Name = el1.getName();
031                    String el2Name = el2.getName();
032    
033                    if (!el1Name.equals(el2Name)) {
034                            return el1Name.compareTo(el2Name);
035                    }
036    
037                    String el1Text = el1.getTextTrim();
038                    String el2Text = el2.getTextTrim();
039    
040                    if (!el1Text.equals(el2Text)) {
041                            return el1Text.compareTo(el2Text);
042                    }
043    
044                    List<Attribute> el1Attributes = el1.attributes();
045                    List<Attribute> el2Attributes = el2.attributes();
046    
047                    if (el1Attributes.size() < el2Attributes.size()) {
048                            return -1;
049                    }
050                    else if (el1Attributes.size() > el2Attributes.size()) {
051                            return 1;
052                    }
053    
054                    for (Attribute attr : el1Attributes) {
055                            int value = _compare(
056                                    el2Attributes, attr, new AttributeComparator());
057    
058                            if (value != 0) {
059                                    return value;
060                            }
061                    }
062    
063                    List<Element> el1Elements = el1.elements();
064                    List<Element> el2Elements = el2.elements();
065    
066                    if (el1Elements.size() < el2Elements.size()) {
067                            return -1;
068                    }
069                    else if (el1Elements.size() > el2Elements.size()) {
070                            return 1;
071                    }
072    
073                    for (Element el : el1Elements) {
074                            int value = _compare(el2Elements, el, new ElementComparator());
075    
076                            if (value != 0) {
077                                    return value;
078                            }
079                    }
080    
081                    return 0;
082            }
083    
084            private int _compare(
085                    List<Attribute> list, Attribute obj, Comparator<Attribute> comparator) {
086    
087                    int firstValue = -1;
088    
089                    for (int i = 0; i < list.size(); i++) {
090                            Attribute o = list.get(i);
091    
092                            int value = comparator.compare(obj, o);
093    
094                            if (i == 0) {
095                                    firstValue = value;
096                            }
097    
098                            if (value == 0) {
099                                    return 0;
100                            }
101                    }
102    
103                    return firstValue;
104            }
105    
106            private int _compare(
107                    List<Element> list, Element obj, Comparator<Element> comparator) {
108    
109                    int firstValue = -1;
110    
111                    for (int i = 0; i < list.size(); i++) {
112                            Element o = list.get(i);
113    
114                            int value = comparator.compare(obj, o);
115    
116                            if (i == 0) {
117                                    firstValue = value;
118                            }
119    
120                            if (value == 0) {
121                                    return 0;
122                            }
123                    }
124    
125                    return firstValue;
126            }
127    
128    }