From 61c6da81a6d8a49f144a8ab7c4e294a3a509b8ff Mon Sep 17 00:00:00 2001 From: Gerbershagen Date: Fri, 9 Nov 2018 13:28:55 +0100 Subject: [PATCH 1/3] v2.2.0 added comment lines --- changelog.txt | 4 +++ zcl_text2tab_parser.clas.abap | 44 ++++++++++++++++++++++- zcl_text2tab_parser.clas.testclasses.abap | 15 ++++++-- zcl_text2tab_parser.clas.xml | 2 +- zcl_text2tab_serializer.clas.xml | 2 +- zcl_text2tab_utils.clas.xml | 2 +- zcx_text2tab_error.clas.abap | 2 +- zcx_text2tab_error.clas.xml | 2 +- ztext2tab_example.prog.xml | 9 +---- 9 files changed, 65 insertions(+), 17 deletions(-) diff --git a/changelog.txt b/changelog.txt index deb7a04..b8c1054 100644 --- a/changelog.txt +++ b/changelog.txt @@ -8,6 +8,10 @@ Legend + : added - : removed +v2.2.0, 2018-11-09 +------------------ ++ comment lines + v2.1.0, 2018-11-08 ------------------ + field name mapping feature (rename fields on-the-fly) diff --git a/zcl_text2tab_parser.clas.abap b/zcl_text2tab_parser.clas.abap index 58d0670..625d6f6 100644 --- a/zcl_text2tab_parser.clas.abap +++ b/zcl_text2tab_parser.clas.abap @@ -27,6 +27,7 @@ public section. !I_PATTERN type ANY " target structure or table !I_AMOUNT_FORMAT type CHAR2 optional !I_DATE_FORMAT type CHAR4 optional + i_begin_comment TYPE clike OPTIONAL returning value(RO_PARSER) type ref to ZCL_TEXT2TAB_PARSER raising @@ -56,6 +57,7 @@ private section. data MV_CURRENT_FIELD type STRING . data MV_LINE_INDEX type SY-TABIX . data MV_IS_TYPELESS type ABAP_BOOL . + data MV_BEGIN_COMMENT type RVARI_VAL_255 . methods PARSE_TYPEFULL importing @@ -157,11 +159,16 @@ private section. !CODE type CHAR2 optional raising ZCX_TEXT2TAB_ERROR . - class-methods BREAK_TO_LINES + methods BREAK_TO_LINES importing !I_TEXT type STRING returning value(RT_TAB) type STRING_TABLE . + methods IS_COMMENT_LINE + importing + !LINE type STRING + returning + value(IS) type ABAP_BOOL . ENDCLASS. @@ -204,7 +211,9 @@ endmethod. "apply_conv_exit method BREAK_TO_LINES. data: l_found type i, + idx TYPE i, l_break type string value c_crlf. + FIELD-SYMBOLS: TYPE string. " Detect line break l_found = find( val = i_text sub = c_crlf ). @@ -217,6 +226,13 @@ method BREAK_TO_LINES. split i_text at l_break into table rt_tab. + LOOP AT rt_tab ASSIGNING . + idx = sy-tabix. + if is_comment_line( ) = abap_true. + delete rt_tab INDEX idx. + endif. + ENDLOOP. + endmethod. @@ -239,6 +255,18 @@ method CREATE. ro_parser->mv_date_format = i_date_format. endif. + CLEAR ro_parser->mv_begin_comment. + IF i_begin_comment IS SUPPLIED. + ro_parser->mv_begin_comment = i_begin_comment. + ELSE. + SELECT low UP TO 1 ROWS FROM tvarvc INTO ro_parser->mv_begin_comment + WHERE name = 'ZMOCKUP_LOADER_COMMENT' AND type = 'P'. + ENDSELECT. + " IF sy-subrc <> 0. + " by default no comment lines + " ENDIF. + ENDIF. + endmethod. "create @@ -273,6 +301,20 @@ method GET_SAFE_STRUC_DESCR. endmethod. "get_safe_struc_descr + method IS_COMMENT_LINE. + DATA: offset TYPE offset. + + is = abap_false. + + FIND FIRST OCCURRENCE OF REGEX '[^[:space:]]' IN line + MATCH OFFSET offset. + IF sy-subrc = 0 AND line+offset(1) CO mv_begin_comment. + is = abap_true. + ENDIF. + + endmethod. + + method MAP_HEAD_STRUCTURE. data: l_field_cnt type i, diff --git a/zcl_text2tab_parser.clas.testclasses.abap b/zcl_text2tab_parser.clas.testclasses.abap index 0bea87f..0625aa4 100644 --- a/zcl_text2tab_parser.clas.testclasses.abap +++ b/zcl_text2tab_parser.clas.testclasses.abap @@ -119,7 +119,8 @@ class lcl_text2tab_parser_test definition for testing methods parse_field_unsupp for testing. methods map_head_structure for testing. methods get_safe_struc_descr for testing. - methods break_to_lines for testing. + methods break_to_lines for testing + RAISING zcx_text2tab_error. methods parse_line_negative for testing. methods parse_data_empty_line for testing. @@ -233,9 +234,17 @@ class lcl_text2tab_parser_test implementation. append 'line1' to lt_exp. append 'line2' to lt_exp. - lt_act = zcl_text2tab_parser=>break_to_lines( 'line1' && c_crlf && 'line2' ). + lt_act = o->break_to_lines( 'line1' && c_crlf && 'line2' ). cl_abap_unit_assert=>assert_equals( act = lt_act exp = lt_exp ). - lt_act = zcl_text2tab_parser=>break_to_lines( 'line1' && c_lf && 'line2' ). + lt_act = o->break_to_lines( 'line1' && c_lf && 'line2' ). + cl_abap_unit_assert=>assert_equals( act = lt_act exp = lt_exp ). + + " create instance with comment line + o = zcl_text2tab_parser=>create( i_pattern = c_dummy i_begin_comment = '*' ). + CLEAR lt_exp. + APPEND 'not a comment 1' to lt_exp. + APPEND 'not a comment 2' to lt_exp. + lt_act = o->break_to_lines( '*a comment' && c_lf && 'not a comment 1' && c_lf && 'not a comment 2' ). cl_abap_unit_assert=>assert_equals( act = lt_act exp = lt_exp ). endmethod. " break_to_lines. diff --git a/zcl_text2tab_parser.clas.xml b/zcl_text2tab_parser.clas.xml index 291d5f8..c087079 100644 --- a/zcl_text2tab_parser.clas.xml +++ b/zcl_text2tab_parser.clas.xml @@ -1,4 +1,4 @@ - + diff --git a/zcl_text2tab_serializer.clas.xml b/zcl_text2tab_serializer.clas.xml index e7b5186..d2dc80c 100644 --- a/zcl_text2tab_serializer.clas.xml +++ b/zcl_text2tab_serializer.clas.xml @@ -1,4 +1,4 @@ - + diff --git a/zcl_text2tab_utils.clas.xml b/zcl_text2tab_utils.clas.xml index ce480c9..8e45adf 100644 --- a/zcl_text2tab_utils.clas.xml +++ b/zcl_text2tab_utils.clas.xml @@ -1,4 +1,4 @@ - + diff --git a/zcx_text2tab_error.clas.abap b/zcx_text2tab_error.clas.abap index 54ff387..e679399 100644 --- a/zcx_text2tab_error.clas.abap +++ b/zcx_text2tab_error.clas.abap @@ -46,7 +46,7 @@ ENDCLASS. CLASS ZCX_TEXT2TAB_ERROR IMPLEMENTATION. -method CONSTRUCTOR. +method CONSTRUCTOR ##ADT_SUPPRESS_GENERATION. CALL METHOD SUPER->CONSTRUCTOR EXPORTING PREVIOUS = PREVIOUS diff --git a/zcx_text2tab_error.clas.xml b/zcx_text2tab_error.clas.xml index 16d6f43..63e7172 100644 --- a/zcx_text2tab_error.clas.xml +++ b/zcx_text2tab_error.clas.xml @@ -1,4 +1,4 @@ - + diff --git a/ztext2tab_example.prog.xml b/ztext2tab_example.prog.xml index e80a960..27108ff 100644 --- a/ztext2tab_example.prog.xml +++ b/ztext2tab_example.prog.xml @@ -1,4 +1,4 @@ - + @@ -11,13 +11,6 @@ X X - - - R - Table parser - 12 - - From 55c657a2405a92174ccb49128650a296ad760364 Mon Sep 17 00:00:00 2001 From: Gerbershagen Date: Tue, 13 Nov 2018 13:44:35 +0100 Subject: [PATCH 2/3] feedback from pull request #1 --- zcl_text2tab_parser.clas.abap | 67 ++++++++--------------- zcl_text2tab_parser.clas.testclasses.abap | 22 ++++---- 2 files changed, 34 insertions(+), 55 deletions(-) diff --git a/zcl_text2tab_parser.clas.abap b/zcl_text2tab_parser.clas.abap index 625d6f6..6f9191e 100644 --- a/zcl_text2tab_parser.clas.abap +++ b/zcl_text2tab_parser.clas.abap @@ -57,7 +57,7 @@ private section. data MV_CURRENT_FIELD type STRING . data MV_LINE_INDEX type SY-TABIX . data MV_IS_TYPELESS type ABAP_BOOL . - data MV_BEGIN_COMMENT type RVARI_VAL_255 . + data MV_BEGIN_COMMENT type char1. methods PARSE_TYPEFULL importing @@ -159,16 +159,12 @@ private section. !CODE type CHAR2 optional raising ZCX_TEXT2TAB_ERROR . - methods BREAK_TO_LINES + class-methods BREAK_TO_LINES importing !I_TEXT type STRING + i_begin_comment TYPE char1 returning value(RT_TAB) type STRING_TABLE . - methods IS_COMMENT_LINE - importing - !LINE type STRING - returning - value(IS) type ABAP_BOOL . ENDCLASS. @@ -208,12 +204,11 @@ method APPLY_CONV_EXIT. endmethod. "apply_conv_exit -method BREAK_TO_LINES. +method break_to_lines. data: - l_found type i, - idx TYPE i, - l_break type string value c_crlf. - FIELD-SYMBOLS: TYPE string. + l_found type i, + l_break type string value c_crlf. + field-symbols: type string. " Detect line break l_found = find( val = i_text sub = c_crlf ). @@ -226,12 +221,18 @@ method BREAK_TO_LINES. split i_text at l_break into table rt_tab. - LOOP AT rt_tab ASSIGNING . - idx = sy-tabix. - if is_comment_line( ) = abap_true. - delete rt_tab INDEX idx. - endif. - ENDLOOP. + if i_begin_comment <> space. + loop at rt_tab assigning . + try. + if +0(1) = i_begin_comment. + delete rt_tab index sy-tabix. + endif. + catch cx_sy_range_out_of_bounds. + " if the row only consist of a linefeed. Some text editors add always a line feed at the end of the document + delete rt_tab index sy-tabix. + endtry. + endloop. + endif. endmethod. @@ -255,17 +256,7 @@ method CREATE. ro_parser->mv_date_format = i_date_format. endif. - CLEAR ro_parser->mv_begin_comment. - IF i_begin_comment IS SUPPLIED. - ro_parser->mv_begin_comment = i_begin_comment. - ELSE. - SELECT low UP TO 1 ROWS FROM tvarvc INTO ro_parser->mv_begin_comment - WHERE name = 'ZMOCKUP_LOADER_COMMENT' AND type = 'P'. - ENDSELECT. - " IF sy-subrc <> 0. - " by default no comment lines - " ENDIF. - ENDIF. + ro_parser->mv_begin_comment = i_begin_comment. endmethod. "create @@ -301,20 +292,6 @@ method GET_SAFE_STRUC_DESCR. endmethod. "get_safe_struc_descr - method IS_COMMENT_LINE. - DATA: offset TYPE offset. - - is = abap_false. - - FIND FIRST OCCURRENCE OF REGEX '[^[:space:]]' IN line - MATCH OFFSET offset. - IF sy-subrc = 0 AND line+offset(1) CO mv_begin_comment. - is = abap_true. - ENDIF. - - endmethod. - - method MAP_HEAD_STRUCTURE. data: l_field_cnt type i, @@ -842,7 +819,7 @@ method PARSE_TYPEFULL. raise_error( msg = 'Container type does not fit pattern' code = 'TE' ). "#EC NOTEXT endif. - lt_data = break_to_lines( i_data ). + lt_data = break_to_lines( i_text = i_data i_begin_comment = mv_begin_comment ). " Read and process header line if i_has_head = abap_true. @@ -877,7 +854,7 @@ endmethod. "parse_typefull data lt_map type int4_table. field-symbols like line of e_head_fields. - lt_data = break_to_lines( i_data ). + lt_data = break_to_lines( i_text = i_data i_begin_comment = mv_begin_comment ). " Read and process header line parse_head_line( diff --git a/zcl_text2tab_parser.clas.testclasses.abap b/zcl_text2tab_parser.clas.testclasses.abap index 0625aa4..fd60092 100644 --- a/zcl_text2tab_parser.clas.testclasses.abap +++ b/zcl_text2tab_parser.clas.testclasses.abap @@ -119,8 +119,7 @@ class lcl_text2tab_parser_test definition for testing methods parse_field_unsupp for testing. methods map_head_structure for testing. methods get_safe_struc_descr for testing. - methods break_to_lines for testing - RAISING zcx_text2tab_error. + methods break_to_lines for testing. methods parse_line_negative for testing. methods parse_data_empty_line for testing. @@ -234,17 +233,20 @@ class lcl_text2tab_parser_test implementation. append 'line1' to lt_exp. append 'line2' to lt_exp. - lt_act = o->break_to_lines( 'line1' && c_crlf && 'line2' ). + lt_act = zcl_text2tab_parser=>break_to_lines( i_text = 'line1' && c_crlf && 'line2' + i_begin_comment = space ). cl_abap_unit_assert=>assert_equals( act = lt_act exp = lt_exp ). - lt_act = o->break_to_lines( 'line1' && c_lf && 'line2' ). + lt_act = zcl_text2tab_parser=>break_to_lines( i_text = 'line1' && c_lf && 'line2' + i_begin_comment = space ). cl_abap_unit_assert=>assert_equals( act = lt_act exp = lt_exp ). - " create instance with comment line - o = zcl_text2tab_parser=>create( i_pattern = c_dummy i_begin_comment = '*' ). - CLEAR lt_exp. - APPEND 'not a comment 1' to lt_exp. - APPEND 'not a comment 2' to lt_exp. - lt_act = o->break_to_lines( '*a comment' && c_lf && 'not a comment 1' && c_lf && 'not a comment 2' ). + " with comment line + clear lt_exp. + append 'not a comment 1' to lt_exp. + append 'not a comment 2' to lt_exp. + lt_act = zcl_text2tab_parser=>break_to_lines( i_text = + '*a comment' && c_lf && 'not a comment 1' && c_lf && 'not a comment 2' && c_lf + && cl_abap_char_utilities=>newline i_begin_comment = '*' ). cl_abap_unit_assert=>assert_equals( act = lt_act exp = lt_exp ). endmethod. " break_to_lines. From 190d95451f1caf69cb362aaebe00e78d79de4078 Mon Sep 17 00:00:00 2001 From: Gerbershagen Date: Wed, 21 Nov 2018 12:37:47 +0100 Subject: [PATCH 3/3] documentation for comment lines --- README.md | 16 ++++++++++++++++ zcl_text2tab_parser.clas.abap | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f0842b4..a26cb3b 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,22 @@ To do serialization use `ZCL_TEXT2TAB_SERIALIZER` class. Flat tables and structu lv_string = lo_serializer->serialize( lt_some_table ). ``` +## Comments + +Since version 2.2.0 the text file can contain comment lines. A comment lines begins with one specific char, which is supplied in the factory method ```create```. +A sample text-file: +``` +* comment line: expected birthdays in test class ... +NAME BIRTHDAY +JOHN 01.01.1990 +``` +Now we should call the factory method like this and the first line is interpreted as a comment: +```abap +zcl_text2tab_parser=>create( i_pattern = ls_birthday i_begin_comment = '*' ). +``` +The char '*' must have the first position in the text line. Otherwise it isn't interpreted as a comment. + + ## Error message redefinition The exception class - `zcx_text2tab_error` - exposes `struc`, `field`, `line` and `msg` attributes (and some others). They can be used to reformat the message text if needed. For example: diff --git a/zcl_text2tab_parser.clas.abap b/zcl_text2tab_parser.clas.abap index 6f9191e..8156b32 100644 --- a/zcl_text2tab_parser.clas.abap +++ b/zcl_text2tab_parser.clas.abap @@ -27,7 +27,7 @@ public section. !I_PATTERN type ANY " target structure or table !I_AMOUNT_FORMAT type CHAR2 optional !I_DATE_FORMAT type CHAR4 optional - i_begin_comment TYPE clike OPTIONAL + i_begin_comment TYPE char1 OPTIONAL returning value(RO_PARSER) type ref to ZCL_TEXT2TAB_PARSER raising