xref: /aoo41x/main/ucb/source/ucp/inc/urihelper.hxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #ifndef INCLUDED_URIHELPER_HXX
29*cdf0e10cSrcweir #define INCLUDED_URIHELPER_HXX
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "rtl/ustring.hxx"
32*cdf0e10cSrcweir #include "rtl/ustrbuf.hxx"
33*cdf0e10cSrcweir #include "rtl/uri.hxx"
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir //=========================================================================
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir namespace ucb_impl { namespace urihelper {
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir     inline ::rtl::OUString encodeSegment( const ::rtl::OUString & rSegment )
40*cdf0e10cSrcweir     {
41*cdf0e10cSrcweir         return rtl::Uri::encode( rSegment,
42*cdf0e10cSrcweir                                  rtl_UriCharClassPchar,
43*cdf0e10cSrcweir                                  rtl_UriEncodeIgnoreEscapes,
44*cdf0e10cSrcweir                                  RTL_TEXTENCODING_UTF8 );
45*cdf0e10cSrcweir     }
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir     inline ::rtl::OUString decodeSegment( const rtl::OUString& rSegment )
48*cdf0e10cSrcweir     {
49*cdf0e10cSrcweir         return rtl::Uri::decode( rSegment,
50*cdf0e10cSrcweir                                  rtl_UriDecodeWithCharset,
51*cdf0e10cSrcweir                                  RTL_TEXTENCODING_UTF8 );
52*cdf0e10cSrcweir     }
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir     inline ::rtl::OUString encodeURI( const ::rtl::OUString & rURI )
55*cdf0e10cSrcweir     {
56*cdf0e10cSrcweir         rtl::OUString aFragment;
57*cdf0e10cSrcweir         rtl::OUString aParams;
58*cdf0e10cSrcweir         rtl::OUString aURI;
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir         sal_Int32 nFragment = rURI.lastIndexOf( sal_Unicode( '#' ) );
61*cdf0e10cSrcweir         if ( nFragment != -1 )
62*cdf0e10cSrcweir             aFragment = rURI.copy( nFragment + 1 );
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir         sal_Int32 nParams = ( nFragment == -1 )
65*cdf0e10cSrcweir             ? rURI.lastIndexOf( sal_Unicode( '?' ) )
66*cdf0e10cSrcweir             : rURI.lastIndexOf( sal_Unicode( '?' ), nFragment );
67*cdf0e10cSrcweir         if ( nParams != -1 )
68*cdf0e10cSrcweir             aParams = ( nFragment == -1 )
69*cdf0e10cSrcweir                 ? rURI.copy( nParams + 1 )
70*cdf0e10cSrcweir                 : rURI.copy( nParams + 1, nFragment - nParams - 1 );
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir         aURI = ( nParams != -1 )
73*cdf0e10cSrcweir             ? rURI.copy( 0, nParams )
74*cdf0e10cSrcweir             : ( nFragment != -1 )
75*cdf0e10cSrcweir                   ? rURI.copy( 0, nFragment )
76*cdf0e10cSrcweir                   : rURI;
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir         if ( aFragment.getLength() > 1 )
79*cdf0e10cSrcweir             aFragment =
80*cdf0e10cSrcweir                 rtl::Uri::encode( aFragment,
81*cdf0e10cSrcweir                                   rtl_UriCharClassUric,
82*cdf0e10cSrcweir                                   rtl_UriEncodeKeepEscapes, /* #i81690# */
83*cdf0e10cSrcweir                                   RTL_TEXTENCODING_UTF8 );
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir         if ( aParams.getLength() > 1 )
86*cdf0e10cSrcweir             aParams =
87*cdf0e10cSrcweir                 rtl::Uri::encode( aParams,
88*cdf0e10cSrcweir                                   rtl_UriCharClassUric,
89*cdf0e10cSrcweir                                   rtl_UriEncodeKeepEscapes, /* #i81690# */
90*cdf0e10cSrcweir                                   RTL_TEXTENCODING_UTF8 );
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir         rtl::OUStringBuffer aResult;
93*cdf0e10cSrcweir         sal_Int32 nIndex = 0;
94*cdf0e10cSrcweir         do
95*cdf0e10cSrcweir         {
96*cdf0e10cSrcweir             aResult.append(
97*cdf0e10cSrcweir                 rtl::Uri::encode( aURI.getToken( 0, '/', nIndex ),
98*cdf0e10cSrcweir                                   rtl_UriCharClassPchar,
99*cdf0e10cSrcweir                                   rtl_UriEncodeKeepEscapes, /* #i81690# */
100*cdf0e10cSrcweir                                   RTL_TEXTENCODING_UTF8 ) );
101*cdf0e10cSrcweir             if ( nIndex >= 0 )
102*cdf0e10cSrcweir                 aResult.append( sal_Unicode( '/' ) );
103*cdf0e10cSrcweir         }
104*cdf0e10cSrcweir         while ( nIndex >= 0 );
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir         if ( aParams.getLength() > 0 )
107*cdf0e10cSrcweir         {
108*cdf0e10cSrcweir             aResult.append( sal_Unicode( '?' ) );
109*cdf0e10cSrcweir             aResult.append( aParams );
110*cdf0e10cSrcweir         }
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir         if ( aFragment.getLength() > 0 )
113*cdf0e10cSrcweir         {
114*cdf0e10cSrcweir             aResult.append( sal_Unicode( '#' ) );
115*cdf0e10cSrcweir             aResult.append( aFragment );
116*cdf0e10cSrcweir         }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir         return aResult.makeStringAndClear();
119*cdf0e10cSrcweir     }
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir } } // namespace
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir #endif /* !INCLUDED_URIHELPER_HXX */
124