KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > setup > xml > A_CmsSetupXmlUpdate


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/setup/xml/A_CmsSetupXmlUpdate.java,v $
3  * Date : $Date: 2006/03/27 14:52:44 $
4  * Version: $Revision: 1.2 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.setup.xml;
33
34 import org.opencms.configuration.CmsConfigurationManager;
35 import org.opencms.i18n.CmsEncoder;
36 import org.opencms.setup.CmsSetupBean;
37 import org.opencms.util.CmsStringUtil;
38 import org.opencms.xml.CmsXmlUtils;
39
40 import java.util.Collections JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.List JavaDoc;
43
44 import org.dom4j.Branch;
45 import org.dom4j.Document;
46 import org.dom4j.DocumentFactory;
47 import org.dom4j.Node;
48
49 /**
50  * Skeleton for xml update plugins.<p>
51  *
52  * @author Michael Moossen
53  *
54  * @version $Revision: 1.2 $
55  *
56  * @since 6.1.8
57  */

58 public abstract class A_CmsSetupXmlUpdate implements I_CmsSetupXmlUpdate {
59
60     /**
61      * @see org.opencms.setup.xml.I_CmsSetupXmlUpdate#execute(org.opencms.setup.CmsSetupBean)
62      */

63     public void execute(CmsSetupBean setupBean) throws Exception JavaDoc {
64
65         Document doc = setupBean.getXmlHelper().getDocument(getXmlFilename());
66         Iterator JavaDoc itRemove = getXPathsToRemove().iterator();
67         while (itRemove.hasNext()) {
68             String JavaDoc xpath = (String JavaDoc)itRemove.next();
69             CmsSetupXmlHelper.setValue(doc, xpath, null);
70         }
71         Iterator JavaDoc itUpdate = getXPathsToUpdate().iterator();
72         while (itUpdate.hasNext()) {
73             String JavaDoc xpath = (String JavaDoc)itUpdate.next();
74             executeUpdate(doc, xpath);
75         }
76     }
77
78     /**
79      * @see org.opencms.setup.xml.I_CmsSetupXmlUpdate#getCodeToChange(org.opencms.setup.CmsSetupBean)
80      */

81     public String JavaDoc getCodeToChange(CmsSetupBean setupBean) throws Exception JavaDoc {
82
83         String JavaDoc ret = "";
84         Document doc = setupBean.getXmlHelper().getDocument(getXmlFilename());
85
86         // get the nodes to be deleted
87
Iterator JavaDoc itRemove = getXPathsToRemove().iterator();
88         while (itRemove.hasNext()) {
89             String JavaDoc xpath = (String JavaDoc)itRemove.next();
90             Node node = doc.selectSingleNode(xpath);
91             if (node != null) {
92                 ret += CmsXmlUtils.marshal(node, CmsEncoder.ENCODING_UTF_8);
93             }
94         }
95
96         // create new temp doc to modify
97
String JavaDoc parentPath = getCommonPath();
98         // could be better done...
99
Document newDoc = prepareDoc(doc);
100
101         boolean modified = false;
102         // update the temp doc
103
Iterator JavaDoc itUpdate = getXPathsToUpdate().iterator();
104         while (itUpdate.hasNext()) {
105             String JavaDoc xpath = (String JavaDoc)itUpdate.next();
106             updateDoc(doc, newDoc, xpath);
107             boolean exe = executeUpdate(newDoc, xpath);
108             modified = modified || exe;
109             if (parentPath == null && exe) {
110                 Node node = newDoc.selectSingleNode(xpath);
111                 if (node != null) {
112                     ret += CmsXmlUtils.marshal(node, CmsEncoder.ENCODING_UTF_8);
113                 }
114             }
115         }
116         if (parentPath != null && modified) {
117             Node node = newDoc.selectSingleNode(parentPath);
118             if (node != null) {
119                 ret += CmsXmlUtils.marshal(node, CmsEncoder.ENCODING_UTF_8);
120             }
121         }
122         return ret.trim();
123     }
124     
125     /**
126      * Updates the given doc inserting the given node corresponding to the given xpath.<p>
127      *
128      * @param document the original document to update
129      * @param newDoc the document to update
130      * @param xpath the corresponding xpath
131      */

132     protected void updateDoc(Document document, Document newDoc, String JavaDoc xpath) {
133
134         Node node = document.selectSingleNode(xpath);
135         if (node != null) {
136             CmsSetupXmlHelper.setValue(newDoc, CmsXmlUtils.removeLastComplexXpathElement(xpath), " ");
137             node = (Node)node.clone();
138             node.setParent(null);
139             ((Branch)newDoc.selectSingleNode(CmsXmlUtils.removeLastComplexXpathElement(xpath))).add(node);
140         }
141     }
142
143     /**
144      * Returns a parent path that is common for all nodes to modify.<p>
145      *
146      * @return common parent path
147      */

148     protected String JavaDoc getCommonPath() {
149         
150         return null;
151     }
152
153     /**
154      * @see org.opencms.setup.xml.I_CmsSetupXmlUpdate#validate(org.opencms.setup.CmsSetupBean)
155      */

156     public boolean validate(CmsSetupBean setupBean) throws Exception JavaDoc {
157
158         return CmsStringUtil.isNotEmptyOrWhitespaceOnly(getCodeToChange(setupBean));
159     }
160
161     /**
162      * Executes the adding/updating changes on the given document.<p>
163      *
164      * Only needs to be overriden if {@link #getXPathsToUpdate()} is not empty.<p>
165      *
166      * @param document the document to apply the changes to
167      * @param xpath the xpath to execute the changes for
168      *
169      * @return if something was modified
170      */

171     protected boolean executeUpdate(Document document, String JavaDoc xpath) {
172
173         // do something to avoid warning
174
return ((Object JavaDoc)document == (Object JavaDoc)xpath);
175     }
176
177     /**
178      * Returns a list of xpaths for the nodes to remove.<p>
179      *
180      * @return a list of strings
181      */

182     protected List JavaDoc getXPathsToRemove() {
183
184         return Collections.EMPTY_LIST;
185     }
186
187     /**
188      * Returns a list of xpaths for the nodes to add/update.<p>
189      *
190      * @return a list of strings
191      */

192     protected List JavaDoc getXPathsToUpdate() {
193
194         return Collections.EMPTY_LIST;
195     }
196
197     /**
198      * Prepares a new document.<p>
199      *
200      * @param doc the original document
201      *
202      * @return a new document
203      */

204     protected Document prepareDoc(Document doc) {
205
206         Document newDoc = new DocumentFactory().createDocument();
207         newDoc.addElement(CmsConfigurationManager.N_ROOT);
208         newDoc.setName(doc.getName());
209         return newDoc;
210     }
211 }
Popular Tags