[ABAP] RegEx: HTML-Tags aus String entfernen

* HTML
DATA(lv_html) = |<!DOCTYPE html>| &&
                |<html>| &&
                |<head>| &&
                |  <meta charset="utf-8">| &&
                |  <meta name="viewport" content="width=device-width">| &&
                |  <title>DOM Read</title>| &&
                |</head>| &&
                |<body>| &&
                |  <div id="bodytext"><b>Hallo Welt!</b></div>| &&
                |  <div id="bodytext">Weiterlesen: <a href="https://codezentrale.de">Link</a></div>| &&
                |</body>| &&
                |</html>|.

* Alle HTML-Tags finden
DATA(matcher) = cl_abap_matcher=>create( pattern     = '<([!A-Za-z][A-Za-z0-9]*)([^>]*)>|</([A-Za-z][A-Za-z0-9]*)>'
                                         text        = lv_html
                                         ignore_case = abap_true ).

* Alle gefundenen HTML-Tags mit '' ersetzen
IF matcher->replace_all( '' ) > 0.
  WRITE: / matcher->text.
ELSE.
  WRITE: / |Keine Tags gefunden.|.
ENDIF.

[ABAP] Google-API: QR-Code generieren und Bild herunterladen/speichern

* Dateiname der Bilddatei zum hochladen
PARAMETERS: p_fname TYPE file_table-filename OBLIGATORY.

START-OF-SELECTION.
* URL zur Google-API für die Erstellung des QR-Codes
  DATA(lv_url) = |http://chart.apis.google.com/chart?chs=200x200&cht=qr&chld=\|1&chl=viele%20lustige%20Zeichen/chart.png|.

  TRY.
      DATA: o_client TYPE REF TO if_http_client.
* Client-Objekt erzeugen
      cl_http_client=>create_by_url( EXPORTING
                                       url     = lv_url
                                     IMPORTING
                                       client  = o_client ).
      IF sy-subrc = 0.
* HTTP GET senden
        o_client->send( ).

* Response lesen
        o_client->receive( ).

        DATA: lv_http_status TYPE i.
        DATA: lv_status_text TYPE string.

* HTTP Return Code holen
        o_client->response->get_status( IMPORTING
                                          code   = lv_http_status
                                          reason = lv_status_text ).

* Wenn Status 200 (Ok)
        IF lv_http_status = 200.

* Binärdaten (QR-Code) auslesen
          DATA(lv_xdata) = o_client->response->get_data( ).

          o_client->close( ).

* xstring -> solix
          DATA(it_img_conv_data) = cl_bcs_convert=>xstring_to_solix( iv_xstring = lv_xdata ).

          WRITE: / |{ p_fname }.png|.

* Image lokal speichern
          cl_gui_frontend_services=>gui_download( EXPORTING
                                                    filename     = |{ p_fname }.png|
                                                    filetype     = 'BIN'
                                                    bin_filesize = xstrlen( lv_xdata )
                                                  CHANGING
                                                    data_tab     = it_img_conv_data ).

        ENDIF.
      ENDIF.
    CATCH cx_root INTO DATA(e_txt).
      WRITE: / e_txt->get_text( ).
  ENDTRY.

[ABAP] Einfacher REST-Service (ohne OData)

Piyush Philip auf sap.com hat eine simple Möglichkeit beschrieben, wie man in ABAP einen REST-Service mit den entsprechenden CRUD-Methoden implementieren kann. Dazu ist im wesentlichen nur eine eigene Handler-Klasse zu implementieren, die von IF_HTTP_EXTENSION erbt. Diese Handler-Klasse wird von einem Service des ICF gerufen.

  1. SE80 -> Neue Klasse anlegen
  2. In der Klasse im Reiter Interfaces: vom Interface IF_HTTP_EXTENSION erben
  3. die Interface-Methode IF_HTTP_EXTENSION~HANDLE_REQUEST implementieren (implementiert nur REST, kein OData-Sevice!)
    METHOD if_http_extension~handle_request.
    
    * Typen
        TYPES: BEGIN OF ty_json_request,
                 param1 TYPE char64,
                 param2 TYPE char64,
               END OF ty_json_request.
    
        TYPES: BEGIN OF ty_json_response,
                 success   TYPE string,
                 msg       TYPE string,
                 operation TYPE string,
               END OF ty_json_response.
    
    * Variablen
        DATA: rv_response TYPE ty_json_response.
        DATA: it_path_info TYPE stringtab.
        DATA: it_query_strings TYPE stringtab.
    
    * Header Fields auslesen
        DATA: it_header_fields TYPE tihttpnvp.
        server->request->get_header_fields( CHANGING fields = it_header_fields ).
    
    * Request Methode (POST, GET, PUT, DELETE ...)
        DATA(lv_request_method) = server->request->get_header_field( name = '~request_method' ).
    
    * URL Pathinfo
        DATA(lv_path_info) = server->request->get_header_field( name = '~path_info' ).
    * erstes '/' entfernen
        SHIFT lv_path_info LEFT BY 1 PLACES.
        SPLIT lv_path_info AT '/' INTO TABLE it_path_info.
    
    * URL Parameter (Query Strings) für Auswertung
        DATA(lv_query_string) = server->request->get_header_field( name = '~query_string' ).
        SPLIT lv_query_string AT '&' INTO TABLE it_query_strings.
    
    * Request Methoden behandeln
        CASE lv_request_method.
    * C (Create)
          WHEN 'POST'.
    * Beispiel: per Request übermittelte JSON-Daten auslesen und behandeln
    * Content-Typ auslesen
            DATA(lv_request_content_type) = server->request->get_content_type( ).
    
    * Wenn Content-Typ JSON
            IF lv_request_content_type CP if_rest_media_type=>gc_appl_json.
    * JSON-Daten auslesen und nach ABAP-Typ konvertieren -> Parameternamen müssen übereinstimmen, sonst HTTP 500!
              DATA(lv_request_cdata) = server->request->get_cdata( ).
    
              DATA: lv_request_abap TYPE ty_json_request.
    
              TRY.
                  /ui2/cl_json=>deserialize( EXPORTING json = lv_request_cdata
                                                       pretty_name = /ui2/cl_json=>pretty_mode-camel_case
                                             CHANGING  data = lv_request_abap ).
    
                CATCH cx_root INTO DATA(e_txt).
                  MESSAGE e_txt->get_text( ) TYPE 'S' DISPLAY LIKE 'E'.
              ENDTRY.
    
              rv_response = VALUE #( success   = 'true'
                                     msg       = lv_request_abap-param1 && ' | ' && lv_request_abap-param2
                                     operation = lv_request_method ).
            ELSE.
              rv_response = VALUE #( success   = 'false'
                                     msg       = 'Request content type must be application/json'
                                     operation = lv_request_method ).
            ENDIF.
    * R (Read)
          WHEN 'GET'.
            DATA(lv_param) = server->request->get_form_field( 'param' ).
    
            rv_response = VALUE #( success   = 'true'
                                   msg       = lv_param
                                   operation = lv_request_method ).
    * U (Update)
          WHEN 'PUT'.
            rv_response = VALUE #( success   = 'true'
                                   msg       = ''
                                   operation = lv_request_method ).
    * D (Delete)
          WHEN 'DELETE'.
            rv_response = VALUE #( success   = 'true'
                                   msg       = ''
                                   operation = lv_request_method ).
    
    * unerlaubte Methoden abfangen
          WHEN OTHERS.
    * Fehlercode 405 zurückgeben
            server->response->set_status( code   = cl_rest_status_code=>gc_client_error_meth_not_allwd
                                          reason = 'method not allowed' ).
    
            server->response->set_content_type( if_rest_media_type=>gc_appl_json ).
    
            server->response->set_header_field( name  = if_http_header_fields=>allow
                                                value = 'POST,GET,PUT,DELETE' ).
    
            rv_response = VALUE #( success   = 'false'
                                   msg       = 'error'
                                   operation = lv_request_method ).
        ENDCASE.
    
    * wenn erfolgreich, dann Wert zurückgeben
        IF rv_response-success = 'true'.
    * HTTP-Status 200
          server->response->set_status( code   = cl_rest_status_code=>gc_success_ok
                                        reason = 'Ok.' ).
    
    * application/json
          server->response->set_content_type( if_rest_media_type=>gc_appl_json ).
    
    * Service-Response als JSON zurückgeben
          server->response->set_cdata( data = /ui2/cl_json=>serialize( data        = rv_response
                                                                       pretty_name = /ui2/cl_json=>pretty_mode-camel_case ) ).
    
        ENDIF.
    
    ENDMETHOD.
    
  4. SICF -> neue ICF-Node (Sub-Element) unterhalb default_host -> sap -> bc als neuen unabhängigen Service anlegen
  5. in der Handlerliste des Service die erstellte Klasse als „Handler“ angeben
  6. auf die neue ICF-Node rechtsklicken -> Service aktivieren
  7. auf die neue ICF-Node rechtsklicken -> Service testen
  8. URL aus Browserfenster kopieren (http://domain.de/sap/bc/servicename?sap-client=900)
  9. im Browser mir einem Rest-Plugin (FireFox-Prugin: Rested) testen

Links

[ABAP] Dateneingabe mit cl_demo_input

DATA: lv_carrid TYPE sflight-carrid VALUE 'AA'.
DATA: lv_date TYPE dats VALUE '20181231'.
DATA: lv_ok TYPE abap_bool VALUE abap_true.

* Eingabefelder
cl_demo_input=>add_field( EXPORTING
                            text  = 'Fluggesellschaft:'
                          CHANGING
                            field = lv_carrid ).

cl_demo_input=>add_field( EXPORTING
                            text  = 'Datum:'
                          CHANGING
                            field = lv_date ).

* Trennzeile
cl_demo_input=>add_line( ).

cl_demo_input=>add_field( EXPORTING
                            text        = 'Check:'
                            as_checkbox = abap_true
                          CHANGING
                            field       = lv_ok ).

* Eingabe anfordern
cl_demo_input=>request( ).

WRITE: / lv_carrid.
WRITE: / lv_date.
WRITE: / lv_ok.

[ABAP] Email-Adresse validieren

DATA: lv_address_normal TYPE sx_address.
DATA: lv_local TYPE sx_addr.
DATA: lv_domain TYPE sx_addr.
DATA: lv_comment TYPE sx_addr.
DATA: lv_addr_normal_no_upper TYPE sx_address.
DATA: lv_addr_normal_no_up TYPE sx_address.

* Type
* FAX	Faxnummer
* INT	Internet Mail Adresse
* X40	X.400 Mail Adresse
* RML	SAPoffice Adresse
* PAG	Pager(SMS) Nummer
* PRT	Druckername
* *	Alle
DATA(lv_address_unstruct) = value sx_address( address = 'email (Test)@email.com'
                                              type    = 'INT' ).

WRITE: / lv_address_unstruct-type, lv_address_unstruct-address.

CALL FUNCTION 'SX_INTERNET_ADDRESS_TO_NORMAL'
  EXPORTING
    address_unstruct               = lv_address_unstruct
  IMPORTING
    address_normal                 = lv_address_normal        " Normalform: Local@DOMAIN
    local                          = lv_local                 " Lokaler Teil der Adresse
    domain                         = lv_domain                " Domäne (konvertiert zu Großbuchstaben)
    comment                        = lv_comment               " Kommentar der Adresse
    addr_normal_no_upper           = lv_addr_normal_no_upper  " Adresse: Local@Domain (Domäne unkonvertiert)
    addr_normal_no_up_with_comment = lv_addr_normal_no_up     " Normalform mit Kommentar
  EXCEPTIONS
    error_address_type             = 1
    error_address                  = 2
    error_group_address            = 3
    OTHERS                         = 4.

IF sy-subrc = 0.
  WRITE: / '                    Normalform:', lv_address_normal-type, lv_address_normal-address.
  WRITE: / '                  Lokaler Teil:', lv_local.
  WRITE: / '                        Domäne:', lv_domain.
  WRITE: / '                     Kommentar:', lv_comment.
  WRITE: / 'Adresse (Domäne unkonvertiert):', lv_addr_normal_no_upper-type, lv_addr_normal_no_upper-address.
  WRITE: / '      Normalform mit Kommentar:', lv_addr_normal_no_up-type, lv_addr_normal_no_up-address.
ELSE.
  WRITE: / 'Error:', sy-subrc.
ENDIF.

[ABAP] CSS-Code als ABAP-INCLUDE speichern und laden

Über die Nutzung von Includes ist es möglich beliebigen Code im SAP abzulegen und darauf zuzugreifen. Folgende Schritte sind notwendig:

  • SE80 -> neues INCLUDE anlegen (Bsp.: ZCSSINCLUDE)
  • CSS-Code im INCLUDE eintragen
  • Aktvieren! (Ja, das funktioniert.)
  • CSS-Code (INCLUDE) im ABAP-Code lesen
* Name des INCLUDEs mit dem CSS-Code
CONSTANTS: co_cssinclude TYPE char12 VALUE 'ZCSSINCLUDE'.
* String für CSS-Daten
DATA: lv_css TYPE string.
	
* Prüfen, ob INCLUDE im System (TADIR) aktiv vorhanden
SELECT SINGLE obj_name
  INTO @DATA(lv_obj_name)
  FROM tadir
  WHERE obj_name = @co_w3css
    AND pgmid    = 'R3TR'
    AND object   = 'PROG'.

IF sy-subrc = 0.

* Stringtab für Code
  DATA: it_incl_code TYPE stringtab.

* Code des INCLUDEs lesen
  READ REPORT co_w3css INTO it_incl_code.

  IF sy-subrc = 0.
* String-Tabelle mit CSS-Code in String wandeln, Trennzeichen ist CRLF
    lv_css = REDUCE string( INIT s = ||
                            FOR <s> IN it_incl_code
                            NEXT s = COND #( WHEN s IS INITIAL THEN |{ <s> }| ELSE |{ s }{ cl_abap_char_utilities=>cr_lf }{ <s> }| ) ).
  ENDIF.
ENDIF.

* HTML mit CSS-INCLUDE
DATA(lv_html) = '<!DOCTYPE html>' &&
                '<html>' &&
                '<title>Lagermaterialkatalog</title>' &&
                '<meta name="viewport" content="width=device-width, initial-scale=1">' &&
                '<style>' && lv_css && '</style>' &&
                '<body>' &&
                '</body>' &&
                '</html>'.

[ABAP] HTML im Browser-Fenster anzeigen

* Variante 1 (cl_abap_browser)
* Dummy Screen
SELECTION-SCREEN BEGIN OF SCREEN 100.
SELECTION-SCREEN END OF SCREEN 100.

* Daten holen
SELECT * FROM mara INTO TABLE @DATA(it_mara).

* Browserfenster mit HTML-Code im cl_gui_container=>default_screen anzeigen
cl_abap_browser=>show_html( html_string = cl_demo_output=>get( it_mara )
                            container   = cl_gui_container=>default_screen ).

* Toolbar ausblenden
cl_abap_list_layout=>suppress_toolbar( ).

* leeres Dynpro anzeigen und Ausgabe von cl_gui_container=>default_screen erzwingen
CALL SELECTION-SCREEN 100.