org.opencms.setup.xml
Class CmsSetupXmlHelper

java.lang.Object
  extended by org.opencms.setup.xml.CmsSetupXmlHelper

public class CmsSetupXmlHelper
extends java.lang.Object

Helper class to modify xml files.

For more info about xpath see:

Since:
6.1.8
Version:
$Revision: 1.4 $
Author:
Michael Moossen

Constructor Summary
CmsSetupXmlHelper()
          Default constructor.
CmsSetupXmlHelper(java.lang.String basePath)
          Uses an optional base file path.
 
Method Summary
 void flush(java.lang.String xmlFilename)
          Discards the changes in the given file.
 void flushAll()
          Discards the changes in all files.
static java.lang.String format(java.lang.String xml)
          Unmarshals (reads) an XML string into a new document.
 java.lang.String getBasePath()
          Returns the base file Path.
 org.dom4j.Document getDocument(java.lang.String xmlFilename)
          Returns the document for the given filename.
static java.lang.String getValue(org.dom4j.Document document, java.lang.String xPath)
          Returns the value in the given xpath of the given xml file.
 java.lang.String getValue(java.lang.String xmlFilename, java.lang.String xPath)
          Returns the value in the given xpath of the given xml file.
static int setValue(org.dom4j.Document document, java.lang.String xPath, java.lang.String value)
          Sets the given value in all nodes identified by the given xpath of the given xml file.
 int setValue(java.lang.String xmlFilename, java.lang.String xPath, java.lang.String value)
          Sets the given value in all nodes identified by the given xpath of the given xml file.
 void write(java.lang.String xmlFilename)
          Writes the given file back to disk.
 void writeAll()
          Flushes all cached documents.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CmsSetupXmlHelper

public CmsSetupXmlHelper()
Default constructor.

Uses no base path.


CmsSetupXmlHelper

public CmsSetupXmlHelper(java.lang.String basePath)
Uses an optional base file path.

Parameters:
basePath - the base file path to use;
Method Detail

format

public static java.lang.String format(java.lang.String xml)
                               throws CmsXmlException
Unmarshals (reads) an XML string into a new document.

Parameters:
xml - the XML code to unmarshal
Returns:
the generated document
Throws:
CmsXmlException - if something goes wrong

getValue

public static java.lang.String getValue(org.dom4j.Document document,
                                        java.lang.String xPath)
Returns the value in the given xpath of the given xml file.

Parameters:
document - the xml document
xPath - the xpath to read (should select a single node or attribute)
Returns:
the value in the given xpath of the given xml file, or null if no matching node

setValue

public static int setValue(org.dom4j.Document document,
                           java.lang.String xPath,
                           java.lang.String value)
Sets the given value in all nodes identified by the given xpath of the given xml file.

If value is null, all nodes identified by the given xpath will be deleted.

If the node identified by the given xpath does not exists, the missing nodes will be created (if value not null).

Parameters:
document - the xml document
xPath - the xpath to set
value - the value to set (can be null for deletion)
Returns:
the number of successful changed or deleted nodes

flush

public void flush(java.lang.String xmlFilename)
Discards the changes in the given file.

Parameters:
xmlFilename - the xml config file (could be relative to the base path)

flushAll

public void flushAll()
Discards the changes in all files.


getBasePath

public java.lang.String getBasePath()
Returns the base file Path.

Returns:
the base file Path

getDocument

public org.dom4j.Document getDocument(java.lang.String xmlFilename)
                               throws CmsXmlException
Returns the document for the given filename.

It can be new read or come from the document cache.

Parameters:
xmlFilename - the filename to read
Returns:
the document for the given filename
Throws:
CmsXmlException - if something goes wrong while reading

getValue

public java.lang.String getValue(java.lang.String xmlFilename,
                                 java.lang.String xPath)
                          throws CmsXmlException
Returns the value in the given xpath of the given xml file.

Parameters:
xmlFilename - the xml config file (could be relative to the base path)
xPath - the xpath to read (should select a single node or attribute)
Returns:
the value in the given xpath of the given xml file, or null if no matching node
Throws:
CmsXmlException - if something goes wrong while reading

setValue

public int setValue(java.lang.String xmlFilename,
                    java.lang.String xPath,
                    java.lang.String value)
             throws CmsXmlException
Sets the given value in all nodes identified by the given xpath of the given xml file.

If value is null, all nodes identified by the given xpath will be deleted.

If the node identified by the given xpath does not exists, the missing nodes will be created (if value not null).

Parameters:
xmlFilename - the xml config file (could be relative to the base path)
xPath - the xpath to set
value - the value to set (can be null for deletion)
Returns:
the number of successful changed or deleted nodes
Throws:
CmsXmlException - if something goes wrong

write

public void write(java.lang.String xmlFilename)
           throws CmsXmlException
Writes the given file back to disk.

Parameters:
xmlFilename - the xml config file (could be relative to the base path)
Throws:
CmsXmlException - if something wrong while writing

writeAll

public void writeAll()
              throws CmsXmlException
Flushes all cached documents.

Throws:
CmsXmlException - if something wrong while writing