001
014
015 package com.liferay.util.xml.descriptor;
016
017 import com.liferay.util.xml.ElementComparator;
018 import com.liferay.util.xml.ElementIdentifier;
019
020 import org.dom4j.Document;
021 import org.dom4j.Element;
022
023
026 public abstract class SimpleXMLDescriptor implements XMLDescriptor {
027
028 public boolean areEqual(Element el1, Element el2) {
029 String name1 = el1.getName();
030 String name2 = el2.getName();
031
032 if ((name1 == null) || !name1.equals(name2)) {
033 return false;
034 }
035
036 if (_isIncluded(el1, getUniqueElements())) {
037 return true;
038 }
039
040 ElementIdentifier[] elIds = getElementsIdentifiedByAttribute();
041 for (int i = 0; i < elIds.length; i++) {
042 if (name1.equals(elIds[i].getElementName())) {
043 if (_compareAttribute(
044 el1, el2, elIds[i].getIdentifierName()) == 0) {
045
046 return true;
047 }
048 else {
049 return false;
050 }
051 }
052 }
053
054 elIds = getElementsIdentifiedByChild();
055 for (int i = 0; i < elIds.length; i++) {
056 if (name1.equals(elIds[i].getElementName())) {
057 if (_compareChildText(
058 el1, el2, elIds[i].getIdentifierName()) == 0) {
059 return true;
060 }
061 else {
062 return false;
063 }
064 }
065 }
066
067 ElementComparator comparator = new ElementComparator();
068
069 if (comparator.compare(el1, el2) == 0) {
070 return true;
071 }
072 else {
073 return false;
074 }
075 }
076
077 public abstract boolean canHandleType(String doctype, Document root);
078
079 public boolean canJoinChildren(Element element) {
080 return _isIncluded(element, getJoinableElements());
081 }
082
083 public String[] getRootChildrenOrder() {
084 return new String[0];
085 }
086
087 public String[] getChildrenOrder(Element parentElement) {
088 return new String[0];
089 }
090
091 public ElementIdentifier[] getElementsIdentifiedByAttribute() {
092 return new ElementIdentifier[0];
093 }
094
095 public ElementIdentifier[] getElementsIdentifiedByChild() {
096 return new ElementIdentifier[0];
097 }
098
099 public String[] getUniqueElements() {
100 return new String[0];
101 }
102
103 public String[] getJoinableElements() {
104 return new String[0];
105 }
106
107 private int _compareAttribute(Element el1, Element el2, String attrName) {
108 String name1 = el1.attributeValue(attrName);
109 String name2 = el2.attributeValue(attrName);
110
111 if ((name1 == null) || (name2 == null)) {
112 return -1;
113 }
114
115 return name1.compareTo(name2);
116 }
117
118 private int _compareChildText(Element el1, Element el2, String childName) {
119 Element child1 = _getChild(el1, childName);
120 Element child2 = _getChild(el2, childName);
121
122 if ((child1 == null) || (child2 == null)) {
123 return -1;
124 }
125
126 String name1 = child1.getText();
127 String name2 = child2.getText();
128
129 if ((name1 == null) || (name2 == null)) {
130 return -1;
131 }
132
133 return name1.compareTo(name2);
134 }
135
136 private Element _getChild(Element parent, String childName) {
137 Element child = parent.element(childName);
138
139
142
143 return child;
144 }
145
146 private boolean _isIncluded(Element element, String[] elemNames) {
147 for (int i = 0; i < elemNames.length; i++) {
148 if (element.getName().equals(elemNames[i])) {
149 return true;
150 }
151 }
152
153 return false;
154 }
155
156 }