LCOV - code coverage report
Current view: top level - sc/source/filter/oox - worksheetbuffer.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 60 81 74.1 %
Date: 2012-08-25 Functions: 13 17 76.5 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 60 154 39.0 %

           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 "worksheetbuffer.hxx"
      30                 :            : 
      31                 :            : #include <com/sun/star/container/XIndexAccess.hpp>
      32                 :            : #include <com/sun/star/container/XNameAccess.hpp>
      33                 :            : #include <com/sun/star/container/XNamed.hpp>
      34                 :            : #include <com/sun/star/sheet/XExternalSheetName.hpp>
      35                 :            : #include <com/sun/star/sheet/XSheetLinkable.hpp>
      36                 :            : #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
      37                 :            : #include <rtl/ustrbuf.hxx>
      38                 :            : #include "oox/core/filterbase.hxx"
      39                 :            : #include "oox/helper/attributelist.hxx"
      40                 :            : #include "oox/helper/containerhelper.hxx"
      41                 :            : #include "oox/helper/propertyset.hxx"
      42                 :            : #include "oox/token/properties.hxx"
      43                 :            : #include "biffinputstream.hxx"
      44                 :            : #include "excelhandlers.hxx"
      45                 :            : 
      46                 :            : namespace oox {
      47                 :            : namespace xls {
      48                 :            : 
      49                 :            : // ============================================================================
      50                 :            : 
      51                 :            : using namespace ::com::sun::star::container;
      52                 :            : using namespace ::com::sun::star::sheet;
      53                 :            : using namespace ::com::sun::star::uno;
      54                 :            : 
      55                 :            : using ::rtl::OUString;
      56                 :            : using ::rtl::OUStringBuffer;
      57                 :            : 
      58                 :            : // ============================================================================
      59                 :            : 
      60                 :         60 : SheetInfoModel::SheetInfoModel() :
      61                 :            :     mnBiffHandle( -1 ),
      62                 :            :     mnSheetId( -1 ),
      63                 :         60 :     mnState( XML_visible )
      64                 :            : {
      65                 :         60 : }
      66                 :            : 
      67                 :            : // ============================================================================
      68                 :            : 
      69                 :         24 : WorksheetBuffer::WorksheetBuffer( const WorkbookHelper& rHelper ) :
      70 [ +  - ][ +  - ]:         24 :     WorkbookHelper( rHelper )
      71                 :            : {
      72                 :         24 : }
      73                 :            : 
      74                 :         60 : void WorksheetBuffer::importSheet( const AttributeList& rAttribs )
      75                 :            : {
      76                 :         60 :     SheetInfoModel aModel;
      77         [ +  - ]:         60 :     aModel.maRelId = rAttribs.getString( R_TOKEN( id ), OUString() );
      78         [ +  - ]:         60 :     aModel.maName = rAttribs.getXString( XML_name, OUString() );
      79         [ +  - ]:         60 :     aModel.mnSheetId = rAttribs.getInteger( XML_sheetId, -1 );
      80         [ +  - ]:         60 :     aModel.mnState = rAttribs.getToken( XML_state, XML_visible );
      81         [ +  - ]:         60 :     insertSheet( aModel );
      82                 :         60 : }
      83                 :            : 
      84                 :          0 : void WorksheetBuffer::importSheet( SequenceInputStream& rStrm )
      85                 :            : {
      86                 :            :     sal_Int32 nState;
      87                 :          0 :     SheetInfoModel aModel;
      88 [ #  # ][ #  # ]:          0 :     rStrm >> nState >> aModel.mnSheetId >> aModel.maRelId >> aModel.maName;
         [ #  # ][ #  # ]
      89                 :            :     static const sal_Int32 spnStates[] = { XML_visible, XML_hidden, XML_veryHidden };
      90         [ #  # ]:          0 :     aModel.mnState = STATIC_ARRAY_SELECT( spnStates, nState, XML_visible );
      91         [ #  # ]:          0 :     insertSheet( aModel );
      92                 :          0 : }
      93                 :            : 
      94                 :          0 : sal_Int16 WorksheetBuffer::insertEmptySheet( const OUString& rPreferredName, bool bVisible )
      95                 :            : {
      96                 :          0 :     return createSheet( rPreferredName, SAL_MAX_INT32, bVisible ).first;
      97                 :            : }
      98                 :            : 
      99                 :         48 : sal_Int32 WorksheetBuffer::getWorksheetCount() const
     100                 :            : {
     101                 :         48 :     return static_cast< sal_Int32 >( maSheetInfos.size() );
     102                 :            : }
     103                 :            : 
     104                 :         60 : OUString WorksheetBuffer::getWorksheetRelId( sal_Int32 nWorksheet ) const
     105                 :            : {
     106                 :         60 :     const SheetInfo* pSheetInfo = maSheetInfos.get( nWorksheet ).get();
     107         [ +  - ]:         60 :     return pSheetInfo ? pSheetInfo->maRelId : OUString();
     108                 :            : }
     109                 :            : 
     110                 :        135 : sal_Int16 WorksheetBuffer::getCalcSheetIndex( sal_Int32 nWorksheet ) const
     111                 :            : {
     112                 :        135 :     const SheetInfo* pSheetInfo = maSheetInfos.get( nWorksheet ).get();
     113         [ +  - ]:        135 :     return pSheetInfo ? pSheetInfo->mnCalcSheet : -1;
     114                 :            : }
     115                 :            : 
     116                 :         84 : OUString WorksheetBuffer::getCalcSheetName( sal_Int32 nWorksheet ) const
     117                 :            : {
     118                 :         84 :     const SheetInfo* pSheetInfo = maSheetInfos.get( nWorksheet ).get();
     119         [ +  - ]:         84 :     return pSheetInfo ? pSheetInfo->maCalcName : OUString();
     120                 :            : }
     121                 :            : 
     122                 :          0 : sal_Int16 WorksheetBuffer::getCalcSheetIndex( const OUString& rWorksheetName ) const
     123                 :            : {
     124 [ #  # ][ #  # ]:          0 :     const SheetInfo* pSheetInfo = maSheetInfosByName.get( rWorksheetName ).get();
     125         [ #  # ]:          0 :     return pSheetInfo ? pSheetInfo->mnCalcSheet : -1;
     126                 :            : }
     127                 :            : 
     128                 :          0 : OUString WorksheetBuffer::getCalcSheetName( const OUString& rWorksheetName ) const
     129                 :            : {
     130 [ #  # ][ #  # ]:          0 :     if( const SheetInfo* pSheetInfo = maSheetInfosByName.get( rWorksheetName ).get() )
                 [ #  # ]
     131                 :            :     {
     132                 :          0 :         bool bIsQuoted = pSheetInfo->maName != rWorksheetName;
     133         [ #  # ]:          0 :         return bIsQuoted ? pSheetInfo->maCalcQuotedName : pSheetInfo->maCalcName;
     134                 :            :     }
     135                 :          0 :     return OUString();
     136                 :            : }
     137                 :            : 
     138                 :            : // private --------------------------------------------------------------------
     139                 :            : 
     140                 :            : namespace {
     141                 :            : 
     142                 :        120 : OUString lclQuoteName( const OUString& rName )
     143                 :            : {
     144         [ +  - ]:        120 :     OUStringBuffer aBuffer( rName );
     145                 :            :     // duplicate all quote characters
     146         [ +  + ]:        840 :     for( sal_Int32 nPos = aBuffer.getLength() - 1; nPos >= 0; --nPos )
     147         [ -  + ]:        720 :         if( aBuffer[nPos] == '\'' )
     148         [ #  # ]:          0 :             aBuffer.insert( nPos, sal_Unicode( '\'' ) );
     149                 :            :     // add outer quotes and return
     150 [ +  - ][ +  - ]:        120 :     return aBuffer.insert( 0, sal_Unicode( '\'' ) ).append( sal_Unicode( '\'' ) ).makeStringAndClear();
                 [ +  - ]
     151                 :            : }
     152                 :            : 
     153                 :            : } // namespace
     154                 :            : 
     155                 :         60 : WorksheetBuffer::SheetInfo::SheetInfo( const SheetInfoModel& rModel, sal_Int16 nCalcSheet, const OUString& rCalcName ) :
     156                 :            :     SheetInfoModel( rModel ),
     157                 :            :     maCalcName( rCalcName ),
     158                 :            :     maCalcQuotedName( lclQuoteName( rCalcName ) ),
     159         [ +  - ]:         60 :     mnCalcSheet( nCalcSheet )
     160                 :            : {
     161                 :         60 : }
     162                 :            : 
     163                 :         60 : WorksheetBuffer::IndexNamePair WorksheetBuffer::createSheet( const OUString& rPreferredName, sal_Int32 nSheetPos, bool bVisible )
     164                 :            : {
     165                 :            :     try
     166                 :            :     {
     167 [ +  - ][ +  - ]:         60 :         Reference< XSpreadsheets > xSheets( getDocument()->getSheets(), UNO_QUERY_THROW );
         [ +  - ][ +  - ]
     168         [ +  - ]:         60 :         Reference< XIndexAccess > xSheetsIA( xSheets, UNO_QUERY_THROW );
     169         [ +  - ]:         60 :         Reference< XNameAccess > xSheetsNA( xSheets, UNO_QUERY_THROW );
     170                 :         60 :         sal_Int16 nCalcSheet = -1;
     171 [ -  + ][ #  # ]:         60 :         OUString aSheetName = rPreferredName.isEmpty() ? CREATE_OUSTRING( "Sheet" ) : rPreferredName;
     172         [ +  - ]:         60 :         PropertySet aPropSet;
     173 [ +  - ][ +  - ]:         60 :         if( nSheetPos < xSheetsIA->getCount() )
                 [ +  + ]
     174                 :            :         {
     175                 :         24 :             nCalcSheet = static_cast< sal_Int16 >( nSheetPos );
     176                 :            :             // existing sheet - try to rename
     177 [ +  - ][ +  - ]:         24 :             Reference< XNamed > xSheetName( xSheetsIA->getByIndex( nSheetPos ), UNO_QUERY_THROW );
                 [ +  - ]
     178 [ +  - ][ -  + ]:         24 :             if( xSheetName->getName() != aSheetName )
                 [ +  - ]
     179                 :            :             {
     180         [ #  # ]:          0 :                 aSheetName = ContainerHelper::getUnusedName( xSheetsNA, aSheetName, ' ' );
     181 [ #  # ][ #  # ]:          0 :                 xSheetName->setName( aSheetName );
     182                 :            :             }
     183         [ +  - ]:         24 :             aPropSet.set( xSheetName );
     184                 :            :         }
     185                 :            :         else
     186                 :            :         {
     187 [ +  - ][ +  - ]:         36 :             nCalcSheet = static_cast< sal_Int16 >( xSheetsIA->getCount() );
     188                 :            :             // new sheet - insert with unused name
     189         [ +  - ]:         36 :             aSheetName = ContainerHelper::getUnusedName( xSheetsNA, aSheetName, ' ' );
     190 [ +  - ][ +  - ]:         36 :             xSheets->insertNewByName( aSheetName, nCalcSheet );
     191 [ +  - ][ +  - ]:         36 :             aPropSet.set( xSheetsIA->getByIndex( nCalcSheet ) );
                 [ +  - ]
     192                 :            :         }
     193                 :            : 
     194                 :            :         // sheet properties
     195         [ +  - ]:         60 :         aPropSet.setProperty( PROP_IsVisible, bVisible );
     196                 :            : 
     197                 :            :         // return final sheet index if sheet exists
     198 [ +  - ][ #  # ]:         60 :         return IndexNamePair( nCalcSheet, aSheetName );
     199                 :            :     }
     200                 :          0 :     catch( Exception& )
     201                 :            :     {
     202                 :            :         OSL_FAIL( "WorksheetBuffer::createSheet - cannot insert or rename worksheet" );
     203                 :            :     }
     204                 :          0 :     return IndexNamePair( -1, OUString() );
     205                 :            : }
     206                 :            : 
     207                 :         60 : void WorksheetBuffer::insertSheet( const SheetInfoModel& rModel )
     208                 :            : {
     209                 :         60 :     sal_Int32 nWorksheet = static_cast< sal_Int32 >( maSheetInfos.size() );
     210         [ +  - ]:         60 :     IndexNamePair aIndexName = createSheet( rModel.maName, nWorksheet, rModel.mnState == XML_visible );
     211 [ +  - ][ +  - ]:         60 :     ::boost::shared_ptr< SheetInfo > xSheetInfo( new SheetInfo( rModel, aIndexName.first, aIndexName.second ) );
                 [ +  - ]
     212         [ +  - ]:         60 :     maSheetInfos.push_back( xSheetInfo );
     213 [ +  - ][ +  - ]:         60 :     maSheetInfosByName[ rModel.maName ] = xSheetInfo;
     214 [ +  - ][ +  - ]:         60 :     maSheetInfosByName[ lclQuoteName( rModel.maName ) ] = xSheetInfo;
         [ +  - ][ +  - ]
     215                 :         60 : }
     216                 :            : 
     217                 :            : // ============================================================================
     218                 :            : 
     219                 :            : } // namespace xls
     220 [ +  - ][ +  - ]:         24 : } // namespace oox
     221                 :            : 
     222                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10