All files / src/objects/core zcl_abapgit_filename_logic.clas.abap

99.52% Statements 417/419
93.75% Branches 15/16
100% Functions 1/1
99.52% Lines 417/419

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 4201x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 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x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 3x 3x 5x 5x 1x 1x 3x 3x 1x 1x 1x 1x  
CLASS zcl_abapgit_filename_logic DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .
 
  PUBLIC SECTION.
 
    CONSTANTS:
      BEGIN OF c_package_file,
        obj_name  TYPE c LENGTH 7 VALUE 'package',
        sep1      TYPE c LENGTH 1 VALUE '.',
        obj_type  TYPE c LENGTH 4 VALUE 'devc',
        sep2      TYPE c LENGTH 1 VALUE '.',
        extension TYPE c LENGTH 3 VALUE 'xml',
      END OF c_package_file.
 
    CONSTANTS:
      BEGIN OF c_json_file,
        extension TYPE c LENGTH 4 VALUE 'json',
      END OF c_json_file.
 
    CLASS-METHODS detect_obj_definition
      IMPORTING
        !iv_type    TYPE string
        !iv_ext     TYPE string
      EXPORTING
        !ev_is_xml  TYPE abap_bool
        !ev_is_json TYPE abap_bool.
 
    CLASS-METHODS is_obj_definition_file
      IMPORTING
        !iv_filename  TYPE string
      RETURNING
        VALUE(rv_yes) TYPE abap_bool.
 
    CLASS-METHODS file_to_object
      IMPORTING
        !iv_filename TYPE string
        !iv_path     TYPE string
        !iv_devclass TYPE devclass OPTIONAL
        !io_dot      TYPE REF TO zcl_abapgit_dot_abapgit
      EXPORTING
        !es_item     TYPE zif_abapgit_definitions=>ty_item
        !ev_is_xml   TYPE abap_bool
        !ev_is_json  TYPE abap_bool
      RAISING
        zcx_abapgit_exception .
 
    CLASS-METHODS i18n_file_to_object
      IMPORTING
        !iv_filename TYPE string
        !iv_path     TYPE string
      EXPORTING
        !es_item     TYPE zif_abapgit_definitions=>ty_item
        !ev_lang     TYPE laiso
        !ev_ext      TYPE string
      RAISING
        zcx_abapgit_exception .
 
    CLASS-METHODS object_to_file
      IMPORTING
        !is_item           TYPE zif_abapgit_definitions=>ty_item
        !iv_ext            TYPE string
        !iv_extra          TYPE clike OPTIONAL
      RETURNING
        VALUE(rv_filename) TYPE string .
 
    CLASS-METHODS object_to_i18n_file
      IMPORTING
        !is_item           TYPE zif_abapgit_definitions=>ty_item
        !iv_lang           TYPE laiso
        !iv_ext            TYPE string
      RETURNING
        VALUE(rv_filename) TYPE string.
 
  PROTECTED SECTION.
  PRIVATE SECTION.
 
    CLASS-DATA:
      go_aff_registry TYPE REF TO zif_abapgit_aff_registry.
 
    CLASS-METHODS name_escape
      IMPORTING
        !iv_name       TYPE csequence
      RETURNING
        VALUE(rv_name) TYPE string.
 
    CLASS-METHODS name_unescape
      IMPORTING
        !iv_name       TYPE csequence
      RETURNING
        VALUE(rv_name) TYPE string.
 
    CLASS-METHODS map_filename_to_object
      IMPORTING
        !iv_filename TYPE string
        !iv_path     TYPE string
        !iv_package  TYPE devclass
        !io_dot      TYPE REF TO zcl_abapgit_dot_abapgit
      CHANGING
        cs_item      TYPE zif_abapgit_definitions=>ty_item
      RAISING
        zcx_abapgit_exception.
 
    CLASS-METHODS map_object_to_filename
      IMPORTING
        !is_item    TYPE zif_abapgit_definitions=>ty_item
        !iv_ext     TYPE string
        !iv_extra   TYPE clike
      CHANGING
        cv_filename TYPE string
      RAISING
        zcx_abapgit_exception.
 
    CLASS-METHODS get_lang_and_ext
      IMPORTING
        iv_filename TYPE string
      EXPORTING
        ev_lang     TYPE laiso
        ev_ext      TYPE string
      RAISING
        zcx_abapgit_exception.
 
ENDCLASS.
 
 
 
CLASS zcl_abapgit_filename_logic IMPLEMENTATION.
 
 
  METHOD detect_obj_definition.
 
    ev_is_xml  = boolc( iv_ext = to_upper( c_package_file-extension ) AND strlen( iv_type ) = 4 ).
    ev_is_json = boolc( iv_ext = to_upper( c_json_file-extension ) AND strlen( iv_type ) = 4 ).
 
  ENDMETHOD.
 
 
  METHOD file_to_object.
 
    DATA:
      lv_name TYPE string,
      lv_type TYPE string,
      lv_ext  TYPE string.
 
    " Guess object type and name
    SPLIT to_upper( iv_filename ) AT '.' INTO lv_name lv_type lv_ext.
 
    " Handle namespaces
    REPLACE ALL OCCURRENCES OF '#' IN lv_name WITH '/'.
    REPLACE ALL OCCURRENCES OF '#' IN lv_type WITH '/'.
    REPLACE ALL OCCURRENCES OF '#' IN lv_ext WITH '/'.
 
    " Assume AFF namespace convention
    IF go_aff_registry IS INITIAL.
      CREATE OBJECT go_aff_registry TYPE zcl_abapgit_aff_registry.
    ENDIF.
 
    IF go_aff_registry->is_supported_object_type( |{ lv_type }| ) = abap_true.
      REPLACE ALL OCCURRENCES OF '(' IN lv_name WITH '/'.
      REPLACE ALL OCCURRENCES OF ')' IN lv_name WITH '/'.
    ENDIF.
 
    " Get original object name
    lv_name = name_unescape( lv_name ).
 
    CLEAR es_item.
    es_item-obj_type = lv_type.
    es_item-obj_name = lv_name.
 
    " Get mapping specific to object type
    map_filename_to_object(
      EXPORTING
        iv_filename = iv_filename
        iv_path     = iv_path
        io_dot      = io_dot
        iv_package  = iv_devclass
      CHANGING
        cs_item     = es_item ).
 
    detect_obj_definition(
      EXPORTING
        iv_ext     = lv_ext
        iv_type    = lv_type
      IMPORTING
        ev_is_xml  = ev_is_xml
        ev_is_json = ev_is_json ).
 
  ENDMETHOD.
 
 
  METHOD i18n_file_to_object.
 
    DATA lo_dot TYPE REF TO zcl_abapgit_dot_abapgit.
 
    CLEAR: es_item, ev_lang, ev_ext.
    lo_dot = zcl_abapgit_dot_abapgit=>build_default( ).
 
    file_to_object(
      EXPORTING
        iv_filename = iv_filename
        iv_path     = iv_path
        io_dot      = lo_dot
      IMPORTING
        es_item     = es_item ).
 
    get_lang_and_ext(
      EXPORTING
        iv_filename = iv_filename
      IMPORTING
        ev_lang     = ev_lang
        ev_ext      = ev_ext ).
 
  ENDMETHOD.
 
 
  METHOD is_obj_definition_file.
 
    DATA:
      lv_xml  TYPE abap_bool,
      lv_json TYPE abap_bool,
      lv_name TYPE string,
      lv_type TYPE string,
      lv_ext  TYPE string.
 
    SPLIT to_upper( iv_filename ) AT '.' INTO lv_name lv_type lv_ext.
 
    detect_obj_definition(
      EXPORTING
        iv_ext     = lv_ext
        iv_type    = lv_type
      IMPORTING
        ev_is_xml  = lv_xml
        ev_is_json = lv_json ).
 
    rv_yes = boolc( lv_json = abap_true OR lv_xml = abap_true ).
 
  ENDMETHOD.
 
 
  METHOD map_filename_to_object.
 
    DATA lv_class TYPE seoclsname.
 
    " TODO: Add check for supported object types to avoid calls to non-existing classes
    " zcl_abapgit_objects=>is_type_supported( is_item-obj_type )
    " This will trigger class constructor of zcl_abapgit_objects_bridge reading table seometarel
    " which is currently not supported by abaplint test runner
 
    TRY.
        lv_class = 'ZCL_ABAPGIT_OBJECT_' && cs_item-obj_type.
 
        CALL METHOD (lv_class)=>('ZIF_ABAPGIT_OBJECT~MAP_FILENAME_TO_OBJECT')
          EXPORTING
            iv_filename = iv_filename
            iv_path     = iv_path
            io_dot      = io_dot
            iv_package  = iv_package
          CHANGING
            cs_item     = cs_item.
      CATCH cx_sy_dyn_call_illegal_class.
        " Map data config to TABU object type
        IF cs_item-obj_type = 'CONF'.
          cs_item-obj_type = 'TABU'.
        ENDIF.
    ENDTRY.
 
  ENDMETHOD.
 
 
  METHOD map_object_to_filename.
 
    DATA lv_class TYPE seoclsname.
 
    " TODO: Add check for supported object types to avoid calls to non-existing classes
    " zcl_abapgit_objects=>is_type_supported( is_item-obj_type )
    " This will trigger class constructor of zcl_abapgit_objects_bridge reading table seometarel
    " which is currently not supported by abaplint test runner
 
    TRY.
        lv_class = 'ZCL_ABAPGIT_OBJECT_' && is_item-obj_type.
 
        CALL METHOD (lv_class)=>('ZIF_ABAPGIT_OBJECT~MAP_OBJECT_TO_FILENAME')
          EXPORTING
            is_item     = is_item
            iv_ext      = iv_ext
            iv_extra    = iv_extra
          CHANGING
            cv_filename = cv_filename.
      CATCH cx_sy_dyn_call_illegal_class ##NO_HANDLER.
    ENDTRY.
 
  ENDMETHOD.
 
 
  METHOD name_escape.
    " Some characters in object names cause problems when identifying the object later
    " -> we escape these characters here
    " cl_http_utility=>escape_url doesn't do dots but escapes slash which we use for namespaces
    " -> we escape just some selected characters
    rv_name = iv_name.
    REPLACE ALL OCCURRENCES OF `#` IN rv_name WITH '%23'.
    REPLACE ALL OCCURRENCES OF `%` IN rv_name WITH '%25'.
    REPLACE ALL OCCURRENCES OF `.` IN rv_name WITH '%2e'.
    REPLACE ALL OCCURRENCES OF `<` IN rv_name WITH '%3c'.
    REPLACE ALL OCCURRENCES OF `=` IN rv_name WITH '%3d'.
    REPLACE ALL OCCURRENCES OF `>` IN rv_name WITH '%3e'.
    REPLACE ALL OCCURRENCES OF `?` IN rv_name WITH '%3f'.
  ENDMETHOD.
 
 
  METHOD name_unescape.
    " Replace all %xy with encoded character
    rv_name = cl_http_utility=>unescape_url( iv_name ).
  ENDMETHOD.
 
 
  METHOD object_to_file.
 
    DATA lv_obj_name TYPE string.
    DATA lv_obj_type TYPE string.
    DATA lv_nb_of_slash TYPE string.
 
    " Get escaped object name
    lv_obj_name = to_lower( name_escape( is_item-obj_name ) ).
    lv_obj_type = to_lower( is_item-obj_type ).
 
    IF iv_extra IS INITIAL.
      CONCATENATE lv_obj_name '.' lv_obj_type INTO rv_filename.
    ELSE.
      CONCATENATE lv_obj_name '.' lv_obj_type '.' iv_extra INTO rv_filename.
    ENDIF.
 
    IF iv_ext IS NOT INITIAL.
      CONCATENATE rv_filename '.' iv_ext INTO rv_filename.
    ENDIF.
 
    " Get mapping specific to object type
    TRY.
        map_object_to_filename(
          EXPORTING
            is_item     = is_item
            iv_ext      = iv_ext
            iv_extra    = iv_extra
          CHANGING
            cv_filename = rv_filename ).
      CATCH zcx_abapgit_exception ##NO_HANDLER.
    ENDTRY.
 
    " Handle namespaces
    CREATE OBJECT go_aff_registry TYPE zcl_abapgit_aff_registry.
 
    IF go_aff_registry->is_supported_object_type( is_item-obj_type ) = abap_true.
      FIND ALL OCCURRENCES OF `/` IN rv_filename MATCH COUNT lv_nb_of_slash.
      IF lv_nb_of_slash = 2.
        REPLACE FIRST OCCURRENCE OF `/` IN rv_filename WITH `(`.
        REPLACE `/` IN rv_filename WITH `)`.
      ENDIF.
    ELSE.
      REPLACE ALL OCCURRENCES OF '/' IN rv_filename WITH '#'.
    ENDIF.
 
    IF iv_ext = 'properties'.
      RETURN.
    ENDIF.
 
    TRANSLATE rv_filename TO LOWER CASE.
 
  ENDMETHOD.
 
 
  METHOD object_to_i18n_file.
    DATA: lv_langu_sap1 TYPE sy-langu,
          lv_langu_bcp47 TYPE string.
 
    lv_langu_sap1 = zcl_abapgit_convert=>language_sap2_to_sap1( to_upper( iv_lang ) ).
    lv_langu_bcp47 = zcl_abapgit_convert=>language_sap1_to_bcp47( lv_langu_sap1 ).
 
    rv_filename = object_to_file(
      is_item  = is_item
      iv_extra = |i18n.{ lv_langu_bcp47 }|
      iv_ext   = iv_ext ).
 
  ENDMETHOD.
 
  METHOD get_lang_and_ext.
 
    DATA lt_filename_elements TYPE string_table.
    DATA lv_langu_bcp47 TYPE string.
    DATA lv_sap1 TYPE sy-langu.
 
    SPLIT iv_filename AT '.' INTO TABLE lt_filename_elements.
 
    READ TABLE lt_filename_elements INDEX lines( lt_filename_elements ) INTO ev_ext.
    IF sy-subrc <> 0.
      zcx_abapgit_exception=>raise( |Could not derive file extension of file { iv_filename }| ).
    ENDIF.
 
    READ TABLE lt_filename_elements WITH KEY table_line = `i18n` TRANSPORTING NO FIELDS.
    IF sy-subrc = 0.
      READ TABLE lt_filename_elements INDEX ( sy-tabix + 1 ) INTO lv_langu_bcp47.
      IF sy-subrc = 0.
        lv_sap1 = zcl_abapgit_convert=>language_bcp47_to_sap1( lv_langu_bcp47 ).
        ev_lang = zcl_abapgit_convert=>language_sap1_to_sap2( lv_sap1 ). " actually it is to_upper( ISO-639 )
 
        " to not break existing PO file implementations
        IF ev_ext = `po`.
          ev_lang = to_lower( ev_lang ).
        ENDIF.
      ENDIF.
    ENDIF.
 
    IF ev_lang IS INITIAL.
      CLEAR ev_ext.
    ENDIF.
 
  ENDMETHOD.
 
ENDCLASS.