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            @Override
030            public boolean areEqual(Element el1, Element el2) {
031                    String name1 = el1.getName();
032                    String name2 = el2.getName();
033    
034                    if ((name1 == null) || !name1.equals(name2)) {
035                            return false;
036                    }
037    
038                    if (ArrayUtil.contains(getUniqueElements(), el1.getName())) {
039                            return true;
040                    }
041    
042                    ElementIdentifier[] elIds = getElementsIdentifiedByAttribute();
043    
044                    for (int i = 0; i < elIds.length; i++) {
045                            if (name1.equals(elIds[i].getElementName())) {
046                                    if (_compareAttribute(
047                                                    el1, el2, elIds[i].getIdentifierName()) == 0) {
048    
049                                            return true;
050                                    }
051                                    else {
052                                            return false;
053                                    }
054                            }
055                    }
056    
057                    elIds = getElementsIdentifiedByChild();
058    
059                    for (int i = 0; i < elIds.length; i++) {
060                            if (name1.equals(elIds[i].getElementName())) {
061                                    if (_compareChildText(
062                                                    el1, el2, elIds[i].getIdentifierName()) == 0) {
063    
064                                            return true;
065                                    }
066                                    else {
067                                            return false;
068                                    }
069                            }
070                    }
071    
072                    ElementComparator comparator = new ElementComparator();
073    
074                    if (comparator.compare(el1, el2) == 0) {
075                            return true;
076                    }
077                    else {
078                            return false;
079                    }
080            }
081    
082            @Override
083            public abstract boolean canHandleType(String doctype, Document root);
084    
085            @Override
086            public boolean canJoinChildren(Element element) {
087                    return ArrayUtil.contains(getJoinableElements(), element.getName());
088            }
089    
090            @Override
091            public String[] getChildrenOrder(Element parentElement) {
092                    return new String[0];
093            }
094    
095            public ElementIdentifier[] getElementsIdentifiedByAttribute() {
096                    return new ElementIdentifier[0];
097            }
098    
099            public ElementIdentifier[] getElementsIdentifiedByChild() {
100                    return new ElementIdentifier[0];
101            }
102    
103            public String[] getJoinableElements() {
104                    return new String[0];
105            }
106    
107            @Override
108            public String[] getRootChildrenOrder() {
109                    return new String[0];
110            }
111    
112            public String[] getUniqueElements() {
113                    return new String[0];
114            }
115    
116            private int _compareAttribute(Element el1, Element el2, String attrName) {
117                    String name1 = el1.attributeValue(attrName);
118                    String name2 = el2.attributeValue(attrName);
119    
120                    if ((name1 == null) || (name2 == null)) {
121                            return -1;
122                    }
123    
124                    return name1.compareTo(name2);
125            }
126    
127            private int _compareChildText(Element el1, Element el2, String childName) {
128                    Element child1 = _getChild(el1, childName);
129                    Element child2 = _getChild(el2, childName);
130    
131                    if ((child1 == null) || (child2 == null)) {
132                            return -1;
133                    }
134    
135                    String name1 = child1.getText();
136                    String name2 = child2.getText();
137    
138                    if ((name1 == null) || (name2 == null)) {
139                            return -1;
140                    }
141    
142                    return name1.compareTo(name2);
143            }
144    
145            private Element _getChild(Element parent, String childName) {
146                    Element child = parent.element(childName);
147    
148                    /*if (child == null) {
149                            child = parent.element(childName, parent.getNamespace());
150                    }*/
151    
152                    return child;
153            }
154    
155    }