The JavaScript engine supports the following text data formats out-of-the-box:
For each format, the engine provides two APIs:
- An API to convert a string to a format-specific object that can be easily inspected and modified
- An API to convert a format-specific object to a string
These APIs can be used in conjunction with file operation APIs to read and write files in the above formats.
XML Support
The agent's JavaScript engine implements ECMA-357 ECMAScript for XML (E4X). JavaScript has built-in APIs to manipulate XML data. For example, the following code constructs an XML object from a string, modifies the object, and converts the modified object back to a string:
var xmlString = "<employees>" +
"<employee department=\"HR\"><name>John</name><age>25</age></employee>" +
"<employee department=\"IT\"><name>Sue</name><age>32</age></employee>" +
"<employee department=\"HR\"><name>Anne</name><age>28</age></employee>" +
"</employees>";
var employees = new XML(xmlString);
employees.employee[0].age = 26;
mobicontrol.log.info(employees.toString());
Accessing XML attributes
Attributes are properties of their parent elements. Attribute values can be accessed by preceding an attribute name with at-sign (@). For example, this will log the department of the first employee:
mobicontrol.log.info(employees.employee[0].@department);
To log department attributes of all employees, concatenated with a comma, the following script can be used. It will log HR,IT,HR to the deployment server:
var departments = [];
for each (var department in employees.employee.@department) {
departments.push(department)
}
mobicontrol.log.info(departments.join(','));
A set of elements with a particular attribute value can be accessed using element.(@attribute == value) predicate. For example, the following statement returns employees that work for HR department:
employees.employee.(@department == "HR");
Retrieving contained elements
The XML object provides methods that allow you to retrieve elements contained at various levels of the tree:
XML.children() gets the direct child elements, including text elements.
XML.elements() gets the direct child elements that are XML tags, but does not get text.
XML.descendants() allows to match a specific tag, and gets all matching elements at any level of nesting. A double dot notation can be also used to access descendants of an element. For example, the following code logs all employees' names to deployment server (the expected log is John,Sue,Anne):
var xmlString = "<employees>" +
"<employee department=\"HR\"><name>John</name><age>25</age></employee>" +
"<employee department=\"IT\"><name>Sue</name><age>32</age></employee>" +
"<employee department=\"HR\"><name>Anne</name><age>28</age></employee>" +
"</employees>";
var employees = new XML(xmlString);
var names = [];
for each (var name in employees..name) {
names.push(name)
}
mobicontrol.log.info(names.join(','));
INI Support
The agent's JavaScript engine integrates INI 1.3.5 module as a system module. To manipulate INI data, scripts must require the 'ini' module to obtain the INI parser and serializer functionality. For example, the following code constructs an INI object from a string, modifies the resulting object, and converts the modified object back to a string:
var ini = require('ini');
var iniString =
'[database]\n' +
'user = dbuser\n' +
'password = dbpassword\n' +
'database = use_this_database\n';
var config = ini.parse(iniString);
config.database.user = 'jdoe';
mobicontrol.log.info(ini.stringify(config));
JSON Support
JavaScript has built-in APIs to manipulate JSON data. For example, the following code constructs a JavaScript object from a JSON-formatted string, modifies the object, and converts the modified object back to a JSON-formatted string:
var text = '{"employee":[' +
'{"firstName":"John","lastName":"Doe" },' +
'{"firstName":"Anna","lastName":"Smith" },' +
'{"firstName":"Peter","lastName":"Jones" }]}';
var employeeList = JSON.parse(text);
employeeList.employee[1].lastName = "Brooks";
mobicontrol.log.info(JSON.stringify(employeeList));
The same JavaScript object can be constructed directly, with no need for the initial string and JSON.parse
:
var employeeList = {};
employeeList.employee = [
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]
employeeList.employee[1].lastName = "Brooks";
mobicontrol.log.info(JSON.stringify(employeeList));
JSON Serialization tutorial explains how to serialize and deserialize instances of JavaScript classes.
Reading and Writing Text Files
The JavaScript engine allows you to read from, and write to, the supported text data format files. mobicontrol.File.readText reads a UTF-8 encoded text file into a string, and mobicontrol.File.writeText writes a string into a text file. The above APIs can be used in conjunction with format-specific conversion APIs to process files of these specific formats. The following example builds on the JSON example above, creating a JavaScript object that contains a list of employees. It then reads from the file, modifies the list, and writes it back into the file:
var employeeList = {};
employeeList.employee = [
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]
var jsonFile = new mobicontrol.io.File(mobicontrol.storage.internal.dataDirectory + '/employeeList.json');
jsonFile.writeText(JSON.stringify(employeeList));
employeeList = JSON.parse(jsonFile.readText());
employeeList.employee[1].lastName = "Brooks";
jsonFile.writeText(JSON.stringify(employeeList));