LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/framework/source/services - urltransformer.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 53 123 43.1 %
Date: 2013-07-09 Functions: 12 18 66.7 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10