Class CmsSetupXmlHelper


  • public class CmsSetupXmlHelper
    extends java.lang.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
      CmsSetupXmlHelper()
      Default constructor.
      CmsSetupXmlHelper​(java.lang.String basePath)
      Uses an optional base file path.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      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.
      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 java.lang.String getValue​(org.dom4j.Document document, java.lang.String xPath)
      Returns the value in the given xpath of the given xml file.
      boolean setAttribute​(java.lang.String xmlFilename, java.lang.String xPath, java.lang.String attribute, java.lang.String value)
      Replaces a attibute's value in the given node addressed by the xPath.
      static boolean setAttribute​(org.dom4j.Document document, java.lang.String xPath, java.lang.String attribute, java.lang.String value)
      Replaces a attibute's value in the given node addressed by the xPath.
      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.
      int setValue​(java.lang.String xmlFilename, java.lang.String xPath, java.lang.String value, java.lang.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, 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.
      static int setValue​(org.dom4j.Document document, java.lang.String xPath, java.lang.String value, java.lang.String nodeToInsert)
      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 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 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
      • setAttribute

        public static boolean setAttribute​(org.dom4j.Document document,
                                           java.lang.String xPath,
                                           java.lang.String attribute,
                                           java.lang.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,
                                   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
      • setValue

        public static int setValue​(org.dom4j.Document document,
                                   java.lang.String xPath,
                                   java.lang.String value,
                                   java.lang.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​(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 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 java.lang.String getValue​(java.lang.String xmlFilename,
                                         java.lang.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​(java.lang.String xmlFilename,
                                    java.lang.String xPath,
                                    java.lang.String attribute,
                                    java.lang.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​(java.lang.String xmlFilename,
                            java.lang.String xPath,
                            java.lang.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​(java.lang.String xmlFilename,
                            java.lang.String xPath,
                            java.lang.String value,
                            java.lang.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​(java.lang.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