1 31 32 package org.opencms.setup.xml; 33 34 import org.opencms.i18n.CmsEncoder; 35 import org.opencms.i18n.CmsMessageContainer; 36 import org.opencms.main.CmsLog; 37 import org.opencms.util.CmsStringUtil; 38 import org.opencms.xml.CmsXmlException; 39 import org.opencms.xml.CmsXmlUtils; 40 41 import java.io.File ; 42 import java.io.FileNotFoundException ; 43 import java.io.FileOutputStream ; 44 import java.io.FileReader ; 45 import java.io.OutputStream ; 46 import java.io.StringReader ; 47 import java.util.ArrayList ; 48 import java.util.HashMap ; 49 import java.util.Iterator ; 50 import java.util.Map ; 51 52 import org.apache.commons.logging.Log; 53 54 import org.dom4j.Document; 55 import org.dom4j.Element; 56 import org.dom4j.Node; 57 import org.xml.sax.EntityResolver ; 58 import org.xml.sax.InputSource ; 59 60 75 public class CmsSetupXmlHelper { 76 77 78 private static final EntityResolver NO_ENTITY_RESOLVER = new EntityResolver () { 79 80 83 public InputSource resolveEntity(String publicId, String systemId) { 84 85 return new InputSource (new StringReader ("")); 86 } 87 }; 88 89 90 private String m_basePath = null; 91 92 93 private Map m_cache = new HashMap (); 94 95 100 public CmsSetupXmlHelper() { 101 102 } 104 105 110 public CmsSetupXmlHelper(String basePath) { 111 112 m_basePath = basePath; 113 } 114 115 124 public static String format(String xml) throws CmsXmlException { 125 126 return CmsXmlUtils.marshal((Node)CmsXmlUtils.unmarshalHelper(xml, null), CmsEncoder.ENCODING_UTF_8); 127 } 128 129 137 public static String getValue(Document document, String xPath) { 138 139 Node node = document.selectSingleNode(xPath); 140 if (node != null) { 141 return node.getText(); 143 } else { 144 return null; 145 } 146 } 147 148 149 private static final Log LOG = CmsLog.getLog(CmsSetupXmlHelper.class); 150 151 165 public static int setValue(Document document, String xPath, String value) { 166 167 int changes = 0; 168 Iterator itNodes = document.selectNodes(xPath).iterator(); 170 171 if (!itNodes.hasNext()) { 173 if (value == null) { 174 return 0; 176 } 177 Iterator it = CmsStringUtil.splitAsList(xPath, "/", false).iterator(); 179 Node currentNode = document; 180 while (it.hasNext()) { 181 String nodeName = (String )it.next(); 182 while (nodeName.indexOf("='") > 0 && nodeName.indexOf("']") < 0) { 184 nodeName += "/" + (String )it.next(); 185 } 186 Node node = currentNode.selectSingleNode(nodeName); 187 if (node != null) { 188 currentNode = node; 190 if (!it.hasNext()) { 191 currentNode.setText(value); 192 } 193 } else if (currentNode.getNodeType() == Node.ELEMENT_NODE) { 194 Element elem = (Element)currentNode; 195 if (!nodeName.startsWith("@")) { 196 String childName = null; 198 String childValue = ""; 199 int pos = nodeName.indexOf("["); 200 if (pos > 0) { 201 int pos2 = nodeName.indexOf("=\'", pos); 203 if (pos2 > 0) { 204 childName = nodeName.substring(pos + 1, pos2); 205 childValue = nodeName.substring(pos2 + 2, nodeName.indexOf('\'', pos2 + 2)); 206 } 207 nodeName = nodeName.substring(0, pos); 208 } 209 elem = elem.addElement(nodeName); 211 if (childName != null) { 212 if (childName.startsWith("@")) { 214 elem.addAttribute(childName.substring(1), childValue); 215 } else { 216 Element child = elem.addElement(childName); 217 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(childValue)) { 218 child.addText(childValue); 219 } 220 } 221 } 222 if (!it.hasNext()) { 223 elem.setText(value); 224 } 225 } else { 226 elem.addAttribute(nodeName.substring(1), value); 228 } 229 currentNode = elem; 230 } else { 231 if (LOG.isDebugEnabled()) { 233 LOG.debug(Messages.get().getBundle().key(Messages.ERR_XML_SET_VALUE_2, xPath, value)); 234 } 235 break; 236 } 237 } 238 return 1; 239 } 240 241 while (itNodes.hasNext()) { 243 Node node = (Node)itNodes.next(); 244 if (value != null) { 245 node.setText(value); 247 } else { 248 node.getParent().remove(node); 250 } 251 changes++; 252 } 253 return changes; 254 } 255 256 261 public void flush(String xmlFilename) { 262 263 m_cache.remove(xmlFilename); 264 } 265 266 269 public void flushAll() { 270 271 m_cache.clear(); 272 } 273 274 279 public String getBasePath() { 280 281 return m_basePath; 282 } 283 284 294 public Document getDocument(String xmlFilename) throws CmsXmlException { 295 296 Document document = (Document)m_cache.get(xmlFilename); 298 299 if (document == null) { 300 try { 301 document = CmsXmlUtils.unmarshalHelper( 302 new InputSource (new FileReader (getFile(xmlFilename))), 303 NO_ENTITY_RESOLVER); 304 } catch (FileNotFoundException e) { 305 throw new CmsXmlException(new CmsMessageContainer(null, e.toString())); 306 } 307 m_cache.put(xmlFilename, document); 309 } 310 return document; 311 } 312 313 323 public String getValue(String xmlFilename, String xPath) throws CmsXmlException { 324 325 return getValue(getDocument(xmlFilename), xPath); 326 } 327 328 344 public int setValue(String xmlFilename, String xPath, String value) throws CmsXmlException { 345 346 return setValue(getDocument(xmlFilename), xPath, value); 347 } 348 349 356 public void write(String xmlFilename) throws CmsXmlException { 357 358 Document document = (Document)m_cache.get(xmlFilename); 360 361 if (document != null) { 362 try { 363 OutputStream out = new FileOutputStream (getFile(xmlFilename)); 364 CmsXmlUtils.marshal(document, out, CmsEncoder.ENCODING_UTF_8); 365 } catch (FileNotFoundException e) { 366 throw new CmsXmlException(new CmsMessageContainer(null, e.toString())); 367 } 368 } 369 } 370 371 376 public void writeAll() throws CmsXmlException { 377 378 Iterator it = new ArrayList (m_cache.keySet()).iterator(); 379 while (it.hasNext()) { 380 String filename = (String )it.next(); 381 write(filename); 382 } 383 } 384 385 392 private File getFile(String xmlFilename) { 393 394 File file = new File (m_basePath + xmlFilename); 395 if (!file.exists() || !file.canRead()) { 396 file = new File (xmlFilename); 397 } 398 return file; 399 } 400 } | Popular Tags |