Utils
Purpose¶
Provides utility methods.
Methods¶
Binding name: p6.utils
escapeXml¶
Deprecated
Since 6.10.7, this method has been moved to the xml dsl
Escape the given XML string.
Syntax
String p6.utils.escapeXml(String xmlStr)
Example
p6.log.debug p6.utils.espaceXml('<root><node>value</node></root>')
getXPath¶
Deprecated
Since 6.10.7, this method has been moved to the xml dsl
Provide a javax.xml.xpath.XPath
using the Saxon factory.
Syntax
XPath p6.utils.getXPath()
Example
def xpath = p6.utils.getXPath()
unescapeXml¶
Deprecated
Since 6.10.7, this method has been moved to the xml dsl
Unescape the given escaped XML string.
Syntax
String p6.utils.unescapeXml(String escStr)
Example
p6.log.debug p6.utils.unescapeXml('<root><node>value</node></root>')
pause¶
Pauses the execution until Platform 6 is stopped or the user manually stops the script via the UI.
Syntax
String p6.utils.pause()
Example
p6.log.debug p6.utils.pause()
jsonToString¶
Deprecated
Since 6.10.7, this method has been moved to the json dsl
Convert the JSON to a stringify and pretty print version (Converted using JsonOutput toJson and prettyPrint)
Syntax
String p6.utils.jsonToString(Map<?, Object> json)
Example
p6.log.debug p6.utils.jsonToString([
names: ['fee', 'foo', 'bar'],
values: [1, 2]
])
stringToJson¶
Deprecated
Since 6.10.7, this method has been moved to the json dsl
Convert the string to a JSON (Converted using JsonSlurper.parseText)
Syntax
Object p6.utils.stringToJson(String json)
Example
p6.log.debug p6.utils.stringToJson('{"names": ["fee", "foo", "bar"], "values": [1, 2]}')
extractZip¶
Extract a zip file and pass the name and the zip entry byte array to the closure. The method returns the list of the files not expanded
Syntax
List<String> p6.utils.extractZip(byte[] zip, Closure<Void> closure)
Warning
The name contains the path file (i.e. folder/subfolder/file.ext
)
Note
This extraction method is protected against zip bomb attack
Example
def zip = [] as byte[]
List<String> excluded = p6.utils.extractZip(zipFile) { name, content ->
final Path path = Paths.get(new URL('p6file://${P6_DATA}/resources/path/to/' + name).toURI())
Files.createDirectories(path.getParent())
Files.write(path, content.toByteArray())
return
}
if (!excluded.isEmpty()) {
p6.log.debug 'Files not expanded: ' + excluded
}
performDateTimeExpansion¶
Performs an expansion of the date based on the expression.
Syntax
String p6.utils.performDateTimeExpansion(Calendar cal, String expression)
Expansion mechanism
Substring parts matching the syntax ${DATE<mod>}
or ${TIME<mod>}
will be replaced by the expanded version.
<mod>
is a succession of operations to add +
or subtract -
a number of units:
s
, for secondsm
, for minutesh
, for hoursd
, for daysM
, for monthsy
, for years
Based on the operation, DATE
or TIME
a datetime or a timestamp will be returned.
You can have multiple operations for a date or a time and even multiple occurrences inside a single expression.
Note
Dates are using the format defined in p6.service.counters.date.format
Dates are using the default timezone unless one is defined for p6.service.counters.date.timezone
Examples
p6.log.debug p6.utils.performDateTimeExpansion(Calendar.now(), 'Due date: ${DATE+1M+5d-1h}')
# Due date: 20220428T11:34:57.12
p6.log.debug p6.utils.performDateTimeExpansion(Calendar.now(), 'Time: ${TIME-2d+1h}')
# Time: 1368268497012
p6.log.debug p6.utils.performDateTimeExpansion(Calendar.now(), 'Next month: ${DATE+1M} / In half an hour ${TIME+30m}')
# Next month: 20220428T11:34:57.12 / In half an hour 1368268497012
createTempFolder¶
Since 6.9.8
create a temporary folder with the prefix name.
Syntax
File p6.utils.createTempFolder(String name)
Note
If it fails to create it, throws P6Exception
Example
groovy
p6.log.debug p6.utils.createTempFolder('temp')
createTempFile¶
Since 6.9.8
create a temporary file with the prefix and suffix.
Syntax
File p6.utils.createTempFile(String prefix, String suffix)
Note
If it fails to create it, throws P6Exception
Example
p6.log.debug p6.utils.createTempFile('test', '.txt')
deobfuscate¶
Since 6.10.12
Deobfuscate a string using the P6 deobfuscation algorithm.
Syntax
String p6.utils.deobfuscate(String data)
Example
p6.log.debug p6.utils.deobfuscate('p6.obf:J5R7rNZHcypAwTWiV60dcw')