All files / src/utils zcl_abapgit_convert.clas.abap

98.49% Statements 523/531
66.66% Branches 10/15
100% Functions 0/0
98.49% Lines 523/531

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 5321x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1756x 1756x 505x 505x 1x 4467x 4467x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x         1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 57x 57x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
CLASS zcl_abapgit_convert DEFINITION
  PUBLIC
  CREATE PUBLIC .
 
  PUBLIC SECTION.
 
    CLASS-METHODS bitbyte_to_int
      IMPORTING
        !iv_bits      TYPE clike
      RETURNING
        VALUE(rv_int) TYPE i .
    CLASS-METHODS x_to_bitbyte
      IMPORTING
        !iv_x             TYPE x
      RETURNING
        VALUE(rv_bitbyte) TYPE zif_abapgit_git_definitions=>ty_bitbyte .
    CLASS-METHODS string_to_xstring_utf8
      IMPORTING
        !iv_string        TYPE string
      RETURNING
        VALUE(rv_xstring) TYPE xstring
      RAISING
        zcx_abapgit_exception .
    CLASS-METHODS xstring_to_string_utf8
      IMPORTING
        !iv_data         TYPE xsequence
        !iv_length       TYPE i OPTIONAL
      RETURNING
        VALUE(rv_string) TYPE string
      RAISING
        zcx_abapgit_exception .
    CLASS-METHODS string_to_xstring_utf8_bom
      IMPORTING
        !iv_string        TYPE string
      RETURNING
        VALUE(rv_xstring) TYPE xstring
      RAISING
        zcx_abapgit_exception .
    CLASS-METHODS xstring_to_string_utf8_bom
      IMPORTING
        !iv_xstring      TYPE xstring
      RETURNING
        VALUE(rv_string) TYPE string
      RAISING
        zcx_abapgit_exception .
    CLASS-METHODS xstring_to_int
      IMPORTING
        !iv_xstring TYPE xstring
      RETURNING
        VALUE(rv_i) TYPE i
      RAISING
        zcx_abapgit_exception .
    CLASS-METHODS int_to_xstring4
      IMPORTING
        !iv_i             TYPE i
      RETURNING
        VALUE(rv_xstring) TYPE xstring .
    CLASS-METHODS split_string
      IMPORTING
        !iv_string      TYPE string
      RETURNING
        VALUE(rt_lines) TYPE string_table .
    CLASS-METHODS conversion_exit_isola_output
      IMPORTING
        !iv_spras       TYPE spras
      RETURNING
        VALUE(rv_spras) TYPE laiso .
    CLASS-METHODS string_to_xstring
      IMPORTING
        !iv_str        TYPE string
      RETURNING
        VALUE(rv_xstr) TYPE xstring
      RAISING
        zcx_abapgit_exception .
    CLASS-METHODS string_to_tab
      IMPORTING
        !iv_str  TYPE string
      EXPORTING
        !ev_size TYPE i
        !et_tab  TYPE STANDARD TABLE .
    CLASS-METHODS base64_to_xstring
      IMPORTING
        !iv_base64     TYPE string
      RETURNING
        VALUE(rv_xstr) TYPE xstring .
    CLASS-METHODS xstring_to_bintab
      IMPORTING
        !iv_xstr   TYPE xstring
      EXPORTING
        !ev_size   TYPE i
        !et_bintab TYPE STANDARD TABLE .
 
    CLASS-METHODS language_sap1_to_sap2
      IMPORTING
        im_lang_sap1        TYPE sy-langu
      RETURNING
        VALUE(re_lang_sap2) TYPE string
      EXCEPTIONS
        no_assignment.
 
    CLASS-METHODS language_sap2_to_sap1
      IMPORTING
        im_lang_sap2        TYPE laiso
      RETURNING
        VALUE(re_lang_sap1) TYPE sy-langu
      EXCEPTIONS
        no_assignment.
 
    CLASS-METHODS language_sap1_to_bcp47
      IMPORTING
        im_lang_sap1         TYPE sy-langu
      RETURNING
        VALUE(re_lang_bcp47) TYPE string
      EXCEPTIONS
        no_assignment.
 
    CLASS-METHODS language_bcp47_to_sap1
      IMPORTING
        im_lang_bcp47       TYPE string
      RETURNING
        VALUE(re_lang_sap1) TYPE sy-langu
      EXCEPTIONS
        no_assignment.
 
    TYPES ty_char02 TYPE c LENGTH 2.
    CLASS-METHODS uccp
      IMPORTING
        iv_uccp        TYPE string
      RETURNING
        VALUE(rv_char) TYPE ty_char02
      EXCEPTIONS
        no_assignment.
 
  PROTECTED SECTION.
  PRIVATE SECTION.
    CLASS-METHODS xstring_remove_bom
      IMPORTING
        iv_xstr        TYPE xsequence
      RETURNING
        VALUE(rv_xstr) TYPE xstring.
ENDCLASS.
 
 
 
CLASS zcl_abapgit_convert IMPLEMENTATION.
 
  METHOD base64_to_xstring.
 
    rv_xstr = cl_http_utility=>decode_x_base64( iv_base64 ).
 
  ENDMETHOD.
 
 
  METHOD bitbyte_to_int.
 
    DATA: lv_bitbyte TYPE string,
          lv_len     TYPE i,
          lv_offset  TYPE i.
 
    lv_bitbyte = iv_bits.
    SHIFT lv_bitbyte LEFT DELETING LEADING '0 '.
    lv_len     = strlen( lv_bitbyte ).
    lv_offset  = lv_len - 1.
 
    rv_int = 0.
    DO lv_len TIMES.
 
      IF sy-index = 1.
        "Initialize
        IF lv_bitbyte+lv_offset(1) = '1'.
          rv_int = 1.
        ENDIF.
      ELSEIF lv_bitbyte+lv_offset(1) = '1'.
        rv_int = rv_int + ( 2 ** ( sy-index - 1 ) ).
      ENDIF.
 
      lv_offset = lv_offset - 1. "Move Cursor
 
    ENDDO.
 
  ENDMETHOD.
 
 
  METHOD conversion_exit_isola_output.
 
    language_sap1_to_sap2(
      EXPORTING
        im_lang_sap1  = iv_spras
      RECEIVING
        re_lang_sap2  = rv_spras
      EXCEPTIONS
        no_assignment = 1
        OTHERS        = 2 ).                              "#EC CI_SUBRC
 
    TRANSLATE rv_spras TO UPPER CASE.
 
  ENDMETHOD.
 
 
  METHOD int_to_xstring4.
* returns xstring of length 4 containing the integer value iv_i
 
    DATA lv_x TYPE x LENGTH 4.
 
    lv_x = iv_i.
    rv_xstring = lv_x.
 
  ENDMETHOD.
 
  METHOD language_sap1_to_bcp47.
    DATA lv_converter_instance TYPE REF TO object.
    DATA lv_converter_class_name TYPE string VALUE `CL_AFF_LANGUAGE_CONVERTER`.
 
    TRY.
        CALL METHOD (lv_converter_class_name)=>create_instance
          RECEIVING
            result = lv_converter_instance.
 
        TRY.
            CALL METHOD lv_converter_instance->(`IF_AFF_LANGUAGE_CONVERTER~SAP1_TO_BCP47`)
              EXPORTING
                language      = im_lang_sap1
              RECEIVING
                result        = re_lang_bcp47.
          CATCH cx_static_check.
            RAISE no_assignment.
        ENDTRY.
      CATCH cx_sy_dyn_call_error.
        TRY.
            re_lang_bcp47 = lcl_bcp47_language_table=>sap1_to_bcp47( im_lang_sap1 ).
          CATCH zcx_abapgit_exception.
            RAISE no_assignment.
        ENDTRY.
    ENDTRY.
  ENDMETHOD.
 
  METHOD language_bcp47_to_sap1.
    DATA lv_converter_instance TYPE REF TO object.
    DATA lv_converter_class_name TYPE string VALUE `CL_AFF_LANGUAGE_CONVERTER`.
    DATA lv_regex TYPE REF TO cl_abap_regex.
    DATA lv_abap_matcher TYPE REF TO cl_abap_matcher.
 
    DATA lv_sap2_lang_code TYPE laiso.
 
    TRY.
        CALL METHOD (lv_converter_class_name)=>create_instance
          RECEIVING
            result = lv_converter_instance.
 
        TRY.
            CALL METHOD lv_converter_instance->(`IF_AFF_LANGUAGE_CONVERTER~BCP47_TO_SAP1`)
              EXPORTING
                language = im_lang_bcp47
              RECEIVING
                result   = re_lang_sap1.
 
          CATCH cx_static_check.
            RAISE no_assignment.
        ENDTRY.
 
      CATCH cx_sy_dyn_call_error.
        TRY.
            re_lang_sap1 = lcl_bcp47_language_table=>bcp47_to_sap1( im_lang_bcp47 ).
          CATCH zcx_abapgit_exception.
 
            CREATE OBJECT lv_regex EXPORTING pattern = `[A-Z0-9]{2}`.
            lv_abap_matcher = lv_regex->create_matcher( text = im_lang_bcp47 ).
 
            IF abap_true = lv_abap_matcher->match( ).
              "Fallback try to convert from SAP language
              lv_sap2_lang_code = im_lang_bcp47.
 
              language_sap2_to_sap1(
                EXPORTING
                  im_lang_sap2  = lv_sap2_lang_code
                RECEIVING
                  re_lang_sap1  = re_lang_sap1
                EXCEPTIONS
                  no_assignment = 1
                  OTHERS        = 2 ).
              IF sy-subrc <> 0.
                RAISE no_assignment.
              ENDIF.
 
            ELSE.
              RAISE no_assignment.
            ENDIF.
        ENDTRY.
    ENDTRY.
  ENDMETHOD.
 
  METHOD language_sap1_to_sap2.
 
    TRY.
        re_lang_sap2 = lcl_bcp47_language_table=>sap1_to_sap2( im_lang_sap1 ).
      CATCH zcx_abapgit_exception.
        RAISE no_assignment.
    ENDTRY.
 
  ENDMETHOD.
 
 
  METHOD language_sap2_to_sap1.
 
    TRY.
        re_lang_sap1 = lcl_bcp47_language_table=>sap2_to_sap1( im_lang_sap2 ).
      CATCH zcx_abapgit_exception.
        RAISE no_assignment.
    ENDTRY.
 
  ENDMETHOD.
 
 
  METHOD split_string.
 
    FIND FIRST OCCURRENCE OF cl_abap_char_utilities=>cr_lf IN iv_string.
 
    " Convert string into table depending on separator type CR_LF vs. LF
    IF sy-subrc = 0.
      SPLIT iv_string AT cl_abap_char_utilities=>cr_lf INTO TABLE rt_lines.
    ELSE.
      SPLIT iv_string AT cl_abap_char_utilities=>newline INTO TABLE rt_lines.
    ENDIF.
 
  ENDMETHOD.
 
 
  METHOD string_to_tab.
 
    DATA lv_length TYPE i.
    DATA lv_iterations TYPE i.
    DATA lv_offset TYPE i.
 
    FIELD-SYMBOLS <lg_line> TYPE any.
 
 
    CLEAR et_tab.
    ev_size = strlen( iv_str ).
 
    APPEND INITIAL LINE TO et_tab ASSIGNING <lg_line>.
    <lg_line> = iv_str.
    lv_length = cl_abap_typedescr=>describe_by_data( <lg_line> )->length / cl_abap_char_utilities=>charsize.
    lv_iterations = ev_size DIV lv_length.
 
    DO lv_iterations TIMES.
      lv_offset = sy-index * lv_length.
      APPEND INITIAL LINE TO et_tab ASSIGNING <lg_line>.
      <lg_line> = iv_str+lv_offset.
    ENDDO.
 
  ENDMETHOD.
 
 
  METHOD string_to_xstring.
 
    rv_xstr = string_to_xstring_utf8( iv_str ).
 
  ENDMETHOD.
 
 
  METHOD string_to_xstring_utf8.
 
    rv_xstring = lcl_out=>convert( iv_string ).
 
  ENDMETHOD.
 
 
  METHOD string_to_xstring_utf8_bom.
 
    IF iv_string IS INITIAL.
      RETURN.
    ENDIF.
 
    rv_xstring = string_to_xstring_utf8( iv_string ).
 
    " Add UTF-8 BOM
    IF xstrlen( rv_xstring ) < 3 OR rv_xstring(3) <> cl_abap_char_utilities=>byte_order_mark_utf8.
      rv_xstring = cl_abap_char_utilities=>byte_order_mark_utf8 && rv_xstring.
    ENDIF.
 
  ENDMETHOD.
 
 
  METHOD xstring_remove_bom.
 
    rv_xstr = iv_xstr.
 
    " cl_abap_conv_in_ce does not handle BOM in non-Unicode systems, so we remove it
    IF cl_abap_char_utilities=>charsize = 1 AND xstrlen( rv_xstr ) > 3
        AND rv_xstr(3) = cl_abap_char_utilities=>byte_order_mark_utf8.

      rv_xstr = rv_xstr+3.

    ENDIF.
 
  ENDMETHOD.
 
 
  METHOD xstring_to_bintab.
 
    DATA lv_length TYPE i.
    DATA lv_iterations TYPE i.
    DATA lv_offset TYPE i.
    DATA lv_struct TYPE abap_bool.
 
    FIELD-SYMBOLS <lg_line> TYPE any.
 
 
    CLEAR et_bintab.
    ev_size = xstrlen( iv_xstr ).
 
    APPEND INITIAL LINE TO et_bintab ASSIGNING <lg_line>.
    lv_struct = boolc(
      cl_abap_typedescr=>describe_by_data( <lg_line> )->type_kind = cl_abap_typedescr=>typekind_struct1 ).
    IF lv_struct = abap_true.
      ASSIGN COMPONENT 1 OF STRUCTURE <lg_line> TO <lg_line>.
    ENDIF.
    <lg_line> = iv_xstr.
 
    lv_length = cl_abap_typedescr=>describe_by_data( <lg_line> )->length.
    lv_iterations = ev_size DIV lv_length.
 
    DO lv_iterations TIMES.
      lv_offset = sy-index * lv_length.
      APPEND INITIAL LINE TO et_bintab ASSIGNING <lg_line>.
      IF lv_struct = abap_true.
        ASSIGN COMPONENT 1 OF STRUCTURE <lg_line> TO <lg_line>.
      ENDIF.
      <lg_line> = iv_xstr+lv_offset.
    ENDDO.
 
  ENDMETHOD.
 
 
  METHOD xstring_to_int.
 
* use the built-in type conversion
    rv_i = iv_xstring.
 
  ENDMETHOD.
 
 
  METHOD xstring_to_string_utf8.
 
    DATA lv_data   TYPE xstring.
    DATA lv_length TYPE i.
 
    " Remove BOM for non-Unicode systems
    lv_data = xstring_remove_bom( iv_data ).
 
    lv_length = iv_length.
    IF lv_length <= 0.
      lv_length = xstrlen( lv_data ).
    ENDIF.
 
    rv_string = lcl_in=>convert(
      iv_data   = lv_data
      iv_length = lv_length ).
 
  ENDMETHOD.
 
 
  METHOD xstring_to_string_utf8_bom.
 
    DATA lv_xstring TYPE xstring.
 
    IF iv_xstring IS INITIAL.
      RETURN.
    ENDIF.
 
    lv_xstring = iv_xstring.
    " Add UTF-8 BOM
    IF xstrlen( lv_xstring ) < 3 OR lv_xstring(3) <> cl_abap_char_utilities=>byte_order_mark_utf8.
      lv_xstring = cl_abap_char_utilities=>byte_order_mark_utf8 && lv_xstring.
    ENDIF.
 
    rv_string = xstring_to_string_utf8( lv_xstring ).
 
  ENDMETHOD.
 
 
  METHOD x_to_bitbyte.
 
    CLEAR rv_bitbyte.
 
    GET BIT 1 OF iv_x INTO rv_bitbyte+0(1).
    GET BIT 2 OF iv_x INTO rv_bitbyte+1(1).
    GET BIT 3 OF iv_x INTO rv_bitbyte+2(1).
    GET BIT 4 OF iv_x INTO rv_bitbyte+3(1).
    GET BIT 5 OF iv_x INTO rv_bitbyte+4(1).
    GET BIT 6 OF iv_x INTO rv_bitbyte+5(1).
    GET BIT 7 OF iv_x INTO rv_bitbyte+6(1).
    GET BIT 8 OF iv_x INTO rv_bitbyte+7(1).
 
  ENDMETHOD.
 
  METHOD uccp.
 
    DATA lv_class    TYPE string.
    DATA lv_xstr     TYPE xstring.
    DATA lo_instance TYPE REF TO object.
 
    lv_class = 'CL_ABAP_CONV_IN_CE'.
 
    TRY.
        CALL METHOD (lv_class)=>uccp
          EXPORTING
            uccp = iv_uccp
          RECEIVING
            char = rv_char.
      CATCH cx_sy_dyn_call_illegal_class.
        lv_xstr = iv_uccp.
 
        CALL METHOD ('CL_ABAP_CONV_CODEPAGE')=>create_in
          EXPORTING
            codepage = 'UTF-16'
          RECEIVING
            instance = lo_instance.
 
* convert endianness
        CONCATENATE lv_xstr+1(1) lv_xstr(1) INTO lv_xstr IN BYTE MODE.
 
        CALL METHOD lo_instance->('IF_ABAP_CONV_IN~CONVERT')
          EXPORTING
            source = lv_xstr
          RECEIVING
            result = rv_char.
    ENDTRY.
 
  ENDMETHOD.
ENDCLASS.