Class CmsXmlUtils
- Since:
- 6.0.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
concatXpath
(String prefix, String suffix) Concatenates two Xpath expressions, ensuring that exactly one slash "/" is between them.static Document
convertDocumentFromDom4jToW3C
(org.dom4j.Document doc) Converts an org.dom4j.Document to a org.w3c.dom.Document.static org.dom4j.Document
Converts an org.w3c.dom.Document to an org.dom4j.Document.static String
createXpath
(String path, int index) Translates a simple lookup path to the simplified Xpath format used for the internal bookmarks.static String
createXpathElement
(String path, int index) Appends the provided index parameter in square brackets to the given name, likepath[index]
.static String
createXpathElementCheck
(String path, int index) Ensures that a provided simplified Xpath has the formattitle[1]
.static String
getFirstXpathElement
(String path) Returns the first Xpath element from the provided path, without the index value.static String
getLastXpathElement
(String path) Returns the last Xpath element from the provided path, without the index value.static String
Returns the last Xpath element from the provided path.static int
getSchemaVersion
(org.dom4j.Document doc) Helper method to get the version number from a schema's/content's XML document.static String
getXpathIndex
(String path) Returns the last Xpath index from the given path.static int
getXpathIndexInt
(String path) Returns the last Xpath index from the given path as integer.static void
Initializes XML processing system properties to avoid evaluating the XML parser and reader implementation each time an XML document is read.static boolean
isDeepXpath
(String path) Returnstrue
if the given path is a Xpath with at least 2 elements.static OutputStream
marshal
(org.dom4j.Document document, OutputStream out, String encoding) Marshals (writes) an XML document into an output stream using XML pretty-print formatting.static String
Marshals (writes) an XML document to a String using XML pretty-print formatting.static String
Marshals (writes) an XML node into an output stream using XML pretty-print formatting.static String
removeAllXpathIndices
(String path) Removes all Xpath indices from the given path.static String
Removes the first Xpath element from the path.static String
Removes the last complex Xpath element from the path.static String
removeLastXpathElement
(String path) Removes the last Xpath element from the path.static String
removeXpath
(String path) Removes all Xpath index information from the given input path.static String
removeXpathIndex
(String path) Removes the last Xpath index from the given path.static String
simplifyXpath
(String path) Simplifies an Xpath by removing a leading and a trailing slash from the given path.splitXpath
(String xpath) Splits a content value path into its components, ignoring leading or trailing slashes.static org.dom4j.Document
unmarshalHelper
(byte[] xmlData, EntityResolver resolver) Helper to unmarshal (read) xml contents from a byte array into a document.static org.dom4j.Document
unmarshalHelper
(byte[] xmlData, EntityResolver resolver, boolean validate) Helper to unmarshal (read) xml contents from a byte array into a document.static org.dom4j.Document
unmarshalHelper
(String xmlData, EntityResolver resolver) Helper to unmarshal (read) xml contents from a String into a document.static org.dom4j.Document
unmarshalHelper
(InputSource source, EntityResolver resolver) Helper to unmarshal (read) xml contents from an input source into a document.static org.dom4j.Document
unmarshalHelper
(InputSource source, EntityResolver resolver, boolean validate) Helper to unmarshal (read) xml contents from an input source into a document.static void
validateXmlStructure
(byte[] xmlData, EntityResolver resolver) Validates the structure of a XML document contained in a byte array with the DTD or XML schema used by the document.static void
validateXmlStructure
(InputStream xmlStream, EntityResolver resolver) Validates the structure of a XML document contained in a byte array with the DTD or XML schema used by the document.static void
validateXmlStructure
(org.dom4j.Document document, String encoding, EntityResolver resolver) Validates the structure of a XML document with the DTD or XML schema used by the document.
-
Method Details
-
concatXpath
Concatenates two Xpath expressions, ensuring that exactly one slash "/" is between them.Use this method if it's uncertain if the given arguments are starting or ending with a slash "/".
Examples:
"title", "subtitle"
becomestitle/subtitle
"title[1]/", "subtitle"
becomestitle[1]/subtitle
"title[1]/", "/subtitle[1]"
becomestitle[1]/subtitle[1]
- Parameters:
prefix
- the prefix Xpathsuffix
- the suffix Xpath- Returns:
- the concatenated Xpath build from prefix and suffix
-
convertDocumentFromDom4jToW3C
public static Document convertDocumentFromDom4jToW3C(org.dom4j.Document doc) throws org.dom4j.DocumentException Converts an org.dom4j.Document to a org.w3c.dom.Document.- Parameters:
doc
- the document to convert- Returns:
- the converted document
- Throws:
org.dom4j.DocumentException
-
convertDocumentFromW3CToDom4j
Converts an org.w3c.dom.Document to an org.dom4j.Document.- Parameters:
doc
- the document to convert- Returns:
- the converted document
-
createXpath
Translates a simple lookup path to the simplified Xpath format used for the internal bookmarks.Examples:
title
becomestitle[1]
title[1]
is left untouched
title/subtitle
becomestitle[1]/subtitle[1]
title/subtitle[1]
becomestitle[1]/subtitle[1]
Note: If the name already has the format
title[1]
then provided index parameter is ignored.- Parameters:
path
- the path to get the simplified Xpath forindex
- the index to append (if required)- Returns:
- the simplified Xpath for the given name
-
createXpathElement
Appends the provided index parameter in square brackets to the given name, likepath[index]
.This method is used if it's clear that some path does not have a square bracket already appended.
- Parameters:
path
- the path append the index toindex
- the index to append- Returns:
- the simplified Xpath for the given name
-
createXpathElementCheck
Ensures that a provided simplified Xpath has the formattitle[1]
.This method is used if it's uncertain if some path does have a square bracket already appended or not.
Note: If the name already has the format
title[1]
, then provided index parameter is ignored.- Parameters:
path
- the path to get the simplified Xpath forindex
- the index to append (if required)- Returns:
- the simplified Xpath for the given name
-
getFirstXpathElement
Returns the first Xpath element from the provided path, without the index value.Examples:
title
is left untouched
title[1]
becomestitle
title/subtitle
becomestitle
title[1]/subtitle[1]
becomestitle
- Parameters:
path
- the path to get the first Xpath element from- Returns:
- the first Xpath element from the provided path
-
getLastXpathElement
Returns the last Xpath element from the provided path, without the index value.Examples:
title
is left untouched
title[1]
becomestitle
title/subtitle
becomessubtitle
title[1]/subtitle[1]
becomessubtitle
- Parameters:
path
- the path to get the last Xpath element from- Returns:
- the last Xpath element from the provided path
-
getLastXpathElementWithIndex
Returns the last Xpath element from the provided path. Examples:
title
is left untouched
title[1]/subtitle[1]
becomessubtitle[1]
- Parameters:
path
- the path to get the last Xpath element from- Returns:
- the last Xpath element from the provided path
-
getSchemaVersion
Helper method to get the version number from a schema's/content's XML document.- Parameters:
doc
- the document- Returns:
- the version (returns 0 if no version is set)
-
getXpathIndex
Returns the last Xpath index from the given path.Examples:
title
returns the empty Stringtitle[1]
returns[1]
title/subtitle
returns them empty Stringtitle[1]/subtitle[1]
returns[1]
- Parameters:
path
- the path to extract the Xpath index from- Returns:
- the last Xpath index from the given path
-
getXpathIndexInt
Returns the last Xpath index from the given path as integer.Examples:
title
returns 1title[1]
returns 1title/subtitle
returns 1title[1]/subtitle[2]
returns 2- Parameters:
path
- the path to extract the Xpath index from- Returns:
- the last Xpath index from the given path as integer
-
initSystemProperties
Initializes XML processing system properties to avoid evaluating the XML parser and reader implementation each time an XML document is read.This is done for performance improvements only.
-
isDeepXpath
Returnstrue
if the given path is a Xpath with at least 2 elements.Examples:
title
returnsfalse
title[1]
returnsfalse
title/subtitle
returnstrue
title[1]/subtitle[1]
returnstrue
- Parameters:
path
- the path to check- Returns:
- true if the given path is a Xpath with at least 2 elements
-
marshal
public static OutputStream marshal(org.dom4j.Document document, OutputStream out, String encoding) throws CmsXmlException Marshals (writes) an XML document into an output stream using XML pretty-print formatting.- Parameters:
document
- the XML document to marshalout
- the output stream to write toencoding
- the encoding to use- Returns:
- the output stream with the xml content
- Throws:
CmsXmlException
- if something goes wrong
-
marshal
Marshals (writes) an XML document to a String using XML pretty-print formatting.- Parameters:
document
- the XML document to marshalencoding
- the encoding to use- Returns:
- the marshalled XML document
- Throws:
CmsXmlException
- if something goes wrong
-
marshal
Marshals (writes) an XML node into an output stream using XML pretty-print formatting.- Parameters:
node
- the XML node to marshalencoding
- the encoding to use- Returns:
- the string with the xml content
- Throws:
CmsXmlException
- if something goes wrong
-
removeAllXpathIndices
Removes all Xpath indices from the given path.Example:
title
is left untouched
title[1]
becomestitle
title/subtitle
is left untouched
title[1]/subtitle[1]
becomestitle/subtitle
- Parameters:
path
- the path to remove the Xpath index from- Returns:
- the path with all Xpath indices removed
-
removeFirstXpathElement
Removes the first Xpath element from the path.If the provided path does not contain a "/" character, it is returned unchanged.
Examples:
title
is left untouched
title[1]
is left untouched
title/subtitle
becomessubtitle
title[1]/subtitle[1]
becomessubtitle[1]
- Parameters:
path
- the Xpath to remove the first element from- Returns:
- the path with the first element removed
-
removeLastComplexXpathElement
Removes the last complex Xpath element from the path.The same as
removeLastXpathElement(String)
both it works with more complex xpaths.Example:
system/backup[@date='23/10/2003']/resource[path='/a/b/c']
becomessystem/backup[@date='23/10/2003']
- Parameters:
path
- the Xpath to remove the last element from- Returns:
- the path with the last element removed
-
removeLastXpathElement
Removes the last Xpath element from the path.If the provided path does not contain a "/" character, it is returned unchanged.
Examples:
title
is left untouched
title[1]
is left untouched
title/subtitle
becomestitle
title[1]/subtitle[1]
becomestitle[1]
- Parameters:
path
- the Xpath to remove the last element from- Returns:
- the path with the last element removed
-
removeXpath
Removes all Xpath index information from the given input path.Examples:
title
is left untouched
title[1]
becomestitle
title/subtitle
is left untouched
title[1]/subtitle[1]
becomestitle/subtitle
- Parameters:
path
- the path to remove the Xpath index information from- Returns:
- the simplified Xpath for the given name
-
removeXpathIndex
Removes the last Xpath index from the given path.Examples:
title
is left untouched
title[1]
becomestitle
title/subtitle
is left untouched
title[1]/subtitle[1]
becomestitle[1]/subtitle
- Parameters:
path
- the path to remove the Xpath index from- Returns:
- the path with the last Xpath index removed
-
simplifyXpath
Simplifies an Xpath by removing a leading and a trailing slash from the given path.Examples:
title/
becomestitle
/title[1]/
becomestitle[1]
/title/subtitle/
becomestitle/subtitle
/title/subtitle[1]/
becomestitle/subtitle[1]
- Parameters:
path
- the path to process- Returns:
- the input with a leading and a trailing slash removed
-
splitXpath
Splits a content value path into its components, ignoring leading or trailing slashes.Note: this does not work for XPaths in general, only for the paths used to identify values in OpenCms contents.
- Parameters:
xpath
- the xpath- Returns:
- the path components
-
unmarshalHelper
public static org.dom4j.Document unmarshalHelper(byte[] xmlData, EntityResolver resolver) throws CmsXmlException Helper to unmarshal (read) xml contents from a byte array into a document.Using this method ensures that the OpenCms XML entity resolver is used.
- Parameters:
xmlData
- the XML data in a byte arrayresolver
- the XML entity resolver to use- Returns:
- the base object initialized with the unmarshalled XML document
- Throws:
CmsXmlException
- if something goes wrong- See Also:
-
unmarshalHelper
public static org.dom4j.Document unmarshalHelper(byte[] xmlData, EntityResolver resolver, boolean validate) throws CmsXmlException Helper to unmarshal (read) xml contents from a byte array into a document.Using this method ensures that the OpenCms XML entity resolver is used.
- Parameters:
xmlData
- the XML data in a byte arrayresolver
- the XML entity resolver to usevalidate
- if the reader should try to validate the xml code- Returns:
- the base object initialized with the unmarshalled XML document
- Throws:
CmsXmlException
- if something goes wrong- See Also:
-
unmarshalHelper
public static org.dom4j.Document unmarshalHelper(InputSource source, EntityResolver resolver) throws CmsXmlException Helper to unmarshal (read) xml contents from an input source into a document.Using this method ensures that the OpenCms XML entity resolver is used.
Important: The encoding provided will NOT be used during unmarshalling, the XML parser will do this on the base of the information in the source String. The encoding is used for initializing the created instance of the document, which means it will be used when marshalling the document again later.
- Parameters:
source
- the XML input source to useresolver
- the XML entity resolver to use- Returns:
- the unmarshalled XML document
- Throws:
CmsXmlException
- if something goes wrong
-
unmarshalHelper
public static org.dom4j.Document unmarshalHelper(InputSource source, EntityResolver resolver, boolean validate) throws CmsXmlException Helper to unmarshal (read) xml contents from an input source into a document.Using this method ensures that the OpenCms XML entity resolver is used.
Important: The encoding provided will NOT be used during unmarshalling, the XML parser will do this on the base of the information in the source String. The encoding is used for initializing the created instance of the document, which means it will be used when marshalling the document again later.
- Parameters:
source
- the XML input source to useresolver
- the XML entity resolver to usevalidate
- if the reader should try to validate the xml code- Returns:
- the unmarshalled XML document
- Throws:
CmsXmlException
- if something goes wrong
-
unmarshalHelper
public static org.dom4j.Document unmarshalHelper(String xmlData, EntityResolver resolver) throws CmsXmlException Helper to unmarshal (read) xml contents from a String into a document.Using this method ensures that the OpenCms XML entitiy resolver is used.
- Parameters:
xmlData
- the xml data in a Stringresolver
- the XML entity resolver to use- Returns:
- the base object initialized with the unmarshalled XML document
- Throws:
CmsXmlException
- if something goes wrong- See Also:
-
validateXmlStructure
public static void validateXmlStructure(byte[] xmlData, EntityResolver resolver) throws CmsXmlException Validates the structure of a XML document contained in a byte array with the DTD or XML schema used by the document.- Parameters:
xmlData
- a byte array containing a XML document that should be validatedresolver
- the XML entity resolver to use- Throws:
CmsXmlException
- if the validation fails
-
validateXmlStructure
public static void validateXmlStructure(org.dom4j.Document document, String encoding, EntityResolver resolver) throws CmsXmlException Validates the structure of a XML document with the DTD or XML schema used by the document.- Parameters:
document
- a XML document that should be validatedencoding
- the encoding to use when marshalling the XML document (required)resolver
- the XML entity resolver to use- Throws:
CmsXmlException
- if the validation fails
-
validateXmlStructure
public static void validateXmlStructure(InputStream xmlStream, EntityResolver resolver) throws CmsXmlException Validates the structure of a XML document contained in a byte array with the DTD or XML schema used by the document.- Parameters:
xmlStream
- a source providing a XML document that should be validatedresolver
- the XML entity resolver to use- Throws:
CmsXmlException
- if the validation fails
-