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