Update ooo320-m1
[ooovba.git] / ucb / source / ucp / inc / urihelper.hxx
bloba3618293edb9be5ce40e67d38fd61e66748029ee
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: urihelper.hxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef INCLUDED_URIHELPER_HXX
32 #define INCLUDED_URIHELPER_HXX
34 #include "rtl/ustring.hxx"
35 #include "rtl/ustrbuf.hxx"
36 #include "rtl/uri.hxx"
38 //=========================================================================
40 namespace ucb_impl { namespace urihelper {
42 inline ::rtl::OUString encodeSegment( const ::rtl::OUString & rSegment )
44 return rtl::Uri::encode( rSegment,
45 rtl_UriCharClassPchar,
46 rtl_UriEncodeIgnoreEscapes,
47 RTL_TEXTENCODING_UTF8 );
50 inline ::rtl::OUString decodeSegment( const rtl::OUString& rSegment )
52 return rtl::Uri::decode( rSegment,
53 rtl_UriDecodeWithCharset,
54 RTL_TEXTENCODING_UTF8 );
57 inline ::rtl::OUString encodeURI( const ::rtl::OUString & rURI )
59 rtl::OUString aFragment;
60 rtl::OUString aParams;
61 rtl::OUString aURI;
63 sal_Int32 nFragment = rURI.lastIndexOf( sal_Unicode( '#' ) );
64 if ( nFragment != -1 )
65 aFragment = rURI.copy( nFragment + 1 );
67 sal_Int32 nParams = ( nFragment == -1 )
68 ? rURI.lastIndexOf( sal_Unicode( '?' ) )
69 : rURI.lastIndexOf( sal_Unicode( '?' ), nFragment );
70 if ( nParams != -1 )
71 aParams = ( nFragment == -1 )
72 ? rURI.copy( nParams + 1 )
73 : rURI.copy( nParams + 1, nFragment - nParams - 1 );
75 aURI = ( nParams != -1 )
76 ? rURI.copy( 0, nParams )
77 : ( nFragment != -1 )
78 ? rURI.copy( 0, nFragment )
79 : rURI;
81 if ( aFragment.getLength() > 1 )
82 aFragment =
83 rtl::Uri::encode( aFragment,
84 rtl_UriCharClassUric,
85 rtl_UriEncodeKeepEscapes, /* #i81690# */
86 RTL_TEXTENCODING_UTF8 );
88 if ( aParams.getLength() > 1 )
89 aParams =
90 rtl::Uri::encode( aParams,
91 rtl_UriCharClassUric,
92 rtl_UriEncodeKeepEscapes, /* #i81690# */
93 RTL_TEXTENCODING_UTF8 );
95 rtl::OUStringBuffer aResult;
96 sal_Int32 nIndex = 0;
99 aResult.append(
100 rtl::Uri::encode( aURI.getToken( 0, '/', nIndex ),
101 rtl_UriCharClassPchar,
102 rtl_UriEncodeKeepEscapes, /* #i81690# */
103 RTL_TEXTENCODING_UTF8 ) );
104 if ( nIndex >= 0 )
105 aResult.append( sal_Unicode( '/' ) );
107 while ( nIndex >= 0 );
109 if ( aParams.getLength() > 0 )
111 aResult.append( sal_Unicode( '?' ) );
112 aResult.append( aParams );
115 if ( aFragment.getLength() > 0 )
117 aResult.append( sal_Unicode( '#' ) );
118 aResult.append( aFragment );
121 return aResult.makeStringAndClear();
124 } } // namespace
126 #endif /* !INCLUDED_URIHELPER_HXX */