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