001    /**
002     * Copyright (c) 2000-2010 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.util.xml.ElementComparator;
018    import com.liferay.util.xml.ElementIdentifier;
019    
020    import org.dom4j.Document;
021    import org.dom4j.Element;
022    
023    /**
024     * @author Jorge Ferrer
025     */
026    public abstract class SimpleXMLDescriptor implements XMLDescriptor {
027    
028            public boolean areEqual(Element el1, Element el2) {
029                    String name1 = el1.getName();
030                    String name2 = el2.getName();
031    
032                    if ((name1 == null) || !name1.equals(name2)) {
033                            return false;
034                    }
035    
036                    if (_isIncluded(el1, getUniqueElements())) {
037                            return true;
038                    }
039    
040                    ElementIdentifier[] elIds = getElementsIdentifiedByAttribute();
041                    for (int i = 0; i < elIds.length; i++) {
042                            if (name1.equals(elIds[i].getElementName())) {
043                                    if (_compareAttribute(
044                                                    el1, el2, elIds[i].getIdentifierName()) == 0) {
045    
046                                            return true;
047                                    }
048                                    else {
049                                            return false;
050                                    }
051                            }
052                    }
053    
054                    elIds = getElementsIdentifiedByChild();
055                    for (int i = 0; i < elIds.length; i++) {
056                            if (name1.equals(elIds[i].getElementName())) {
057                                    if (_compareChildText(
058                                                    el1, el2, elIds[i].getIdentifierName()) == 0) {
059                                            return true;
060                                    }
061                                    else {
062                                            return false;
063                                    }
064                            }
065                    }
066    
067                    ElementComparator comparator = new ElementComparator();
068    
069                    if (comparator.compare(el1, el2) == 0) {
070                            return true;
071                    }
072                    else {
073                            return false;
074                    }
075            }
076    
077            public abstract boolean canHandleType(String doctype, Document root);
078    
079            public boolean canJoinChildren(Element element) {
080                    return _isIncluded(element, getJoinableElements());
081            }
082    
083            public String[] getRootChildrenOrder() {
084                    return new String[0];
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[] getUniqueElements() {
100                    return new String[0];
101            }
102    
103            public String[] getJoinableElements() {
104                    return new String[0];
105            }
106    
107            private int _compareAttribute(Element el1, Element el2, String attrName) {
108                    String name1 = el1.attributeValue(attrName);
109                    String name2 = el2.attributeValue(attrName);
110    
111                    if ((name1 == null) || (name2 == null)) {
112                            return -1;
113                    }
114    
115                    return name1.compareTo(name2);
116            }
117    
118            private int _compareChildText(Element el1, Element el2, String childName) {
119                    Element child1 = _getChild(el1, childName);
120                    Element child2 = _getChild(el2, childName);
121    
122                    if ((child1 == null) || (child2 == null)) {
123                            return -1;
124                    }
125    
126                    String name1 = child1.getText();
127                    String name2 = child2.getText();
128    
129                    if ((name1 == null) || (name2 == null)) {
130                            return -1;
131                    }
132    
133                    return name1.compareTo(name2);
134            }
135    
136            private Element _getChild(Element parent, String childName) {
137                    Element child = parent.element(childName);
138    
139                    /*if (child == null) {
140                            child = parent.element(childName, parent.getNamespace());
141                    }*/
142    
143                    return child;
144            }
145    
146            private boolean _isIncluded(Element element, String[] elemNames) {
147                    for (int i = 0; i < elemNames.length; i++) {
148                            if (element.getName().equals(elemNames[i])) {
149                                    return true;
150                            }
151                    }
152    
153                    return false;
154            }
155    
156    }