xref: /AOO41X/main/svl/inc/svl/urihelper.hxx (revision 39a19a47feaddbaa21988da8c7bf801707fd3d48)
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 SVTOOLS_URIHELPER_HXX
25 #define SVTOOLS_URIHELPER_HXX
26 
27 #include "svl/svldllapi.h"
28 #include "com/sun/star/uno/Reference.hxx"
29 #include <com/sun/star/uno/RuntimeException.hpp>
30 #include <rtl/textenc.h>
31 #include <tools/link.hxx>
32 #include <tools/solar.h>
33 #include <tools/urlobj.hxx>
34 
35 namespace com { namespace sun { namespace star {
36     namespace uno { class XComponentContext; }
37     namespace uri { class XUriReference; }
38 } } }
39 namespace rtl { class OUString; }
40 class ByteString;
41 class CharClass;
42 class UniString;
43 
44 //============================================================================
45 namespace URIHelper {
46 
47 /**
48    @ATT
49    Calling this function with defaulted arguments rMaybeFileHdl = Link() and
50    bCheckFileExists = true often leads to results that are not intended:
51    Whenever the given rTheBaseURIRef is a file URL, the given rTheRelURIRef is
52    relative, and rTheRelURIRef could also be smart-parsed as a non-file URL
53    (e.g., the relative URL "foo/bar" can be smart-parsed as "http://foo/bar"),
54    then SmartRel2Abs called with rMaybeFileHdl = Link() and bCheckFileExists =
55    true returns the non-file URL interpretation.  To avoid this, either pass
56    some non-null rMaybeFileHdl if you want to check generated file URLs for
57    existence (see URIHelper::GetMaybeFileHdl), or use bCheckFileExists = false
58    if you want to generate file URLs without checking for their existence.
59 */
60 SVL_DLLPUBLIC UniString
61 SmartRel2Abs(INetURLObject const & rTheBaseURIRef,
62              ByteString const & rTheRelURIRef,
63              Link const & rMaybeFileHdl = Link(),
64              bool bCheckFileExists = true,
65              bool bIgnoreFragment = false,
66              INetURLObject::EncodeMechanism eEncodeMechanism
67                  = INetURLObject::WAS_ENCODED,
68              INetURLObject::DecodeMechanism eDecodeMechanism
69                  = INetURLObject::DECODE_TO_IURI,
70              rtl_TextEncoding eCharset = RTL_TEXTENCODING_UTF8,
71              bool bRelativeNonURIs = false,
72              INetURLObject::FSysStyle eStyle = INetURLObject::FSYS_DETECT);
73 
74 /**
75    @ATT
76    Calling this function with defaulted arguments rMaybeFileHdl = Link() and
77    bCheckFileExists = true often leads to results that are not intended:
78    Whenever the given rTheBaseURIRef is a file URL, the given rTheRelURIRef is
79    relative, and rTheRelURIRef could also be smart-parsed as a non-file URL
80    (e.g., the relative URL "foo/bar" can be smart-parsed as "http://foo/bar"),
81    then SmartRel2Abs called with rMaybeFileHdl = Link() and bCheckFileExists =
82    true returns the non-file URL interpretation.  To avoid this, either pass
83    some non-null rMaybeFileHdl if you want to check generated file URLs for
84    existence (see URIHelper::GetMaybeFileHdl), or use bCheckFileExists = false
85    if you want to generate file URLs without checking for their existence.
86 */
87 SVL_DLLPUBLIC UniString
88 SmartRel2Abs(INetURLObject const & rTheBaseURIRef,
89              UniString const & rTheRelURIRef,
90              Link const & rMaybeFileHdl = Link(),
91              bool bCheckFileExists = true,
92              bool bIgnoreFragment = false,
93              INetURLObject::EncodeMechanism eEncodeMechanism
94                  = INetURLObject::WAS_ENCODED,
95              INetURLObject::DecodeMechanism eDecodeMechanism
96                  = INetURLObject::DECODE_TO_IURI,
97              rtl_TextEncoding eCharset = RTL_TEXTENCODING_UTF8,
98              bool bRelativeNonURIs = false,
99              INetURLObject::FSysStyle eStyle = INetURLObject::FSYS_DETECT);
100 
101 //============================================================================
102 SVL_DLLPUBLIC void SetMaybeFileHdl(Link const & rTheMaybeFileHdl);
103 
104 //============================================================================
105 SVL_DLLPUBLIC Link GetMaybeFileHdl();
106 
107 /**
108    Converts a URI reference to a relative one, ignoring certain differences (for
109    example, treating file URLs for case-ignoring file systems
110    case-insensitively).
111 
112    @param context a component context; must not be null
113 
114    @param baseUriReference a base URI reference
115 
116    @param uriReference a URI reference
117 
118    @return a URI reference representing the given uriReference relative to the
119    given baseUriReference; if the given baseUriReference is not an absolute,
120    hierarchical URI reference, or the given uriReference is not a valid URI
121    reference, null is returned
122 
123    @exception std::bad_alloc if an out-of-memory condition occurs
124 
125    @exception com::sun::star::uno::RuntimeException if any error occurs
126  */
127 SVL_DLLPUBLIC com::sun::star::uno::Reference< com::sun::star::uri::XUriReference >
128 normalizedMakeRelative(
129     com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >
130     const & context,
131     rtl::OUString const & baseUriReference, rtl::OUString const & uriReference);
132 
133 /**
134    A variant of normalizedMakeRelative with a simplified interface.
135 
136    Internally calls normalizedMakeRelative with the default component context.
137 
138    @param baseUriReference a base URI reference, passed to
139    normalizedMakeRelative
140 
141    @param uriReference a URI reference, passed to normalizedMakeRelative
142 
143    @return if the XUriReference returnd by normalizedMakeRelative is empty,
144    uriReference is returned unmodified; otherwise, the result of calling
145    XUriReference::getUriReference on the XUriReference returnd by
146    normalizedMakeRelative is returned
147 
148    @exception std::bad_alloc if an out-of-memory condition occurs
149 
150    @exception com::sun::star::uno::RuntimeException if any error occurs
151 
152    @deprecated
153    No code should rely on the default component context.
154 */
155 SVL_DLLPUBLIC rtl::OUString simpleNormalizedMakeRelative(
156     rtl::OUString const & baseUriReference, rtl::OUString const & uriReference);
157 
158 //============================================================================
159 SVL_DLLPUBLIC UniString
160 FindFirstURLInText(UniString const & rText,
161                    xub_StrLen & rBegin,
162                    xub_StrLen & rEnd,
163                    CharClass const & rCharClass,
164                    INetURLObject::EncodeMechanism eMechanism
165                        = INetURLObject::WAS_ENCODED,
166                    rtl_TextEncoding eCharset = RTL_TEXTENCODING_UTF8,
167                    INetURLObject::FSysStyle eStyle
168                        = INetURLObject::FSYS_DETECT);
169 
170 //============================================================================
171 /** Remove any password component from both absolute and relative URLs.
172 
173     @ATT  The current implementation will not remove a password from a
174     relative URL that has an authority component (e.g., the password is not
175     removed from the relative ftp URL <//user:password@domain/path>).  But
176     since our functions to translate between absolute and relative URLs never
177     produce relative URLs with authority components, this is no real problem.
178 
179     @ATT  For relative URLs (or anything not recognized as an absolute URI),
180     the current implementation will return the input unmodified, not applying
181     any translations implied by the encode/decode parameters.
182 
183     @param rURI  An absolute or relative URI reference.
184 
185     @param eEncodeMechanism  See the general discussion for INetURLObject set-
186     methods.
187 
188     @param eDecodeMechanism  See the general discussion for INetURLObject get-
189     methods.
190 
191     @param eCharset  See the general discussion for INetURLObject get- and
192     set-methods.
193 
194     @return  The input URI with any password component removed.
195  */
196 SVL_DLLPUBLIC UniString
197 removePassword(UniString const & rURI,
198                INetURLObject::EncodeMechanism eEncodeMechanism
199                    = INetURLObject::WAS_ENCODED,
200                INetURLObject::DecodeMechanism eDecodeMechanism
201                    = INetURLObject::DECODE_TO_IURI,
202                rtl_TextEncoding eCharset = RTL_TEXTENCODING_UTF8);
203 
204 //============================================================================
205 /** Query the notational conventions used in the file system provided by some
206     file content provider.
207 
208     @param rFileUrl  This file URL determines which file content provider is
209     used to query the desired information.  (The UCB's usual mapping from URLs
210     to content providers is used.)
211 
212     @param bAddConvenienceStyles  If true, the return value contains not only
213     the style bit corresponding to the queried content provider's conventions,
214     but may also contain additional style bits that make using this function
215     more convenient in certain situations.  Currently, the effect is that
216     FSYS_UNX is extended with FSYS_VOS, and both FSYS_DOS and FSYS_MAC are
217     extended with FSYS_VOS and FSYS_UNX (i.e., the---unambiguous---detection
218     of VOS style and Unix style file system paths is always enabled); also, in
219     case the content provider's conventions cannot be determined, FSYS_DETECT
220     is returned instead of FSysStyle(0).
221 
222     @return  The style bit corresponding to the queried content provider's
223     conventions, or FSysStyle(0) if these cannot be determined.
224  */
225 SVL_DLLPUBLIC INetURLObject::FSysStyle queryFSysStyle(UniString const & rFileUrl,
226                                         bool bAddConvenienceStyles = true)
227     throw (com::sun::star::uno::RuntimeException);
228 
229 }
230 
231 #endif // SVTOOLS_URIHELPER_HXX
232