LCOV - code coverage report
Current view: top level - ucb/source/ucp/expand - ucpexpand.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 37 60 61.7 %
Date: 2012-08-25 Functions: 13 17 76.5 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 26 82 31.7 %

           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                 :            : 
      30                 :            : #include "rtl/uri.hxx"
      31                 :            : #include "osl/mutex.hxx"
      32                 :            : #include "cppuhelper/compbase2.hxx"
      33                 :            : #include "cppuhelper/factory.hxx"
      34                 :            : #include "cppuhelper/implementationentry.hxx"
      35                 :            : #include "ucbhelper/content.hxx"
      36                 :            : #include "com/sun/star/uno/XComponentContext.hpp"
      37                 :            : #include "com/sun/star/lang/DisposedException.hpp"
      38                 :            : #include "com/sun/star/lang/XServiceInfo.hpp"
      39                 :            : #include "com/sun/star/lang/XMultiServiceFactory.hpp"
      40                 :            : #include "com/sun/star/registry/XRegistryKey.hpp"
      41                 :            : #include "com/sun/star/util/XMacroExpander.hpp"
      42                 :            : #include "com/sun/star/ucb/XContentProvider.hpp"
      43                 :            : 
      44                 :            : #define EXPAND_PROTOCOL "vnd.sun.star.expand"
      45                 :            : #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
      46                 :            : #define ARLEN(x) sizeof (x) / sizeof *(x)
      47                 :            : 
      48                 :            : 
      49                 :            : using namespace ::com::sun::star;
      50                 :            : using ::rtl::OUString;
      51                 :            : 
      52                 :            : namespace
      53                 :            : {
      54                 :            : 
      55                 :        248 : struct MutexHolder
      56                 :            : {
      57                 :            :     mutable ::osl::Mutex m_mutex;
      58                 :            : };
      59                 :            : 
      60                 :            : typedef ::cppu::WeakComponentImplHelper2<
      61                 :            :     lang::XServiceInfo, ucb::XContentProvider > t_impl_helper;
      62                 :            : 
      63                 :            : //==============================================================================
      64                 :            : class ExpandContentProviderImpl : protected MutexHolder, public t_impl_helper
      65                 :            : {
      66                 :            :     uno::Reference< util::XMacroExpander > m_xMacroExpander;
      67                 :            :     OUString expandUri(
      68                 :            :         uno::Reference< ucb::XContentIdentifier > const & xIdentifier ) const;
      69                 :            : 
      70                 :            : protected:
      71                 :            :     inline void check() const;
      72                 :            :     virtual void SAL_CALL disposing();
      73                 :            : 
      74                 :            : public:
      75                 :        124 :     inline ExpandContentProviderImpl(
      76                 :            :         uno::Reference< uno::XComponentContext > const & xComponentContext )
      77                 :            :         : t_impl_helper( m_mutex ),
      78                 :            :           m_xMacroExpander(
      79         [ +  - ]:        124 :               xComponentContext->getValueByName(
      80                 :        124 :                   OUSTR("/singletons/com.sun.star.util.theMacroExpander") ),
      81 [ +  - ][ +  - ]:        248 :               uno::UNO_QUERY_THROW )
                 [ +  - ]
      82                 :        124 :         {}
      83                 :            :     virtual ~ExpandContentProviderImpl() throw ();
      84                 :            : 
      85                 :            :     // XServiceInfo
      86                 :            :     virtual OUString SAL_CALL getImplementationName()
      87                 :            :         throw (uno::RuntimeException);
      88                 :            :     virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName )
      89                 :            :         throw (uno::RuntimeException);
      90                 :            :     virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames()
      91                 :            :         throw (uno::RuntimeException);
      92                 :            : 
      93                 :            :     // XContentProvider
      94                 :            :     virtual uno::Reference< ucb::XContent > SAL_CALL queryContent(
      95                 :            :         uno::Reference< ucb::XContentIdentifier > const & xIdentifier )
      96                 :            :         throw (ucb::IllegalIdentifierException, uno::RuntimeException);
      97                 :            :     virtual sal_Int32 SAL_CALL compareContentIds(
      98                 :            :         uno::Reference< ucb::XContentIdentifier > const & xId1,
      99                 :            :         uno::Reference< ucb::XContentIdentifier > const & xId2 )
     100                 :            :         throw (uno::RuntimeException);
     101                 :            : };
     102                 :            : 
     103                 :            : //______________________________________________________________________________
     104                 :      24891 : inline void ExpandContentProviderImpl::check() const
     105                 :            : {
     106                 :            :     // xxx todo guard?
     107                 :            : //     MutexGuard guard( m_mutex );
     108 [ +  - ][ -  + ]:      24891 :     if (rBHelper.bInDispose || rBHelper.bDisposed)
     109                 :            :     {
     110                 :            :         throw lang::DisposedException(
     111                 :            :             OUSTR("expand content provider instance has "
     112                 :            :                   "already been disposed!"),
     113                 :            :             static_cast< OWeakObject * >(
     114 [ #  # ][ #  # ]:          0 :                 const_cast< ExpandContentProviderImpl * >(this) ) );
                 [ #  # ]
     115                 :            :     }
     116                 :      24891 : }
     117                 :            : 
     118                 :            : //______________________________________________________________________________
     119         [ +  - ]:        124 : ExpandContentProviderImpl::~ExpandContentProviderImpl() throw ()
     120                 :            : {
     121         [ -  + ]:        248 : }
     122                 :            : 
     123                 :            : //______________________________________________________________________________
     124                 :        124 : void ExpandContentProviderImpl::disposing()
     125                 :            : {
     126                 :        124 : }
     127                 :            : 
     128                 :            : //==============================================================================
     129                 :        124 : static uno::Reference< uno::XInterface > SAL_CALL create(
     130                 :            :     uno::Reference< uno::XComponentContext > const & xComponentContext )
     131                 :            :     SAL_THROW( (uno::Exception) )
     132                 :            : {
     133                 :            :     return static_cast< ::cppu::OWeakObject * >(
     134         [ +  - ]:        124 :         new ExpandContentProviderImpl( xComponentContext ) );
     135                 :            : }
     136                 :            : 
     137                 :            : //==============================================================================
     138                 :        124 : static OUString SAL_CALL implName()
     139                 :            : {
     140                 :        124 :     return OUSTR("com.sun.star.comp.ucb.ExpandContentProvider");
     141                 :            : }
     142                 :            : 
     143                 :            : //==============================================================================
     144                 :        124 : static uno::Sequence< OUString > SAL_CALL supportedServices()
     145                 :            : {
     146                 :            :     OUString names [] = {
     147                 :            :         OUSTR("com.sun.star.ucb.ExpandContentProvider"),
     148                 :            :         OUSTR("com.sun.star.ucb.ContentProvider")
     149 [ +  - ][ +  - ]:        372 :     };
           [ #  #  #  # ]
     150 [ +  - ][ +  + ]:        372 :     return uno::Sequence< OUString >( names, ARLEN(names) );
                 [ #  # ]
     151                 :            : }
     152                 :            : 
     153                 :            : // XServiceInfo
     154                 :            : //______________________________________________________________________________
     155                 :          0 : OUString ExpandContentProviderImpl::getImplementationName()
     156                 :            :     throw (uno::RuntimeException)
     157                 :            : {
     158                 :          0 :     check();
     159                 :          0 :     return implName();
     160                 :            : }
     161                 :            : 
     162                 :            : //______________________________________________________________________________
     163                 :          0 : uno::Sequence< OUString > ExpandContentProviderImpl::getSupportedServiceNames()
     164                 :            :     throw (uno::RuntimeException)
     165                 :            : {
     166                 :          0 :     check();
     167                 :          0 :     return supportedServices();
     168                 :            : }
     169                 :            : 
     170                 :            : //______________________________________________________________________________
     171                 :          0 : sal_Bool ExpandContentProviderImpl::supportsService(
     172                 :            :     OUString const & serviceName )
     173                 :            :     throw (uno::RuntimeException)
     174                 :            : {
     175                 :            : //     check();
     176         [ #  # ]:          0 :     uno::Sequence< OUString > supported_services( getSupportedServiceNames() );
     177                 :          0 :     OUString const * ar = supported_services.getConstArray();
     178         [ #  # ]:          0 :     for ( sal_Int32 pos = supported_services.getLength(); pos--; )
     179                 :            :     {
     180         [ #  # ]:          0 :         if (ar[ pos ].equals( serviceName ))
     181                 :          0 :             return true;
     182                 :            :     }
     183         [ #  # ]:          0 :     return false;
     184                 :            : }
     185                 :            : 
     186                 :            : //______________________________________________________________________________
     187                 :      24891 : OUString ExpandContentProviderImpl::expandUri(
     188                 :            :     uno::Reference< ucb::XContentIdentifier > const & xIdentifier ) const
     189                 :            : {
     190 [ +  - ][ +  - ]:      24891 :     OUString uri( xIdentifier->getContentIdentifier() );
     191         [ -  + ]:      24891 :     if (uri.compareToAscii(
     192                 :      24891 :             RTL_CONSTASCII_STRINGPARAM(EXPAND_PROTOCOL ":") ) != 0)
     193                 :            :     {
     194                 :            :         throw ucb::IllegalIdentifierException(
     195                 :            :             OUSTR("expected protocol " EXPAND_PROTOCOL "!"),
     196                 :            :             static_cast< OWeakObject * >(
     197 [ #  # ][ #  # ]:          0 :                 const_cast< ExpandContentProviderImpl * >(this) ) );
                 [ #  # ]
     198                 :            :     }
     199                 :            :     // cut protocol
     200                 :      24891 :     OUString str( uri.copy( sizeof (EXPAND_PROTOCOL ":") -1 ) );
     201                 :            :     // decode uric class chars
     202                 :            :     str = ::rtl::Uri::decode(
     203                 :      24891 :         str, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
     204                 :            :     // expand macro string
     205 [ +  - ][ +  - ]:      24891 :     return m_xMacroExpander->expandMacros( str );
     206                 :            : }
     207                 :            : 
     208                 :            : // XContentProvider
     209                 :            : //______________________________________________________________________________
     210                 :      24891 : uno::Reference< ucb::XContent > ExpandContentProviderImpl::queryContent(
     211                 :            :     uno::Reference< ucb::XContentIdentifier > const & xIdentifier )
     212                 :            :     throw (ucb::IllegalIdentifierException, uno::RuntimeException)
     213                 :            : {
     214         [ +  - ]:      24891 :     check();
     215         [ +  - ]:      24891 :     OUString uri( expandUri( xIdentifier ) );
     216                 :            : 
     217         [ +  - ]:      24891 :     ::ucbhelper::Content ucb_content;
     218         [ +  - ]:      24891 :     if (::ucbhelper::Content::create(
     219         [ +  - ]:      24891 :             uri, uno::Reference< ucb::XCommandEnvironment >(), ucb_content ))
     220                 :            :     {
     221         [ +  - ]:      24891 :         return ucb_content.get();
     222                 :            :     }
     223                 :            :     else
     224                 :            :     {
     225                 :          0 :         return uno::Reference< ucb::XContent >();
     226         [ +  - ]:      24891 :     }
     227                 :            : }
     228                 :            : 
     229                 :            : //______________________________________________________________________________
     230                 :          0 : sal_Int32 ExpandContentProviderImpl::compareContentIds(
     231                 :            :     uno::Reference< ucb::XContentIdentifier > const & xId1,
     232                 :            :     uno::Reference< ucb::XContentIdentifier > const & xId2 )
     233                 :            :     throw (uno::RuntimeException)
     234                 :            : {
     235                 :          0 :     check();
     236                 :            :     try
     237                 :            :     {
     238         [ #  # ]:          0 :         OUString uri1( expandUri( xId1 ) );
     239         [ #  # ]:          0 :         OUString uri2( expandUri( xId2 ) );
     240         [ #  # ]:          0 :         return uri1.compareTo( uri2 );
     241                 :            :     }
     242                 :          0 :     catch (const ucb::IllegalIdentifierException & exc)
     243                 :            :     {
     244                 :            :         (void) exc; // unused
     245                 :            :         OSL_FAIL(
     246                 :            :             ::rtl::OUStringToOString(
     247                 :            :                 exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
     248                 :          0 :         return -1;
     249                 :            :     }
     250                 :            : }
     251                 :            : 
     252                 :            : static const ::cppu::ImplementationEntry s_entries [] =
     253                 :            : {
     254                 :            :     {
     255                 :            :         create,
     256                 :            :         implName,
     257                 :            :         supportedServices,
     258                 :            :         ::cppu::createSingleComponentFactory,
     259                 :            :         0, 0
     260                 :            :     },
     261                 :            :     { 0, 0, 0, 0, 0, 0 }
     262                 :            : };
     263                 :            : 
     264                 :            : }
     265                 :            : 
     266                 :            : extern "C"
     267                 :            : {
     268                 :            : 
     269                 :        124 : SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
     270                 :            :     const sal_Char * pImplName,
     271                 :            :     lang::XMultiServiceFactory * pServiceManager,
     272                 :            :     registry::XRegistryKey * pRegistryKey )
     273                 :            : {
     274                 :            :     return ::cppu::component_getFactoryHelper(
     275                 :        124 :         pImplName, pServiceManager, pRegistryKey, s_entries );
     276                 :            : }
     277                 :            : 
     278                 :            : }
     279                 :            : 
     280                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10