1   /**
2    * Copyright (c) 2000-2007 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          if (attributeComparison != 0) {
84              return attributeComparison;
85          }
86  
87          int childrenComparison = _compareChildren(el1, el2);
88          if (childrenComparison != 0) {
89              return childrenComparison;
90          }
91  
92          return 0;
93      }
94  
95      private int _compareAttributes(Element el1, Element el2) {
96          List el1Attrs = el1.attributes();
97          List el2Attrs = el2.attributes();
98  
99          if (el1Attrs.size() < el2Attrs.size()) {
100             return -1;
101         }
102         else if (el1Attrs.size() > el2Attrs.size()) {
103             return 1;
104         }
105 
106         for (int i = 0; i < el1Attrs.size(); i++) {
107             Attribute attr = (Attribute)el1Attrs.get(i);
108 
109             int value = _contains(el2Attrs, attr, new AttributeComparator());
110             if (value != 0) {
111                 return value;
112             }
113         }
114         return -1;
115     }
116 
117     private int _compareChildren(Element el1, Element el2) {
118         List el1Children = el1.elements();
119         List el2Children = el2.elements();
120 
121         if (el1Children.size() < el2Children.size()) {
122             return -1;
123         }
124         else if (el1Children.size() > el2Children.size()) {
125             return 1;
126         }
127 
128         for (int i = 0; i < el1Children.size(); i++) {
129             Element el = (Element)el1Children.get(i);
130 
131             int value = _contains(el2Children, el, new ElementComparator());
132 
133             if (value != 0) {
134                 return value;
135             }
136         }
137         return -1;
138     }
139 
140     private int _contains(List list, Object obj, Comparator comparator) {
141         int firstValue = -1;
142 
143         for (int i = 0; i < list.size(); i++) {
144             Object o = list.get(i);
145 
146             int value = comparator.compare(obj, o);
147 
148             if (i == 0) {
149                 firstValue = value;
150             }
151 
152             if (value == 0) {
153                 return 0;
154             }
155         }
156 
157         return firstValue;
158     }
159 
160     private static final String[] _ROOT_ORDERED_CHILDREN = {
161     };
162 
163 }