Skip to content

Utils

Purpose

Provides utility methods.

Methods

Binding name: p6.utils

escapeXml

Escape the given XML string.

Syntax

String p6.utils.escapeXml(String xmlStr)
Example
println p6.utils.espaceXml("<root><node>value</node></root>")

getXPath

Provide a javax.xml.xpath.XPath using the Saxon factory.

Syntax

XPath p6.utils.getXPath()
Example
def xpath = p6.utils.getXPath()

unescapeXml

Unescape the given escaped XML string.

Syntax

String p6.utils.unescapeXml(String escStr)
Example
println p6.utils.unescapeXml("&lt;root&gt;&lt;node&gt;value&lt;/node&gt;&lt;/root&gt;")

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
println p6.utils.pause()

jsonToString

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
println p6.utils.jsonToString([
  names: ["fee", "foo", "bar"],
  values: [1, 2]
])

stringToJson

Convert the string to a JSON (Converted using JsonSlurper.parseText)

Syntax

Object p6.utils.stringToJson(String json)
Example
println p6.utils.stringToJson("{\"names\": [\"fee\", \"foov, \"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()) {
  println "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 seconds
  • m, for minutes
  • h, for hours
  • d, for days
  • M, for months
  • y, 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
println p6.utils.performDateTimeExpansion(Calendar.now(), "Due date: ${DATE+1M+5d-1h}")
# Due date: 20220428T11:34:57.12
println p6.utils.performDateTimeExpansion(Calendar.now(), "Time: ${TIME-2d+1h}")
# Time: 1368268497012
println 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