12 December 2008

Special Character Conversion.

Oracle apps : wf_html.conv_special_url_chars

Another way of doing is :utl_i18n.escape_reference(''String'','us7ascii');


FUNCTION conv_special_url_chars (p_url_token IN VARCHAR2) RETURN VARCHAR2
IS
c_unreserved constant varchar2(72) :=
'-_.!*''()~ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
c_reserved constant varchar2(72) := '%">^{}<[]`|/#?&=$:;+';
l_tmp varchar2(32767) := '';
l_onechar varchar2(4);
l_byte_len integer;
i integer;
l_str varchar2(48);

BEGIN
if p_url_token is NULL then
return NULL;
end if;
for i in 1 .. length(p_url_token) loop
l_onechar := substr(p_url_token,i,1);
--Extracting out each character to be replaced.
if instr(c_unreserved, l_onechar) > 0 then
--Check if it is part of the ASCII unreserved
--excluded from encoding just append to the URL
--string
l_tmp := l_tmp || l_onechar;

elsif l_onechar = ' ' then
--Space encoded as '%20'
l_tmp := l_tmp || '%20';

elsif instr(c_reserved,l_onechar) >0 then
--If it is any of the reserved characters in ascii
--replace with equivalent HEX
l_onechar := REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(l_onechar,
'%','%25'),
' ','%20'),
'"','%22'),
'>','%3E'),
'^','%5E'),
'{','%7B'),
'}','%7D'),
'<','%3C'),
'[','%5B'),
']','%5D'),
'`','%60'),
'|','%7C'),
'/','%2F'),
'#','%23'),
'?','%3F'),
'&','%26'),
'=','%3D'),
'$','%24'),
':','%3A'),
';','%3B'),
'+','%2B'),
'''','%27');
l_tmp := l_tmp || l_onechar;
else
--For multibyte
-- 1. Obtain length for each character
-- 2. ascii(l_char)decimal representation in the database
-- character set
-- 3. Change it to the format model :
-- to_char(ascii(l_onechar),'FM0X')
-- 4. Add to the already encoded string.
-- characters
l_byte_len := lengthb(l_onechar);
if l_byte_len = 1 then
l_tmp := l_tmp || '%' ||
substr(to_char(ascii(l_onechar),'FM0X'),1,2);
elsif l_byte_len = 2 then
l_str := to_char(ascii(l_onechar),'FM0XXX');
l_tmp := l_tmp
|| '%' || substr(l_str,1,2)
|| '%' || substr(l_str,3,2);
elsif l_byte_len = 3 then
l_str := to_char(ascii(l_onechar),'FM0XXXXX');
l_tmp := l_tmp
|| '%' || substr(l_str,1,2)
|| '%' || substr(l_str,3,2)
|| '%' || substr(l_str,5,2);
elsif l_byte_len = 4 then
l_str := to_char(ascii(l_onechar),'FM0XXXXXXX');
l_tmp := l_tmp
|| '%' || substr(l_str,1,2)
|| '%' || substr(l_str,3,2)
|| '%' || substr(l_str,5,2)
|| '%' || substr(l_str,7,2);
else -- maximum precision
wf_core.raise('WFENG_PRECESSION_EXCEED');
end if;
end if;
end loop;
return l_tmp;
exception
when others then
Wf_Core.Context('wfa_html', 'conv_special_url_chars',
p_url_token);
wfa_html.Error;
END conv_special_url_chars;

No comments: