1   /**
2    * Copyright (c) 2000-2007 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 ElementIdentifier[] getElementsIdentifiedByAttribute() {
99          return new ElementIdentifier[0];
100     }
101 
102     public ElementIdentifier[] getElementsIdentifiedByChild() {
103         return new ElementIdentifier[0];
104     }
105 
106     public String[] getUniqueElements() {
107         return new String[0];
108     }
109 
110     public String[] getJoinableElements() {
111         return new String[0];
112     }
113 
114     private int _compareAttribute(Element el1, Element el2, String attrName) {
115         String name1 = el1.attributeValue(attrName);
116         String name2 = el2.attributeValue(attrName);
117 
118         if ((name1 == null) || (name2 == null)) {
119             return -1;
120         }
121 
122         return name1.compareTo(name2);
123     }
124 
125     private int _compareChildText(Element el1, Element el2, String childName) {
126         Element child1 = _getChild(el1, childName);
127         Element child2 = _getChild(el2, childName);
128 
129         if ((child1 == null) || (child2 == null)) {
130             return -1;
131         }
132 
133         String name1 = child1.getText();
134         String name2 = child2.getText();
135 
136         if ((name1 == null) || (name2 == null)) {
137             return -1;
138         }
139 
140         return name1.compareTo(name2);
141     }
142 
143     private Element _getChild(Element parent, String childName) {
144         Element child = parent.element(childName);
145 
146         /*if (child == null) {
147             child = parent.element(childName, parent.getNamespace());
148         }*/
149 
150         return child;
151     }
152 
153     private boolean _isIncluded(Element element, String[] elemNames) {
154         for (int i = 0; i < elemNames.length; i++) {
155             if (element.getName().equals(elemNames[i])) {
156                 return true;
157             }
158         }
159 
160         return false;
161     }
162 
163 }