You can code JavaScript directly in your ABAP code.
If you want to do some JavaScript tests in ABAP there is a good program to use : DEMO_JAVA_SCRIPT_MINI_EDITOR. When you run this program, you can code your Java in an ABAP editor, compile and execute it to test the result.
Now, here is a small example on how to insert JavaScript in ABAP:
- Code: Select all
*&---------------------------------------------------------------------*
*& Report ZDANY_JAVASCRIPT_TEST
*&---------------------------------------------------------------------*
REPORT ZDANY_JAVASCRIPT_TEST.
data: l_JS_PROCESSOR type ref to CL_JAVA_SCRIPT,
l_RETURN_VALUE type STRING,
l_SOURCE type STRING.
* Creation of a new javaScript
l_JS_PROCESSOR = CL_JAVA_SCRIPT=>CREATE( ).
* this is the Javascript code
concatenate
'var l_source = "Hello World,"; '
'l_source += " I''m"; '
'l_source += " JavaScript!"; '
'l_source; '
into l_SOURCE separated by CL_ABAP_CHAR_UTILITIES=>CR_LF.
* we compile the JavaScript code
l_JS_PROCESSOR->COMPILE( SCRIPT_NAME = 'DANYTEST.JS' SCRIPT = L_SOURCE ).
* Any syntax error ?
if l_JS_PROCESSOR->LAST_CONDITION_CODE <> 0.
write: / 'Error in COMPILE', l_JS_PROCESSOR->LAST_ERROR_MESSAGE.
exit.
else.
write / 'JavaScript was compiled'.
endif.
* Let's go and run the script
l_JS_PROCESSOR->EXECUTE( SCRIPT_NAME = 'DANYTEST.JS' ).
* Any problem during execution ?
if l_JS_PROCESSOR->LAST_CONDITION_CODE <> 0.
write: / 'Error in EXECUTE',l_JS_PROCESSOR->LAST_ERROR_MESSAGE.
exit.
else.
write / 'JavaScript was executed'.
endif.
* we read the output variable
l_RETURN_VALUE = l_JS_PROCESSOR->EVALUATE( JAVA_SCRIPT = 'l_source;' ).
write : / l_RETURN_VALUE.
(Courtesy Dany Charbonneau)
Here is another example that uses the Regex search functionality in Javascript:
- Code: Select all
*Eject
*&---------------------------------------------------------------------*
*& Form Check_Regex
*&---------------------------------------------------------------------*
* text This form checks a characteristic value against a Regular
* expression
*----------------------------------------------------------------------*
* -->pu_Charact The characteristic to check the list for
* -->pu_Value The actual value of the characteristic
* <--pc_Valid If the characteristic is within the limits above then
* this will be true, otherwise false.
*----------------------------------------------------------------------*
Form Check_Regex using pu_RegEx type ZEP03_Regular_Expression
pu_Value type ZEP03_Characteristic_Value
Changing pc_Valid type Boolean.
*
Data: w_Modifier Type String,
w_Last_Index Type I,
w_Leftcontext Type String,
w_Rightcontext Type String,
w_Index Type I,
w_Found Type Boolean,
w_Return_Value Type String,
w_Error_Message Type String,
w_Source Type String,
w_Match Type ZEP03_Match,
t_Match Type standard table of ZEP03_Match,
o_Javascript Type Ref To cl_java_script.
Clear pc_Valid.
*
* Do the Regex.
*
o_Javascript = cl_java_script=>create( ).
o_Javascript->bind( Exporting name_obj = ' '
name_prop = 'regex'
Changing data = pu_Regex ).
o_Javascript->bind( Exporting name_obj = ' '
name_prop = 'searchstring'
Changing data = pu_Value ).
o_Javascript->bind( Exporting name_obj = ' '
name_prop = 'modifier'
Changing data = w_modifier ).
o_Javascript->bind( Exporting name_obj = ' '
name_prop = 'Index'
Changing data = w_Index ).
o_Javascript->bind( Exporting name_obj = 'abap'
name_prop = 'Match'
Changing data = t_Match ).
o_Javascript->bind( Exporting name_obj = ' '
name_prop = 'Last_Index'
Changing data = w_Last_Index ).
o_Javascript->bind( Exporting name_obj = ' '
name_prop = 'Leftcontext'
Changing data = w_Leftcontext ).
o_Javascript->bind( Exporting name_obj = ' '
name_prop = 'Rightcontext'
Changing data = w_Rightcontext ).
o_Javascript->bind( Exporting name_obj = ' '
name_prop = 'Found'
Changing data = w_Found ).
w_Match-comp = ' '.
append w_Match to t_Match.
CONCATENATE 'var re = new RegExp(regex, modifier);'
''
'var m = re.exec(searchstring);'
' if (m == null) {'
' Found = false;'
' } else {'
' Found = true; '
' Index = m.Index;'
' Last_Index = m.Last_Index;'
' Leftcontext = m.Leftcontext;'
' Rightcontext = m.righContext; '
' var len = abap.Match.length;'
' for (i = 0; i < m.length; i++) {'
' abap.Match[len-1].comp = m[i];'
' abap.Match.appendLine();'
' len++;'
' }'
'}'
Into w_source Separated By cl_abap_char_utilities=>cr_lf.
w_Return_Value = o_Javascript->evaluate( w_source ).
w_Error_Message = o_Javascript->Last_Error_Message.
Free o_Javascript.
*
If w_Error_Message is Initial.
Move c_True to pc_Valid.
EndIf.
*
Endform. " Regex
This is adapted from some code found here.

