Skip to content

Utils

Purpose

Provides utility methods.

Methods

Binding name: p6.utils

Method: String p6.utils.escapeXml(String xmlStr)

Escape the given XML string.


Method: XPath p6.utils.getXPath()

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


Method: String p6.utils.unescapeXml(String escStr)

Unescape the given escaped XML string.


Method: String p6.utils.pause()

Pauses the execution until Platform 6 is stopped or the user manually stops the script via the UI.


Method: String jsonToString(Map<?, Object> json)

Convert the JSON to a stringify and pretty print version (Converted using JsonOutput toJson and prettyPrint)


Method: Object stringToJson(String json)

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


Method: void extractZip(byte[] zip, Closure<Void> closure)

Extract a zip file and pass the name and the zip entry byte array to the closure.

Warning

The name contains the path file (i.e. folder/subfolder/file.ext)

Note

This extraction method is protected against zip bomb attack


Method: String performDateTimeExpansion(Calendar cal, String expression)

Performs an expansion of the date based on the expression. 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.espaceXml("<root><node>value</node></root>")

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

println p6.utils.pause()

println p6.utils.jsonToString([
  names: ["fee", "foo", "bar"],
  values: [1, 2]
])

println p6.utils.stringToJson("{\"names\": [\"fee\", \"foov, \"bar\"], \"values\": [1, 2]}")

def zip = [] as byte[]
p6.utils.extractZip(zip)  { name, content ->
    println("Extracting File:" + name);
    new File('p6file://${P6_DATA}/path/to/' + name).withOutputStream { stream ->
        stream.write(content);
    }
};

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