001: /*
002: * File : $Source: /usr/local/cvs/opencms/src-setup/org/opencms/setup/xml/A_CmsSetupXmlUpdate.java,v $
003: * Date : $Date: 2008-02-27 12:05:37 $
004: * Version: $Revision: 1.2 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.setup.xml;
033:
034: import org.opencms.configuration.CmsConfigurationManager;
035: import org.opencms.i18n.CmsEncoder;
036: import org.opencms.setup.CmsSetupBean;
037: import org.opencms.util.CmsStringUtil;
038: import org.opencms.xml.CmsXmlUtils;
039:
040: import java.util.Collections;
041: import java.util.Iterator;
042: import java.util.List;
043:
044: import org.dom4j.Branch;
045: import org.dom4j.Document;
046: import org.dom4j.DocumentFactory;
047: import org.dom4j.Node;
048:
049: /**
050: * Skeleton for xml update plugins.<p>
051: *
052: * @author Michael Moossen
053: *
054: * @version $Revision: 1.2 $
055: *
056: * @since 6.1.8
057: */
058: public abstract class A_CmsSetupXmlUpdate implements
059: I_CmsSetupXmlUpdate {
060:
061: /**
062: * @see org.opencms.setup.xml.I_CmsSetupXmlUpdate#execute(org.opencms.setup.CmsSetupBean)
063: */
064: public void execute(CmsSetupBean setupBean) throws Exception {
065:
066: Document doc = setupBean.getXmlHelper().getDocument(
067: getXmlFilename());
068: Iterator itRemove = getXPathsToRemove().iterator();
069: while (itRemove.hasNext()) {
070: String xpath = (String) itRemove.next();
071: CmsSetupXmlHelper.setValue(doc, xpath, null);
072: }
073: Iterator itUpdate = getXPathsToUpdate().iterator();
074: while (itUpdate.hasNext()) {
075: String xpath = (String) itUpdate.next();
076: executeUpdate(doc, xpath);
077: }
078: }
079:
080: /**
081: * @see org.opencms.setup.xml.I_CmsSetupXmlUpdate#getCodeToChange(org.opencms.setup.CmsSetupBean)
082: */
083: public String getCodeToChange(CmsSetupBean setupBean)
084: throws Exception {
085:
086: String ret = "";
087: Document doc = setupBean.getXmlHelper().getDocument(
088: getXmlFilename());
089:
090: // get the nodes to be deleted
091: Iterator itRemove = getXPathsToRemove().iterator();
092: while (itRemove.hasNext()) {
093: String xpath = (String) itRemove.next();
094: Iterator it = doc.selectNodes(xpath).iterator();
095: while (it.hasNext()) {
096: Node node = (Node) it.next();
097: if (node != null) {
098: ret += CmsXmlUtils.marshal(node,
099: CmsEncoder.ENCODING_UTF_8);
100: }
101: }
102: }
103:
104: // create new temp doc to modify
105: String parentPath = getCommonPath();
106: // could be better done...
107: Document newDoc = prepareDoc(doc);
108:
109: boolean modified = false;
110: // update the temp doc
111: Iterator itUpdate = getXPathsToUpdate().iterator();
112: while (itUpdate.hasNext()) {
113: String xpath = (String) itUpdate.next();
114: updateDoc(doc, newDoc, xpath);
115: boolean exe = executeUpdate(newDoc, xpath);
116: modified = modified || exe;
117: if ((parentPath == null) && exe) {
118: Node node = newDoc.selectSingleNode(xpath);
119: if (node != null) {
120: ret += CmsXmlUtils.marshal(node,
121: CmsEncoder.ENCODING_UTF_8);
122: }
123: }
124: }
125: if ((parentPath != null) && modified) {
126: Node node = newDoc.selectSingleNode(parentPath);
127: if (node != null) {
128: ret += CmsXmlUtils.marshal(node,
129: CmsEncoder.ENCODING_UTF_8);
130: }
131: }
132: return ret.trim();
133: }
134:
135: /**
136: * Updates the given doc inserting the given node corresponding to the given xpath.<p>
137: *
138: * @param document the original document to update
139: * @param newDoc the document to update
140: * @param xpath the corresponding xpath
141: */
142: protected void updateDoc(Document document, Document newDoc,
143: String xpath) {
144:
145: Node node = document.selectSingleNode(xpath);
146: if (node != null) {
147: CmsSetupXmlHelper.setValue(newDoc, CmsXmlUtils
148: .removeLastComplexXpathElement(xpath), " ");
149: node = (Node) node.clone();
150: node.setParent(null);
151: ((Branch) newDoc.selectSingleNode(CmsXmlUtils
152: .removeLastComplexXpathElement(xpath))).add(node);
153: }
154: }
155:
156: /**
157: * Returns a parent path that is common for all nodes to modify.<p>
158: *
159: * @return common parent path
160: */
161: protected String getCommonPath() {
162:
163: return null;
164: }
165:
166: /**
167: * @see org.opencms.setup.xml.I_CmsSetupXmlUpdate#validate(org.opencms.setup.CmsSetupBean)
168: */
169: public boolean validate(CmsSetupBean setupBean) throws Exception {
170:
171: return CmsStringUtil
172: .isNotEmptyOrWhitespaceOnly(getCodeToChange(setupBean));
173: }
174:
175: /**
176: * Executes the adding/updating changes on the given document.<p>
177: *
178: * Only needs to be overriden if {@link #getXPathsToUpdate()} is not empty.<p>
179: *
180: * @param document the document to apply the changes to
181: * @param xpath the xpath to execute the changes for
182: *
183: * @return if something was modified
184: */
185: protected boolean executeUpdate(Document document, String xpath) {
186:
187: // do something to avoid warning
188: return ((Object) document == (Object) xpath);
189: }
190:
191: /**
192: * Returns a list of xpaths for the nodes to remove.<p>
193: *
194: * @return a list of strings
195: */
196: protected List getXPathsToRemove() {
197:
198: return Collections.EMPTY_LIST;
199: }
200:
201: /**
202: * Returns a list of xpaths for the nodes to add/update.<p>
203: *
204: * @return a list of strings
205: */
206: protected List getXPathsToUpdate() {
207:
208: return Collections.EMPTY_LIST;
209: }
210:
211: /**
212: * Prepares a new document.<p>
213: *
214: * @param doc the original document
215: *
216: * @return a new document
217: */
218: protected Document prepareDoc(Document doc) {
219:
220: Document newDoc = new DocumentFactory().createDocument();
221: newDoc.addElement(CmsConfigurationManager.N_ROOT);
222: newDoc.setName(doc.getName());
223: return newDoc;
224: }
225: }
|