1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_framework.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 32*cdf0e10cSrcweir // my own includes 33*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 34*cdf0e10cSrcweir #include <services/urltransformer.hxx> 35*cdf0e10cSrcweir #include <threadhelp/resetableguard.hxx> 36*cdf0e10cSrcweir #include <macros/debug.hxx> 37*cdf0e10cSrcweir #include <services.h> 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 40*cdf0e10cSrcweir // interface includes 41*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 44*cdf0e10cSrcweir // includes of other projects 45*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 46*cdf0e10cSrcweir #include <tools/urlobj.hxx> 47*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx> 48*cdf0e10cSrcweir #include <vcl/svapp.hxx> 49*cdf0e10cSrcweir 50*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 51*cdf0e10cSrcweir // namespace 52*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir namespace framework{ 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir using namespace ::osl ; 57*cdf0e10cSrcweir using namespace ::cppu ; 58*cdf0e10cSrcweir using namespace ::com::sun::star::uno ; 59*cdf0e10cSrcweir using namespace ::com::sun::star::lang ; 60*cdf0e10cSrcweir using namespace ::com::sun::star::util ; 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 63*cdf0e10cSrcweir // non exported const 64*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 65*cdf0e10cSrcweir 66*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 67*cdf0e10cSrcweir // non exported definitions 68*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 69*cdf0e10cSrcweir 70*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 71*cdf0e10cSrcweir // declarations 72*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 73*cdf0e10cSrcweir 74*cdf0e10cSrcweir //***************************************************************************************************************** 75*cdf0e10cSrcweir // constructor 76*cdf0e10cSrcweir //***************************************************************************************************************** 77*cdf0e10cSrcweir URLTransformer::URLTransformer( const Reference< XMultiServiceFactory >& /*xFactory*/ ) 78*cdf0e10cSrcweir { 79*cdf0e10cSrcweir // Safe impossible cases. 80*cdf0e10cSrcweir // Method not defined for all incoming parameter. 81*cdf0e10cSrcweir //LOG_ASSERT( xFactory.is(), "URLTransformer::URLTransformer()\nInvalid parameter detected!\n" ) 82*cdf0e10cSrcweir } 83*cdf0e10cSrcweir 84*cdf0e10cSrcweir //***************************************************************************************************************** 85*cdf0e10cSrcweir // destructor 86*cdf0e10cSrcweir //***************************************************************************************************************** 87*cdf0e10cSrcweir URLTransformer::~URLTransformer() 88*cdf0e10cSrcweir { 89*cdf0e10cSrcweir } 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir //***************************************************************************************************************** 92*cdf0e10cSrcweir // XInterface, XTypeProvider, XServiceInfo 93*cdf0e10cSrcweir //***************************************************************************************************************** 94*cdf0e10cSrcweir 95*cdf0e10cSrcweir DEFINE_XSERVICEINFO_MULTISERVICE ( URLTransformer , 96*cdf0e10cSrcweir OWeakObject , 97*cdf0e10cSrcweir SERVICENAME_URLTRANSFORMER , 98*cdf0e10cSrcweir IMPLEMENTATIONNAME_URLTRANSFORMER 99*cdf0e10cSrcweir ) 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir DEFINE_INIT_SERVICE ( URLTransformer, 102*cdf0e10cSrcweir { 103*cdf0e10cSrcweir } 104*cdf0e10cSrcweir ) 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir namespace 107*cdf0e10cSrcweir { 108*cdf0e10cSrcweir void lcl_ParserHelper(INetURLObject& _rParser,URL& _rURL,bool _bUseIntern) 109*cdf0e10cSrcweir { 110*cdf0e10cSrcweir // Get all information about this URL. 111*cdf0e10cSrcweir _rURL.Protocol = INetURLObject::GetScheme( _rParser.GetProtocol() ); 112*cdf0e10cSrcweir _rURL.User = _rParser.GetUser ( INetURLObject::DECODE_WITH_CHARSET ); 113*cdf0e10cSrcweir _rURL.Password = _rParser.GetPass ( INetURLObject::DECODE_WITH_CHARSET ); 114*cdf0e10cSrcweir _rURL.Server = _rParser.GetHost ( INetURLObject::DECODE_WITH_CHARSET ); 115*cdf0e10cSrcweir _rURL.Port = (sal_Int16)_rParser.GetPort(); 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir sal_Int32 nCount = _rParser.getSegmentCount( false ); 118*cdf0e10cSrcweir if ( nCount > 0 ) 119*cdf0e10cSrcweir { 120*cdf0e10cSrcweir // Don't add last segment as it is the name! 121*cdf0e10cSrcweir --nCount; 122*cdf0e10cSrcweir 123*cdf0e10cSrcweir rtl::OUStringBuffer aPath; 124*cdf0e10cSrcweir for ( sal_Int32 nIndex = 0; nIndex < nCount; nIndex++ ) 125*cdf0e10cSrcweir { 126*cdf0e10cSrcweir aPath.append( sal_Unicode( '/' )); 127*cdf0e10cSrcweir aPath.append( _rParser.getName( nIndex, false, INetURLObject::NO_DECODE )); 128*cdf0e10cSrcweir } 129*cdf0e10cSrcweir 130*cdf0e10cSrcweir if ( nCount > 0 ) 131*cdf0e10cSrcweir aPath.append( sal_Unicode( '/' )); // final slash! 132*cdf0e10cSrcweir 133*cdf0e10cSrcweir _rURL.Path = aPath.makeStringAndClear(); 134*cdf0e10cSrcweir _rURL.Name = _rParser.getName( INetURLObject::LAST_SEGMENT, false, INetURLObject::NO_DECODE ); 135*cdf0e10cSrcweir } 136*cdf0e10cSrcweir else 137*cdf0e10cSrcweir { 138*cdf0e10cSrcweir _rURL.Path = _rParser.GetURLPath( INetURLObject::NO_DECODE ); 139*cdf0e10cSrcweir _rURL.Name = _rParser.GetName ( ); 140*cdf0e10cSrcweir } 141*cdf0e10cSrcweir 142*cdf0e10cSrcweir _rURL.Arguments = _rParser.GetParam ( INetURLObject::NO_DECODE ); 143*cdf0e10cSrcweir _rURL.Mark = _rParser.GetMark ( INetURLObject::DECODE_WITH_CHARSET ); 144*cdf0e10cSrcweir 145*cdf0e10cSrcweir // INetURLObject supports only an intelligent method of parsing URL's. So write 146*cdf0e10cSrcweir // back Complete to have a valid encoded URL in all cases! 147*cdf0e10cSrcweir _rURL.Complete = _rParser.GetMainURL( INetURLObject::NO_DECODE ); 148*cdf0e10cSrcweir if ( _bUseIntern ) 149*cdf0e10cSrcweir _rURL.Complete = _rURL.Complete.intern(); 150*cdf0e10cSrcweir 151*cdf0e10cSrcweir _rParser.SetMark ( ::rtl::OUString() ); 152*cdf0e10cSrcweir _rParser.SetParam( ::rtl::OUString() ); 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir _rURL.Main = _rParser.GetMainURL( INetURLObject::NO_DECODE ); 155*cdf0e10cSrcweir } 156*cdf0e10cSrcweir } 157*cdf0e10cSrcweir //***************************************************************************************************************** 158*cdf0e10cSrcweir // XURLTransformer 159*cdf0e10cSrcweir //***************************************************************************************************************** 160*cdf0e10cSrcweir sal_Bool SAL_CALL URLTransformer::parseStrict( URL& aURL ) throw( RuntimeException ) 161*cdf0e10cSrcweir { 162*cdf0e10cSrcweir // Safe impossible cases. 163*cdf0e10cSrcweir if (( &aURL == NULL ) || 164*cdf0e10cSrcweir ( aURL.Complete.getLength() < 1 ) ) 165*cdf0e10cSrcweir { 166*cdf0e10cSrcweir return sal_False; 167*cdf0e10cSrcweir } 168*cdf0e10cSrcweir // Try to extract the protocol 169*cdf0e10cSrcweir sal_Int32 nURLIndex = aURL.Complete.indexOf( sal_Unicode( ':' )); 170*cdf0e10cSrcweir ::rtl::OUString aProtocol; 171*cdf0e10cSrcweir if ( nURLIndex > 1 ) 172*cdf0e10cSrcweir { 173*cdf0e10cSrcweir aProtocol = aURL.Complete.copy( 0, nURLIndex+1 ); 174*cdf0e10cSrcweir 175*cdf0e10cSrcweir // If INetURLObject knows this protocol let it parse 176*cdf0e10cSrcweir if ( INetURLObject::CompareProtocolScheme( aProtocol ) != INET_PROT_NOT_VALID ) 177*cdf0e10cSrcweir { 178*cdf0e10cSrcweir // Initialize parser with given URL. 179*cdf0e10cSrcweir INetURLObject aParser( aURL.Complete ); 180*cdf0e10cSrcweir 181*cdf0e10cSrcweir // Get all information about this URL. 182*cdf0e10cSrcweir INetProtocol eINetProt = aParser.GetProtocol(); 183*cdf0e10cSrcweir if ( eINetProt == INET_PROT_NOT_VALID ) 184*cdf0e10cSrcweir { 185*cdf0e10cSrcweir return sal_False; 186*cdf0e10cSrcweir } 187*cdf0e10cSrcweir else if ( !aParser.HasError() ) 188*cdf0e10cSrcweir { 189*cdf0e10cSrcweir lcl_ParserHelper(aParser,aURL,false); 190*cdf0e10cSrcweir // Return "URL is parsed". 191*cdf0e10cSrcweir return sal_True; 192*cdf0e10cSrcweir } 193*cdf0e10cSrcweir } 194*cdf0e10cSrcweir else 195*cdf0e10cSrcweir { 196*cdf0e10cSrcweir // Minmal support for unknown protocols. This is mandatory to support the "Protocol Handlers" implemented 197*cdf0e10cSrcweir // in framework! 198*cdf0e10cSrcweir aURL.Protocol = aProtocol; 199*cdf0e10cSrcweir aURL.Main = aURL.Complete; 200*cdf0e10cSrcweir aURL.Path = aURL.Complete.copy( nURLIndex+1 );; 201*cdf0e10cSrcweir 202*cdf0e10cSrcweir // Return "URL is parsed". 203*cdf0e10cSrcweir return sal_True; 204*cdf0e10cSrcweir } 205*cdf0e10cSrcweir } 206*cdf0e10cSrcweir 207*cdf0e10cSrcweir return sal_False; 208*cdf0e10cSrcweir } 209*cdf0e10cSrcweir 210*cdf0e10cSrcweir //***************************************************************************************************************** 211*cdf0e10cSrcweir // XURLTransformer 212*cdf0e10cSrcweir //***************************************************************************************************************** 213*cdf0e10cSrcweir sal_Bool SAL_CALL URLTransformer::parseSmart( URL& aURL , 214*cdf0e10cSrcweir const ::rtl::OUString& sSmartProtocol ) throw( RuntimeException ) 215*cdf0e10cSrcweir { 216*cdf0e10cSrcweir // Safe impossible cases. 217*cdf0e10cSrcweir if (( &aURL == NULL ) || 218*cdf0e10cSrcweir ( aURL.Complete.getLength() < 1 ) ) 219*cdf0e10cSrcweir { 220*cdf0e10cSrcweir return sal_False; 221*cdf0e10cSrcweir } 222*cdf0e10cSrcweir 223*cdf0e10cSrcweir // Initialize parser with given URL. 224*cdf0e10cSrcweir INetURLObject aParser; 225*cdf0e10cSrcweir 226*cdf0e10cSrcweir aParser.SetSmartProtocol( INetURLObject::CompareProtocolScheme( sSmartProtocol )); 227*cdf0e10cSrcweir bool bOk = aParser.SetSmartURL( aURL.Complete ); 228*cdf0e10cSrcweir if ( bOk ) 229*cdf0e10cSrcweir { 230*cdf0e10cSrcweir lcl_ParserHelper(aParser,aURL,true); 231*cdf0e10cSrcweir // Return "URL is parsed". 232*cdf0e10cSrcweir return sal_True; 233*cdf0e10cSrcweir } 234*cdf0e10cSrcweir else 235*cdf0e10cSrcweir { 236*cdf0e10cSrcweir // Minmal support for unknown protocols. This is mandatory to support the "Protocol Handlers" implemented 237*cdf0e10cSrcweir // in framework! 238*cdf0e10cSrcweir if ( INetURLObject::CompareProtocolScheme( sSmartProtocol ) == INET_PROT_NOT_VALID ) 239*cdf0e10cSrcweir { 240*cdf0e10cSrcweir // Try to extract the protocol 241*cdf0e10cSrcweir sal_Int32 nIndex = aURL.Complete.indexOf( sal_Unicode( ':' )); 242*cdf0e10cSrcweir ::rtl::OUString aProtocol; 243*cdf0e10cSrcweir if ( nIndex > 1 ) 244*cdf0e10cSrcweir { 245*cdf0e10cSrcweir aProtocol = aURL.Complete.copy( 0, nIndex+1 ); 246*cdf0e10cSrcweir 247*cdf0e10cSrcweir // If INetURLObject knows this protocol something is wrong as detected before => 248*cdf0e10cSrcweir // give up and return false! 249*cdf0e10cSrcweir if ( INetURLObject::CompareProtocolScheme( aProtocol ) != INET_PROT_NOT_VALID ) 250*cdf0e10cSrcweir return sal_False; 251*cdf0e10cSrcweir else 252*cdf0e10cSrcweir aURL.Protocol = aProtocol; 253*cdf0e10cSrcweir } 254*cdf0e10cSrcweir else 255*cdf0e10cSrcweir return sal_False; 256*cdf0e10cSrcweir 257*cdf0e10cSrcweir aURL.Main = aURL.Complete; 258*cdf0e10cSrcweir aURL.Path = aURL.Complete.copy( nIndex+1 ); 259*cdf0e10cSrcweir return sal_True; 260*cdf0e10cSrcweir } 261*cdf0e10cSrcweir else 262*cdf0e10cSrcweir return sal_False; 263*cdf0e10cSrcweir } 264*cdf0e10cSrcweir } 265*cdf0e10cSrcweir 266*cdf0e10cSrcweir //***************************************************************************************************************** 267*cdf0e10cSrcweir // XURLTransformer 268*cdf0e10cSrcweir //***************************************************************************************************************** 269*cdf0e10cSrcweir sal_Bool SAL_CALL URLTransformer::assemble( URL& aURL ) throw( RuntimeException ) 270*cdf0e10cSrcweir { 271*cdf0e10cSrcweir // Safe impossible cases. 272*cdf0e10cSrcweir if ( &aURL == NULL ) 273*cdf0e10cSrcweir return sal_False ; 274*cdf0e10cSrcweir 275*cdf0e10cSrcweir // Initialize parser. 276*cdf0e10cSrcweir INetURLObject aParser; 277*cdf0e10cSrcweir 278*cdf0e10cSrcweir if ( INetURLObject::CompareProtocolScheme( aURL.Protocol ) != INET_PROT_NOT_VALID ) 279*cdf0e10cSrcweir { 280*cdf0e10cSrcweir ::rtl::OUStringBuffer aCompletePath( aURL.Path ); 281*cdf0e10cSrcweir 282*cdf0e10cSrcweir // Concat the name if it is provided, just support a final slash 283*cdf0e10cSrcweir if ( aURL.Name.getLength() > 0 ) 284*cdf0e10cSrcweir { 285*cdf0e10cSrcweir sal_Int32 nIndex = aURL.Path.lastIndexOf( sal_Unicode('/') ); 286*cdf0e10cSrcweir if ( nIndex == ( aURL.Path.getLength() -1 )) 287*cdf0e10cSrcweir aCompletePath.append( aURL.Name ); 288*cdf0e10cSrcweir else 289*cdf0e10cSrcweir { 290*cdf0e10cSrcweir aCompletePath.append( sal_Unicode( '/' ) ); 291*cdf0e10cSrcweir aCompletePath.append( aURL.Name ); 292*cdf0e10cSrcweir } 293*cdf0e10cSrcweir } 294*cdf0e10cSrcweir 295*cdf0e10cSrcweir bool bResult = aParser.ConcatData( 296*cdf0e10cSrcweir INetURLObject::CompareProtocolScheme( aURL.Protocol ) , 297*cdf0e10cSrcweir aURL.User , 298*cdf0e10cSrcweir aURL.Password , 299*cdf0e10cSrcweir aURL.Server , 300*cdf0e10cSrcweir aURL.Port , 301*cdf0e10cSrcweir aCompletePath.makeStringAndClear() ); 302*cdf0e10cSrcweir 303*cdf0e10cSrcweir if ( !bResult ) 304*cdf0e10cSrcweir return sal_False; 305*cdf0e10cSrcweir 306*cdf0e10cSrcweir // First parse URL WITHOUT ... 307*cdf0e10cSrcweir aURL.Main = aParser.GetMainURL( INetURLObject::NO_DECODE ); 308*cdf0e10cSrcweir // ...and then WITH parameter and mark. 309*cdf0e10cSrcweir aParser.SetParam( aURL.Arguments); 310*cdf0e10cSrcweir aParser.SetMark ( aURL.Mark, INetURLObject::ENCODE_ALL ); 311*cdf0e10cSrcweir aURL.Complete = aParser.GetMainURL( INetURLObject::NO_DECODE ); 312*cdf0e10cSrcweir 313*cdf0e10cSrcweir // Return "URL is assembled". 314*cdf0e10cSrcweir return sal_True; 315*cdf0e10cSrcweir } 316*cdf0e10cSrcweir else if ( aURL.Protocol.getLength() > 0 ) 317*cdf0e10cSrcweir { 318*cdf0e10cSrcweir // Minimal support for unknown protocols 319*cdf0e10cSrcweir ::rtl::OUStringBuffer aBuffer( aURL.Protocol ); 320*cdf0e10cSrcweir aBuffer.append( aURL.Path ); 321*cdf0e10cSrcweir aURL.Complete = aBuffer.makeStringAndClear(); 322*cdf0e10cSrcweir aURL.Main = aURL.Complete; 323*cdf0e10cSrcweir return sal_True; 324*cdf0e10cSrcweir } 325*cdf0e10cSrcweir 326*cdf0e10cSrcweir return sal_False; 327*cdf0e10cSrcweir } 328*cdf0e10cSrcweir 329*cdf0e10cSrcweir //***************************************************************************************************************** 330*cdf0e10cSrcweir // XURLTransformer 331*cdf0e10cSrcweir //***************************************************************************************************************** 332*cdf0e10cSrcweir ::rtl::OUString SAL_CALL URLTransformer::getPresentation( const URL& aURL , 333*cdf0e10cSrcweir sal_Bool bWithPassword ) throw( RuntimeException ) 334*cdf0e10cSrcweir { 335*cdf0e10cSrcweir // Safe impossible cases. 336*cdf0e10cSrcweir if (( &aURL == NULL ) || 337*cdf0e10cSrcweir ( aURL.Complete.getLength() < 1 ) || 338*cdf0e10cSrcweir (( bWithPassword != sal_True ) && 339*cdf0e10cSrcweir ( bWithPassword != sal_False ) ) ) 340*cdf0e10cSrcweir { 341*cdf0e10cSrcweir return ::rtl::OUString(); 342*cdf0e10cSrcweir } 343*cdf0e10cSrcweir 344*cdf0e10cSrcweir // Check given URL 345*cdf0e10cSrcweir URL aTestURL = aURL; 346*cdf0e10cSrcweir sal_Bool bParseResult = parseSmart( aTestURL, aTestURL.Protocol ); 347*cdf0e10cSrcweir if ( bParseResult ) 348*cdf0e10cSrcweir { 349*cdf0e10cSrcweir if ( !bWithPassword && aTestURL.Password.getLength() > 0 ) 350*cdf0e10cSrcweir { 351*cdf0e10cSrcweir // Exchange password text with other placeholder string 352*cdf0e10cSrcweir aTestURL.Password = ::rtl::OUString::createFromAscii( "<******>" ); 353*cdf0e10cSrcweir assemble( aTestURL ); 354*cdf0e10cSrcweir } 355*cdf0e10cSrcweir 356*cdf0e10cSrcweir // Convert internal URLs to "praesentation"-URLs! 357*cdf0e10cSrcweir rtl::OUString sPraesentationURL; 358*cdf0e10cSrcweir INetURLObject::translateToExternal( aTestURL.Complete, sPraesentationURL, INetURLObject::DECODE_UNAMBIGUOUS ); 359*cdf0e10cSrcweir 360*cdf0e10cSrcweir return sPraesentationURL; 361*cdf0e10cSrcweir } 362*cdf0e10cSrcweir else 363*cdf0e10cSrcweir return ::rtl::OUString(); 364*cdf0e10cSrcweir } 365*cdf0e10cSrcweir 366*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 367*cdf0e10cSrcweir // debug methods 368*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 369*cdf0e10cSrcweir 370*cdf0e10cSrcweir 371*cdf0e10cSrcweir } // namespace framework 372*cdf0e10cSrcweir 373