001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.xml.descriptor;
022:
023: import com.liferay.util.xml.ElementComparator;
024: import com.liferay.util.xml.ElementIdentifier;
025:
026: import org.dom4j.Document;
027: import org.dom4j.Element;
028:
029: /**
030: * <a href="SimpleXMLDescriptor.java.html"><b><i>View Source</i></b></a>
031: *
032: * @author Jorge Ferrer
033: *
034: */
035: public abstract class SimpleXMLDescriptor implements XMLDescriptor {
036:
037: public boolean areEqual(Element el1, Element el2) {
038: String name1 = el1.getName();
039: String name2 = el2.getName();
040:
041: if ((name1 == null) || !name1.equals(name2)) {
042: return false;
043: }
044:
045: if (_isIncluded(el1, getUniqueElements())) {
046: return true;
047: }
048:
049: ElementIdentifier[] elIds = getElementsIdentifiedByAttribute();
050: for (int i = 0; i < elIds.length; i++) {
051: if (name1.equals(elIds[i].getElementName())) {
052: if (_compareAttribute(el1, el2, elIds[i]
053: .getIdentifierName()) == 0) {
054:
055: return true;
056: } else {
057: return false;
058: }
059: }
060: }
061:
062: elIds = getElementsIdentifiedByChild();
063: for (int i = 0; i < elIds.length; i++) {
064: if (name1.equals(elIds[i].getElementName())) {
065: if (_compareChildText(el1, el2, elIds[i]
066: .getIdentifierName()) == 0) {
067: return true;
068: } else {
069: return false;
070: }
071: }
072: }
073:
074: ElementComparator comparator = new ElementComparator();
075:
076: if (comparator.compare(el1, el2) == 0) {
077: return true;
078: } else {
079: return false;
080: }
081: }
082:
083: public abstract boolean canHandleType(String doctype, Document root);
084:
085: public boolean canJoinChildren(Element element) {
086: return _isIncluded(element, getJoinableElements());
087: }
088:
089: public String[] getRootChildrenOrder() {
090: return new String[0];
091: }
092:
093: public ElementIdentifier[] getElementsIdentifiedByAttribute() {
094: return new ElementIdentifier[0];
095: }
096:
097: public ElementIdentifier[] getElementsIdentifiedByChild() {
098: return new ElementIdentifier[0];
099: }
100:
101: public String[] getUniqueElements() {
102: return new String[0];
103: }
104:
105: public String[] getJoinableElements() {
106: return new String[0];
107: }
108:
109: private int _compareAttribute(Element el1, Element el2,
110: String attrName) {
111: String name1 = el1.attributeValue(attrName);
112: String name2 = el2.attributeValue(attrName);
113:
114: if ((name1 == null) || (name2 == null)) {
115: return -1;
116: }
117:
118: return name1.compareTo(name2);
119: }
120:
121: private int _compareChildText(Element el1, Element el2,
122: 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: private boolean _isIncluded(Element element, String[] elemNames) {
151: for (int i = 0; i < elemNames.length; i++) {
152: if (element.getName().equals(elemNames[i])) {
153: return true;
154: }
155: }
156:
157: return false;
158: }
159:
160: }
|