xref: /aoo42x/main/sal/inc/rtl/uri.hxx (revision 565d668c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _RTL_URI_HXX_
25 #define _RTL_URI_HXX_
26 
27 #include "rtl/malformeduriexception.hxx"
28 #include "rtl/uri.h"
29 #include "rtl/textenc.h"
30 #include "rtl/ustring.hxx"
31 #include "sal/types.h"
32 
33 namespace rtl {
34 
35 /** A wrapper around the C functions from <rtl/uri.h>.
36  */
37 class Uri
38 {
39 public:
40     /** A wrapper around rtl_uriEncode() from <rtl/uri.h> (see there), using
41         an array of 128 booleans as char class.
42      */
43     static inline rtl::OUString encode(rtl::OUString const & rText,
44                                        sal_Bool const * pCharClass,
45                                        rtl_UriEncodeMechanism eMechanism,
46                                        rtl_TextEncoding eCharset)
47         SAL_THROW(());
48 
49     /** A wrapper around rtl_uriEncode() from <rtl/uri.h> (see there), using
50         a predefined rtl_UriCharClass enumeration member.
51      */
52     static inline rtl::OUString encode(rtl::OUString const & rText,
53                                        rtl_UriCharClass eCharClass,
54                                        rtl_UriEncodeMechanism eMechanism,
55                                        rtl_TextEncoding eCharset)
56         SAL_THROW(());
57 
58     /** A wrapper around rtl_uriDecode() from <rtl/uri.h> (see there).
59      */
60     static inline rtl::OUString decode(rtl::OUString const & rText,
61                                        rtl_UriDecodeMechanism eMechanism,
62                                        rtl_TextEncoding eCharset)
63         SAL_THROW(());
64 
65     /** A wrapper around rtl_uriConvertRelToAbs() from <rtl/uri.h> (see there).
66 
67         @exception MalformedUriException
68         Thrown in case rtl_uriConvertRelToAbs() signals an exception due to a
69         malformed base URI.
70      */
71     static inline rtl::OUString convertRelToAbs(
72         rtl::OUString const & rBaseUriRef, rtl::OUString const & rRelUriRef);
73 
74 private:
75     /** not implemented
76         @internal */
77     Uri();
78 
79     /** not implemented
80         @internal */
81     Uri(Uri &);
82 
83     /** not implemented
84         @internal */
85     ~Uri();
86 
87     /** not implemented
88         @internal */
89     void operator =(Uri);
90 };
91 
encode(rtl::OUString const & rText,sal_Bool const * pCharClass,rtl_UriEncodeMechanism eMechanism,rtl_TextEncoding eCharset)92 inline rtl::OUString Uri::encode(rtl::OUString const & rText,
93                                  sal_Bool const * pCharClass,
94                                  rtl_UriEncodeMechanism eMechanism,
95                                  rtl_TextEncoding eCharset)
96     SAL_THROW(())
97 {
98     rtl::OUString aResult;
99     rtl_uriEncode(const_cast< rtl::OUString & >(rText).pData,
100                   pCharClass,
101                   eMechanism,
102                   eCharset,
103                   &aResult.pData);
104     return aResult;
105 }
106 
encode(rtl::OUString const & rText,rtl_UriCharClass eCharClass,rtl_UriEncodeMechanism eMechanism,rtl_TextEncoding eCharset)107 inline rtl::OUString Uri::encode(rtl::OUString const & rText,
108                                  rtl_UriCharClass eCharClass,
109                                  rtl_UriEncodeMechanism eMechanism,
110                                  rtl_TextEncoding eCharset)
111     SAL_THROW(())
112 {
113     rtl::OUString aResult;
114     rtl_uriEncode(const_cast< rtl::OUString & >(rText).pData,
115                   rtl_getUriCharClass(eCharClass),
116                   eMechanism,
117                   eCharset,
118                   &aResult.pData);
119     return aResult;
120 }
121 
decode(rtl::OUString const & rText,rtl_UriDecodeMechanism eMechanism,rtl_TextEncoding eCharset)122 inline rtl::OUString Uri::decode(rtl::OUString const & rText,
123                                  rtl_UriDecodeMechanism eMechanism,
124                                  rtl_TextEncoding eCharset)
125     SAL_THROW(())
126 {
127     rtl::OUString aResult;
128     rtl_uriDecode(const_cast< rtl::OUString & >(rText).pData,
129                   eMechanism,
130                   eCharset,
131                   &aResult.pData);
132     return aResult;
133 }
134 
convertRelToAbs(rtl::OUString const & rBaseUriRef,rtl::OUString const & rRelUriRef)135 inline rtl::OUString Uri::convertRelToAbs(rtl::OUString const & rBaseUriRef,
136                                           rtl::OUString const & rRelUriRef)
137 {
138     rtl::OUString aResult;
139     rtl::OUString aException;
140     if (!rtl_uriConvertRelToAbs(
141             const_cast< rtl::OUString & >(rBaseUriRef).pData,
142             const_cast< rtl::OUString & >(rRelUriRef).pData, &aResult.pData,
143             &aException.pData))
144         throw MalformedUriException(aException);
145     return aResult;
146 }
147 
148 }
149 
150 #endif // _RTL_URI_HXX_
151