1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.util.xml.descriptor;
21  
22  import com.liferay.util.xml.ElementComparator;
23  import com.liferay.util.xml.ElementIdentifier;
24  
25  import org.dom4j.Document;
26  import org.dom4j.Element;
27  
28  /**
29   * <a href="SimpleXMLDescriptor.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Jorge Ferrer
32   *
33   */
34  public abstract class SimpleXMLDescriptor implements XMLDescriptor {
35  
36      public boolean areEqual(Element el1, Element el2) {
37          String name1 = el1.getName();
38          String name2 = el2.getName();
39  
40          if ((name1 == null) || !name1.equals(name2)) {
41              return false;
42          }
43  
44          if (_isIncluded(el1, getUniqueElements())) {
45              return true;
46          }
47  
48          ElementIdentifier[] elIds = getElementsIdentifiedByAttribute();
49          for (int i = 0; i < elIds.length; i++) {
50              if (name1.equals(elIds[i].getElementName())) {
51                  if (_compareAttribute(
52                          el1, el2, elIds[i].getIdentifierName()) == 0) {
53  
54                      return true;
55                  }
56                  else {
57                      return false;
58                  }
59              }
60          }
61  
62          elIds = getElementsIdentifiedByChild();
63          for (int i = 0; i < elIds.length; i++) {
64              if (name1.equals(elIds[i].getElementName())) {
65                  if (_compareChildText(
66                          el1, el2, elIds[i].getIdentifierName()) == 0) {
67                      return true;
68                  }
69                  else {
70                      return false;
71                  }
72              }
73          }
74  
75          ElementComparator comparator = new ElementComparator();
76  
77          if (comparator.compare(el1, el2) == 0) {
78              return true;
79          }
80          else {
81              return false;
82          }
83      }
84  
85      public abstract boolean canHandleType(String doctype, Document root);
86  
87      public boolean canJoinChildren(Element element) {
88          return _isIncluded(element, getJoinableElements());
89      }
90  
91      public String[] getRootChildrenOrder() {
92          return new String[0];
93      }
94  
95      public String[] getChildrenOrder(Element parentElement) {
96          return new String[0];
97      }
98  
99      public ElementIdentifier[] getElementsIdentifiedByAttribute() {
100         return new ElementIdentifier[0];
101     }
102 
103     public ElementIdentifier[] getElementsIdentifiedByChild() {
104         return new ElementIdentifier[0];
105     }
106 
107     public String[] getUniqueElements() {
108         return new String[0];
109     }
110 
111     public String[] getJoinableElements() {
112         return new String[0];
113     }
114 
115     private int _compareAttribute(Element el1, Element el2, String attrName) {
116         String name1 = el1.attributeValue(attrName);
117         String name2 = el2.attributeValue(attrName);
118 
119         if ((name1 == null) || (name2 == null)) {
120             return -1;
121         }
122 
123         return name1.compareTo(name2);
124     }
125 
126     private int _compareChildText(Element el1, Element el2, String childName) {
127         Element child1 = _getChild(el1, childName);
128         Element child2 = _getChild(el2, childName);
129 
130         if ((child1 == null) || (child2 == null)) {
131             return -1;
132         }
133 
134         String name1 = child1.getText();
135         String name2 = child2.getText();
136 
137         if ((name1 == null) || (name2 == null)) {
138             return -1;
139         }
140 
141         return name1.compareTo(name2);
142     }
143 
144     private Element _getChild(Element parent, String childName) {
145         Element child = parent.element(childName);
146 
147         /*if (child == null) {
148             child = parent.element(childName, parent.getNamespace());
149         }*/
150 
151         return child;
152     }
153 
154     private boolean _isIncluded(Element element, String[] elemNames) {
155         for (int i = 0; i < elemNames.length; i++) {
156             if (element.getName().equals(elemNames[i])) {
157                 return true;
158             }
159         }
160 
161         return false;
162     }
163 
164 }