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.descriptor;
022:
023: import com.liferay.util.xml.AttributeComparator;
024: import com.liferay.util.xml.ElementComparator;
025:
026: import java.util.Comparator;
027: import java.util.List;
028:
029: import org.dom4j.Attribute;
030: import org.dom4j.Document;
031: import org.dom4j.Element;
032:
033: /**
034: * <a href="StrictXMLDescriptor.java.html"><b><i>View Source</i></b></a>
035: *
036: * @author Jorge Ferrer
037: *
038: */
039: public class StrictXMLDescriptor implements XMLDescriptor {
040:
041: public boolean areEqual(Element el1, Element el2) {
042: if (_compare(el1, el2) == 0) {
043: return true;
044: } else {
045: return false;
046: }
047: }
048:
049: public boolean canHandleType(String doctype, Document root) {
050: return false;
051: }
052:
053: public boolean canJoinChildren(Element element) {
054: return false;
055: }
056:
057: public String[] getRootChildrenOrder() {
058: return _ROOT_ORDERED_CHILDREN;
059: }
060:
061: private int _compare(Object obj1, Object obj2) {
062: Element el1 = (Element) obj1;
063: Element el2 = (Element) obj2;
064:
065: String el1Name = el1.getName();
066: String el2Name = el2.getName();
067:
068: if (!el1Name.equals(el2Name)) {
069: return el1Name.compareTo(el2Name);
070: }
071:
072: String el1Text = el1.getTextTrim();
073: String el2Text = el2.getTextTrim();
074:
075: if (!el1Text.equals(el2Text)) {
076: return el1Text.compareTo(el2Text);
077: }
078:
079: int attributeComparison = _compareAttributes(el1, el2);
080: if (attributeComparison != 0) {
081: return attributeComparison;
082: }
083:
084: int childrenComparison = _compareChildren(el1, el2);
085: if (childrenComparison != 0) {
086: return childrenComparison;
087: }
088:
089: return 0;
090: }
091:
092: private int _compareAttributes(Element el1, Element el2) {
093: List el1Attrs = el1.attributes();
094: List el2Attrs = el2.attributes();
095:
096: if (el1Attrs.size() < el2Attrs.size()) {
097: return -1;
098: } else if (el1Attrs.size() > el2Attrs.size()) {
099: return 1;
100: }
101:
102: for (int i = 0; i < el1Attrs.size(); i++) {
103: Attribute attr = (Attribute) el1Attrs.get(i);
104:
105: int value = _contains(el2Attrs, attr,
106: new AttributeComparator());
107: if (value != 0) {
108: return value;
109: }
110: }
111: return -1;
112: }
113:
114: private int _compareChildren(Element el1, Element el2) {
115: List el1Children = el1.elements();
116: List el2Children = el2.elements();
117:
118: if (el1Children.size() < el2Children.size()) {
119: return -1;
120: } else if (el1Children.size() > el2Children.size()) {
121: return 1;
122: }
123:
124: for (int i = 0; i < el1Children.size(); i++) {
125: Element el = (Element) el1Children.get(i);
126:
127: int value = _contains(el2Children, el,
128: new ElementComparator());
129:
130: if (value != 0) {
131: return value;
132: }
133: }
134: return -1;
135: }
136:
137: private int _contains(List list, Object obj, Comparator comparator) {
138: int firstValue = -1;
139:
140: for (int i = 0; i < list.size(); i++) {
141: Object o = list.get(i);
142:
143: int value = comparator.compare(obj, o);
144:
145: if (i == 0) {
146: firstValue = value;
147: }
148:
149: if (value == 0) {
150: return 0;
151: }
152: }
153:
154: return firstValue;
155: }
156:
157: private static final String[] _ROOT_ORDERED_CHILDREN = {};
158:
159: }
|