text2tab

website·Indexed 29 days ago

https://sbcgua.github.io/text2tab

An ABAP utility used to parse TAB-delimited text into an internal table of an arbitrary flat structure. It features strict and corresponding parsing, field name remapping, and serialization via the ZCL_TEXT2TAB_SERIALIZER class. The tool supports deep parsing for tables or structures using methods in ZCL_TEXT2TAB_UTILS and can be installed via abapGit.

Tokens
4.2K
Snippets
21
Records
30
Agent score
75%

What's inside text2tab

  1. Overview of text2tab capabilities

    text2tab is a tool for parsing tab-delimited text into ABAP structures or tables and serializing ABAP data back into tab-delimited text. Key features include:

    • Data Validation: Supports strict and corresponding parsing modes.
    • Header Handling: Supports parsing with or without header lines, including automatic field order detection.
    • Parsing Flexibility: Supports type-less parsing (all fields as strings) and deep parsing for filling deep structures.
    • Formatting Control: Allows configuration of date formats, number formats, and line-break characters.
    • Field Mapping: Supports on-the-fly field name remapping (e.g., converting a text field foo to an ABAP field bar).
    • Serialization: Converts ABAP tables or structures into tab-delimited text, with optional header lines.
  2. Overview of Text2tab parsing and serialization features

    Text2tab is an ABAP utility used to parse TAB-delimited text into an internal table of an arbitrary flat structure, or to serialize a flat table/structure back into text. Key capabilities include:

    • Non-strict mode: Allows skipping fields in source data when only specific fields are being loaded.
    • Corresponding parsing: Fills only fields that exist in the target structure.
    • Header support: Uses the first line of text as a header specification, allowing the text field order to differ from the ABAP structure order.
    • Type-less parsing: Dynamically creates a table with string fields without checking against an existing structure.
    • Formatting: Supports custom date and amount formats, as well as custom line-break symbols for serialization.
    • Field Remapping: Supports on-the-fly remapping of field names (e.g., mapping field FOO in text to BAR in the target table).
    • Deep Parsing: Supports filling structure or table components within the target data container.
  3. Perform type-less parsing

    Use create_typeless() to parse data without validating against a predefined structure. The parser dynamically generates a table where each field is an unconverted string. The list of detected field names is returned in e_head_fields.
    data: lr_data   type ref to data,
         lt_fields type string_table.
    
    zcl_text2tab_parser=>create_typeless( )->parse( 
      exporting 
        i_data      = my_get_some_raw_text_data( )
      importing 
        e_head_fields = lt_fields
        e_container   = lr_data ).
  4. Verify the minimum required version of text2tab

    Use the check_version_fits method of the zcl_text2tab_parser class to determine if the installed text2tab package meets a minimum version requirement. This method returns a boolean indicating whether the current version is greater than or equal to the version string provided as an argument.
    " Returns false if current version is 2.1.0 and 2.1.2 is required
    zcl_text2tab_parser=>check_version_fits( 'v2.1.2' ). 
    
    " Returns true if current version is 2.1.0 and 2.0.0 is required
    zcl_text2tab_parser=>check_version_fits( 'v2.0.0' ). 
  5. Install text2tab using abapGit

    The text2tab tool is an open-source project distributed under the MIT license. It can be installed into an ABAP system using abapGit. The package is divided into two subpackages: core (containing the tool logic) and examples (containing example programs). If you only require the tool for deserialization, you can skip the examples package by using abapGit's filename filtering and specifying */examples/* in the repository settings.
  6. Reformat text2tab error messages using zcx_text2tab_error

    The zcx_text2tab_error exception class provides attributes that allow developers to customize and reformat error messages for better end-user reporting. Available attributes include structure, field, line, and msg. This functionality is currently supported only in the parser; the serializer does not produce many line-level errors.
    ...
    catch zcx_text2tab_error into lx. " Reformat to -> Import error at line LINE, field 'FIELD': MSG
    
      l_error_msg = 'Import error'.
      if lx->line is not initial.
        l_error_msg = |{ l_error_msg } at line { lx->line }|.
      endif.
      if lx->field is not initial.
        l_error_msg = |{ l_error_msg }, field '{ lx->field }'|.
      endif.
      l_error_msg = |{ l_error_msg }: { lx->msg }|.
    
      raise exception type lcx_my_program_error
        exporting msg = l_error_msg.
    endtry.
  7. Parse TAB-delimited text into an ABAP internal table

    The text2tab (lcl_data_parser) library allows parsing TAB-delimited text (such as data copied from Excel) into an arbitrary flat structure or internal table in ABAP. It uses CRLF as the line delimiter and TAB as the field delimiter.
    types: 
      begin of my_table_type,
        name      type char10,
        birthdate type datum,
      end of my_table_type.
    
    data lt_container type table of my_table_type.
    
    lcl_data_parser=>create( lt_container )->parse(
      exporting i_data      = lv_some_string_with_data
      importing e_container = lt_container ).
  8. Implement deep parsing for nested tables and structures

    To fill deep fields (such as nested tables or structures) during a single parser run, you must implement the zif_text2tab_deep_provider interface and pass the implementation to the parser. This allows the parser to fetch additional data from external sources (files, web, or database) when it encounters a field designated as 'deep'.
    zcl_text2tab_parser=>create( 
      i_pattern = lt_container 
      i_deep_provider = lo_implementation_of_zif_text2tab_deep_provider
    )->parse(
      exporting
        i_data          = my_raw_text_data
      importing
        e_container = lt_container ).
  9. Perform corresponding parsing

    To move only fields that have matching names between the source text and the target structure, set i_corresponding = abap_true and i_strict = abap_false in the parse method.
    zcl_text2tab_parser=>create( lt_container )->parse(
      exporting
        i_data          = my_get_some_raw_text_data( )
        i_strict        = abap_false
        i_corresponding = abap_true
      importing
        e_container = lt_container ).
  10. Configure date and line-break formats in TEXT2TAB

    When using the TEXT2TAB serializer or parser, you can specify the date format using a 4-character string (e.g., DMY.). The first three characters define the order of Day, Month, and Year, and the fourth character defines the separator symbol. Additionally, the parser now supports auto-detection of LF line breaks and handles double quotes around field values by removing them during parsing.