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