1*ca5ec200SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*ca5ec200SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*ca5ec200SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*ca5ec200SAndrew Rist * distributed with this work for additional information 6*ca5ec200SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*ca5ec200SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*ca5ec200SAndrew Rist * "License"); you may not use this file except in compliance 9*ca5ec200SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*ca5ec200SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*ca5ec200SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*ca5ec200SAndrew Rist * software distributed under the License is distributed on an 15*ca5ec200SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*ca5ec200SAndrew Rist * KIND, either express or implied. See the License for the 17*ca5ec200SAndrew Rist * specific language governing permissions and limitations 18*ca5ec200SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*ca5ec200SAndrew Rist *************************************************************/ 21*ca5ec200SAndrew Rist 22*ca5ec200SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #include "oox/xls/worksheetbuffer.hxx" 25cdf0e10cSrcweir 26cdf0e10cSrcweir #include <com/sun/star/container/XIndexAccess.hpp> 27cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp> 28cdf0e10cSrcweir #include <com/sun/star/container/XNamed.hpp> 29cdf0e10cSrcweir #include <com/sun/star/sheet/XExternalSheetName.hpp> 30cdf0e10cSrcweir #include <com/sun/star/sheet/XSheetLinkable.hpp> 31cdf0e10cSrcweir #include <com/sun/star/sheet/XSpreadsheetDocument.hpp> 32cdf0e10cSrcweir #include <rtl/ustrbuf.hxx> 33cdf0e10cSrcweir #include "oox/core/filterbase.hxx" 34cdf0e10cSrcweir #include "oox/helper/attributelist.hxx" 35cdf0e10cSrcweir #include "oox/helper/containerhelper.hxx" 36cdf0e10cSrcweir #include "oox/helper/propertyset.hxx" 37cdf0e10cSrcweir #include "oox/xls/biffinputstream.hxx" 38cdf0e10cSrcweir #include "oox/xls/excelhandlers.hxx" 39cdf0e10cSrcweir 40cdf0e10cSrcweir namespace oox { 41cdf0e10cSrcweir namespace xls { 42cdf0e10cSrcweir 43cdf0e10cSrcweir // ============================================================================ 44cdf0e10cSrcweir 45cdf0e10cSrcweir using namespace ::com::sun::star::container; 46cdf0e10cSrcweir using namespace ::com::sun::star::sheet; 47cdf0e10cSrcweir using namespace ::com::sun::star::uno; 48cdf0e10cSrcweir 49cdf0e10cSrcweir using ::rtl::OUString; 50cdf0e10cSrcweir using ::rtl::OUStringBuffer; 51cdf0e10cSrcweir 52cdf0e10cSrcweir // ============================================================================ 53cdf0e10cSrcweir 54cdf0e10cSrcweir SheetInfoModel::SheetInfoModel() : 55cdf0e10cSrcweir mnBiffHandle( -1 ), 56cdf0e10cSrcweir mnSheetId( -1 ), 57cdf0e10cSrcweir mnState( XML_visible ) 58cdf0e10cSrcweir { 59cdf0e10cSrcweir } 60cdf0e10cSrcweir 61cdf0e10cSrcweir // ============================================================================ 62cdf0e10cSrcweir 63cdf0e10cSrcweir WorksheetBuffer::WorksheetBuffer( const WorkbookHelper& rHelper ) : 64cdf0e10cSrcweir WorkbookHelper( rHelper ) 65cdf0e10cSrcweir { 66cdf0e10cSrcweir } 67cdf0e10cSrcweir 68cdf0e10cSrcweir /*static*/ OUString WorksheetBuffer::getBaseFileName( const OUString& rUrl ) 69cdf0e10cSrcweir { 70cdf0e10cSrcweir sal_Int32 nFileNamePos = ::std::max< sal_Int32 >( rUrl.lastIndexOf( '/' ) + 1, 0 ); 71cdf0e10cSrcweir sal_Int32 nExtPos = rUrl.lastIndexOf( '.' ); 72cdf0e10cSrcweir if( nExtPos <= nFileNamePos ) nExtPos = rUrl.getLength(); 73cdf0e10cSrcweir return rUrl.copy( nFileNamePos, nExtPos - nFileNamePos ); 74cdf0e10cSrcweir } 75cdf0e10cSrcweir 76cdf0e10cSrcweir void WorksheetBuffer::initializeSingleSheet() 77cdf0e10cSrcweir { 78cdf0e10cSrcweir OSL_ENSURE( maSheetInfos.empty(), "WorksheetBuffer::initializeSingleSheet - invalid call" ); 79cdf0e10cSrcweir SheetInfoModel aModel; 80cdf0e10cSrcweir aModel.maName = getBaseFileName( getBaseFilter().getFileUrl() ); 81cdf0e10cSrcweir insertSheet( aModel ); 82cdf0e10cSrcweir } 83cdf0e10cSrcweir 84cdf0e10cSrcweir void WorksheetBuffer::importSheet( const AttributeList& rAttribs ) 85cdf0e10cSrcweir { 86cdf0e10cSrcweir SheetInfoModel aModel; 87cdf0e10cSrcweir aModel.maRelId = rAttribs.getString( R_TOKEN( id ), OUString() ); 88cdf0e10cSrcweir aModel.maName = rAttribs.getXString( XML_name, OUString() ); 89cdf0e10cSrcweir aModel.mnSheetId = rAttribs.getInteger( XML_sheetId, -1 ); 90cdf0e10cSrcweir aModel.mnState = rAttribs.getToken( XML_state, XML_visible ); 91cdf0e10cSrcweir insertSheet( aModel ); 92cdf0e10cSrcweir } 93cdf0e10cSrcweir 94cdf0e10cSrcweir void WorksheetBuffer::importSheet( SequenceInputStream& rStrm ) 95cdf0e10cSrcweir { 96cdf0e10cSrcweir sal_Int32 nState; 97cdf0e10cSrcweir SheetInfoModel aModel; 98cdf0e10cSrcweir rStrm >> nState >> aModel.mnSheetId >> aModel.maRelId >> aModel.maName; 99cdf0e10cSrcweir static const sal_Int32 spnStates[] = { XML_visible, XML_hidden, XML_veryHidden }; 100cdf0e10cSrcweir aModel.mnState = STATIC_ARRAY_SELECT( spnStates, nState, XML_visible ); 101cdf0e10cSrcweir insertSheet( aModel ); 102cdf0e10cSrcweir } 103cdf0e10cSrcweir 104cdf0e10cSrcweir void WorksheetBuffer::importSheet( BiffInputStream& rStrm ) 105cdf0e10cSrcweir { 106cdf0e10cSrcweir SheetInfoModel aModel; 107cdf0e10cSrcweir if( getBiff() >= BIFF5 ) 108cdf0e10cSrcweir { 109cdf0e10cSrcweir rStrm.enableDecoder( false ); 110cdf0e10cSrcweir aModel.mnBiffHandle = rStrm.readuInt32(); 111cdf0e10cSrcweir rStrm.enableDecoder( true ); 112cdf0e10cSrcweir sal_uInt16 nState = rStrm.readuInt16(); 113cdf0e10cSrcweir static const sal_Int32 spnStates[] = { XML_visible, XML_hidden, XML_veryHidden }; 114cdf0e10cSrcweir aModel.mnState = STATIC_ARRAY_SELECT( spnStates, nState, XML_visible ); 115cdf0e10cSrcweir } 116cdf0e10cSrcweir aModel.maName = (getBiff() == BIFF8) ? 117cdf0e10cSrcweir rStrm.readUniStringBody( rStrm.readuInt8() ) : 118cdf0e10cSrcweir rStrm.readByteStringUC( false, getTextEncoding() ); 119cdf0e10cSrcweir insertSheet( aModel ); 120cdf0e10cSrcweir } 121cdf0e10cSrcweir 122cdf0e10cSrcweir sal_Int16 WorksheetBuffer::insertEmptySheet( const OUString& rPreferredName, bool bVisible ) 123cdf0e10cSrcweir { 124cdf0e10cSrcweir return createSheet( rPreferredName, SAL_MAX_INT32, bVisible ).first; 125cdf0e10cSrcweir } 126cdf0e10cSrcweir 127cdf0e10cSrcweir sal_Int32 WorksheetBuffer::getWorksheetCount() const 128cdf0e10cSrcweir { 129cdf0e10cSrcweir return static_cast< sal_Int32 >( maSheetInfos.size() ); 130cdf0e10cSrcweir } 131cdf0e10cSrcweir 132cdf0e10cSrcweir OUString WorksheetBuffer::getWorksheetRelId( sal_Int32 nWorksheet ) const 133cdf0e10cSrcweir { 134cdf0e10cSrcweir const SheetInfo* pSheetInfo = maSheetInfos.get( nWorksheet ).get(); 135cdf0e10cSrcweir return pSheetInfo ? pSheetInfo->maRelId : OUString(); 136cdf0e10cSrcweir } 137cdf0e10cSrcweir 138cdf0e10cSrcweir sal_Int64 WorksheetBuffer::getBiffRecordHandle( sal_Int32 nWorksheet ) const 139cdf0e10cSrcweir { 140cdf0e10cSrcweir const SheetInfo* pSheetInfo = maSheetInfos.get( nWorksheet ).get(); 141cdf0e10cSrcweir return pSheetInfo ? pSheetInfo->mnBiffHandle : -1; 142cdf0e10cSrcweir } 143cdf0e10cSrcweir 144cdf0e10cSrcweir sal_Int16 WorksheetBuffer::getCalcSheetIndex( sal_Int32 nWorksheet ) const 145cdf0e10cSrcweir { 146cdf0e10cSrcweir const SheetInfo* pSheetInfo = maSheetInfos.get( nWorksheet ).get(); 147cdf0e10cSrcweir return pSheetInfo ? pSheetInfo->mnCalcSheet : -1; 148cdf0e10cSrcweir } 149cdf0e10cSrcweir 150cdf0e10cSrcweir OUString WorksheetBuffer::getCalcSheetName( sal_Int32 nWorksheet ) const 151cdf0e10cSrcweir { 152cdf0e10cSrcweir const SheetInfo* pSheetInfo = maSheetInfos.get( nWorksheet ).get(); 153cdf0e10cSrcweir return pSheetInfo ? pSheetInfo->maCalcName : OUString(); 154cdf0e10cSrcweir } 155cdf0e10cSrcweir 156cdf0e10cSrcweir sal_Int16 WorksheetBuffer::getCalcSheetIndex( const OUString& rWorksheetName ) const 157cdf0e10cSrcweir { 158cdf0e10cSrcweir const SheetInfo* pSheetInfo = maSheetInfosByName.get( rWorksheetName ).get(); 159cdf0e10cSrcweir return pSheetInfo ? pSheetInfo->mnCalcSheet : -1; 160cdf0e10cSrcweir } 161cdf0e10cSrcweir 162cdf0e10cSrcweir OUString WorksheetBuffer::getCalcSheetName( const OUString& rWorksheetName ) const 163cdf0e10cSrcweir { 164cdf0e10cSrcweir if( const SheetInfo* pSheetInfo = maSheetInfosByName.get( rWorksheetName ).get() ) 165cdf0e10cSrcweir { 166cdf0e10cSrcweir bool bIsQuoted = pSheetInfo->maName != rWorksheetName; 167cdf0e10cSrcweir return bIsQuoted ? pSheetInfo->maCalcQuotedName : pSheetInfo->maCalcName; 168cdf0e10cSrcweir } 169cdf0e10cSrcweir return OUString(); 170cdf0e10cSrcweir } 171cdf0e10cSrcweir 172cdf0e10cSrcweir // private -------------------------------------------------------------------- 173cdf0e10cSrcweir 174cdf0e10cSrcweir namespace { 175cdf0e10cSrcweir 176cdf0e10cSrcweir OUString lclQuoteName( const OUString& rName ) 177cdf0e10cSrcweir { 178cdf0e10cSrcweir OUStringBuffer aBuffer( rName ); 179cdf0e10cSrcweir // duplicate all quote characters 180cdf0e10cSrcweir for( sal_Int32 nPos = aBuffer.getLength() - 1; nPos >= 0; --nPos ) 181cdf0e10cSrcweir if( aBuffer.charAt( nPos ) == '\'' ) 182cdf0e10cSrcweir aBuffer.insert( nPos, sal_Unicode( '\'' ) ); 183cdf0e10cSrcweir // add outer quotes and return 184cdf0e10cSrcweir return aBuffer.insert( 0, sal_Unicode( '\'' ) ).append( sal_Unicode( '\'' ) ).makeStringAndClear(); 185cdf0e10cSrcweir } 186cdf0e10cSrcweir 187cdf0e10cSrcweir } // namespace 188cdf0e10cSrcweir 189cdf0e10cSrcweir WorksheetBuffer::SheetInfo::SheetInfo( const SheetInfoModel& rModel, sal_Int16 nCalcSheet, const OUString& rCalcName ) : 190cdf0e10cSrcweir SheetInfoModel( rModel ), 191cdf0e10cSrcweir maCalcName( rCalcName ), 192cdf0e10cSrcweir maCalcQuotedName( lclQuoteName( rCalcName ) ), 193cdf0e10cSrcweir mnCalcSheet( nCalcSheet ) 194cdf0e10cSrcweir { 195cdf0e10cSrcweir } 196cdf0e10cSrcweir 197cdf0e10cSrcweir WorksheetBuffer::IndexNamePair WorksheetBuffer::createSheet( const OUString& rPreferredName, sal_Int32 nSheetPos, bool bVisible ) 198cdf0e10cSrcweir { 199cdf0e10cSrcweir try 200cdf0e10cSrcweir { 201cdf0e10cSrcweir Reference< XSpreadsheets > xSheets( getDocument()->getSheets(), UNO_QUERY_THROW ); 202cdf0e10cSrcweir Reference< XIndexAccess > xSheetsIA( xSheets, UNO_QUERY_THROW ); 203cdf0e10cSrcweir Reference< XNameAccess > xSheetsNA( xSheets, UNO_QUERY_THROW ); 204cdf0e10cSrcweir sal_Int16 nCalcSheet = -1; 205cdf0e10cSrcweir OUString aSheetName = (rPreferredName.getLength() == 0) ? CREATE_OUSTRING( "Sheet" ) : rPreferredName; 206cdf0e10cSrcweir PropertySet aPropSet; 207cdf0e10cSrcweir if( nSheetPos < xSheetsIA->getCount() ) 208cdf0e10cSrcweir { 209cdf0e10cSrcweir nCalcSheet = static_cast< sal_Int16 >( nSheetPos ); 210cdf0e10cSrcweir // existing sheet - try to rename 211cdf0e10cSrcweir Reference< XNamed > xSheetName( xSheetsIA->getByIndex( nSheetPos ), UNO_QUERY_THROW ); 212cdf0e10cSrcweir if( xSheetName->getName() != aSheetName ) 213cdf0e10cSrcweir { 214cdf0e10cSrcweir aSheetName = ContainerHelper::getUnusedName( xSheetsNA, aSheetName, ' ' ); 215cdf0e10cSrcweir xSheetName->setName( aSheetName ); 216cdf0e10cSrcweir } 217cdf0e10cSrcweir aPropSet.set( xSheetName ); 218cdf0e10cSrcweir } 219cdf0e10cSrcweir else 220cdf0e10cSrcweir { 221cdf0e10cSrcweir nCalcSheet = static_cast< sal_Int16 >( xSheetsIA->getCount() ); 222cdf0e10cSrcweir // new sheet - insert with unused name 223cdf0e10cSrcweir aSheetName = ContainerHelper::getUnusedName( xSheetsNA, aSheetName, ' ' ); 224cdf0e10cSrcweir xSheets->insertNewByName( aSheetName, nCalcSheet ); 225cdf0e10cSrcweir aPropSet.set( xSheetsIA->getByIndex( nCalcSheet ) ); 226cdf0e10cSrcweir } 227cdf0e10cSrcweir 228cdf0e10cSrcweir // sheet properties 229cdf0e10cSrcweir aPropSet.setProperty( PROP_IsVisible, bVisible ); 230cdf0e10cSrcweir 231cdf0e10cSrcweir // return final sheet index if sheet exists 232cdf0e10cSrcweir return IndexNamePair( nCalcSheet, aSheetName ); 233cdf0e10cSrcweir } 234cdf0e10cSrcweir catch( Exception& ) 235cdf0e10cSrcweir { 236cdf0e10cSrcweir OSL_ENSURE( false, "WorksheetBuffer::createSheet - cannot insert or rename worksheet" ); 237cdf0e10cSrcweir } 238cdf0e10cSrcweir return IndexNamePair( -1, OUString() ); 239cdf0e10cSrcweir } 240cdf0e10cSrcweir 241cdf0e10cSrcweir void WorksheetBuffer::insertSheet( const SheetInfoModel& rModel ) 242cdf0e10cSrcweir { 243cdf0e10cSrcweir sal_Int32 nWorksheet = static_cast< sal_Int32 >( maSheetInfos.size() ); 244cdf0e10cSrcweir IndexNamePair aIndexName = createSheet( rModel.maName, nWorksheet, rModel.mnState == XML_visible ); 245cdf0e10cSrcweir ::boost::shared_ptr< SheetInfo > xSheetInfo( new SheetInfo( rModel, aIndexName.first, aIndexName.second ) ); 246cdf0e10cSrcweir maSheetInfos.push_back( xSheetInfo ); 247cdf0e10cSrcweir maSheetInfosByName[ rModel.maName ] = xSheetInfo; 248cdf0e10cSrcweir maSheetInfosByName[ lclQuoteName( rModel.maName ) ] = xSheetInfo; 249cdf0e10cSrcweir } 250cdf0e10cSrcweir 251cdf0e10cSrcweir // ============================================================================ 252cdf0e10cSrcweir 253cdf0e10cSrcweir } // namespace xls 254cdf0e10cSrcweir } // namespace oox 255