LCOV - code coverage report
Current view: top level - oox/source/core - relations.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 42 48 87.5 %
Date: 2012-08-25 Functions: 9 10 90.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 43 74 58.1 %

           Branch data     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 "oox/core/relations.hxx"
      21                 :            : 
      22                 :            : #include <rtl/ustrbuf.hxx>
      23                 :            : #include "oox/helper/helper.hxx"
      24                 :            : 
      25                 :            : namespace oox {
      26                 :            : namespace core {
      27                 :            : 
      28                 :            : // ============================================================================
      29                 :            : 
      30                 :            : using ::rtl::OUString;
      31                 :            : using ::rtl::OUStringBuffer;
      32                 :            : 
      33                 :            : // ============================================================================
      34                 :            : 
      35                 :            : namespace {
      36                 :            : 
      37                 :        455 : OUString lclRemoveFileName( const OUString& rPath )
      38                 :            : {
      39         [ +  - ]:        455 :     return rPath.copy( 0, ::std::max< sal_Int32 >( rPath.lastIndexOf( '/' ), 0 ) );
      40                 :            : }
      41                 :            : 
      42                 :        645 : OUString lclAppendFileName( const OUString& rPath, const OUString& rFileName )
      43                 :            : {
      44                 :        645 :     return rPath.isEmpty() ? rFileName :
      45 [ +  - ][ +  - ]:        645 :         OUStringBuffer( rPath ).append( sal_Unicode( '/' ) ).append( rFileName ).makeStringAndClear();
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
           [ #  #  #  # ]
                 [ -  + ]
      46                 :            : }
      47                 :            : 
      48                 :            : } // namespace
      49                 :            : 
      50                 :            : // ============================================================================
      51                 :            : 
      52                 :        755 : Relations::Relations( const OUString& rFragmentPath ) :
      53                 :        755 :     maFragmentPath( rFragmentPath )
      54                 :            : {
      55                 :        755 : }
      56                 :            : 
      57                 :        280 : const Relation* Relations::getRelationFromRelId( const OUString& rId ) const
      58                 :            : {
      59         [ +  - ]:        280 :     const_iterator aIt = find( rId );
      60         [ +  + ]:        280 :     return (aIt == end()) ? 0 : &aIt->second;
      61                 :            : }
      62                 :            : 
      63                 :        495 : const Relation* Relations::getRelationFromFirstType( const OUString& rType ) const
      64                 :            : {
      65         [ +  + ]:       1625 :     for( const_iterator aIt = begin(), aEnd = end(); aIt != aEnd; ++aIt )
      66         [ +  + ]:       1340 :         if( aIt->second.maType.equalsIgnoreAsciiCase( rType ) )
      67                 :        210 :             return &aIt->second;
      68                 :        495 :     return 0;
      69                 :            : }
      70                 :            : 
      71                 :        300 : RelationsRef Relations::getRelationsFromType( const OUString& rType ) const
      72                 :            : {
      73 [ +  - ][ +  - ]:        300 :     RelationsRef xRelations( new Relations( maFragmentPath ) );
      74         [ +  + ]:        405 :     for( const_iterator aIt = begin(), aEnd = end(); aIt != aEnd; ++aIt )
      75         [ -  + ]:        105 :         if( aIt->second.maType.equalsIgnoreAsciiCase( rType ) )
      76         [ #  # ]:          0 :             (*xRelations)[ aIt->first ] = aIt->second;
      77                 :        300 :     return xRelations;
      78                 :            : }
      79                 :            : 
      80                 :          0 : OUString Relations::getExternalTargetFromRelId( const OUString& rRelId ) const
      81                 :            : {
      82                 :          0 :     const Relation* pRelation = getRelationFromRelId( rRelId );
      83 [ #  # ][ #  # ]:          0 :     return (pRelation && pRelation->mbExternal) ? pRelation->maTarget : OUString();
      84                 :            : }
      85                 :            : 
      86                 :        420 : OUString Relations::getFragmentPathFromRelation( const Relation& rRelation ) const
      87                 :            : {
      88                 :            :     // no target, no fragment path
      89 [ +  - ][ -  + ]:        420 :     if( rRelation.mbExternal || rRelation.maTarget.isEmpty() )
                 [ -  + ]
      90                 :          0 :         return OUString();
      91                 :            : 
      92                 :            :     // absolute target: return it without leading slash (#i100978)
      93         [ -  + ]:        420 :     if( rRelation.maTarget[ 0 ] == '/' )
      94                 :          0 :         return rRelation.maTarget.copy( 1 );
      95                 :            : 
      96                 :            :     // empty fragment path: return target
      97         [ +  + ]:        420 :     if( maFragmentPath.isEmpty() )
      98                 :         55 :         return rRelation.maTarget;
      99                 :            : 
     100                 :            :     // resolve relative target path according to base path
     101         [ +  - ]:        365 :     OUString aPath = lclRemoveFileName( maFragmentPath );
     102                 :        365 :     sal_Int32 nStartPos = 0;
     103         [ +  + ]:       1100 :     while( nStartPos < rRelation.maTarget.getLength() )
     104                 :            :     {
     105                 :        735 :         sal_Int32 nSepPos = rRelation.maTarget.indexOf( '/', nStartPos );
     106         [ +  + ]:        735 :         if( nSepPos < 0 ) nSepPos = rRelation.maTarget.getLength();
     107                 :            :         // append next directory name from aTarget to aPath, or remove last directory on '../'
     108 [ +  + ][ +  - ]:        735 :         if( (nStartPos + 2 == nSepPos) && (rRelation.maTarget[ nStartPos ] == '.') && (rRelation.maTarget[ nStartPos + 1 ] == '.') )
         [ +  - ][ +  + ]
     109         [ +  - ]:         90 :             aPath = lclRemoveFileName( aPath );
     110                 :            :         else
     111         [ +  - ]:        645 :             aPath = lclAppendFileName( aPath, rRelation.maTarget.copy( nStartPos, nSepPos - nStartPos ) );
     112                 :            :         // move nStartPos to next directory name
     113                 :        735 :         nStartPos = nSepPos + 1;
     114                 :            :     }
     115                 :            : 
     116                 :        420 :     return aPath;
     117                 :            : }
     118                 :            : 
     119                 :        180 : OUString Relations::getFragmentPathFromRelId( const OUString& rRelId ) const
     120                 :            : {
     121                 :        180 :     const Relation* pRelation = getRelationFromRelId( rRelId );
     122         [ +  + ]:        180 :     return pRelation ? getFragmentPathFromRelation( *pRelation ) : OUString();
     123                 :            : }
     124                 :            : 
     125                 :        495 : OUString Relations::getFragmentPathFromFirstType( const OUString& rType ) const
     126                 :            : {
     127                 :        495 :     const Relation* pRelation = getRelationFromFirstType( rType );
     128         [ +  + ]:        495 :     return pRelation ? getFragmentPathFromRelation( *pRelation ) : OUString();
     129                 :            : }
     130                 :            : 
     131                 :            : // ============================================================================
     132                 :            : 
     133                 :            : } // namespace core
     134                 :            : } // namespace oox
     135                 :            : 
     136                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10