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 private int _compare(Object obj1, Object obj2) {
65 Element el1 = (Element)obj1;
66 Element el2 = (Element)obj2;
67
68 String el1Name = el1.getName();
69 String el2Name = el2.getName();
70
71 if (!el1Name.equals(el2Name)) {
72 return el1Name.compareTo(el2Name);
73 }
74
75 String el1Text = el1.getTextTrim();
76 String el2Text = el2.getTextTrim();
77
78 if (!el1Text.equals(el2Text)) {
79 return el1Text.compareTo(el2Text);
80 }
81
82 int attributeComparison = _compareAttributes(el1, el2);
83
84 if (attributeComparison != 0) {
85 return attributeComparison;
86 }
87
88 int childrenComparison = _compareChildren(el1, el2);
89
90 if (childrenComparison != 0) {
91 return childrenComparison;
92 }
93
94 return 0;
95 }
96
97 private int _compareAttributes(Element el1, Element el2) {
98 List<Attribute> el1Attrs = el1.attributes();
99 List<Attribute> el2Attrs = el2.attributes();
100
101 if (el1Attrs.size() < el2Attrs.size()) {
102 return -1;
103 }
104 else if (el1Attrs.size() > el2Attrs.size()) {
105 return 1;
106 }
107
108 for (Attribute attr : el1Attrs) {
109 int value = _contains(el2Attrs, attr, new AttributeComparator());
110
111 if (value != 0) {
112 return value;
113 }
114 }
115
116 return -1;
117 }
118
119 private int _compareChildren(Element el1, Element el2) {
120 List<Element> el1Children = el1.elements();
121 List<Element> el2Children = el2.elements();
122
123 if (el1Children.size() < el2Children.size()) {
124 return -1;
125 }
126 else if (el1Children.size() > el2Children.size()) {
127 return 1;
128 }
129
130 for (Element el : el1Children) {
131 int value = _contains(el2Children, el, new ElementComparator());
132
133 if (value != 0) {
134 return value;
135 }
136 }
137
138 return -1;
139 }
140
141 private int _contains(List<?> list, Object obj, Comparator comparator) {
142 int firstValue = -1;
143
144 for (int i = 0; i < list.size(); i++) {
145 Object 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 static final String[] _ROOT_ORDERED_CHILDREN = {
162 };
163
164 }