Update ooo320-m1
[ooovba.git] / framework / source / services / urltransformer.cxx
blobf04827592e4b81bc9990c7f471581b245a96bb79
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: urltransformer.cxx,v $
10 * $Revision: 1.17 $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_framework.hxx"
34 //_________________________________________________________________________________________________________________
35 // my own includes
36 //_________________________________________________________________________________________________________________
37 #include <services/urltransformer.hxx>
38 #include <threadhelp/resetableguard.hxx>
39 #include <macros/debug.hxx>
40 #include <services.h>
42 //_________________________________________________________________________________________________________________
43 // interface includes
44 //_________________________________________________________________________________________________________________
46 //_________________________________________________________________________________________________________________
47 // includes of other projects
48 //_________________________________________________________________________________________________________________
49 #include <tools/urlobj.hxx>
50 #include <rtl/ustrbuf.hxx>
51 #include <vcl/svapp.hxx>
53 //_________________________________________________________________________________________________________________
54 // namespace
55 //_________________________________________________________________________________________________________________
57 namespace framework{
59 using namespace ::osl ;
60 using namespace ::cppu ;
61 using namespace ::com::sun::star::uno ;
62 using namespace ::com::sun::star::lang ;
63 using namespace ::com::sun::star::util ;
65 //_________________________________________________________________________________________________________________
66 // non exported const
67 //_________________________________________________________________________________________________________________
69 //_________________________________________________________________________________________________________________
70 // non exported definitions
71 //_________________________________________________________________________________________________________________
73 //_________________________________________________________________________________________________________________
74 // declarations
75 //_________________________________________________________________________________________________________________
77 //*****************************************************************************************************************
78 // constructor
79 //*****************************************************************************************************************
80 URLTransformer::URLTransformer( const Reference< XMultiServiceFactory >& /*xFactory*/ )
82 // Safe impossible cases.
83 // Method not defined for all incoming parameter.
84 //LOG_ASSERT( xFactory.is(), "URLTransformer::URLTransformer()\nInvalid parameter detected!\n" )
87 //*****************************************************************************************************************
88 // destructor
89 //*****************************************************************************************************************
90 URLTransformer::~URLTransformer()
94 //*****************************************************************************************************************
95 // XInterface, XTypeProvider, XServiceInfo
96 //*****************************************************************************************************************
98 DEFINE_XSERVICEINFO_MULTISERVICE ( URLTransformer ,
99 OWeakObject ,
100 SERVICENAME_URLTRANSFORMER ,
101 IMPLEMENTATIONNAME_URLTRANSFORMER
104 DEFINE_INIT_SERVICE ( URLTransformer,
109 namespace
111 void lcl_ParserHelper(INetURLObject& _rParser,URL& _rURL,bool _bUseIntern)
113 // Get all information about this URL.
114 _rURL.Protocol = INetURLObject::GetScheme( _rParser.GetProtocol() );
115 _rURL.User = _rParser.GetUser ( INetURLObject::DECODE_WITH_CHARSET );
116 _rURL.Password = _rParser.GetPass ( INetURLObject::DECODE_WITH_CHARSET );
117 _rURL.Server = _rParser.GetHost ( INetURLObject::DECODE_WITH_CHARSET );
118 _rURL.Port = (sal_Int16)_rParser.GetPort();
120 sal_Int32 nCount = _rParser.getSegmentCount( false );
121 if ( nCount > 0 )
123 // Don't add last segment as it is the name!
124 --nCount;
126 rtl::OUStringBuffer aPath;
127 for ( sal_Int32 nIndex = 0; nIndex < nCount; nIndex++ )
129 aPath.append( sal_Unicode( '/' ));
130 aPath.append( _rParser.getName( nIndex, false, INetURLObject::NO_DECODE ));
133 if ( nCount > 0 )
134 aPath.append( sal_Unicode( '/' )); // final slash!
136 _rURL.Path = aPath.makeStringAndClear();
137 _rURL.Name = _rParser.getName( INetURLObject::LAST_SEGMENT, false, INetURLObject::NO_DECODE );
139 else
141 _rURL.Path = _rParser.GetURLPath( INetURLObject::NO_DECODE );
142 _rURL.Name = _rParser.GetName ( );
145 _rURL.Arguments = _rParser.GetParam ( INetURLObject::NO_DECODE );
146 _rURL.Mark = _rParser.GetMark ( INetURLObject::DECODE_WITH_CHARSET );
148 // INetURLObject supports only an intelligent method of parsing URL's. So write
149 // back Complete to have a valid encoded URL in all cases!
150 _rURL.Complete = _rParser.GetMainURL( INetURLObject::NO_DECODE );
151 if ( _bUseIntern )
152 _rURL.Complete = _rURL.Complete.intern();
154 _rParser.SetMark ( ::rtl::OUString() );
155 _rParser.SetParam( ::rtl::OUString() );
157 _rURL.Main = _rParser.GetMainURL( INetURLObject::NO_DECODE );
160 //*****************************************************************************************************************
161 // XURLTransformer
162 //*****************************************************************************************************************
163 sal_Bool SAL_CALL URLTransformer::parseStrict( URL& aURL ) throw( RuntimeException )
165 // Safe impossible cases.
166 if (( &aURL == NULL ) ||
167 ( aURL.Complete.getLength() < 1 ) )
169 return sal_False;
171 // Try to extract the protocol
172 sal_Int32 nURLIndex = aURL.Complete.indexOf( sal_Unicode( ':' ));
173 ::rtl::OUString aProtocol;
174 if ( nURLIndex > 1 )
176 aProtocol = aURL.Complete.copy( 0, nURLIndex+1 );
178 // If INetURLObject knows this protocol let it parse
179 if ( INetURLObject::CompareProtocolScheme( aProtocol ) != INET_PROT_NOT_VALID )
181 // Initialize parser with given URL.
182 INetURLObject aParser( aURL.Complete );
184 // Get all information about this URL.
185 INetProtocol eINetProt = aParser.GetProtocol();
186 if ( eINetProt == INET_PROT_NOT_VALID )
188 return sal_False;
190 else if ( !aParser.HasError() )
192 lcl_ParserHelper(aParser,aURL,false);
193 // Return "URL is parsed".
194 return sal_True;
197 else
199 // Minmal support for unknown protocols. This is mandatory to support the "Protocol Handlers" implemented
200 // in framework!
201 aURL.Protocol = aProtocol;
202 aURL.Main = aURL.Complete;
203 aURL.Path = aURL.Complete.copy( nURLIndex+1 );;
205 // Return "URL is parsed".
206 return sal_True;
210 return sal_False;
213 //*****************************************************************************************************************
214 // XURLTransformer
215 //*****************************************************************************************************************
216 sal_Bool SAL_CALL URLTransformer::parseSmart( URL& aURL ,
217 const ::rtl::OUString& sSmartProtocol ) throw( RuntimeException )
219 // Safe impossible cases.
220 if (( &aURL == NULL ) ||
221 ( aURL.Complete.getLength() < 1 ) )
223 return sal_False;
226 // Initialize parser with given URL.
227 INetURLObject aParser;
229 aParser.SetSmartProtocol( INetURLObject::CompareProtocolScheme( sSmartProtocol ));
230 bool bOk = aParser.SetSmartURL( aURL.Complete );
231 if ( bOk )
233 lcl_ParserHelper(aParser,aURL,true);
234 // Return "URL is parsed".
235 return sal_True;
237 else
239 // Minmal support for unknown protocols. This is mandatory to support the "Protocol Handlers" implemented
240 // in framework!
241 if ( INetURLObject::CompareProtocolScheme( sSmartProtocol ) == INET_PROT_NOT_VALID )
243 // Try to extract the protocol
244 sal_Int32 nIndex = aURL.Complete.indexOf( sal_Unicode( ':' ));
245 ::rtl::OUString aProtocol;
246 if ( nIndex > 1 )
248 aProtocol = aURL.Complete.copy( 0, nIndex+1 );
250 // If INetURLObject knows this protocol something is wrong as detected before =>
251 // give up and return false!
252 if ( INetURLObject::CompareProtocolScheme( aProtocol ) != INET_PROT_NOT_VALID )
253 return sal_False;
254 else
255 aURL.Protocol = aProtocol;
257 else
258 return sal_False;
260 aURL.Main = aURL.Complete;
261 aURL.Path = aURL.Complete.copy( nIndex+1 );
262 return sal_True;
264 else
265 return sal_False;
269 //*****************************************************************************************************************
270 // XURLTransformer
271 //*****************************************************************************************************************
272 sal_Bool SAL_CALL URLTransformer::assemble( URL& aURL ) throw( RuntimeException )
274 // Safe impossible cases.
275 if ( &aURL == NULL )
276 return sal_False ;
278 // Initialize parser.
279 INetURLObject aParser;
281 if ( INetURLObject::CompareProtocolScheme( aURL.Protocol ) != INET_PROT_NOT_VALID )
283 ::rtl::OUStringBuffer aCompletePath( aURL.Path );
285 // Concat the name if it is provided, just support a final slash
286 if ( aURL.Name.getLength() > 0 )
288 sal_Int32 nIndex = aURL.Path.lastIndexOf( sal_Unicode('/') );
289 if ( nIndex == ( aURL.Path.getLength() -1 ))
290 aCompletePath.append( aURL.Name );
291 else
293 aCompletePath.append( sal_Unicode( '/' ) );
294 aCompletePath.append( aURL.Name );
298 bool bResult = aParser.ConcatData(
299 INetURLObject::CompareProtocolScheme( aURL.Protocol ) ,
300 aURL.User ,
301 aURL.Password ,
302 aURL.Server ,
303 aURL.Port ,
304 aCompletePath.makeStringAndClear() );
306 if ( !bResult )
307 return sal_False;
309 // First parse URL WITHOUT ...
310 aURL.Main = aParser.GetMainURL( INetURLObject::NO_DECODE );
311 // ...and then WITH parameter and mark.
312 aParser.SetParam( aURL.Arguments);
313 aParser.SetMark ( aURL.Mark, INetURLObject::ENCODE_ALL );
314 aURL.Complete = aParser.GetMainURL( INetURLObject::NO_DECODE );
316 // Return "URL is assembled".
317 return sal_True;
319 else if ( aURL.Protocol.getLength() > 0 )
321 // Minimal support for unknown protocols
322 ::rtl::OUStringBuffer aBuffer( aURL.Protocol );
323 aBuffer.append( aURL.Path );
324 aURL.Complete = aBuffer.makeStringAndClear();
325 aURL.Main = aURL.Complete;
326 return sal_True;
329 return sal_False;
332 //*****************************************************************************************************************
333 // XURLTransformer
334 //*****************************************************************************************************************
335 ::rtl::OUString SAL_CALL URLTransformer::getPresentation( const URL& aURL ,
336 sal_Bool bWithPassword ) throw( RuntimeException )
338 // Safe impossible cases.
339 if (( &aURL == NULL ) ||
340 ( aURL.Complete.getLength() < 1 ) ||
341 (( bWithPassword != sal_True ) &&
342 ( bWithPassword != sal_False ) ) )
344 return ::rtl::OUString();
347 // Check given URL
348 URL aTestURL = aURL;
349 sal_Bool bParseResult = parseSmart( aTestURL, aTestURL.Protocol );
350 if ( bParseResult )
352 if ( !bWithPassword && aTestURL.Password.getLength() > 0 )
354 // Exchange password text with other placeholder string
355 aTestURL.Password = ::rtl::OUString::createFromAscii( "<******>" );
356 assemble( aTestURL );
359 // Convert internal URLs to "praesentation"-URLs!
360 rtl::OUString sPraesentationURL;
361 INetURLObject::translateToExternal( aTestURL.Complete, sPraesentationURL, INetURLObject::DECODE_UNAMBIGUOUS );
363 return sPraesentationURL;
365 else
366 return ::rtl::OUString();
369 //_________________________________________________________________________________________________________________
370 // debug methods
371 //_________________________________________________________________________________________________________________
374 } // namespace framework