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