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