xref: /aoo42x/main/ucb/source/ucp/inc/urihelper.hxx (revision 6df1ea1f)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef INCLUDED_URIHELPER_HXX
25 #define INCLUDED_URIHELPER_HXX
26 
27 #include "rtl/ustring.hxx"
28 #include "rtl/ustrbuf.hxx"
29 #include "rtl/uri.hxx"
30 
31 //=========================================================================
32 
33 namespace ucb_impl { namespace urihelper {
34 
encodeSegment(const::rtl::OUString & rSegment)35     inline ::rtl::OUString encodeSegment( const ::rtl::OUString & rSegment )
36     {
37         return rtl::Uri::encode( rSegment,
38                                  rtl_UriCharClassPchar,
39                                  rtl_UriEncodeIgnoreEscapes,
40                                  RTL_TEXTENCODING_UTF8 );
41     }
42 
decodeSegment(const rtl::OUString & rSegment)43     inline ::rtl::OUString decodeSegment( const rtl::OUString& rSegment )
44     {
45         return rtl::Uri::decode( rSegment,
46                                  rtl_UriDecodeWithCharset,
47                                  RTL_TEXTENCODING_UTF8 );
48     }
49 
encodeURI(const::rtl::OUString & rURI)50     inline ::rtl::OUString encodeURI( const ::rtl::OUString & rURI )
51     {
52         rtl::OUString aFragment;
53         rtl::OUString aParams;
54         rtl::OUString aURI;
55 
56         sal_Int32 nFragment = rURI.lastIndexOf( sal_Unicode( '#' ) );
57         if ( nFragment != -1 )
58             aFragment = rURI.copy( nFragment + 1 );
59 
60         sal_Int32 nParams = ( nFragment == -1 )
61             ? rURI.lastIndexOf( sal_Unicode( '?' ) )
62             : rURI.lastIndexOf( sal_Unicode( '?' ), nFragment );
63         if ( nParams != -1 )
64             aParams = ( nFragment == -1 )
65                 ? rURI.copy( nParams + 1 )
66                 : rURI.copy( nParams + 1, nFragment - nParams - 1 );
67 
68         aURI = ( nParams != -1 )
69             ? rURI.copy( 0, nParams )
70             : ( nFragment != -1 )
71                   ? rURI.copy( 0, nFragment )
72                   : rURI;
73 
74         if ( aFragment.getLength() > 1 )
75             aFragment =
76                 rtl::Uri::encode( aFragment,
77                                   rtl_UriCharClassUric,
78                                   rtl_UriEncodeKeepEscapes, /* #i81690# */
79                                   RTL_TEXTENCODING_UTF8 );
80 
81         if ( aParams.getLength() > 1 )
82             aParams =
83                 rtl::Uri::encode( aParams,
84                                   rtl_UriCharClassUric,
85                                   rtl_UriEncodeKeepEscapes, /* #i81690# */
86                                   RTL_TEXTENCODING_UTF8 );
87 
88         rtl::OUStringBuffer aResult;
89         sal_Int32 nIndex = 0;
90         do
91         {
92             aResult.append(
93                 rtl::Uri::encode( aURI.getToken( 0, '/', nIndex ),
94                                   rtl_UriCharClassPchar,
95                                   rtl_UriEncodeKeepEscapes, /* #i81690# */
96                                   RTL_TEXTENCODING_UTF8 ) );
97             if ( nIndex >= 0 )
98                 aResult.append( sal_Unicode( '/' ) );
99         }
100         while ( nIndex >= 0 );
101 
102         if ( aParams.getLength() > 0 )
103         {
104             aResult.append( sal_Unicode( '?' ) );
105             aResult.append( aParams );
106         }
107 
108         if ( aFragment.getLength() > 0 )
109         {
110             aResult.append( sal_Unicode( '#' ) );
111             aResult.append( aFragment );
112         }
113 
114         return aResult.makeStringAndClear();
115     }
116 
117 } } // namespace
118 
119 #endif /* !INCLUDED_URIHELPER_HXX */
120