1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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   */
37  public abstract class SimpleXMLDescriptor implements XMLDescriptor {
38  
39      public boolean areEqual(Element el1, Element el2) {
40          String name1 = el1.getName();
41          String name2 = el2.getName();
42  
43          if ((name1 == null) || !name1.equals(name2)) {
44              return false;
45          }
46  
47          if (_isIncluded(el1, getUniqueElements())) {
48              return true;
49          }
50  
51          ElementIdentifier[] elIds = getElementsIdentifiedByAttribute();
52          for (int i = 0; i < elIds.length; i++) {
53              if (name1.equals(elIds[i].getElementName())) {
54                  if (_compareAttribute(
55                          el1, el2, elIds[i].getIdentifierName()) == 0) {
56  
57                      return true;
58                  }
59                  else {
60                      return false;
61                  }
62              }
63          }
64  
65          elIds = getElementsIdentifiedByChild();
66          for (int i = 0; i < elIds.length; i++) {
67              if (name1.equals(elIds[i].getElementName())) {
68                  if (_compareChildText(
69                          el1, el2, elIds[i].getIdentifierName()) == 0) {
70                      return true;
71                  }
72                  else {
73                      return false;
74                  }
75              }
76          }
77  
78          ElementComparator comparator = new ElementComparator();
79  
80          if (comparator.compare(el1, el2) == 0) {
81              return true;
82          }
83          else {
84              return false;
85          }
86      }
87  
88      public abstract boolean canHandleType(String doctype, Document root);
89  
90      public boolean canJoinChildren(Element element) {
91          return _isIncluded(element, getJoinableElements());
92      }
93  
94      public String[] getRootChildrenOrder() {
95          return new String[0];
96      }
97  
98      public String[] getChildrenOrder(Element parentElement) {
99          return new String[0];
100     }
101 
102     public ElementIdentifier[] getElementsIdentifiedByAttribute() {
103         return new ElementIdentifier[0];
104     }
105 
106     public ElementIdentifier[] getElementsIdentifiedByChild() {
107         return new ElementIdentifier[0];
108     }
109 
110     public String[] getUniqueElements() {
111         return new String[0];
112     }
113 
114     public String[] getJoinableElements() {
115         return new String[0];
116     }
117 
118     private int _compareAttribute(Element el1, Element el2, String attrName) {
119         String name1 = el1.attributeValue(attrName);
120         String name2 = el2.attributeValue(attrName);
121 
122         if ((name1 == null) || (name2 == null)) {
123             return -1;
124         }
125 
126         return name1.compareTo(name2);
127     }
128 
129     private int _compareChildText(Element el1, Element el2, String childName) {
130         Element child1 = _getChild(el1, childName);
131         Element child2 = _getChild(el2, childName);
132 
133         if ((child1 == null) || (child2 == null)) {
134             return -1;
135         }
136 
137         String name1 = child1.getText();
138         String name2 = child2.getText();
139 
140         if ((name1 == null) || (name2 == null)) {
141             return -1;
142         }
143 
144         return name1.compareTo(name2);
145     }
146 
147     private Element _getChild(Element parent, String childName) {
148         Element child = parent.element(childName);
149 
150         /*if (child == null) {
151             child = parent.element(childName, parent.getNamespace());
152         }*/
153 
154         return child;
155     }
156 
157     private boolean _isIncluded(Element element, String[] elemNames) {
158         for (int i = 0; i < elemNames.length; i++) {
159             if (element.getName().equals(elemNames[i])) {
160                 return true;
161             }
162         }
163 
164         return false;
165     }
166 
167 }