1
22
23 package com.liferay.util.xml.descriptor;
24
25 import com.liferay.util.xml.AttributeComparator;
26 import com.liferay.util.xml.ElementComparator;
27
28 import java.util.Comparator;
29 import java.util.List;
30
31 import org.dom4j.Attribute;
32 import org.dom4j.Document;
33 import org.dom4j.Element;
34
35
41 public class StrictXMLDescriptor implements XMLDescriptor {
42
43 public boolean areEqual(Element el1, Element el2) {
44 if (_compare(el1, el2) == 0) {
45 return true;
46 }
47 else {
48 return false;
49 }
50 }
51
52 public boolean canHandleType(String doctype, Document root) {
53 return false;
54 }
55
56 public boolean canJoinChildren(Element element) {
57 return false;
58 }
59
60 public String[] getRootChildrenOrder() {
61 return _ROOT_ORDERED_CHILDREN;
62 }
63
64 public String[] getChildrenOrder(Element parentElement) {
65 return new String[0];
66 }
67
68 private int _compare(Object obj1, Object obj2) {
69 Element el1 = (Element)obj1;
70 Element el2 = (Element)obj2;
71
72 String el1Name = el1.getName();
73 String el2Name = el2.getName();
74
75 if (!el1Name.equals(el2Name)) {
76 return el1Name.compareTo(el2Name);
77 }
78
79 String el1Text = el1.getTextTrim();
80 String el2Text = el2.getTextTrim();
81
82 if (!el1Text.equals(el2Text)) {
83 return el1Text.compareTo(el2Text);
84 }
85
86 int attributeComparison = _compareAttributes(el1, el2);
87
88 if (attributeComparison != 0) {
89 return attributeComparison;
90 }
91
92 int childrenComparison = _compareChildren(el1, el2);
93
94 if (childrenComparison != 0) {
95 return childrenComparison;
96 }
97
98 return 0;
99 }
100
101 private int _compareAttributes(Element el1, Element el2) {
102 List<Attribute> el1Attrs = el1.attributes();
103 List<Attribute> el2Attrs = el2.attributes();
104
105 if (el1Attrs.size() < el2Attrs.size()) {
106 return -1;
107 }
108 else if (el1Attrs.size() > el2Attrs.size()) {
109 return 1;
110 }
111
112 for (Attribute attr : el1Attrs) {
113 int value = _contains(el2Attrs, attr, new AttributeComparator());
114
115 if (value != 0) {
116 return value;
117 }
118 }
119
120 return -1;
121 }
122
123 private int _compareChildren(Element el1, Element el2) {
124 List<Element> el1Children = el1.elements();
125 List<Element> el2Children = el2.elements();
126
127 if (el1Children.size() < el2Children.size()) {
128 return -1;
129 }
130 else if (el1Children.size() > el2Children.size()) {
131 return 1;
132 }
133
134 for (Element el : el1Children) {
135 int value = _contains(el2Children, el, new ElementComparator());
136
137 if (value != 0) {
138 return value;
139 }
140 }
141
142 return -1;
143 }
144
145 private int _contains(
146 List<Attribute> list, Attribute obj, Comparator<Attribute> comparator) {
147
148 int firstValue = -1;
149
150 for (int i = 0; i < list.size(); i++) {
151 Attribute o = list.get(i);
152
153 int value = comparator.compare(obj, o);
154
155 if (i == 0) {
156 firstValue = value;
157 }
158
159 if (value == 0) {
160 return 0;
161 }
162 }
163
164 return firstValue;
165 }
166
167 private int _contains(
168 List<Element> list, Element obj, Comparator<Element> comparator) {
169
170 int firstValue = -1;
171
172 for (int i = 0; i < list.size(); i++) {
173 Element o = list.get(i);
174
175 int value = comparator.compare(obj, o);
176
177 if (i == 0) {
178 firstValue = value;
179 }
180
181 if (value == 0) {
182 return 0;
183 }
184 }
185
186 return firstValue;
187 }
188
189 private static final String[] _ROOT_ORDERED_CHILDREN = {
190 };
191
192 }