1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.util.xml.descriptor;
16  
17  import com.liferay.util.xml.ElementComparator;
18  import com.liferay.util.xml.ElementIdentifier;
19  
20  import org.dom4j.Document;
21  import org.dom4j.Element;
22  
23  /**
24   * <a href="SimpleXMLDescriptor.java.html"><b><i>View Source</i></b></a>
25   *
26   * @author Jorge Ferrer
27   */
28  public abstract class SimpleXMLDescriptor implements XMLDescriptor {
29  
30      public boolean areEqual(Element el1, Element el2) {
31          String name1 = el1.getName();
32          String name2 = el2.getName();
33  
34          if ((name1 == null) || !name1.equals(name2)) {
35              return false;
36          }
37  
38          if (_isIncluded(el1, getUniqueElements())) {
39              return true;
40          }
41  
42          ElementIdentifier[] elIds = getElementsIdentifiedByAttribute();
43          for (int i = 0; i < elIds.length; i++) {
44              if (name1.equals(elIds[i].getElementName())) {
45                  if (_compareAttribute(
46                          el1, el2, elIds[i].getIdentifierName()) == 0) {
47  
48                      return true;
49                  }
50                  else {
51                      return false;
52                  }
53              }
54          }
55  
56          elIds = getElementsIdentifiedByChild();
57          for (int i = 0; i < elIds.length; i++) {
58              if (name1.equals(elIds[i].getElementName())) {
59                  if (_compareChildText(
60                          el1, el2, elIds[i].getIdentifierName()) == 0) {
61                      return true;
62                  }
63                  else {
64                      return false;
65                  }
66              }
67          }
68  
69          ElementComparator comparator = new ElementComparator();
70  
71          if (comparator.compare(el1, el2) == 0) {
72              return true;
73          }
74          else {
75              return false;
76          }
77      }
78  
79      public abstract boolean canHandleType(String doctype, Document root);
80  
81      public boolean canJoinChildren(Element element) {
82          return _isIncluded(element, getJoinableElements());
83      }
84  
85      public String[] getRootChildrenOrder() {
86          return new String[0];
87      }
88  
89      public String[] getChildrenOrder(Element parentElement) {
90          return new String[0];
91      }
92  
93      public ElementIdentifier[] getElementsIdentifiedByAttribute() {
94          return new ElementIdentifier[0];
95      }
96  
97      public ElementIdentifier[] getElementsIdentifiedByChild() {
98          return new ElementIdentifier[0];
99      }
100 
101     public String[] getUniqueElements() {
102         return new String[0];
103     }
104 
105     public String[] getJoinableElements() {
106         return new String[0];
107     }
108 
109     private int _compareAttribute(Element el1, Element el2, String attrName) {
110         String name1 = el1.attributeValue(attrName);
111         String name2 = el2.attributeValue(attrName);
112 
113         if ((name1 == null) || (name2 == null)) {
114             return -1;
115         }
116 
117         return name1.compareTo(name2);
118     }
119 
120     private int _compareChildText(Element el1, Element el2, String childName) {
121         Element child1 = _getChild(el1, childName);
122         Element child2 = _getChild(el2, childName);
123 
124         if ((child1 == null) || (child2 == null)) {
125             return -1;
126         }
127 
128         String name1 = child1.getText();
129         String name2 = child2.getText();
130 
131         if ((name1 == null) || (name2 == null)) {
132             return -1;
133         }
134 
135         return name1.compareTo(name2);
136     }
137 
138     private Element _getChild(Element parent, String childName) {
139         Element child = parent.element(childName);
140 
141         /*if (child == null) {
142             child = parent.element(childName, parent.getNamespace());
143         }*/
144 
145         return child;
146     }
147 
148     private boolean _isIncluded(Element element, String[] elemNames) {
149         for (int i = 0; i < elemNames.length; i++) {
150             if (element.getName().equals(elemNames[i])) {
151                 return true;
152             }
153         }
154 
155         return false;
156     }
157 
158 }