分类:
2008-05-12 13:38:28
The areas where the CL_JAVA_SCRIPT class is used can be split up as follows:
Executing JavaScript Programs
You can use the CL_JAVA_SCRIPT class to execute JavaScript programs that, for example, have been loaded from external resources or programmed in an Editor you have written yourself: The following are the methods you use, in the sequence in which they are called:
Methods 2 to 4 are combined in the method
.Binding between JavaScript and the ABAP program
JavaScript programs can access data objects and objects in ABAP programs. This technique is based on two methods:
BIND allows you to link variables and objects in JavaScript with data objects and references to objects in ABAP. Changes made in the JavaScript program also change the objects in ABAP. You can also bind objects from ABAP objects directly using BIND_INSTANCE.
You can also access the public instance attributes of bound objects and call public instance methods.
As well as binding JavaScript objects to ABAP objects, you can set or get values from JavaScript objects directly using the
and methods respectively.Debugging
If you want to include and test JavaScript programs in an ABAP development environment, the following methods are available to support you:
Handling breakpoints
Executing programs step by step
Evaluating the call stack
Example
The ABAP program DEMO_JAVA_SCRIPT_MINI_EDITOR from the Examples Library (transaction ABAPDOCU) is an example of a basic JavaScript Editor, where you can edit, execute, and test JavaScript programs. The program was created using ABAP exclusively and uses the CL_JAVA_SCRIPT class. This mini-editor is only a demonstration for the purposes of this documentation, but it shows what you can do using CL_JAVA_SCRIPT in ABAP.
******************************************
report DEMO_JAVA_SCRIPT_MINI_EDITOR.
* Selection Screen
selection-screen: begin of screen 500 title SEL_TIT as window,
begin of line.
parameters PARA(40) type C lower case.
selection-screen: end of line,
end of screen 500.
data FILE_NAME type STRING value `C:\temp\`.
data FIELD_NAME type STRING.
* Processor ------------------------------------------------------------
class EVALUATE definition.
public section.
methods CONSTRUCTOR.
methods PROCESS importing JS_SOURCE type STRING
JS_BP_LINE type I optional.
private section.
data JS_PROCESSOR type ref to CL_JAVA_SCRIPT.
endclass. "EVALUATE DEFINITION
* Class EVALUATE
class EVALUATE implementation.
*
method CONSTRUCTOR.
JS_PROCESSOR = CL_JAVA_SCRIPT=>CREATE( ).
endmethod.
*
method PROCESS.
data RETURN_VALUE type STRING.
data GET_VALUE type STRING.
JS_PROCESSOR->COMPILE(
exporting
SCRIPT_NAME = 'JAVASCRIPT.JS'
SCRIPT = JS_SOURCE ).
if JS_BP_LINE <> 0.
JS_PROCESSOR->SET_BREAKPOINT(
SCRIPT_NAME = 'JAVASCRIPT.JS'
LINE_NUMBER = JS_BP_LINE ).
endif.
RETURN_VALUE = JS_PROCESSOR->EXECUTE( 'JAVASCRIPT.JS' ).
if JS_PROCESSOR->LAST_CONDITION_CODE =
CL_JAVA_SCRIPT=>CC_BREAKPOINT.
message I888(SABAPDOCU) with 'Breakpoint in'
JS_PROCESSOR->BREAKPOINT_LINE_NUMBER.
SEL_TIT = 'Enter field name'.
PARA = FIELD_NAME.
call selection-screen 500 starting at 10 10.
if SY-SUBRC = 0.
FIELD_NAME = PARA.
clear GET_VALUE.
GET_VALUE = JS_PROCESSOR->GET( NAME = FIELD_NAME ).
if JS_PROCESSOR->LAST_CONDITION_CODE = 0.
message I888(SABAPDOCU) with FIELD_NAME ':' GET_VALUE.
JS_PROCESSOR->STEP( ).
if JS_PROCESSOR->LAST_CONDITION_CODE =
CL_JAVA_SCRIPT=>CC_BREAKPOINT.
clear GET_VALUE.
GET_VALUE = JS_PROCESSOR->GET( NAME = FIELD_NAME ).
if JS_PROCESSOR->LAST_CONDITION_CODE = 0.
message I888(SABAPDOCU) with FIELD_NAME
' after single step:'
GET_VALUE.
endif.
endif.
else.
message I888(SABAPDOCU) with FIELD_NAME ' not accessible'.
endif.
endif.
elseif JS_PROCESSOR->LAST_CONDITION_CODE <> 0.
message I888(SABAPDOCU) with JS_PROCESSOR->LAST_ERROR_MESSAGE.
else.
message I888(SABAPDOCU) with RETURN_VALUE.
endif.
JS_PROCESSOR->CLEAR_BREAKPOINTS( ).
JS_PROCESSOR->DESTROY( 'JAVASCRIPT.JS' ).
endmethod.
endclass.
* Global Data ---------------------------------------------------------
data SOURCE_LINE(80) type C.
data SOURCE_TABLE like table of SOURCE_LINE.
data SOURCE type STRING.
data AREA type ref to CL_GUI_CUSTOM_CONTAINER.
data EDITOR type ref to CL_GUI_TEXTEDIT.
data PROCESSOR type ref to EVALUATE.
data CURSOR type I.
data ROW type I.
* ----------------------------------------------------------------------
initialization.
SOURCE_LINE = 'var string = "Hello ABAP";'.
append SOURCE_LINE to SOURCE_TABLE.
SOURCE_LINE = 'function Set_String()'.
append SOURCE_LINE to SOURCE_TABLE.
SOURCE_LINE = ' {'.
append SOURCE_LINE to SOURCE_TABLE.
SOURCE_LINE = ' string += ", this";'.
append SOURCE_LINE to SOURCE_TABLE.
SOURCE_LINE = ' string += " is";'.
append SOURCE_LINE to SOURCE_TABLE.
SOURCE_LINE = ' string += " JavaScript!";'.
append SOURCE_LINE to SOURCE_TABLE.
SOURCE_LINE = ' }'.
append SOURCE_LINE to SOURCE_TABLE.
SOURCE_LINE = 'Set_String();'.
append SOURCE_LINE to SOURCE_TABLE.
SOURCE_LINE = 'string;'.
append SOURCE_LINE to SOURCE_TABLE.
start-of-selection.
call screen 100.
* Dialog Modules -------------------------------------------------------
module STATUS_0100 output.
set pf-status 'SCREEN_100'.
set titlebar 'SCREEN_100'.
if AREA is initial.
create object AREA exporting CONTAINER_NAME = 'EDIT_CONTROL'.
endif.
if EDITOR is initial.
create object EDITOR exporting PARENT = AREA.
endif.
if PROCESSOR is initial.
create object PROCESSOR.
endif.
EDITOR->SET_WORDWRAP_BEHAVIOR(
exporting
WORDWRAP_MODE = 2
WORDWRAP_POSITION = 80 ).
EDITOR->SET_TEXT_AS_R3TABLE(
exporting
TABLE = SOURCE_TABLE ).
if ROW <> 0.
EDITOR->HIGHLIGHT_LINES(
exporting
FROM_LINE = ROW
TO_LINE = ROW ).
endif.
endmodule.
*
module USER_COMMAND_0100 input.
EDITOR->GET_TEXT_AS_R3TABLE(
importing
TABLE = SOURCE_TABLE ).
clear SOURCE.
loop at SOURCE_TABLE into SOURCE_LINE.
if SOURCE is initial.
SOURCE = SOURCE_LINE.
else.
concatenate SOURCE SOURCE_LINE into SOURCE
separated by CL_ABAP_CHAR_UTILITIES=>CR_LF.
endif.
endloop.
case SY-UCOMM.
when 'CANCEL'.
leave program.
when 'EXECUTE'.
PROCESSOR->PROCESS(
exporting
JS_SOURCE = SOURCE
JS_BP_LINE = ROW ).
when 'BREAKPOINT'.
EDITOR->GET_SELECTION_POS(
importing
FROM_LINE = CURSOR ).
if CURSOR <> ROW.
ROW = CURSOR.
else.
ROW = 0.
endif.
when 'JSLOAD'.
SEL_TIT = 'Enter file name'.
PARA = FILE_NAME.
call selection-screen 500 starting at 10 10.
if SY-SUBRC = 0.
FILE_NAME = PARA.
call function 'GUI_UPLOAD'
exporting
FILENAME = FILE_NAME
tables
DATA_TAB = SOURCE_TABLE
exceptions
others = 4.
if SY-SUBRC <> 0.
message I888(SABAPDOCU) with 'Error when loading file'.
endif.
endif.
when 'JSDUMP'.
SEL_TIT = 'Enter file name'.
PARA = FILE_NAME.
call selection-screen 500 starting at 10 10.
if SY-SUBRC = 0.
FILE_NAME = PARA.
call function 'GUI_DOWNLOAD'
exporting
FILENAME = FILE_NAME
tables
DATA_TAB = SOURCE_TABLE
exceptions
others = 4.
if SY-SUBRC <> 0.
message I888(SABAPDOCU) with 'Error when saving file'.
endif.
endif.
endcase.
clear SY-UCOMM.
endmodule.