1
22
23 package com.liferay.util.xml.descriptor;
24
25 import com.liferay.util.xml.ElementComparator;
26 import com.liferay.util.xml.ElementIdentifier;
27
28 import org.dom4j.Document;
29 import org.dom4j.Element;
30
31
36 public abstract class SimpleXMLDescriptor implements XMLDescriptor {
37
38 public boolean areEqual(Element el1, Element el2) {
39 String name1 = el1.getName();
40 String name2 = el2.getName();
41
42 if ((name1 == null) || !name1.equals(name2)) {
43 return false;
44 }
45
46 if (_isIncluded(el1, getUniqueElements())) {
47 return true;
48 }
49
50 ElementIdentifier[] elIds = getElementsIdentifiedByAttribute();
51 for (int i = 0; i < elIds.length; i++) {
52 if (name1.equals(elIds[i].getElementName())) {
53 if (_compareAttribute(
54 el1, el2, elIds[i].getIdentifierName()) == 0) {
55
56 return true;
57 }
58 else {
59 return false;
60 }
61 }
62 }
63
64 elIds = getElementsIdentifiedByChild();
65 for (int i = 0; i < elIds.length; i++) {
66 if (name1.equals(elIds[i].getElementName())) {
67 if (_compareChildText(
68 el1, el2, elIds[i].getIdentifierName()) == 0) {
69 return true;
70 }
71 else {
72 return false;
73 }
74 }
75 }
76
77 ElementComparator comparator = new ElementComparator();
78
79 if (comparator.compare(el1, el2) == 0) {
80 return true;
81 }
82 else {
83 return false;
84 }
85 }
86
87 public abstract boolean canHandleType(String doctype, Document root);
88
89 public boolean canJoinChildren(Element element) {
90 return _isIncluded(element, getJoinableElements());
91 }
92
93 public String[] getRootChildrenOrder() {
94 return new String[0];
95 }
96
97 public String[] getChildrenOrder(Element parentElement) {
98 return new String[0];
99 }
100
101 public ElementIdentifier[] getElementsIdentifiedByAttribute() {
102 return new ElementIdentifier[0];
103 }
104
105 public ElementIdentifier[] getElementsIdentifiedByChild() {
106 return new ElementIdentifier[0];
107 }
108
109 public String[] getUniqueElements() {
110 return new String[0];
111 }
112
113 public String[] getJoinableElements() {
114 return new String[0];
115 }
116
117 private int _compareAttribute(Element el1, Element el2, String attrName) {
118 String name1 = el1.attributeValue(attrName);
119 String name2 = el2.attributeValue(attrName);
120
121 if ((name1 == null) || (name2 == null)) {
122 return -1;
123 }
124
125 return name1.compareTo(name2);
126 }
127
128 private int _compareChildText(Element el1, Element el2, String childName) {
129 Element child1 = _getChild(el1, childName);
130 Element child2 = _getChild(el2, childName);
131
132 if ((child1 == null) || (child2 == null)) {
133 return -1;
134 }
135
136 String name1 = child1.getText();
137 String name2 = child2.getText();
138
139 if ((name1 == null) || (name2 == null)) {
140 return -1;
141 }
142
143 return name1.compareTo(name2);
144 }
145
146 private Element _getChild(Element parent, String childName) {
147 Element child = parent.element(childName);
148
149
152
153 return child;
154 }
155
156 private boolean _isIncluded(Element element, String[] elemNames) {
157 for (int i = 0; i < elemNames.length; i++) {
158 if (element.getName().equals(elemNames[i])) {
159 return true;
160 }
161 }
162
163 return false;
164 }
165
166 }