001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.xml;
022:
023: import java.util.Comparator;
024: import java.util.List;
025:
026: import org.dom4j.Attribute;
027: import org.dom4j.Element;
028:
029: /**
030: * <a href="ElementComparator.java.html"><b><i>View Source</i></b></a>
031: *
032: * @author Brian Wing Shun Chan
033: *
034: */
035: public class ElementComparator implements Comparator {
036:
037: public int compare(Object obj1, Object obj2) {
038: Element el1 = (Element) obj1;
039: Element el2 = (Element) obj2;
040:
041: String el1Name = el1.getName();
042: String el2Name = el2.getName();
043:
044: if (!el1Name.equals(el2Name)) {
045: return el1Name.compareTo(el2Name);
046: }
047:
048: String el1Text = el1.getTextTrim();
049: String el2Text = el2.getTextTrim();
050:
051: if (!el1Text.equals(el2Text)) {
052: return el1Text.compareTo(el2Text);
053: }
054:
055: List el1Attrs = el1.attributes();
056: List el2Attrs = el2.attributes();
057:
058: if (el1Attrs.size() < el2Attrs.size()) {
059: return -1;
060: } else if (el1Attrs.size() > el2Attrs.size()) {
061: return 1;
062: }
063:
064: for (int i = 0; i < el1Attrs.size(); i++) {
065: Attribute attr = (Attribute) el1Attrs.get(i);
066:
067: int value = _compare(el2Attrs, attr,
068: new AttributeComparator());
069:
070: if (value != 0) {
071: return value;
072: }
073: }
074:
075: List el1Elements = el1.elements();
076: List el2Elements = el2.elements();
077:
078: if (el1Elements.size() < el2Elements.size()) {
079: return -1;
080: } else if (el1Elements.size() > el2Elements.size()) {
081: return 1;
082: }
083:
084: for (int i = 0; i < el1Elements.size(); i++) {
085: Element el = (Element) el1Elements.get(i);
086:
087: int value = _compare(el2Elements, el,
088: new ElementComparator());
089:
090: if (value != 0) {
091: return value;
092: }
093: }
094:
095: return 0;
096: }
097:
098: private int _compare(List list, Object obj, Comparator comparator) {
099: int firstValue = -1;
100:
101: for (int i = 0; i < list.size(); i++) {
102: Object o = list.get(i);
103:
104: int value = comparator.compare(obj, o);
105:
106: if (i == 0) {
107: firstValue = value;
108: }
109:
110: if (value == 0) {
111: return 0;
112: }
113: }
114:
115: return firstValue;
116: }
117:
118: }
|