001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util.xml.descriptor;
016    
017    import com.liferay.portal.kernel.util.ArrayUtil;
018    import com.liferay.util.xml.ElementComparator;
019    import com.liferay.util.xml.ElementIdentifier;
020    
021    import org.dom4j.Document;
022    import org.dom4j.Element;
023    
024    /**
025     * @author Jorge Ferrer
026     */
027    public abstract class SimpleXMLDescriptor implements XMLDescriptor {
028    
029            public boolean areEqual(Element el1, Element el2) {
030                    String name1 = el1.getName();
031                    String name2 = el2.getName();
032    
033                    if ((name1 == null) || !name1.equals(name2)) {
034                            return false;
035                    }
036    
037                    if (ArrayUtil.contains(getUniqueElements(), el1.getName())) {
038                            return true;
039                    }
040    
041                    ElementIdentifier[] elIds = getElementsIdentifiedByAttribute();
042    
043                    for (int i = 0; i < elIds.length; i++) {
044                            if (name1.equals(elIds[i].getElementName())) {
045                                    if (_compareAttribute(
046                                                    el1, el2, elIds[i].getIdentifierName()) == 0) {
047    
048                                            return true;
049                                    }
050                                    else {
051                                            return false;
052                                    }
053                            }
054                    }
055    
056                    elIds = getElementsIdentifiedByChild();
057    
058                    for (int i = 0; i < elIds.length; i++) {
059                            if (name1.equals(elIds[i].getElementName())) {
060                                    if (_compareChildText(
061                                                    el1, el2, elIds[i].getIdentifierName()) == 0) {
062    
063                                            return true;
064                                    }
065                                    else {
066                                            return false;
067                                    }
068                            }
069                    }
070    
071                    ElementComparator comparator = new ElementComparator();
072    
073                    if (comparator.compare(el1, el2) == 0) {
074                            return true;
075                    }
076                    else {
077                            return false;
078                    }
079            }
080    
081            public abstract boolean canHandleType(String doctype, Document root);
082    
083            public boolean canJoinChildren(Element element) {
084                    return ArrayUtil.contains(getJoinableElements(), element.getName());
085            }
086    
087            public String[] getChildrenOrder(Element parentElement) {
088                    return new String[0];
089            }
090    
091            public ElementIdentifier[] getElementsIdentifiedByAttribute() {
092                    return new ElementIdentifier[0];
093            }
094    
095            public ElementIdentifier[] getElementsIdentifiedByChild() {
096                    return new ElementIdentifier[0];
097            }
098    
099            public String[] getJoinableElements() {
100                    return new String[0];
101            }
102    
103            public String[] getRootChildrenOrder() {
104                    return new String[0];
105            }
106    
107            public String[] getUniqueElements() {
108                    return new String[0];
109            }
110    
111            private int _compareAttribute(Element el1, Element el2, String attrName) {
112                    String name1 = el1.attributeValue(attrName);
113                    String name2 = el2.attributeValue(attrName);
114    
115                    if ((name1 == null) || (name2 == null)) {
116                            return -1;
117                    }
118    
119                    return name1.compareTo(name2);
120            }
121    
122            private int _compareChildText(Element el1, Element el2, String childName) {
123                    Element child1 = _getChild(el1, childName);
124                    Element child2 = _getChild(el2, childName);
125    
126                    if ((child1 == null) || (child2 == null)) {
127                            return -1;
128                    }
129    
130                    String name1 = child1.getText();
131                    String name2 = child2.getText();
132    
133                    if ((name1 == null) || (name2 == null)) {
134                            return -1;
135                    }
136    
137                    return name1.compareTo(name2);
138            }
139    
140            private Element _getChild(Element parent, String childName) {
141                    Element child = parent.element(childName);
142    
143                    /*if (child == null) {
144                            child = parent.element(childName, parent.getNamespace());
145                    }*/
146    
147                    return child;
148            }
149    
150    }