1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
32   * <a href="SimpleXMLDescriptor.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Jorge Ferrer
35   */
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         /*if (child == null) {
150             child = parent.element(childName, parent.getNamespace());
151         }*/
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 }