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 $
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 //_________________________________________________________________________________________________________________
36 //_________________________________________________________________________________________________________________
37 #include <services/urltransformer.hxx>
38 #include <threadhelp/resetableguard.hxx>
39 #include <macros/debug.hxx>
42 //_________________________________________________________________________________________________________________
44 //_________________________________________________________________________________________________________________
46 //_________________________________________________________________________________________________________________
47 // includes of other projects
48 //_________________________________________________________________________________________________________________
49 #include <tools/urlobj.hxx>
50 #include <rtl/ustrbuf.hxx>
51 #include <vcl/svapp.hxx>
53 //_________________________________________________________________________________________________________________
55 //_________________________________________________________________________________________________________________
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 //_________________________________________________________________________________________________________________
67 //_________________________________________________________________________________________________________________
69 //_________________________________________________________________________________________________________________
70 // non exported definitions
71 //_________________________________________________________________________________________________________________
73 //_________________________________________________________________________________________________________________
75 //_________________________________________________________________________________________________________________
77 //*****************************************************************************************************************
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 //*****************************************************************************************************************
89 //*****************************************************************************************************************
90 URLTransformer::~URLTransformer()
94 //*****************************************************************************************************************
95 // XInterface, XTypeProvider, XServiceInfo
96 //*****************************************************************************************************************
98 DEFINE_XSERVICEINFO_MULTISERVICE ( URLTransformer
,
100 SERVICENAME_URLTRANSFORMER
,
101 IMPLEMENTATIONNAME_URLTRANSFORMER
104 DEFINE_INIT_SERVICE ( URLTransformer
,
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 );
123 // Don't add last segment as it is the name!
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
));
134 aPath
.append( sal_Unicode( '/' )); // final slash!
136 _rURL
.Path
= aPath
.makeStringAndClear();
137 _rURL
.Name
= _rParser
.getName( INetURLObject::LAST_SEGMENT
, false, INetURLObject::NO_DECODE
);
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
);
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 //*****************************************************************************************************************
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 ) )
171 // Try to extract the protocol
172 sal_Int32 nURLIndex
= aURL
.Complete
.indexOf( sal_Unicode( ':' ));
173 ::rtl::OUString aProtocol
;
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
)
190 else if ( !aParser
.HasError() )
192 lcl_ParserHelper(aParser
,aURL
,false);
193 // Return "URL is parsed".
199 // Minmal support for unknown protocols. This is mandatory to support the "Protocol Handlers" implemented
201 aURL
.Protocol
= aProtocol
;
202 aURL
.Main
= aURL
.Complete
;
203 aURL
.Path
= aURL
.Complete
.copy( nURLIndex
+1 );;
205 // Return "URL is parsed".
213 //*****************************************************************************************************************
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 ) )
226 // Initialize parser with given URL.
227 INetURLObject aParser
;
229 aParser
.SetSmartProtocol( INetURLObject::CompareProtocolScheme( sSmartProtocol
));
230 bool bOk
= aParser
.SetSmartURL( aURL
.Complete
);
233 lcl_ParserHelper(aParser
,aURL
,true);
234 // Return "URL is parsed".
239 // Minmal support for unknown protocols. This is mandatory to support the "Protocol Handlers" implemented
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
;
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
)
255 aURL
.Protocol
= aProtocol
;
260 aURL
.Main
= aURL
.Complete
;
261 aURL
.Path
= aURL
.Complete
.copy( nIndex
+1 );
269 //*****************************************************************************************************************
271 //*****************************************************************************************************************
272 sal_Bool SAL_CALL
URLTransformer::assemble( URL
& aURL
) throw( RuntimeException
)
274 // Safe impossible cases.
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
);
293 aCompletePath
.append( sal_Unicode( '/' ) );
294 aCompletePath
.append( aURL
.Name
);
298 bool bResult
= aParser
.ConcatData(
299 INetURLObject::CompareProtocolScheme( aURL
.Protocol
) ,
304 aCompletePath
.makeStringAndClear() );
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".
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
;
332 //*****************************************************************************************************************
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();
349 sal_Bool bParseResult
= parseSmart( aTestURL
, aTestURL
.Protocol
);
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
;
366 return ::rtl::OUString();
369 //_________________________________________________________________________________________________________________
371 //_________________________________________________________________________________________________________________
374 } // namespace framework