LCOV - code coverage report
Current view: top level - framework/source/services - urltransformer.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 54 121 44.6 %
Date: 2012-08-25 Functions: 10 16 62.5 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 37 166 22.3 %

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

Generated by: LCOV version 1.10