1 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 ; 41 import java.util.Iterator ; 42 import java.util.List ; 43 44 import org.dom4j.Branch; 45 import org.dom4j.Document; 46 import org.dom4j.DocumentFactory; 47 import org.dom4j.Node; 48 49 58 public abstract class A_CmsSetupXmlUpdate implements I_CmsSetupXmlUpdate { 59 60 63 public void execute(CmsSetupBean setupBean) throws Exception { 64 65 Document doc = setupBean.getXmlHelper().getDocument(getXmlFilename()); 66 Iterator itRemove = getXPathsToRemove().iterator(); 67 while (itRemove.hasNext()) { 68 String xpath = (String )itRemove.next(); 69 CmsSetupXmlHelper.setValue(doc, xpath, null); 70 } 71 Iterator itUpdate = getXPathsToUpdate().iterator(); 72 while (itUpdate.hasNext()) { 73 String xpath = (String )itUpdate.next(); 74 executeUpdate(doc, xpath); 75 } 76 } 77 78 81 public String getCodeToChange(CmsSetupBean setupBean) throws Exception { 82 83 String ret = ""; 84 Document doc = setupBean.getXmlHelper().getDocument(getXmlFilename()); 85 86 Iterator itRemove = getXPathsToRemove().iterator(); 88 while (itRemove.hasNext()) { 89 String xpath = (String )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 String parentPath = getCommonPath(); 98 Document newDoc = prepareDoc(doc); 100 101 boolean modified = false; 102 Iterator itUpdate = getXPathsToUpdate().iterator(); 104 while (itUpdate.hasNext()) { 105 String xpath = (String )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 132 protected void updateDoc(Document document, Document newDoc, String 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 148 protected String getCommonPath() { 149 150 return null; 151 } 152 153 156 public boolean validate(CmsSetupBean setupBean) throws Exception { 157 158 return CmsStringUtil.isNotEmptyOrWhitespaceOnly(getCodeToChange(setupBean)); 159 } 160 161 171 protected boolean executeUpdate(Document document, String xpath) { 172 173 return ((Object )document == (Object )xpath); 175 } 176 177 182 protected List getXPathsToRemove() { 183 184 return Collections.EMPTY_LIST; 185 } 186 187 192 protected List getXPathsToUpdate() { 193 194 return Collections.EMPTY_LIST; 195 } 196 197 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 |