Class CmsSetupXmlHelper

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

public class CmsSetupXmlHelper extends Object
Helper class to modify xml files.

For more info about xpath see:

  • http://www.w3.org/TR/xpath.html
  • http://www.zvon.org/xxl/XPathTutorial/General/examples.html

Since:
6.1.8
  • Constructor Summary

    Constructors
    Constructor
    Description
    Default constructor.
    Uses an optional base file path.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    flush(String xmlFilename)
    Discards the changes in the given file.
    void
    Discards the changes in all files.
    static String
    Unmarshals (reads) an XML string into a new document.
    Returns the base file Path.
    org.dom4j.Document
    getDocument(String xmlFilename)
    Returns the document for the given filename.
    getValue(String xmlFilename, String xPath)
    Returns the value in the given xpath of the given xml file.
    static String
    getValue(org.dom4j.Document document, String xPath)
    Returns the value in the given xpath of the given xml file.
    boolean
    setAttribute(String xmlFilename, String xPath, String attribute, String value)
    Replaces a attibute's value in the given node addressed by the xPath.
    static boolean
    setAttribute(org.dom4j.Document document, String xPath, String attribute, String value)
    Replaces a attibute's value in the given node addressed by the xPath.
    int
    setValue(String xmlFilename, String xPath, String value)
    Sets the given value in all nodes identified by the given xpath of the given xml file.
    int
    setValue(String xmlFilename, String xPath, String value, String nodeToInsert)
    Sets the given value in all nodes identified by the given xpath of the given xml file.
    static int
    setValue(org.dom4j.Document document, String xPath, String value)
    Sets the given value in all nodes identified by the given xpath of the given xml file.
    static int
    setValue(org.dom4j.Document document, String xPath, String value, String nodeToInsert)
    Sets the given value in all nodes identified by the given xpath of the given xml file.
    void
    write(String xmlFilename)
    Writes the given file back to disk.
    void
    Flushes all cached documents.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • CmsSetupXmlHelper

      Default constructor.

      Uses no base path.

    • CmsSetupXmlHelper

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

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

    • format

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

      Parameters:
      xml - the XML code to unmarshal
      Returns:
      the generated document
      Throws:
      org.opencms.xml.CmsXmlException - if something goes wrong
    • getValue

      public static String getValue(org.dom4j.Document document, 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
    • setAttribute

      public static boolean setAttribute(org.dom4j.Document document, String xPath, String attribute, String value)
      Replaces a attibute's value in the given node addressed by the xPath.

      Parameters:
      document - the document to replace the node attribute
      xPath - the xPath to the node
      attribute - the attribute to replace the value of
      value - the new value to set
      Returns:
      true if successful false otherwise
    • setValue

      public static int setValue(org.dom4j.Document document, String xPath, 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
    • setValue

      public static int setValue(org.dom4j.Document document, String xPath, String value, String nodeToInsert)
      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)
      nodeToInsert - optional, if given it will be inserted after xPath with the given value
      Returns:
      the number of successful changed or deleted nodes
    • flush

      public void flush(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 String getBasePath()
      Returns the base file Path.

      Returns:
      the base file Path
    • getDocument

      public org.dom4j.Document getDocument(String xmlFilename) throws org.opencms.xml.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:
      org.opencms.xml.CmsXmlException - if something goes wrong while reading
    • getValue

      public String getValue(String xmlFilename, String xPath) throws org.opencms.xml.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:
      org.opencms.xml.CmsXmlException - if something goes wrong while reading
    • setAttribute

      public boolean setAttribute(String xmlFilename, String xPath, String attribute, String value) throws org.opencms.xml.CmsXmlException
      Replaces a attibute's value in the given node addressed by the xPath.

      Parameters:
      xmlFilename - the xml file name to get the document from
      xPath - the xPath to the node
      attribute - the attribute to replace the value of
      value - the new value to set
      Returns:
      true if successful false otherwise
      Throws:
      org.opencms.xml.CmsXmlException - if the xml document coudn't be read
    • setValue

      public int setValue(String xmlFilename, String xPath, String value) throws org.opencms.xml.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:
      org.opencms.xml.CmsXmlException - if something goes wrong
    • setValue

      public int setValue(String xmlFilename, String xPath, String value, String nodeToInsert) throws org.opencms.xml.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)
      nodeToInsert - optional, if given it will be inserted after xPath with the given value
      Returns:
      the number of successful changed or deleted nodes
      Throws:
      org.opencms.xml.CmsXmlException - if something goes wrong
    • write

      public void write(String xmlFilename) throws org.opencms.xml.CmsXmlException
      Writes the given file back to disk.

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

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

      Throws:
      org.opencms.xml.CmsXmlException - if something wrong while writing