001
014
015 package com.liferay.util.xml.descriptor;
016
017 import com.liferay.util.xml.AttributeComparator;
018 import com.liferay.util.xml.ElementComparator;
019
020 import java.util.Comparator;
021 import java.util.List;
022
023 import org.dom4j.Attribute;
024 import org.dom4j.Document;
025 import org.dom4j.Element;
026
027
030 public class StrictXMLDescriptor implements XMLDescriptor {
031
032 @Override
033 public boolean areEqual(Element el1, Element el2) {
034 if (_compare(el1, el2) == 0) {
035 return true;
036 }
037 else {
038 return false;
039 }
040 }
041
042 @Override
043 public boolean canHandleType(String doctype, Document root) {
044 return false;
045 }
046
047 @Override
048 public boolean canJoinChildren(Element element) {
049 return false;
050 }
051
052 @Override
053 public String[] getChildrenOrder(Element parentElement) {
054 return new String[0];
055 }
056
057 @Override
058 public String[] getRootChildrenOrder() {
059 return _ROOT_ORDERED_CHILDREN;
060 }
061
062 private int _compare(Object obj1, Object obj2) {
063 Element el1 = (Element)obj1;
064 Element el2 = (Element)obj2;
065
066 String el1Name = el1.getName();
067 String el2Name = el2.getName();
068
069 if (!el1Name.equals(el2Name)) {
070 return el1Name.compareTo(el2Name);
071 }
072
073 String el1Text = el1.getTextTrim();
074 String el2Text = el2.getTextTrim();
075
076 if (!el1Text.equals(el2Text)) {
077 return el1Text.compareTo(el2Text);
078 }
079
080 int attributeComparison = _compareAttributes(el1, el2);
081
082 if (attributeComparison != 0) {
083 return attributeComparison;
084 }
085
086 int childrenComparison = _compareChildren(el1, el2);
087
088 if (childrenComparison != 0) {
089 return childrenComparison;
090 }
091
092 return 0;
093 }
094
095 private int _compareAttributes(Element el1, Element el2) {
096 List<Attribute> el1Attrs = el1.attributes();
097 List<Attribute> el2Attrs = el2.attributes();
098
099 if (el1Attrs.size() < el2Attrs.size()) {
100 return -1;
101 }
102 else if (el1Attrs.size() > el2Attrs.size()) {
103 return 1;
104 }
105
106 for (Attribute attr : el1Attrs) {
107 int value = _contains(el2Attrs, attr, new AttributeComparator());
108
109 if (value != 0) {
110 return value;
111 }
112 }
113
114 return -1;
115 }
116
117 private int _compareChildren(Element el1, Element el2) {
118 List<Element> el1Children = el1.elements();
119 List<Element> el2Children = el2.elements();
120
121 if (el1Children.size() < el2Children.size()) {
122 return -1;
123 }
124 else if (el1Children.size() > el2Children.size()) {
125 return 1;
126 }
127
128 for (Element el : el1Children) {
129 int value = _contains(el2Children, el, new ElementComparator());
130
131 if (value != 0) {
132 return value;
133 }
134 }
135
136 return -1;
137 }
138
139 private int _contains(
140 List<Attribute> list, Attribute obj, Comparator<Attribute> comparator) {
141
142 int firstValue = -1;
143
144 for (int i = 0; i < list.size(); i++) {
145 Attribute o = list.get(i);
146
147 int value = comparator.compare(obj, o);
148
149 if (i == 0) {
150 firstValue = value;
151 }
152
153 if (value == 0) {
154 return 0;
155 }
156 }
157
158 return firstValue;
159 }
160
161 private int _contains(
162 List<Element> list, Element obj, Comparator<Element> comparator) {
163
164 int firstValue = -1;
165
166 for (int i = 0; i < list.size(); i++) {
167 Element o = list.get(i);
168
169 int value = comparator.compare(obj, o);
170
171 if (i == 0) {
172 firstValue = value;
173 }
174
175 if (value == 0) {
176 return 0;
177 }
178 }
179
180 return firstValue;
181 }
182
183 private static final String[] _ROOT_ORDERED_CHILDREN = {
184 };
185
186 }