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.AttributeComparator;
23  import com.liferay.util.xml.ElementComparator;
24  
25  import java.util.Comparator;
26  import java.util.List;
27  
28  import org.dom4j.Attribute;
29  import org.dom4j.Document;
30  import org.dom4j.Element;
31  
32  /**
33   * <a href="StrictXMLDescriptor.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Jorge Ferrer
36   *
37   */
38  public class StrictXMLDescriptor implements XMLDescriptor {
39  
40      public boolean areEqual(Element el1, Element el2) {
41          if (_compare(el1, el2) == 0) {
42              return true;
43          }
44          else {
45              return false;
46          }
47      }
48  
49      public boolean canHandleType(String doctype, Document root) {
50          return false;
51      }
52  
53      public boolean canJoinChildren(Element element) {
54          return false;
55      }
56  
57      public String[] getRootChildrenOrder() {
58          return _ROOT_ORDERED_CHILDREN;
59      }
60  
61      public String[] getChildrenOrder(Element parentElement) {
62          return new String[0];
63      }
64  
65      private int _compare(Object obj1, Object obj2) {
66          Element el1 = (Element)obj1;
67          Element el2 = (Element)obj2;
68  
69          String el1Name = el1.getName();
70          String el2Name = el2.getName();
71  
72          if (!el1Name.equals(el2Name)) {
73              return el1Name.compareTo(el2Name);
74          }
75  
76          String el1Text = el1.getTextTrim();
77          String el2Text = el2.getTextTrim();
78  
79          if (!el1Text.equals(el2Text)) {
80              return el1Text.compareTo(el2Text);
81          }
82  
83          int attributeComparison = _compareAttributes(el1, el2);
84  
85          if (attributeComparison != 0) {
86              return attributeComparison;
87          }
88  
89          int childrenComparison = _compareChildren(el1, el2);
90  
91          if (childrenComparison != 0) {
92              return childrenComparison;
93          }
94  
95          return 0;
96      }
97  
98      private int _compareAttributes(Element el1, Element el2) {
99          List<Attribute> el1Attrs = el1.attributes();
100         List<Attribute> el2Attrs = el2.attributes();
101 
102         if (el1Attrs.size() < el2Attrs.size()) {
103             return -1;
104         }
105         else if (el1Attrs.size() > el2Attrs.size()) {
106             return 1;
107         }
108 
109         for (Attribute attr : el1Attrs) {
110             int value = _contains(el2Attrs, attr, new AttributeComparator());
111 
112             if (value != 0) {
113                 return value;
114             }
115         }
116 
117         return -1;
118     }
119 
120     private int _compareChildren(Element el1, Element el2) {
121         List<Element> el1Children = el1.elements();
122         List<Element> el2Children = el2.elements();
123 
124         if (el1Children.size() < el2Children.size()) {
125             return -1;
126         }
127         else if (el1Children.size() > el2Children.size()) {
128             return 1;
129         }
130 
131         for (Element el : el1Children) {
132             int value = _contains(el2Children, el, new ElementComparator());
133 
134             if (value != 0) {
135                 return value;
136             }
137         }
138 
139         return -1;
140     }
141 
142     private int _contains(
143         List<Attribute> list, Attribute obj, Comparator<Attribute> comparator) {
144 
145         int firstValue = -1;
146 
147         for (int i = 0; i < list.size(); i++) {
148             Attribute o = list.get(i);
149 
150             int value = comparator.compare(obj, o);
151 
152             if (i == 0) {
153                 firstValue = value;
154             }
155 
156             if (value == 0) {
157                 return 0;
158             }
159         }
160 
161         return firstValue;
162     }
163 
164     private int _contains(
165         List<Element> list, Element obj, Comparator<Element> comparator) {
166 
167         int firstValue = -1;
168 
169         for (int i = 0; i < list.size(); i++) {
170             Element o = list.get(i);
171 
172             int value = comparator.compare(obj, o);
173 
174             if (i == 0) {
175                 firstValue = value;
176             }
177 
178             if (value == 0) {
179                 return 0;
180             }
181         }
182 
183         return firstValue;
184     }
185 
186     private static final String[] _ROOT_ORDERED_CHILDREN = {
187     };
188 
189 }