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      private int _compare(Object obj1, Object obj2) {
65          Element el1 = (Element)obj1;
66          Element el2 = (Element)obj2;
67  
68          String el1Name = el1.getName();
69          String el2Name = el2.getName();
70  
71          if (!el1Name.equals(el2Name)) {
72              return el1Name.compareTo(el2Name);
73          }
74  
75          String el1Text = el1.getTextTrim();
76          String el2Text = el2.getTextTrim();
77  
78          if (!el1Text.equals(el2Text)) {
79              return el1Text.compareTo(el2Text);
80          }
81  
82          int attributeComparison = _compareAttributes(el1, el2);
83  
84          if (attributeComparison != 0) {
85              return attributeComparison;
86          }
87  
88          int childrenComparison = _compareChildren(el1, el2);
89  
90          if (childrenComparison != 0) {
91              return childrenComparison;
92          }
93  
94          return 0;
95      }
96  
97      private int _compareAttributes(Element el1, Element el2) {
98          List<Attribute> el1Attrs = el1.attributes();
99          List<Attribute> el2Attrs = el2.attributes();
100 
101         if (el1Attrs.size() < el2Attrs.size()) {
102             return -1;
103         }
104         else if (el1Attrs.size() > el2Attrs.size()) {
105             return 1;
106         }
107 
108         for (Attribute attr : el1Attrs) {
109             int value = _contains(el2Attrs, attr, new AttributeComparator());
110 
111             if (value != 0) {
112                 return value;
113             }
114         }
115 
116         return -1;
117     }
118 
119     private int _compareChildren(Element el1, Element el2) {
120         List<Element> el1Children = el1.elements();
121         List<Element> el2Children = el2.elements();
122 
123         if (el1Children.size() < el2Children.size()) {
124             return -1;
125         }
126         else if (el1Children.size() > el2Children.size()) {
127             return 1;
128         }
129 
130         for (Element el : el1Children) {
131             int value = _contains(el2Children, el, new ElementComparator());
132 
133             if (value != 0) {
134                 return value;
135             }
136         }
137 
138         return -1;
139     }
140 
141     private int _contains(List<?> list, Object obj, Comparator comparator) {
142         int firstValue = -1;
143 
144         for (int i = 0; i < list.size(); i++) {
145             Object o = list.get(i);
146 
147             int value = comparator.compare(obj, o);
148 
149             if (i == 0) {
150                 firstValue = value;
151             }
152 
153             if (value == 0) {
154                 return 0;
155             }
156         }
157 
158         return firstValue;
159     }
160 
161     private static final String[] _ROOT_ORDERED_CHILDREN = {
162     };
163 
164 }