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