LibreOffice Module ucb (master)  1
urihelper.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #ifndef INCLUDED_UCB_SOURCE_UCP_INC_URIHELPER_HXX
21 #define INCLUDED_UCB_SOURCE_UCP_INC_URIHELPER_HXX
22 
23 #include <rtl/ustring.hxx>
24 #include <rtl/ustrbuf.hxx>
25 #include <rtl/uri.hxx>
26 
27 
28 namespace ucb_impl { namespace urihelper {
29 
30  inline OUString encodeSegment( const OUString & rSegment )
31  {
32  return rtl::Uri::encode( rSegment,
33  rtl_UriCharClassPchar,
34  rtl_UriEncodeIgnoreEscapes,
35  RTL_TEXTENCODING_UTF8 );
36  }
37 
38  inline OUString decodeSegment( const OUString& rSegment )
39  {
40  return rtl::Uri::decode( rSegment,
41  rtl_UriDecodeWithCharset,
42  RTL_TEXTENCODING_UTF8 );
43  }
44 
45  inline OUString encodeURI( const OUString & rURI )
46  {
47  OUString aFragment;
48  OUString aParams;
49  OUString aURI;
50 
51  sal_Int32 nFragment = rURI.lastIndexOf( u'#' );
52  if ( nFragment != -1 )
53  aFragment = rURI.copy( nFragment + 1 );
54 
55  sal_Int32 nParams = ( nFragment == -1 )
56  ? rURI.lastIndexOf( u'?' )
57  : rURI.lastIndexOf( u'?', nFragment );
58  if ( nParams != -1 )
59  aParams = ( nFragment == -1 )
60  ? rURI.copy( nParams + 1 )
61  : rURI.copy( nParams + 1, nFragment - nParams - 1 );
62 
63  aURI = ( nParams != -1 )
64  ? rURI.copy( 0, nParams )
65  : ( nFragment != -1 )
66  ? rURI.copy( 0, nFragment )
67  : rURI;
68 
69  if ( aFragment.getLength() > 1 )
70  aFragment =
71  rtl::Uri::encode( aFragment,
72  rtl_UriCharClassUric,
73  rtl_UriEncodeKeepEscapes, /* #i81690# */
74  RTL_TEXTENCODING_UTF8 );
75 
76  if ( aParams.getLength() > 1 )
77  aParams =
78  rtl::Uri::encode( aParams,
79  rtl_UriCharClassUric,
80  rtl_UriEncodeKeepEscapes, /* #i81690# */
81  RTL_TEXTENCODING_UTF8 );
82 
83  OUStringBuffer aResult(256);
84  sal_Int32 nIndex = 0;
85  do
86  {
87  aResult.append(
88  rtl::Uri::encode( aURI.getToken( 0, '/', nIndex ),
89  rtl_UriCharClassPchar,
90  rtl_UriEncodeKeepEscapes, /* #i81690# */
91  RTL_TEXTENCODING_UTF8 ) );
92  if ( nIndex >= 0 )
93  aResult.append( u'/' );
94  }
95  while ( nIndex >= 0 );
96 
97  if ( !aParams.isEmpty() )
98  {
99  aResult.append( u'?' );
100  aResult.append( aParams );
101  }
102 
103  if ( !aFragment.isEmpty() )
104  {
105  aResult.append( u'#' );
106  aResult.append( aFragment );
107  }
108 
109  return aResult.makeStringAndClear();
110  }
111 
112 } } // namespace
113 
114 #endif // INCLUDED_UCB_SOURCE_UCP_INC_URIHELPER_HXX
115 
116 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString encodeURI(const OUString &rURI)
Definition: urihelper.hxx:45
OUString encodeSegment(const OUString &rSegment)
Definition: urihelper.hxx:30
OUString decodeSegment(const OUString &rSegment)
Definition: urihelper.hxx:38
float u