Code20070108 - ooxxvv/MySAPnotes GitHub Wiki

Code20070108

  • 可用來將系統中的程式碼 download 成 HTML 的格式。
  • 只適用於 4.7 以上的系統。

Code

REPORT  zjoey_20070108.

*----------------------------------------------------------------------*
*       ABAP Code WebViewer Beta Version - Date - 20.03.2005
*----------------------------------------------------------------------*
*       Written By: Ram Manohar Tiwari 				
*----------------------------------------------------------------------*
*       Presented By:  http://www.rmtiwari.com
*----------------------------------------------------------------------*
*  This utility tool converts the ABAP Report / FM Code into an html
*  file and downloads it on the chosen directory of your PC.
*  Further,developers can share the code on websites by directly
*  sending it in an HTML format.
*  The code downloaded by this utility is more readable on web for an
*  ABAP developer as:
*      1. It retains the blue color of comments and documentation with
*         the code and hence HTML version looks exactly like the ABAP
*         editor code.
*
*      2. The generated HTML will be search engine friendly as it will
*         have its own title and meta search keywords. This way search
*         engines can directly find the code on your site. Code Sharer
*         has the option of providing his own < title>  suitable for the
*         program and relevant meta keywords to find it on web.
*
*      3. Just like ABAP editor, viewers can click on a variable to
*         reach its directly at its DATA definition.
*
*      4. Similarly, viewers can click on PERFORM calls to reach
*         directly at the defined FORM routine.
*-----------------------------------------------------------------------
*  This utility is developed on MiniSAP and should be compatible with
*  SAP4.7 version. Though with slight modifications to remove
*  the value added aspects will make it compatible with any versions of
*  SAP.
*-----------------------------------------------------------------------

* SELECTION SCREEN
*-----------------------------------------------------------------------
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS : p_prog    TYPE trdir-name,
             p_progf   TYPE tfdir-funcname.
SELECTION-SCREEN END OF BLOCK b1 .

SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-002.

SELECTION-SCREEN BEGIN OF LINE.
PARAMETERS: p_outdir LIKE rlgrap-filename OBLIGATORY.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK b2.

SELECTION-SCREEN BEGIN OF BLOCK b3 WITH FRAME TITLE text-003.

PARAMETERS: p_desc TYPE pfeline.

PARAMETERS: p_keyw TYPE pfeline.

SELECTION-SCREEN END OF BLOCK b3 .

*----------------------------------------------------------------------*
* Table declaration
*----------------------------------------------------------------------*
TABLES: tfdir,
        trdir.

*-----------------------------------------------------------------------

DATA : gv_directory LIKE rlgrap-filename,
       gv_file_name TYPE string,
       gv_html_file_name TYPE trdir-name.
DATA : BEGIN OF gt_html OCCURS 0,
        rec(200) TYPE c,
       END OF gt_html.

DATA : gv_html_wa        LIKE LINE OF gt_html.

*----------------------------------------------------------------------*
* Type declaration
*----------------------------------------------------------------------*
* Type for report
TYPES: BEGIN OF ty_report_wo_index,
       line(300),
       END OF ty_report_wo_index.

* Type for Report with line index
TYPES: BEGIN OF ty_report_with_index,
       index TYPE i,
       line(300),
       END OF ty_report_with_index.

*----------------------------------------------------------------------*
* CONSTANTS                                                            *
*----------------------------------------------------------------------*
CONSTANTS : gc_false(1)     TYPE c VALUE ' ',
            gc_true(1)      TYPE c VALUE 'X',
            gc_quotes1(1)   TYPE c VALUE '''',
            gc_asterik(1)   TYPE c VALUE '*',
            gc_comma(1)     TYPE c VALUE ',',
            gc_quotes(1)    TYPE c VALUE '"',
            gc_percent(1)   TYPE c VALUE '%'.

*----------------------------------------------------------------------*
* Data declaration
*----------------------------------------------------------------------*

DATA : gt_rep_table    TYPE  STANDARD TABLE OF ty_report_wo_index WITH
                            NON-UNIQUE DEFAULT KEY INITIAL SIZE 500,
       gv_rep_wa       TYPE ty_report_wo_index,
       gv_rep_wa_next  TYPE ty_report_wo_index.


DATA : gv_prog_name    TYPE trdir-name.
*-----------------------------------------------------------------------
* ALV stuff
TYPE-POOLS: slis.
DATA: gt_fieldcat TYPE slis_t_fieldcat_alv,
      gs_layout   TYPE slis_layout_alv,
      gt_sort     TYPE slis_t_sortinfo_alv.

*----------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_prog.
  PERFORM get_report_name USING    'P_PROG'
                          CHANGING p_prog.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_progf.
  PERFORM get_report_name USING    'P_PROGF'
                          CHANGING p_progf.


AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_outdir.
  PERFORM get_directory_name USING    'P_OUTDIR'
                             CHANGING p_outdir.

AT SELECTION-SCREEN.
  PERFORM selection_screen_validations.

START-OF-SELECTION.

* read the report code lines in an internal table
  READ REPORT gv_prog_name INTO gt_rep_table.
  IF sy-subrc <>  0.
    MESSAGE e001(aq) WITH
           'Code is blank'.
    EXIT.
  ENDIF.


* Read the lines of report one by one and convert to HTML.
  PERFORM convert_code_to_html TABLES gt_rep_table
                                      gt_html.



  PERFORM prepare_file_name   USING   p_outdir
                                      gv_html_file_name
                           CHANGING   gv_file_name.

  PERFORM download_html_file_on_pc TABLES    gt_html
                                    USING    gv_file_name.

  PERFORM show_html_file USING gv_file_name.



END-OF-SELECTION.

*&---------------------------------------------------------------------*
*&      Form  get_report_name
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->   p1        text
*  < --  p2        text
*----------------------------------------------------------------------*

FORM get_report_name USING    x_report_field_name
                     CHANGING y_report_name.


  PERFORM read_screen_values USING    x_report_field_name
                             CHANGING y_report_name.

  IF x_report_field_name EQ 'P_PROG'.

    CALL FUNCTION 'REPOSITORY_INFO_SYSTEM_F4'
      EXPORTING
        object_type           = 'PROG'
        object_name           = y_report_name
        suppress_selection    = 'X'
        without_personal_list = ' '
      IMPORTING
        object_name_selected  = p_prog
      EXCEPTIONS
        cancel                = 01.

  ELSEIF x_report_field_name EQ 'P_PROGF'.
    CALL FUNCTION 'REPOSITORY_INFO_SYSTEM_F4'
      EXPORTING
        object_type           = 'FUNC'
        object_name           = y_report_name
        suppress_selection    = ' '
        without_personal_list = 'X'
      IMPORTING
        object_name_selected  = p_progf
      EXCEPTIONS
        cancel                = 01.
  ENDIF.

ENDFORM.                    " get_directory_name

*&---------------------------------------------------------------------*
*&      Form  get_file_name
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->   p1        text
*  < --  p2        text
*----------------------------------------------------------------------*

FORM get_directory_name USING    x_file_field_name
                     CHANGING    y_directory_name.

  CLASS cl_gui_frontend_services DEFINITION LOAD.
  DATA : lv_file_path TYPE rlgrap-filename,
         lv_file_name TYPE rlgrap-filename,
         lv_outdir    TYPE string,
         lv_directory TYPE string.

  CONSTANTS : lc_window_title TYPE string
                        VALUE 'Select HTML download folder'.

  PERFORM read_screen_values USING    x_file_field_name
                             CHANGING y_directory_name.

  lv_directory = y_directory_name.

  CALL METHOD cl_gui_frontend_services=>directory_browse
    EXPORTING
      window_title         = lc_window_title
      initial_folder       = lv_directory
    CHANGING
      selected_folder      = lv_outdir
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3.



  IF sy-subrc EQ 0.
    p_outdir = lv_outdir.
  ENDIF.
ENDFORM.                    " get_directory_name
*&--------------------------------------------------------------------*
*&      Form  read_screen_values
*&--------------------------------------------------------------------*
*       text
*---------------------------------------------------------------------*
*      --> P_FIELD    text
*      --> P_VALUE    text
*---------------------------------------------------------------------*

FORM read_screen_values USING    x_field
                        CHANGING y_value.

  DATA: lv_dynpname LIKE d020s-prog,
        lv_dynpnumb LIKE d020s-dnum.

  DATA: BEGIN OF lt_dynpvaluetab OCCURS 1.
          INCLUDE STRUCTURE dynpread.
  DATA: END   OF lt_dynpvaluetab.

  lv_dynpname = sy-repid.
  lv_dynpnumb = sy-dynnr.

  REFRESH lt_dynpvaluetab.
  lt_dynpvaluetab-fieldname = x_field.
  APPEND lt_dynpvaluetab.

  CALL FUNCTION 'DYNP_VALUES_READ'
    EXPORTING
      dyname               = lv_dynpname
      dynumb               = lv_dynpnumb
    TABLES
      dynpfields           = lt_dynpvaluetab
    EXCEPTIONS
      invalid_abapworkarea = 1
      invalid_dynprofield  = 2
      invalid_dynproname   = 3
      invalid_dynpronummer = 4
      invalid_request      = 5
      no_fielddescription  = 6
      invalid_parameter    = 7
      undefind_error       = 8
      double_conversion    = 9
      OTHERS               = 10.
  IF sy-subrc = 0.

    READ TABLE lt_dynpvaluetab INDEX 1.
    MOVE: lt_dynpvaluetab-fieldvalue TO y_value.

  ENDIF.

ENDFORM.                    " read_screen_values
*&---------------------------------------------------------------------*
*&      Form  selection_screen_validations
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->   p1        text
*  < --  p2        text
*----------------------------------------------------------------------*

FORM selection_screen_validations .
  IF NOT p_prog  IS INITIAL AND
      NOT p_progf IS INITIAL.

    MESSAGE e001(aq) WITH
    'Please use either program of function module'.
  ELSEIF p_prog  IS INITIAL AND
         p_progf IS INITIAL.
    MESSAGE e001(aq) WITH
  'Please provide any of the program or function module name'.
  ELSEIF NOT p_progf IS INITIAL.
    SELECT SINGLE *
    FROM   tfdir
    WHERE  funcname = p_progf.
    IF sy-subrc EQ 0.
      CONCATENATE tfdir-pname 'U'(793) tfdir-include INTO gv_prog_name.
      gv_prog_name = gv_prog_name+3(30).
      CONCATENATE p_progf '.html' INTO gv_html_file_name.
    ELSE.
      MESSAGE e001(aq) WITH
    'Function module does not exit'.
    ENDIF.
  ELSE.
    SELECT SINGLE *
    FROM trdir
    WHERE name = p_prog.
    IF sy-subrc NE 0.
      MESSAGE e001(aq) WITH
     'This Report does not exist'.
    ELSE.
      gv_prog_name = p_prog.
      CONCATENATE p_prog '.html' INTO gv_html_file_name.
    ENDIF.
  ENDIF.

ENDFORM.                    " selection_screen_validations

*&--------------------------------------------------------------------*
*&      Form  prepare_file_name
*&--------------------------------------------------------------------*
*       text
*---------------------------------------------------------------------*
*      --> x_directory    text
*      --> x_program_name text
*      --> y_file_name    text
*---------------------------------------------------------------------*

FORM prepare_file_name USING x_directory
                             x_file
                    CHANGING y_file_name.

  CONCATENATE x_directory '\' x_file INTO y_file_name.



ENDFORM.                    " prepare_file_name
*&---------------------------------------------------------------------*
*&      Form  download_html_file_on_pc
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->   p1        text
*  < --  p2        text
*----------------------------------------------------------------------*

FORM download_html_file_on_pc TABLES   yt_download
                              USING    x_outfile TYPE string.


* Use gui_download if file is located on the local PC.
* WS_download only works in foreground
  IF sy-batch EQ 'X'.
    MESSAGE e001(aq) WITH
   'This program cannot be executed in background'.
*   ERROR: Unable to download locally stored files when running in
*   background
  ELSE.

    CALL FUNCTION 'GUI_DOWNLOAD'
      EXPORTING
        filename                = x_outfile
        filetype                = 'ASC'
      TABLES
        data_tab                = yt_download
      EXCEPTIONS
        file_write_error        = 1
        no_batch                = 2
        gui_refuse_filetransfer = 3
        invalid_type            = 4
        no_authority            = 5
        unknown_error           = 6
        header_not_allowed      = 7
        separator_not_allowed   = 8
        filesize_not_allowed    = 9
        header_too_long         = 10
        dp_error_create         = 11
        dp_error_send           = 12
        dp_error_write          = 13
        unknown_dp_error        = 14
        access_denied           = 15
        dp_out_of_memory        = 16
        disk_full               = 17
        dp_timeout              = 18
        file_not_found          = 19
        dataprovider_exception  = 20
        control_flush_error     = 21
        OTHERS                  = 22.
* Status of download
    CASE sy-subrc.
      WHEN 0.
        MESSAGE i002(aq) WITH
        'Code converted to HTML and downloaded as ' x_outfile.

      WHEN OTHERS.
*        Upload unsuccessful - error message
        MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno
           WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDCASE.
  ENDIF.

ENDFORM.                    " download_html_file_on_pc
*&---------------------------------------------------------------------*
*&      Form  convert_code_to_html
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      --> P_GT_REP_TABLE  text
*      --> P_GT_HTML  text
*----------------------------------------------------------------------*

FORM convert_code_to_html  TABLES xt_rep_table
                                  yt_html.

  DEFINE add_html.
    yt_html = &1.
    append yt_html.

  END-OF-DEFINITION.
  DATA : lv_next_line_index  TYPE i.
  DATA : lv_rep_wa_caps      TYPE ty_report_wo_index,
         lv_inline_comment   TYPE ty_report_wo_index,
         lv_form_counter(4)  TYPE n,
         lv_anchor(4)        TYPE n.

  DATA : lt_form_index TYPE STANDARD TABLE OF ty_report_with_index,
         lt_form_index_wa LIKE LINE OF lt_form_index,
         lv_form_name(72) TYPE c,
         lv_string(72)    TYPE c.


* Add Header to HTML
  add_html '<HTML> '.
  add_html '<head> '.
  add_html '<title> '.
  add_html 'ABAP Code WebViewer by www.rmtiwari.com'.
  add_html ' View :'.
  add_html gv_prog_name.
  add_html '</title> '.
  add_html ' '.
  add_html '<meta name="description"'.
  add_html ' content="'.
  add_html p_desc.
  add_html '"> '.
  add_html '<meta name="keywords"'.
  add_html 'content="' .
  add_html 'www.rmtiwari.com,ABAP Code WebViewer,ABAP Code,'.
  add_html p_keyw .
  add_html '"> '.

  add_html '<link type="text/css"> '.
  add_html '</head> '.
  add_html '<BODY> '.
  add_html '<PRE> '.


  LOOP AT xt_rep_table INTO gv_rep_wa.

*    TRANSLATE gv_rep_wa TO UPPER CASE.

*   Modify words in ABAP code which are tags for HTML.
    REPLACE '< ' WITH '< ' INTO gv_rep_wa.
    REPLACE '> ' WITH '> ' INTO gv_rep_wa.

*   For Comments
    IF gv_rep_wa(1) EQ gc_asterik.
      CONCATENATE '<FONT COLOR="BLUE"> ' gv_rep_wa ''
             INTO yt_html.
      add_html yt_html.

*   For Code Lines.
    ELSE.

*     Remove inline comments
      SPLIT gv_rep_wa AT '"' INTO lv_rep_wa_caps lv_inline_comment.
      TRANSLATE lv_rep_wa_caps TO UPPER CASE.
      SHIFT lv_rep_wa_caps LEFT  DELETING LEADING  space.
*     Replace Form routines and performs with anchors .
      IF lv_rep_wa_caps(7) EQ 'PERFORM'.

        SPLIT lv_rep_wa_caps+6 AT space
         INTO lv_inline_comment
              lt_form_index_wa-line lv_inline_comment.
        REPLACE '.' WITH '' INTO lt_form_index_wa-line.

        READ TABLE lt_form_index INTO lt_form_index_wa
        WITH KEY line = lt_form_index_wa-line.
        IF sy-subrc EQ 0.
          lv_anchor = lt_form_index_wa-index.
        ELSE.
*               Populate form index table
          lv_form_counter = lv_form_counter + 1.
          lv_anchor       = lv_form_counter.
          lt_form_index_wa-index =  lv_form_counter.

          APPEND lt_form_index_wa TO lt_form_index.
        ENDIF.

        CONCATENATE '<A HREF=' '"#F' lv_anchor '"> '
               INTO yt_html.

        CONCATENATE yt_html gv_rep_wa INTO yt_html.
        CONCATENATE lt_form_index_wa-line '</A> ' INTO lv_string.
        REPLACE lt_form_index_wa-line IN yt_html
           WITH lv_string IGNORING CASE .
        add_html yt_html.

      ELSE.
        IF lv_rep_wa_caps(4) EQ 'FORM'.

          SPLIT lv_rep_wa_caps+3 AT space
          INTO lv_inline_comment lv_form_name lv_inline_comment.

          READ TABLE lt_form_index INTO lt_form_index_wa
          WITH KEY line = lv_form_name.
          IF sy-subrc EQ 0.
            lv_anchor = lt_form_index_wa-index.
            CONCATENATE '<A NAME="F' lv_anchor '">  '
                   INTO yt_html.
            add_html yt_html.
          ENDIF.
        ENDIF.


        add_html gv_rep_wa.

      ENDIF.
    ENDIF.
  ENDLOOP.

* Close HTML Tags
  add_html '</PRE> '.
  add_html '</BODY> '.
  add_html '</HTML> '.


*--------------------------------------------------------
* Write Anchors for Variables.

  DATA : lt_var_list TYPE STANDARD TABLE OF rsymb WITH HEADER LINE.
  DATA : lv_var_anchor(200) TYPE c,
         lv_var_number(4) TYPE n,
         lv_variable(200)  TYPE c.

* Type for Report with line index
  TYPES: BEGIN OF ty_var_index,
         index TYPE i,
         line(300),
         first_time,
         END OF ty_var_index.

  DATA : lt_var_index TYPE STANDARD TABLE OF ty_var_index,
         lt_var_index_wa TYPE ty_var_index,
         lv_first_time TYPE c,
         lv_length     TYPE i,
         lv_line_length     TYPE i.

  lv_first_time = 'X'.
* Get Variables list.
  LOAD REPORT gv_prog_name  PART 'SYMB' INTO lt_var_list.

  LOOP AT yt_html INTO gv_rep_wa.
    LOOP AT lt_var_list.
      SEARCH lt_var_list-txof FOR '%'.
      IF sy-subrc EQ 0  OR
         gv_rep_wa(5) EQ '<FONT'.
        CONTINUE.
      ENDIF.
      READ TABLE lt_var_index INTO lt_var_index_wa
      WITH KEY line = lt_var_list-txof.
      IF sy-subrc EQ 0.
        lv_first_time = lt_var_index_wa-first_time.
        lv_var_number = lt_var_index_wa-index.
      ELSE.
        lv_first_time = 'X'.
        DESCRIBE TABLE lt_var_index LINES lv_var_number.
        lt_var_index_wa-index = lv_var_number.
        lt_var_index_wa-line  = lt_var_list-txof.
        lt_var_index_wa-first_time  = 'X'.
        APPEND lt_var_index_wa TO lt_var_index.
      ENDIF.

      IF lv_first_time EQ 'X'.

        CONCATENATE '<A NAME="' lv_var_number '"> ' lt_var_list-txof
        '</A> '
        INTO lv_string.

      ELSE.
        CONCATENATE '<A HREF="#' lv_var_number '"> ' lt_var_list-txof
        '</A> '
        INTO lv_string.
      ENDIF.

      CONCATENATE lt_var_list-txof ' ' INTO lv_variable
      SEPARATED BY space.

      SEARCH gv_rep_wa FOR lv_variable .
      IF sy-subrc EQ 0.
        lv_length = sy-fdpos - 1.
        IF lv_length >= 0 .
          CHECK gv_rep_wa+lv_length(1) EQ ' ' OR
                gv_rep_wa+lv_length(1) EQ ':'.
        ENDIF.

        lv_length = STRLEN( lv_variable ) + sy-fdpos.
        lv_line_length = STRLEN( gv_rep_wa ).
        IF lv_length LE lv_line_length .
          CHECK gv_rep_wa+lv_length(1) EQ ' ' OR
                gv_rep_wa+lv_length(1) EQ '.' OR
                gv_rep_wa+lv_length(1) EQ '(' OR
                gv_rep_wa+lv_length(1) EQ '-' OR
                gv_rep_wa+lv_length(1) EQ ','.
        ENDIF.

        CASE lv_variable.
          WHEN 'BLOCK' OR 'BEGIN' OR 'END'.
            SEARCH gv_rep_wa FOR 'OF BLOCK'.
            CHECK sy-subrc NE 0.

          WHEN 'WITH' OR 'FRAME' OR 'TITLE'..
            SEARCH gv_rep_wa FOR 'WITH FRAME TITLE'.
            CHECK sy-subrc NE 0.

          WHEN 'LINE' OR 'BEGIN' OR 'END'.
            SEARCH gv_rep_wa FOR 'OF LINE'.
            CHECK sy-subrc NE 0.
            SEARCH gv_rep_wa FOR 'LIKE LINE OF'.
            CHECK sy-subrc NE 0.

          WHEN 'PARAMETERS'.
            SEARCH gv_rep_wa FOR 'PARAMETERS'.
            CHECK sy-subrc NE 0.



        ENDCASE.

        REPLACE lt_var_list-txof IN gv_rep_wa WITH lv_string
                IGNORING CASE .
        IF sy-subrc EQ 0.
          lt_var_index_wa-first_time = ' '.
          MODIFY lt_var_index
          FROM lt_var_index_wa TRANSPORTING first_time
          WHERE LINE = lt_var_list-txof.

          MODIFY yt_html FROM gv_rep_wa.

        ENDIF.
      ENDIF.
    ENDLOOP.

  ENDLOOP.
*--------------------------------------------------------

ENDFORM.                    " convert_code_to_html
*&---------------------------------------------------------------------*
*&      Form  show_html_file
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      --> X_FILE_NAME  text
*----------------------------------------------------------------------*

FORM show_html_file  USING    x_file_name.

  DATA : lv_url(200) TYPE c.

  lv_url = x_file_name.
  CALL FUNCTION 'CALL_BROWSER'
    EXPORTING
      url = lv_url.


ENDFORM.                    " show_html_file
⚠️ **GitHub.com Fallback** ⚠️