Get Example source ABAP code based on a different SAP table
• escape ABAP_FUNCTION • format escape function
ABAP_STRINGFUNC > - escape>
ABAP_SYNTAX ... escape( val = text> format = format ) ...>
What does it do? This built-in function> returns the content of the character string in text>>, and escapes certain special characters with escape characters according to a rule specified in format>. The possible values of format> are defined as constants with the prefix E_> in the class CL_ABAP_FORMAT>>. Each value defines which special characters are replaced, and how. There are rules for special characters in markup languages (XML and HTML), in URIs and URLs, in JSON, as well as in regular expressions and string templates. It is also important to prevent attacks using Cross Site Scripting (XSS) on Web applications. format> expects data objects of the type i>. An invalid value for format> raises an exception of the class CX_SY_STRG_PAR_VAL>. BEGIN_SECTION VERSION 5 OUT For all characters whose codes are between x00> and xFF>, the program DEMO_ESCAPE>> demonstrates the effect of all associated formats from the class CL_ABAP_FORMAT>. The top line contains the names of the constants from the class CL_ABAP_FORMAT> without the prefix E_>. The remaining lines show the effect on the characters specified in the first two columns. END_SECTION VERSION 5 OUT This function can be specified in general> and character-like expression positions>. The return value has the type string>. ITOC
Rules for Markup Languages (Including JavaScript)
BEGIN_SECTION VERSION 5 OUT The program DEMO_ESCAPE_MARKUP>> demonstrates the replacement rules for markup languages. END_SECTION VERSION 5 OUT Formats with _JS> in their name are intended for content with JavaScript components. The following table summarizes the replacement rules: Format><(> <)>>><(><<)>>>> >>'>>'>>TAB>>LF >>CR>>BS>>FF>> >>ctrl-char>> E_XML_TEXT><(> <)>amp;><(> <)>lt;>----- ----- E_XML_ATTR><(> <)>amp;><(> <)>lt;>- <(> <)>quot;><(> <)>apos;><(> <)>#9;><(> <)>#xA; ><(> <)>#xD;>---- E_XML_ATTR_DQ><(> <)>amp;><(> <)>lt;>- <(> <)>quot;>-<(> <)>#9;><(> <)>#xA;> <(> <)>#xD;>---- E_XML_ATTR_SQ><(> <)>amp;><(> <)>lt;>-- <(> <)>apos;><(> <)>#9;><(> <)>#xA;><(> <)>#xD; >---- E_HTML_TEXT><(> <)>amp;><(> <)>lt;> <(> <)>gt;>--------- E_HTML_ATTR><(> <)>amp;><(> <)>lt;> <(> <)>gt;><(> <)>quot;><(> <)>#39;>------ - E_HTML_ATTR_DQ><(> <)>amp;><(> <)>lt;> <(> <)>gt;><(> <)>quot;>-------- E_HTML_ATTR_SQ><(> <)>amp;><(> <)>lt;> <(> <)>gt;>-<(> <)>#39;>------- E_HTML_JS>--- '> '> t> n> r> b> f> > xhh> E_HTML_JS_HTML><(> <)>amp;><(> <)>lt;> <(> <)>gt;><(> <)>quot;><(> <)>#39;> t> n> r> b> f> > xhh> The first column contains the names of the formats from the class CL_ABAP_FORMAT>. The remaining columns show the escape characters that replace the special characters in the first line. All other characters remain unchanged. TAB>, LF>, CR>, BS >, and FF> are the control characters> for tabulator, Line Feed, Carriage Return, Backspace, and Form Feed, to which the codes x09 > , x0A>, x0D>, x08>, and x0C> are assigned in 7-Bit ASCII. ctrl-char> stands for all control characters> with codes less than x20> that are not covered by those characters listed explicitly. Some of these can be converted to xhh>, where hh > is the hexadecimal value of the code. If there is no value in a field (-), the special character remains unchanged.
ABAP_EXAMPLE_VX5 The result of the following escaping is IF a lt; b>. ABEXA 01017 ABAP_EXAMPLE_END
ABAP_EXAMPLE_ABEXA String Functions, escape> for HTML> ABAP_EXAMPLE_END
Rules for URLs and URIs
BEGIN_SECTION VERSION 5 OUT The program DEMO_ESCAPE_URL_URI>> demonstrates the replacement rules for URLs and URIs. END_SECTION VERSION 5 OUT All characters with codes between x00> and 7F> are converted to %hh> , except for the characters listed in the following table, where hh > is the hexadecimal value of the code. In URI_1> format, quotation marks are not converted to %hh>, but are doubled. Format>Unconverted Characters> E_URL>[0-9]>, [a-z]>, [A-Z]>, !>, $ >, '>, (>, )>, *>, +>, ,> , ->, .>, _>, >, />, :>, ;> , =>, ?>, @> E_URL_FULL>[0-9]>, [a-z]>, [A-Z]>, !> , $>, '>, (>, )>, *>, +>, ,> , ->, .>, _> E_URI>[0-9]>, [a-z]>, [A-Z]>, !>, $ >, '>, (>, )>, *>, +>, ,> , ->, .>, _>, >, />, :>, ;> , =>, ?>, @>, ~>, #>, [>, ]> E_URI_FULL>[0-9]>, [a-z]>, [A-Z]>, -> , .>, _>, ~> E_URI_1>[0-9]>, [a-z]>, [A-Z]>, !>, $>, (>, )>, *>, +>, ,>, -> , .>, _>, ;>, =>, @>, ~>, [> , ]> All characters with codes from x80> are converted to their UTF-8> representation. Depending on the character, one to four bytes are represented in the form %hh>, where hh> is the hexadecimal value of a byte.
ABAP_EXAMPLE_VX5 The result of the following escaping is Numbers%3A%201,%202>. ABEXA 01018 ABAP_EXAMPLE_END
Rules for JSON
BEGIN_SECTION VERSION 5 OUT The program DEMO_ESCAPE_JSON>> demonstrates the replacement rules of the format E_JSON_STRING> for JSON>. END_SECTION VERSION 5 OUT The special characters '> and > are prefixed with the escape character >.