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