Project

Contents

Issue #00005425

New WGA.File() Utility object
Feature/Improvement

For imports and exports you often need access to files in the servers file system.

You can do this using java.io.File objects together with some FileReaders and FileWriters and Input-/Output Streams ...

As a TMLScript developer you wound do this.

We now added a utility object

WGA.File(filename|file-object)

to handle files. WGA.File() creates a WGAFile object that implements some useful utility method to read from and write to files.

Note: All files are treated relative to the OpenWGA config folder. If you need to assess folders outside we advise to create a symbolic link in the WGA config folder to do this.

The default encoding if not explicitly specified is the configured OpenWGA output encoding (normally UTF-8).

Currently we implemented the following methods:

WGAFile.asString([encoding])

Returns the file content as String.

WGAFile.asXMLDocument([encoding])

Reads the file content as XML and returns a DOM4J Document.

WGAFile.write(string [, encoding])
WGAFile.write(XMLDocument [, encoding])

Writes the given String or DOM4J Document to the file.

WGAFile.append(string [, encoding])

Appends the given String to the end of the file.

WGAFile.copy(InputStream)
WGAFile.copy(WGDocument, filename_to_copy)
WGAFile.copy(TMLContext, filename_to_copy)

Copies filedata from an imput steam or content attachment to the file.

WGAFile.listFiles([filter])

Returns a directory listing as list of WGAFile objects. The optional filter argument can be a JS-Function that is called with the current java.io.File object as argument. It must return true or false.

WGAFile.getFile()
WGAFile.setFile(file)

Returns or sets the wrapped java.io.File object.

Samples:

var json_text = WGA.File("data.json").asString();
var data = JSON.parse(json_text)
// reads a json object from a file

var doc = context("name:home");
WGA.File("export.html").copy(doc, "mail.html");
// exports a content attachment "mail.html" to the file system as file "export.html"

var files = WGA.File("import").listFiles();
for(let file in Iterator(files){
    var xml = file.asXMLDocument();
    // process the xml ...
}
//Processes a list of XML files in the file system from folder "import"