Class CmsSetupXmlHelper
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
ConstructorDescriptionDefault constructor.CmsSetupXmlHelper
(String basePath) Uses an optional base file path. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Discards the changes in the given file.void
flushAll()
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.Returns the value in the given xpath of the given xml file.static String
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
Sets the given value in all nodes identified by the given xpath of the given xml file.int
Sets the given value in all nodes identified by the given xpath of the given xml file.static int
Sets the given value in all nodes identified by the given xpath of the given xml file.static int
Sets the given value in all nodes identified by the given xpath of the given xml file.void
Writes the given file back to disk.void
writeAll()
Flushes all cached documents.
-
Constructor Details
-
CmsSetupXmlHelper
public CmsSetupXmlHelper()Default constructor.Uses no base path.
-
CmsSetupXmlHelper
Uses an optional base file path.- Parameters:
basePath
- the base file path to use;
-
-
Method Details
-
format
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
Returns the value in the given xpath of the given xml file.- Parameters:
document
- the xml documentxPath
- 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 attributexPath
- the xPath to the nodeattribute
- the attribute to replace the value ofvalue
- the new value to set- Returns:
true
if successfulfalse
otherwise
-
setValue
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
notnull
).- Parameters:
document
- the xml documentxPath
- the xpath to setvalue
- the value to set (can benull
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
notnull
).- Parameters:
document
- the xml documentxPath
- the xpath to setvalue
- the value to set (can benull
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
Discards the changes in the given file.- Parameters:
xmlFilename
- the xml config file (could be relative to the base path)
-
flushAll
Discards the changes in all files. -
getBasePath
Returns the base file Path.- Returns:
- the base file Path
-
getDocument
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
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 fromxPath
- the xPath to the nodeattribute
- the attribute to replace the value ofvalue
- the new value to set- Returns:
true
if successfulfalse
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
notnull
).- Parameters:
xmlFilename
- the xml config file (could be relative to the base path)xPath
- the xpath to setvalue
- the value to set (can benull
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
notnull
).- Parameters:
xmlFilename
- the xml config file (could be relative to the base path)xPath
- the xpath to setvalue
- the value to set (can benull
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
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
Flushes all cached documents.- Throws:
org.opencms.xml.CmsXmlException
- if something wrong while writing
-