Welcome Guest! Log in
Due to some maintenance operations, stambia.org will be switched to read-only mode during the 13th November. It will be possible to read and download, but impossible to post on the forums or create new accounts. For any question please contact the support team.


It is possible, when using a File metadata, to transform the content of the file "on the fly" using javascript. The file will then be read differently by the mappings, processes, or when using a "consult data". 

There are different ways to transform the file, either by using FILE scripting,  LINE scripting, or both. When using both, the FILE scripting is applied before the LINE scripting.

 

Transformation File

 

This script will be applied to the whole file, and the file is read character by character.

The two objects we are going to use are input/output streams :

  • __in__

This object contains the whole file content.

The method we are going to use is : read()

This method uses the file cursor and returns a char at the current position. If no char is found, returns -1. 

  • __out__

This object will contain the output for the transformed file.

The method we are going to use is : write(char)

It will append the char to the output.

 

Example of a simple file script that replaces every whitespace with an underscore :

 

i=0;
do{
i=__in__.read();
if (i>-1){
ch=String.fromCharCode(i);
if (ch==' ')
ch='_';
i=ch.charCodeAt();
__out__.write(i);
}
}while(i>-1);

 

 

Transformation Line

 

The script will be run for each line of the file.

The available objects are:

  • __string__
    It is used as a string in the script, we can use functions on it like on any other string.
    In order to write the transformed output, just call the variable by its name.
  • __position__
    This is the number of the current original line.

Transformation Line Example #1: transforms everything in uppercase

__string__.toUpperCase();

 

Transformation Line Example #2: split a single line into several lines on the fly

Original file:

CUS_ID;ANSWER1;ANSWER2;ANSWER3
1;"Yes";"Not sure";"Probably"
2;"Never";;

Result:

CUS_ID;QUESTION_NO;ANSWER
1;1;"Yes"
1;2;"Not sure"
1;3;"Probably"
2;1;"Never"

Script (applied for each original line):

/* this array will hold the lines to be returned */
ret=new Array(); if (__position__==0){
    /* header line is changed */
    ret[0] = "CUS_ID;QUESTION_NO;ANSWER";
}else{
    /* split data lines */
    line = __string__.split(";");
    lno = 0;
    
    for (qno=1 ; qno<=3 ; qno++) {
        if (line[qno]!="") {
            ret[lno++] = line[0] + ";" + qno + ";" + line[qno];
        }
    }
}
/* return array */
ret;

File wizard for testing  

 

 In the metadata window, right click on the file -> actions -> Properties.

WIZARD

 

 

Language specified in the metadata

 

The scripting language (rhino/javascript) specified in the metadata has to be accepted by the java version of the production Runtime.

On the other hand, the wizard will work only if the java version of your designer accepts the specified language.

 

You have no rights to post comments

Articles

Suggest a new Article!