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