API-icon.png

Script Libraries

Often the script code in more complex applications will very quickly become confusing, especially if methods declarations are used. These can now be managed separately from the actual scripts in a so-called script library. This script library is basically a simple file called “script.library”, which hast to be located in the directory of Unknown macro: jbeam-sign-Jars. This script library file can now contain all the necessary and frequently used method implementations for scripts. At runtime, the file “script.library” is read and placed directly in front of the actual script code. Thus, in the scripts, these methods can be used without restriction. When using the Java Webstart script.library can also remain on the server (precisely in the folder in which the Unknown macro: jbeam-sign Jar file is located, e.g. in the codebase of the JNLP files). This also enables a separation of the script code from multiple scripts into a single, easily maintainable file.

An Example: You want to write scripts, in which first data be imported in Unknown macro: jbeam-sign. They need also a section of code which implements this import task.

FileImport_Diadem diademImporter = jC.newComponent(jbComponentIF.DIADEM_FILE);
diademImporter.setName("MyImporter");
// set import file
diademImporter.setImportFile(new File("C:/MyData.dat"));
// import data
jC.validateFramework(true);

To improve the structure of scripts, you can outsource this section of code in a method, because you can simply define methods at any point of the script.

// imports the given diadem file and sets the importer name to the given name
void importDiademFile(String importFile, String name) {
// create import
FileImport_Diadem diademImporter = jC.newComponent(jbComponentIF.DIADEM_FILE);
 diademImporter.setName(name);
// set import file
diademImporter.setImportFile(new File(importFile));
// import data
jC.validateFramework(true);
}

Let's say you have such a method “import file diadem (String pathToData)” implemented and call on them at some point in your script. Now you can write this simple method definition in a file called “script.library”, move them into your Unknown macro: jbeam-sign folder and save it. Thus your script contains no such method more, but will still run properly, becaise the method is defined in the script library. The call in your script would be the following:

importDiademFile("C:/MyData.dat", "MyImporter");

Thus you have reduced the number of lines of your script from 6 to 1 and you can now access in each of your scripts on this method. You can also edit the code easier because you have to change only the method in your script library for you to change any of your scripts.

Frequently, entire applications can be divided into individual steps and can be simplified by the script library. Typically the script library defines only methods, that are used in a large part of your scripts. In future, the complete sequence of an evaluation by a script look like this:

// imported data
importDiademFile("C:/MyData.dat", "MyImporter");
// load created Layout/Report
loadLayout("C:/MyLayout.blf");
// export Report as PDF file
exportPDF("C:/Report.pdf");

Of course the above code requires the creation of the “script.library” file with all the methods used in the script.


Copyright by Kistler Chemnitz GmbH 2021
XWiki Enterprise 4.4 - Documentation