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