1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_framework.hxx"
31 //_________________________________________________________________________________________________________________
33 //_________________________________________________________________________________________________________________
34 #include <services/urltransformer.hxx>
35 #include <threadhelp/resetableguard.hxx>
36 #include <macros/debug.hxx>
39 //_________________________________________________________________________________________________________________
41 //_________________________________________________________________________________________________________________
43 //_________________________________________________________________________________________________________________
44 // includes of other projects
45 //_________________________________________________________________________________________________________________
46 #include <tools/urlobj.hxx>
47 #include <rtl/ustrbuf.hxx>
48 #include <vcl/svapp.hxx>
50 //_________________________________________________________________________________________________________________
52 //_________________________________________________________________________________________________________________
56 using namespace ::osl
;
57 using namespace ::cppu
;
58 using namespace ::com::sun::star::uno
;
59 using namespace ::com::sun::star::lang
;
60 using namespace ::com::sun::star::util
;
62 //_________________________________________________________________________________________________________________
64 //_________________________________________________________________________________________________________________
66 //_________________________________________________________________________________________________________________
67 // non exported definitions
68 //_________________________________________________________________________________________________________________
70 //_________________________________________________________________________________________________________________
72 //_________________________________________________________________________________________________________________
74 //*****************************************************************************************************************
76 //*****************************************************************************************************************
77 URLTransformer::URLTransformer( const Reference
< XMultiServiceFactory
>& /*xFactory*/ )
79 // Safe impossible cases.
80 // Method not defined for all incoming parameter.
81 //LOG_ASSERT( xFactory.is(), "URLTransformer::URLTransformer()\nInvalid parameter detected!\n" )
84 //*****************************************************************************************************************
86 //*****************************************************************************************************************
87 URLTransformer::~URLTransformer()
91 //*****************************************************************************************************************
92 // XInterface, XTypeProvider, XServiceInfo
93 //*****************************************************************************************************************
95 DEFINE_XSERVICEINFO_MULTISERVICE ( URLTransformer
,
97 SERVICENAME_URLTRANSFORMER
,
98 IMPLEMENTATIONNAME_URLTRANSFORMER
101 DEFINE_INIT_SERVICE ( URLTransformer
,
108 void lcl_ParserHelper(INetURLObject
& _rParser
,URL
& _rURL
,bool _bUseIntern
)
110 // Get all information about this URL.
111 _rURL
.Protocol
= INetURLObject::GetScheme( _rParser
.GetProtocol() );
112 _rURL
.User
= _rParser
.GetUser ( INetURLObject::DECODE_WITH_CHARSET
);
113 _rURL
.Password
= _rParser
.GetPass ( INetURLObject::DECODE_WITH_CHARSET
);
114 _rURL
.Server
= _rParser
.GetHost ( INetURLObject::DECODE_WITH_CHARSET
);
115 _rURL
.Port
= (sal_Int16
)_rParser
.GetPort();
117 sal_Int32 nCount
= _rParser
.getSegmentCount( false );
120 // Don't add last segment as it is the name!
123 rtl::OUStringBuffer aPath
;
124 for ( sal_Int32 nIndex
= 0; nIndex
< nCount
; nIndex
++ )
126 aPath
.append( sal_Unicode( '/' ));
127 aPath
.append( _rParser
.getName( nIndex
, false, INetURLObject::NO_DECODE
));
131 aPath
.append( sal_Unicode( '/' )); // final slash!
133 _rURL
.Path
= aPath
.makeStringAndClear();
134 _rURL
.Name
= _rParser
.getName( INetURLObject::LAST_SEGMENT
, false, INetURLObject::NO_DECODE
);
138 _rURL
.Path
= _rParser
.GetURLPath( INetURLObject::NO_DECODE
);
139 _rURL
.Name
= _rParser
.GetName ( );
142 _rURL
.Arguments
= _rParser
.GetParam ( INetURLObject::NO_DECODE
);
143 _rURL
.Mark
= _rParser
.GetMark ( INetURLObject::DECODE_WITH_CHARSET
);
145 // INetURLObject supports only an intelligent method of parsing URL's. So write
146 // back Complete to have a valid encoded URL in all cases!
147 _rURL
.Complete
= _rParser
.GetMainURL( INetURLObject::NO_DECODE
);
149 _rURL
.Complete
= _rURL
.Complete
.intern();
151 _rParser
.SetMark ( ::rtl::OUString() );
152 _rParser
.SetParam( ::rtl::OUString() );
154 _rURL
.Main
= _rParser
.GetMainURL( INetURLObject::NO_DECODE
);
157 //*****************************************************************************************************************
159 //*****************************************************************************************************************
160 sal_Bool SAL_CALL
URLTransformer::parseStrict( URL
& aURL
) throw( RuntimeException
)
162 // Safe impossible cases.
163 if (( &aURL
== NULL
) ||
164 ( aURL
.Complete
.getLength() < 1 ) )
168 // Try to extract the protocol
169 sal_Int32 nURLIndex
= aURL
.Complete
.indexOf( sal_Unicode( ':' ));
170 ::rtl::OUString aProtocol
;
173 aProtocol
= aURL
.Complete
.copy( 0, nURLIndex
+1 );
175 // If INetURLObject knows this protocol let it parse
176 if ( INetURLObject::CompareProtocolScheme( aProtocol
) != INET_PROT_NOT_VALID
)
178 // Initialize parser with given URL.
179 INetURLObject
aParser( aURL
.Complete
);
181 // Get all information about this URL.
182 INetProtocol eINetProt
= aParser
.GetProtocol();
183 if ( eINetProt
== INET_PROT_NOT_VALID
)
187 else if ( !aParser
.HasError() )
189 lcl_ParserHelper(aParser
,aURL
,false);
190 // Return "URL is parsed".
196 // Minmal support for unknown protocols. This is mandatory to support the "Protocol Handlers" implemented
198 aURL
.Protocol
= aProtocol
;
199 aURL
.Main
= aURL
.Complete
;
200 aURL
.Path
= aURL
.Complete
.copy( nURLIndex
+1 );;
202 // Return "URL is parsed".
210 //*****************************************************************************************************************
212 //*****************************************************************************************************************
213 sal_Bool SAL_CALL
URLTransformer::parseSmart( URL
& aURL
,
214 const ::rtl::OUString
& sSmartProtocol
) throw( RuntimeException
)
216 // Safe impossible cases.
217 if (( &aURL
== NULL
) ||
218 ( aURL
.Complete
.getLength() < 1 ) )
223 // Initialize parser with given URL.
224 INetURLObject aParser
;
226 aParser
.SetSmartProtocol( INetURLObject::CompareProtocolScheme( sSmartProtocol
));
227 bool bOk
= aParser
.SetSmartURL( aURL
.Complete
);
230 lcl_ParserHelper(aParser
,aURL
,true);
231 // Return "URL is parsed".
236 // Minmal support for unknown protocols. This is mandatory to support the "Protocol Handlers" implemented
238 if ( INetURLObject::CompareProtocolScheme( sSmartProtocol
) == INET_PROT_NOT_VALID
)
240 // Try to extract the protocol
241 sal_Int32 nIndex
= aURL
.Complete
.indexOf( sal_Unicode( ':' ));
242 ::rtl::OUString aProtocol
;
245 aProtocol
= aURL
.Complete
.copy( 0, nIndex
+1 );
247 // If INetURLObject knows this protocol something is wrong as detected before =>
248 // give up and return false!
249 if ( INetURLObject::CompareProtocolScheme( aProtocol
) != INET_PROT_NOT_VALID
)
252 aURL
.Protocol
= aProtocol
;
257 aURL
.Main
= aURL
.Complete
;
258 aURL
.Path
= aURL
.Complete
.copy( nIndex
+1 );
266 //*****************************************************************************************************************
268 //*****************************************************************************************************************
269 sal_Bool SAL_CALL
URLTransformer::assemble( URL
& aURL
) throw( RuntimeException
)
271 // Safe impossible cases.
275 // Initialize parser.
276 INetURLObject aParser
;
278 if ( INetURLObject::CompareProtocolScheme( aURL
.Protocol
) != INET_PROT_NOT_VALID
)
280 ::rtl::OUStringBuffer
aCompletePath( aURL
.Path
);
282 // Concat the name if it is provided, just support a final slash
283 if ( aURL
.Name
.getLength() > 0 )
285 sal_Int32 nIndex
= aURL
.Path
.lastIndexOf( sal_Unicode('/') );
286 if ( nIndex
== ( aURL
.Path
.getLength() -1 ))
287 aCompletePath
.append( aURL
.Name
);
290 aCompletePath
.append( sal_Unicode( '/' ) );
291 aCompletePath
.append( aURL
.Name
);
295 bool bResult
= aParser
.ConcatData(
296 INetURLObject::CompareProtocolScheme( aURL
.Protocol
) ,
301 aCompletePath
.makeStringAndClear() );
306 // First parse URL WITHOUT ...
307 aURL
.Main
= aParser
.GetMainURL( INetURLObject::NO_DECODE
);
308 // ...and then WITH parameter and mark.
309 aParser
.SetParam( aURL
.Arguments
);
310 aParser
.SetMark ( aURL
.Mark
, INetURLObject::ENCODE_ALL
);
311 aURL
.Complete
= aParser
.GetMainURL( INetURLObject::NO_DECODE
);
313 // Return "URL is assembled".
316 else if ( aURL
.Protocol
.getLength() > 0 )
318 // Minimal support for unknown protocols
319 ::rtl::OUStringBuffer
aBuffer( aURL
.Protocol
);
320 aBuffer
.append( aURL
.Path
);
321 aURL
.Complete
= aBuffer
.makeStringAndClear();
322 aURL
.Main
= aURL
.Complete
;
329 //*****************************************************************************************************************
331 //*****************************************************************************************************************
332 ::rtl::OUString SAL_CALL
URLTransformer::getPresentation( const URL
& aURL
,
333 sal_Bool bWithPassword
) throw( RuntimeException
)
335 // Safe impossible cases.
336 if (( &aURL
== NULL
) ||
337 ( aURL
.Complete
.getLength() < 1 ) ||
338 (( bWithPassword
!= sal_True
) &&
339 ( bWithPassword
!= sal_False
) ) )
341 return ::rtl::OUString();
346 sal_Bool bParseResult
= parseSmart( aTestURL
, aTestURL
.Protocol
);
349 if ( !bWithPassword
&& aTestURL
.Password
.getLength() > 0 )
351 // Exchange password text with other placeholder string
352 aTestURL
.Password
= ::rtl::OUString::createFromAscii( "<******>" );
353 assemble( aTestURL
);
356 // Convert internal URLs to "praesentation"-URLs!
357 rtl::OUString sPraesentationURL
;
358 INetURLObject::translateToExternal( aTestURL
.Complete
, sPraesentationURL
, INetURLObject::DECODE_UNAMBIGUOUS
);
360 return sPraesentationURL
;
363 return ::rtl::OUString();
366 //_________________________________________________________________________________________________________________
368 //_________________________________________________________________________________________________________________
371 } // namespace framework