*&---------------------------------------------------------------------*
*&  Include           ZUTIL_CONVERT_TEXT                               *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*& This file is part of ZUTIL.                                         *
*&                                                                     *
*& ZUTIL is free software: you can redistribute it and/or modify       *
*& it under the terms of the GNU General Public License as published   *
*& by the Free Software Foundation, either version 3 of the License,   *
*& or any later version.                                               *
*&                                                                     *
*& ZUTIL is distributed in the hope that it will be useful,            *
*& but WITHOUT ANY WARRANTY; without even the implied warranty of      *
*& MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       *
*& GNU General Public License for more details.                        *
*&                                                                     *
*& You should have received a copy of the GNU General Public License   *
*& along with ZUTIL. If not, see <http://www.gnu.org/licenses/>.       *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&  Author:     Ruediger von Creytz     ruediger.creytz@globalbit.net  *
*&  Copyright:  globalBIT, LLC          http://www.globalbit.net       *
*&                                                                     *
*&---------------------------------------------------------------------*


*-----------------------------------------------------------------------
* decode_html
*-----------------------------------------------------------------------
FORM decode_html
  CHANGING
    c_txt TYPE any.

  DATA:
  l_xstr TYPE xstring,
  l_hex TYPE string,
  l_xpos TYPE i,
  l_str TYPE string,
  l_fdpos TYPE i,
  l_strlen TYPE i,
  l_out TYPE string,
  l_outlen TYPE i,
  l_char TYPE string.

  CHECK NOT c_txt IS INITIAL.
  CHECK c_txt CS '&#x'.

  l_str = c_txt.

  DO.
    IF l_str CS '&#x'.
      l_fdpos = sy-fdpos.
      IF l_fdpos > 0.
        CONCATENATE l_out+0(l_outlen) l_str+0(l_fdpos) INTO l_out.
        l_outlen = l_outlen + l_fdpos.
      ENDIF.
      l_xpos = l_fdpos + 3.
      l_strlen = strlen( l_str ).
      l_strlen = l_strlen - l_xpos.
      l_str = l_str+l_xpos(l_strlen).
      SPLIT l_str AT ';' INTO l_hex l_str.
      l_xstr = l_hex.

      CALL FUNCTION 'NLS_STRING_CONVERT_TO_SYS'
        EXPORTING
          lang_used = sy-langu
          SOURCE    = l_xstr
        IMPORTING
          RESULT    = l_char
        EXCEPTIONS
          OTHERS    = 1.
      IF sy-subrc = 0.
        CONCATENATE l_out+0(l_outlen) l_char+0(1) INTO l_out.
      ELSE.
        CONCATENATE l_out+0(l_outlen) '#' INTO l_out.
      ENDIF.
      l_outlen = l_outlen + 1.
    ELSE.
      EXIT.
    ENDIF.
  ENDDO.

  l_strlen = strlen( l_str ).
  CONCATENATE l_out+0(l_outlen) l_str+0(l_strlen) INTO l_out.
  c_txt = l_out.
ENDFORM.                    "decode_html


*-----------------------------------------------------------------------
* decode_html_io
*-----------------------------------------------------------------------
FORM decode_html_io
  USING
    i_html TYPE string
  CHANGING
    c_txt TYPE any.

  c_txt = i_html.

  PERFORM decode_html
    CHANGING
      c_txt.

ENDFORM.                    "decode_html_io


*-----------------------------------------------------------------------
* encode_html
*-----------------------------------------------------------------------
FORM encode_html
  CHANGING
    c_txt TYPE string.

  CHECK NOT c_txt IS INITIAL.
  CHECK c_txt NS '&#'.

  PERFORM encode_html_force
    CHANGING c_txt.

ENDFORM.                    "encode_html


*-----------------------------------------------------------------------
* encode_html_force
*-----------------------------------------------------------------------
FORM encode_html_force
  CHANGING
    c_txt TYPE string.

  DATA:
  l_xstr TYPE xstring,
  l_hex TYPE string,
  l_hexlen TYPE i,
  l_xlen TYPE i,
  l_xpos TYPE i,
  l_str TYPE string,
  l_strlen TYPE i,
  l_out TYPE string,
  l_outlen TYPE i,
  l_pos TYPE i,
  l_chars(255) TYPE c,
  l_xchar(4) TYPE c.


  CHECK NOT c_txt IS INITIAL.

  CONCATENATE         "characters not to encode
    ' :;,./()-_+*#='
    '0123456789'
    'abcdefghijklmnopqrstuvwxyz'
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    INTO l_chars.

  l_str = c_txt.


  CALL FUNCTION 'NLS_STRING_CONVERT_FROM_SYS'
    EXPORTING
      lang_used = sy-langu
      SOURCE    = l_str
    IMPORTING
      RESULT    = l_xstr.

  l_hex = l_xstr.
  l_hexlen = strlen( l_hex ).
  l_strlen = strlen( l_str ).
  l_xlen = l_hexlen / l_strlen.

  DO.
    l_xpos = l_pos * l_xlen.
    l_xchar = l_hex+l_xpos(l_xlen).
    IF l_chars NA l_str+l_pos(1)
    AND NOT l_xchar = '23'.
      IF l_outlen = 0.
        CONCATENATE '&#x' l_xchar ';' INTO l_out.
      ELSE.
        CONCATENATE l_out+0(l_outlen) '&#x' l_xchar ';'
          INTO l_out.
      ENDIF.
      l_outlen = l_outlen + 4 + l_xlen.
    ELSE.
      IF l_outlen = 0.
        l_out = l_str+l_pos(1).
      ELSE.
        CONCATENATE l_out+0(l_outlen) l_str+l_pos(1) INTO l_out.
      ENDIF.
      l_outlen = l_outlen + 1.
    ENDIF.

    l_pos = l_pos + 1.
    l_strlen = strlen( l_str ).
    IF l_pos = l_strlen.
      EXIT.
    ENDIF.
  ENDDO.

  c_txt = l_out.
ENDFORM.                    "encode_html_force


*-----------------------------------------------------------------------
* form: encode_uri
*-----------------------------------------------------------------------
FORM encode_uri
  CHANGING
    c_url TYPE string.

  DATA:
  l_prot TYPE string,
  l_url TYPE string,
  l_params TYPE string,
  lt_param TYPE it_string,
  l_key TYPE string,
  l_value TYPE string,
  l_len TYPE i.

  FIELD-SYMBOLS:
  <l_param> TYPE string.

  CHECK NOT c_url IS INITIAL.

  IF c_url CA '?' OR c_url NA '='.
    SPLIT c_url AT '?' INTO l_url l_params.
    IF l_url CA ':'.
      SPLIT l_url AT ':' INTO l_prot l_url.
    ENDIF.
    PERFORM encode_uri_link CHANGING l_url.
    IF NOT l_prot IS INITIAL.
      CONCATENATE l_prot ':' l_url INTO l_url.
    ENDIF.
    CONCATENATE l_url '?' INTO l_url.
  ELSEIF c_url CA '='.
    l_params = c_url.
  ENDIF.

  SPLIT l_params AT '&' INTO TABLE lt_param.
  LOOP AT lt_param ASSIGNING <l_param>.
    CLEAR: l_key, l_value.
    SPLIT <l_param> AT '=' INTO l_key l_value.
    PERFORM encode_uri_value CHANGING l_value.
    CONCATENATE l_url l_key '=' l_value '&' INTO l_url.
  ENDLOOP.

  l_len = strlen( l_url ).
  l_len = l_len - 1. "remove last '&'
  c_url = l_url+0(l_len).
ENDFORM.                    "encode_uri


*-----------------------------------------------------------------------
* form: encode_uri_link
*-----------------------------------------------------------------------
FORM encode_uri_link
  CHANGING
    c_value TYPE string.

  DATA:
  l_chars(66) TYPE c.

  CHECK NOT c_value IS INITIAL.

  CONCATENATE         "characters not to encode
    '/_-.'
    '0123456789'
    'abcdefghijklmnopqrstuvwxyz'
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    INTO l_chars.

  PERFORM encode_uri_string
    USING l_chars
    CHANGING c_value.

ENDFORM.                    "encode_uri_link


*-----------------------------------------------------------------------
* form: encode_uri_string
*-----------------------------------------------------------------------
FORM encode_uri_string
  USING
    value(i_chars)
  CHANGING
    c_value TYPE string.

  DATA:
  l_xstr TYPE xstring,
  l_hex TYPE string,
  l_hexlen TYPE i,
  l_chex TYPE string,
  l_chex_pos TYPE i,
  l_xlen TYPE i,
  l_xpos TYPE i,
  l_str TYPE string,
  l_strlen TYPE i,
  l_out TYPE string,
  l_outlen TYPE i,
  l_pos TYPE i.

  CHECK NOT c_value IS INITIAL.

  l_str = c_value.


  CALL FUNCTION 'NLS_STRING_CONVERT_FROM_SYS'
    EXPORTING
      lang_used = sy-langu
      SOURCE    = l_str
    IMPORTING
      RESULT    = l_xstr.

  l_hex = l_xstr.
  l_hexlen = strlen( l_hex ).
  l_strlen = strlen( l_str ).
  l_xlen = l_hexlen / l_strlen.

  DO.
    IF i_chars NA l_str+l_pos(1).
      l_xpos = l_pos * l_xlen.
      l_chex = l_hex+l_xpos(l_xlen).
      l_chex_pos = strlen( l_chex ).
      l_chex_pos = l_chex_pos - 2.
      l_chex = l_chex+l_chex_pos(2).
      IF l_outlen = 0.
        CONCATENATE '%' l_chex INTO l_out.
      ELSE.
        CONCATENATE l_out+0(l_outlen) '%' l_chex
          INTO l_out.
      ENDIF.
      l_outlen = l_outlen + 3.
    ELSE.
      IF l_outlen = 0.
        l_out = l_str+l_pos(1).
      ELSE.
        CONCATENATE l_out+0(l_outlen) l_str+l_pos(1) INTO l_out.
      ENDIF.
      l_outlen = l_outlen + 1.
    ENDIF.

    l_pos = l_pos + 1.
    l_strlen = strlen( l_str ).
    IF l_pos = l_strlen.
      EXIT.
    ENDIF.
  ENDDO.

  c_value = l_out.
ENDFORM.                    "encode_uri_string


*-----------------------------------------------------------------------
* form: encode_uri_value
*-----------------------------------------------------------------------
FORM encode_uri_value
  CHANGING
    c_value TYPE string.

  DATA:
  l_chars(62) TYPE c.

  CHECK NOT c_value IS INITIAL.

  CONCATENATE         "characters not to encode
    '0123456789'
    'abcdefghijklmnopqrstuvwxyz'
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    INTO l_chars.

  PERFORM encode_uri_string
    USING l_chars
    CHANGING c_value.

ENDFORM.                    "encode_uri_value
