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
10*ca5ec200SAndrew Rist  *
11*ca5ec200SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ca5ec200SAndrew Rist  *
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.
19*ca5ec200SAndrew Rist  *
20*ca5ec200SAndrew Rist  *************************************************************/
21*ca5ec200SAndrew Rist 
22*ca5ec200SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "oox/xls/externallinkfragment.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <com/sun/star/sheet/XExternalSheetCache.hpp>
27cdf0e10cSrcweir #include "oox/helper/attributelist.hxx"
28cdf0e10cSrcweir #include "oox/xls/biffinputstream.hxx"
29cdf0e10cSrcweir #include "oox/xls/defnamesbuffer.hxx"
30cdf0e10cSrcweir #include "oox/xls/sheetdatacontext.hxx"
31cdf0e10cSrcweir #include "oox/xls/unitconverter.hxx"
32cdf0e10cSrcweir 
33cdf0e10cSrcweir namespace oox {
34cdf0e10cSrcweir namespace xls {
35cdf0e10cSrcweir 
36cdf0e10cSrcweir // ============================================================================
37cdf0e10cSrcweir 
38cdf0e10cSrcweir using namespace ::com::sun::star::sheet;
39cdf0e10cSrcweir using namespace ::com::sun::star::table;
40cdf0e10cSrcweir using namespace ::com::sun::star::uno;
41cdf0e10cSrcweir using namespace ::oox::core;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir using ::rtl::OUString;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir // ============================================================================
46cdf0e10cSrcweir // ============================================================================
47cdf0e10cSrcweir 
ExternalSheetDataContext(WorkbookFragmentBase & rFragment,const Reference<XExternalSheetCache> & rxSheetCache)48cdf0e10cSrcweir ExternalSheetDataContext::ExternalSheetDataContext(
49cdf0e10cSrcweir         WorkbookFragmentBase& rFragment, const Reference< XExternalSheetCache >& rxSheetCache ) :
50cdf0e10cSrcweir     WorkbookContextBase( rFragment ),
51cdf0e10cSrcweir     mxSheetCache( rxSheetCache )
52cdf0e10cSrcweir {
53cdf0e10cSrcweir     OSL_ENSURE( mxSheetCache.is(), "ExternalSheetDataContext::ExternalSheetDataContext - missing sheet cache" );
54cdf0e10cSrcweir }
55cdf0e10cSrcweir 
onCreateContext(sal_Int32 nElement,const AttributeList & rAttribs)56cdf0e10cSrcweir ContextHandlerRef ExternalSheetDataContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
57cdf0e10cSrcweir {
58cdf0e10cSrcweir     switch( getCurrentElement() )
59cdf0e10cSrcweir     {
60cdf0e10cSrcweir         case XLS_TOKEN( sheetData ):
61cdf0e10cSrcweir             if( nElement == XLS_TOKEN( row ) ) return this;
62cdf0e10cSrcweir         break;
63cdf0e10cSrcweir         case XLS_TOKEN( row ):
64cdf0e10cSrcweir             if( nElement == XLS_TOKEN( cell ) ) { importCell( rAttribs ); return this; }
65cdf0e10cSrcweir         break;
66cdf0e10cSrcweir         case XLS_TOKEN( cell ):
67cdf0e10cSrcweir             if( nElement == XLS_TOKEN( v ) ) return this;   // collect characters in onCharacters()
68cdf0e10cSrcweir         break;
69cdf0e10cSrcweir     }
70cdf0e10cSrcweir     return 0;
71cdf0e10cSrcweir }
72cdf0e10cSrcweir 
onCharacters(const OUString & rChars)73cdf0e10cSrcweir void ExternalSheetDataContext::onCharacters( const OUString& rChars )
74cdf0e10cSrcweir {
75cdf0e10cSrcweir     if( isCurrentElement( XLS_TOKEN( v ) ) )
76cdf0e10cSrcweir     {
77cdf0e10cSrcweir         switch( mnCurrType )
78cdf0e10cSrcweir         {
79cdf0e10cSrcweir             case XML_b:
80cdf0e10cSrcweir             case XML_n:
81cdf0e10cSrcweir                 setCellValue( Any( rChars.toDouble() ) );
82cdf0e10cSrcweir             break;
83cdf0e10cSrcweir             case XML_e:
84cdf0e10cSrcweir                 setCellValue( Any( BiffHelper::calcDoubleFromError( getUnitConverter().calcBiffErrorCode( rChars ) ) ) );
85cdf0e10cSrcweir             break;
86cdf0e10cSrcweir             case XML_str:
87cdf0e10cSrcweir                 setCellValue( Any( rChars ) );
88cdf0e10cSrcweir             break;
89cdf0e10cSrcweir         }
90cdf0e10cSrcweir         mnCurrType = XML_TOKEN_INVALID;
91cdf0e10cSrcweir     }
92cdf0e10cSrcweir }
93cdf0e10cSrcweir 
onCreateRecordContext(sal_Int32 nRecId,SequenceInputStream & rStrm)94cdf0e10cSrcweir ContextHandlerRef ExternalSheetDataContext::onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm )
95cdf0e10cSrcweir {
96cdf0e10cSrcweir     switch( getCurrentElement() )
97cdf0e10cSrcweir     {
98cdf0e10cSrcweir         case BIFF12_ID_EXTSHEETDATA:
99cdf0e10cSrcweir             if( nRecId == BIFF12_ID_EXTROW ) { maCurrPos.Row = rStrm.readInt32(); return this; }
100cdf0e10cSrcweir         break;
101cdf0e10cSrcweir         case BIFF12_ID_EXTROW:
102cdf0e10cSrcweir             switch( nRecId )
103cdf0e10cSrcweir             {
104cdf0e10cSrcweir                 case BIFF12_ID_EXTCELL_BLANK:   importExtCellBlank( rStrm );    break;
105cdf0e10cSrcweir                 case BIFF12_ID_EXTCELL_BOOL:    importExtCellBool( rStrm );     break;
106cdf0e10cSrcweir                 case BIFF12_ID_EXTCELL_DOUBLE:  importExtCellDouble( rStrm );   break;
107cdf0e10cSrcweir                 case BIFF12_ID_EXTCELL_ERROR:   importExtCellError( rStrm );    break;
108cdf0e10cSrcweir                 case BIFF12_ID_EXTCELL_STRING:  importExtCellString( rStrm );   break;
109cdf0e10cSrcweir             }
110cdf0e10cSrcweir         break;
111cdf0e10cSrcweir     }
112cdf0e10cSrcweir     return 0;
113cdf0e10cSrcweir }
114cdf0e10cSrcweir 
115cdf0e10cSrcweir // private --------------------------------------------------------------------
116cdf0e10cSrcweir 
importCell(const AttributeList & rAttribs)117cdf0e10cSrcweir void ExternalSheetDataContext::importCell( const AttributeList& rAttribs )
118cdf0e10cSrcweir {
119cdf0e10cSrcweir     if( getAddressConverter().convertToCellAddress( maCurrPos, rAttribs.getString( XML_r, OUString() ), 0, false ) )
120cdf0e10cSrcweir         mnCurrType = rAttribs.getToken( XML_t, XML_n );
121cdf0e10cSrcweir     else
122cdf0e10cSrcweir         mnCurrType = XML_TOKEN_INVALID;
123cdf0e10cSrcweir }
124cdf0e10cSrcweir 
importExtCellBlank(SequenceInputStream & rStrm)125cdf0e10cSrcweir void ExternalSheetDataContext::importExtCellBlank( SequenceInputStream& rStrm )
126cdf0e10cSrcweir {
127cdf0e10cSrcweir     maCurrPos.Column = rStrm.readInt32();
128cdf0e10cSrcweir     setCellValue( Any( OUString() ) );
129cdf0e10cSrcweir }
130cdf0e10cSrcweir 
importExtCellBool(SequenceInputStream & rStrm)131cdf0e10cSrcweir void ExternalSheetDataContext::importExtCellBool( SequenceInputStream& rStrm )
132cdf0e10cSrcweir {
133cdf0e10cSrcweir     maCurrPos.Column = rStrm.readInt32();
134cdf0e10cSrcweir     double fValue = (rStrm.readuInt8() == 0) ? 0.0 : 1.0;
135cdf0e10cSrcweir     setCellValue( Any( fValue ) );
136cdf0e10cSrcweir }
137cdf0e10cSrcweir 
importExtCellDouble(SequenceInputStream & rStrm)138cdf0e10cSrcweir void ExternalSheetDataContext::importExtCellDouble( SequenceInputStream& rStrm )
139cdf0e10cSrcweir {
140cdf0e10cSrcweir     maCurrPos.Column = rStrm.readInt32();
141cdf0e10cSrcweir     setCellValue( Any( rStrm.readDouble() ) );
142cdf0e10cSrcweir }
143cdf0e10cSrcweir 
importExtCellError(SequenceInputStream & rStrm)144cdf0e10cSrcweir void ExternalSheetDataContext::importExtCellError( SequenceInputStream& rStrm )
145cdf0e10cSrcweir {
146cdf0e10cSrcweir     maCurrPos.Column = rStrm.readInt32();
147cdf0e10cSrcweir     setCellValue( Any( BiffHelper::calcDoubleFromError( rStrm.readuInt8() ) ) );
148cdf0e10cSrcweir }
149cdf0e10cSrcweir 
importExtCellString(SequenceInputStream & rStrm)150cdf0e10cSrcweir void ExternalSheetDataContext::importExtCellString( SequenceInputStream& rStrm )
151cdf0e10cSrcweir {
152cdf0e10cSrcweir     maCurrPos.Column = rStrm.readInt32();
153cdf0e10cSrcweir     setCellValue( Any( BiffHelper::readString( rStrm ) ) );
154cdf0e10cSrcweir }
155cdf0e10cSrcweir 
setCellValue(const Any & rValue)156cdf0e10cSrcweir void ExternalSheetDataContext::setCellValue( const Any& rValue )
157cdf0e10cSrcweir {
158cdf0e10cSrcweir     if( mxSheetCache.is() && getAddressConverter().checkCellAddress( maCurrPos, false ) ) try
159cdf0e10cSrcweir     {
160cdf0e10cSrcweir         mxSheetCache->setCellValue( maCurrPos.Column, maCurrPos.Row, rValue );
161cdf0e10cSrcweir     }
162cdf0e10cSrcweir     catch( Exception& )
163cdf0e10cSrcweir     {
164cdf0e10cSrcweir     }
165cdf0e10cSrcweir }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir // ============================================================================
168cdf0e10cSrcweir 
ExternalLinkFragment(const WorkbookHelper & rHelper,const OUString & rFragmentPath,ExternalLink & rExtLink)169cdf0e10cSrcweir ExternalLinkFragment::ExternalLinkFragment( const WorkbookHelper& rHelper,
170cdf0e10cSrcweir         const OUString& rFragmentPath, ExternalLink& rExtLink ) :
171cdf0e10cSrcweir     WorkbookFragmentBase( rHelper, rFragmentPath ),
172cdf0e10cSrcweir     mrExtLink( rExtLink ),
173cdf0e10cSrcweir     mnResultType( XML_TOKEN_INVALID )
174cdf0e10cSrcweir {
175cdf0e10cSrcweir }
176cdf0e10cSrcweir 
onCreateContext(sal_Int32 nElement,const AttributeList & rAttribs)177cdf0e10cSrcweir ContextHandlerRef ExternalLinkFragment::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
178cdf0e10cSrcweir {
179cdf0e10cSrcweir     switch( getCurrentElement() )
180cdf0e10cSrcweir     {
181cdf0e10cSrcweir         case XML_ROOT_CONTEXT:
182cdf0e10cSrcweir             if( nElement == XLS_TOKEN( externalLink ) ) return this;
183cdf0e10cSrcweir         break;
184cdf0e10cSrcweir 
185cdf0e10cSrcweir         case XLS_TOKEN( externalLink ):
186cdf0e10cSrcweir             switch( nElement )
187cdf0e10cSrcweir             {
188cdf0e10cSrcweir                 case XLS_TOKEN( externalBook ): mrExtLink.importExternalBook( getRelations(), rAttribs );   return this;
189cdf0e10cSrcweir                 case XLS_TOKEN( ddeLink ):      mrExtLink.importDdeLink( rAttribs );                        return this;
190cdf0e10cSrcweir                 case XLS_TOKEN( oleLink ):      mrExtLink.importOleLink( getRelations(), rAttribs );        return this;
191cdf0e10cSrcweir             }
192cdf0e10cSrcweir         break;
193cdf0e10cSrcweir 
194cdf0e10cSrcweir         case XLS_TOKEN( externalBook ):
195cdf0e10cSrcweir             switch( nElement )
196cdf0e10cSrcweir             {
197cdf0e10cSrcweir                 case XLS_TOKEN( sheetNames ):
198cdf0e10cSrcweir                 case XLS_TOKEN( definedNames ):
199cdf0e10cSrcweir                 case XLS_TOKEN( sheetDataSet ): return this;
200cdf0e10cSrcweir             }
201cdf0e10cSrcweir         break;
202cdf0e10cSrcweir 
203cdf0e10cSrcweir         case XLS_TOKEN( sheetNames ):
204cdf0e10cSrcweir             if( nElement == XLS_TOKEN( sheetName ) ) mrExtLink.importSheetName( rAttribs );
205cdf0e10cSrcweir         break;
206cdf0e10cSrcweir         case XLS_TOKEN( definedNames ):
207cdf0e10cSrcweir             if( nElement == XLS_TOKEN( definedName ) ) mrExtLink.importDefinedName( rAttribs );
208cdf0e10cSrcweir         break;
209cdf0e10cSrcweir         case XLS_TOKEN( sheetDataSet ):
210cdf0e10cSrcweir             if( (nElement == XLS_TOKEN( sheetData )) && (mrExtLink.getLinkType() == LINKTYPE_EXTERNAL) )
211cdf0e10cSrcweir                 return createSheetDataContext( rAttribs.getInteger( XML_sheetId, -1 ) );
212cdf0e10cSrcweir         break;
213cdf0e10cSrcweir 
214cdf0e10cSrcweir         case XLS_TOKEN( ddeLink ):
215cdf0e10cSrcweir             if( nElement == XLS_TOKEN( ddeItems ) ) return this;
216cdf0e10cSrcweir         break;
217cdf0e10cSrcweir         case XLS_TOKEN( ddeItems ):
218cdf0e10cSrcweir             if( nElement == XLS_TOKEN( ddeItem ) )
219cdf0e10cSrcweir             {
220cdf0e10cSrcweir                 mxExtName = mrExtLink.importDdeItem( rAttribs );
221cdf0e10cSrcweir                 return this;
222cdf0e10cSrcweir             }
223cdf0e10cSrcweir         break;
224cdf0e10cSrcweir         case XLS_TOKEN( ddeItem ):
225cdf0e10cSrcweir             if( nElement == XLS_TOKEN( values ) )
226cdf0e10cSrcweir             {
227cdf0e10cSrcweir                 if( mxExtName.get() ) mxExtName->importValues( rAttribs );
228cdf0e10cSrcweir                 return this;
229cdf0e10cSrcweir             }
230cdf0e10cSrcweir         break;
231cdf0e10cSrcweir         case XLS_TOKEN( values ):
232cdf0e10cSrcweir             if( nElement == XLS_TOKEN( value ) )
233cdf0e10cSrcweir             {
234cdf0e10cSrcweir                 mnResultType = rAttribs.getToken( XML_t, XML_n );
235cdf0e10cSrcweir                 return this;
236cdf0e10cSrcweir             }
237cdf0e10cSrcweir         break;
238cdf0e10cSrcweir         case XLS_TOKEN( value ):
239cdf0e10cSrcweir             if( nElement == XLS_TOKEN( val ) ) return this; // collect value in onCharacters()
240cdf0e10cSrcweir         break;
241cdf0e10cSrcweir 
242cdf0e10cSrcweir         case XLS_TOKEN( oleLink ):
243cdf0e10cSrcweir             if( nElement == XLS_TOKEN( oleItems ) ) return this;
244cdf0e10cSrcweir         break;
245cdf0e10cSrcweir         case XLS_TOKEN( oleItems ):
246cdf0e10cSrcweir             if( nElement == XLS_TOKEN( oleItem ) ) mxExtName = mrExtLink.importOleItem( rAttribs );
247cdf0e10cSrcweir         break;
248cdf0e10cSrcweir     }
249cdf0e10cSrcweir     return 0;
250cdf0e10cSrcweir }
251cdf0e10cSrcweir 
onCharacters(const OUString & rChars)252cdf0e10cSrcweir void ExternalLinkFragment::onCharacters( const OUString& rChars )
253cdf0e10cSrcweir {
254cdf0e10cSrcweir     if( isCurrentElement( XLS_TOKEN( val ) ) )
255cdf0e10cSrcweir         maResultValue = rChars;
256cdf0e10cSrcweir }
257cdf0e10cSrcweir 
onEndElement()258cdf0e10cSrcweir void ExternalLinkFragment::onEndElement()
259cdf0e10cSrcweir {
260cdf0e10cSrcweir     if( isCurrentElement( XLS_TOKEN( value ) ) && mxExtName.get() ) switch( mnResultType )
261cdf0e10cSrcweir     {
262cdf0e10cSrcweir         case XML_b:
263cdf0e10cSrcweir             mxExtName->appendResultValue( maResultValue.toDouble() );
264cdf0e10cSrcweir         break;
265cdf0e10cSrcweir         case XML_e:
266cdf0e10cSrcweir             mxExtName->appendResultValue( BiffHelper::calcDoubleFromError( getUnitConverter().calcBiffErrorCode( maResultValue ) ) );
267cdf0e10cSrcweir         break;
268cdf0e10cSrcweir         case XML_n:
269cdf0e10cSrcweir             mxExtName->appendResultValue( maResultValue.toDouble() );
270cdf0e10cSrcweir         break;
271cdf0e10cSrcweir         case XML_str:
272cdf0e10cSrcweir             mxExtName->appendResultValue( maResultValue );
273cdf0e10cSrcweir         break;
274cdf0e10cSrcweir         default:
275cdf0e10cSrcweir             mxExtName->appendResultValue( BiffHelper::calcDoubleFromError( BIFF_ERR_NA ) );
276cdf0e10cSrcweir     }
277cdf0e10cSrcweir }
278cdf0e10cSrcweir 
onCreateRecordContext(sal_Int32 nRecId,SequenceInputStream & rStrm)279cdf0e10cSrcweir ContextHandlerRef ExternalLinkFragment::onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm )
280cdf0e10cSrcweir {
281cdf0e10cSrcweir     switch( getCurrentElement() )
282cdf0e10cSrcweir     {
283cdf0e10cSrcweir         case XML_ROOT_CONTEXT:
284cdf0e10cSrcweir             if( nRecId == BIFF12_ID_EXTERNALBOOK )
285cdf0e10cSrcweir             {
286cdf0e10cSrcweir                 mrExtLink.importExternalBook( getRelations(), rStrm );
287cdf0e10cSrcweir                 return this;
288cdf0e10cSrcweir             }
289cdf0e10cSrcweir         break;
290cdf0e10cSrcweir 
291cdf0e10cSrcweir         case BIFF12_ID_EXTERNALBOOK:
292cdf0e10cSrcweir             switch( nRecId )
293cdf0e10cSrcweir             {
294cdf0e10cSrcweir                 case BIFF12_ID_EXTSHEETDATA:
295cdf0e10cSrcweir                     if( mrExtLink.getLinkType() == LINKTYPE_EXTERNAL )
296cdf0e10cSrcweir                         return createSheetDataContext( rStrm.readInt32() );
297cdf0e10cSrcweir                 break;
298cdf0e10cSrcweir 
299cdf0e10cSrcweir                 case BIFF12_ID_EXTSHEETNAMES:       mrExtLink.importExtSheetNames( rStrm );                             break;
300cdf0e10cSrcweir                 case BIFF12_ID_EXTERNALNAME:        mxExtName = mrExtLink.importExternalName( rStrm );                  return this;
301cdf0e10cSrcweir             }
302cdf0e10cSrcweir         break;
303cdf0e10cSrcweir 
304cdf0e10cSrcweir         case BIFF12_ID_EXTERNALNAME:
305cdf0e10cSrcweir             switch( nRecId )
306cdf0e10cSrcweir             {
307cdf0e10cSrcweir                 case BIFF12_ID_EXTERNALNAMEFLAGS:   if( mxExtName.get() ) mxExtName->importExternalNameFlags( rStrm );  break;
308cdf0e10cSrcweir                 case BIFF12_ID_DDEITEMVALUES:       if( mxExtName.get() ) mxExtName->importDdeItemValues( rStrm );      return this;
309cdf0e10cSrcweir             }
310cdf0e10cSrcweir         break;
311cdf0e10cSrcweir 
312cdf0e10cSrcweir         case BIFF12_ID_DDEITEMVALUES:
313cdf0e10cSrcweir             switch( nRecId )
314cdf0e10cSrcweir             {
315cdf0e10cSrcweir                 case BIFF12_ID_DDEITEM_BOOL:        if( mxExtName.get() ) mxExtName->importDdeItemBool( rStrm );        break;
316cdf0e10cSrcweir                 case BIFF12_ID_DDEITEM_DOUBLE:      if( mxExtName.get() ) mxExtName->importDdeItemDouble( rStrm );      break;
317cdf0e10cSrcweir                 case BIFF12_ID_DDEITEM_ERROR:       if( mxExtName.get() ) mxExtName->importDdeItemError( rStrm );       break;
318cdf0e10cSrcweir                 case BIFF12_ID_DDEITEM_STRING:      if( mxExtName.get() ) mxExtName->importDdeItemString( rStrm );      break;
319cdf0e10cSrcweir             }
320cdf0e10cSrcweir         break;
321cdf0e10cSrcweir     }
322cdf0e10cSrcweir     return 0;
323cdf0e10cSrcweir }
324cdf0e10cSrcweir 
createSheetDataContext(sal_Int32 nSheetId)325cdf0e10cSrcweir ContextHandlerRef ExternalLinkFragment::createSheetDataContext( sal_Int32 nSheetId )
326cdf0e10cSrcweir {
327cdf0e10cSrcweir     return new ExternalSheetDataContext( *this, mrExtLink.getSheetCache( nSheetId ) );
328cdf0e10cSrcweir }
329cdf0e10cSrcweir 
getRecordInfos() const330cdf0e10cSrcweir const RecordInfo* ExternalLinkFragment::getRecordInfos() const
331cdf0e10cSrcweir {
332cdf0e10cSrcweir     static const RecordInfo spRecInfos[] =
333cdf0e10cSrcweir     {
334cdf0e10cSrcweir         { BIFF12_ID_DDEITEMVALUES,  BIFF12_ID_DDEITEMVALUES + 1     },
335cdf0e10cSrcweir         { BIFF12_ID_EXTERNALBOOK,   BIFF12_ID_EXTERNALBOOK + 228    },
336cdf0e10cSrcweir         { BIFF12_ID_EXTERNALNAME,   BIFF12_ID_EXTERNALNAME + 10     },
337cdf0e10cSrcweir         { BIFF12_ID_EXTROW,         -1                              },
338cdf0e10cSrcweir         { BIFF12_ID_EXTSHEETDATA,   BIFF12_ID_EXTSHEETDATA + 1      },
339cdf0e10cSrcweir         { -1,                       -1                              }
340cdf0e10cSrcweir     };
341cdf0e10cSrcweir     return spRecInfos;
342cdf0e10cSrcweir }
343cdf0e10cSrcweir 
344cdf0e10cSrcweir // ============================================================================
345cdf0e10cSrcweir // ============================================================================
346cdf0e10cSrcweir 
BiffExternalSheetDataContext(const WorkbookHelper & rHelper,bool bImportDefNames)347cdf0e10cSrcweir BiffExternalSheetDataContext::BiffExternalSheetDataContext( const WorkbookHelper& rHelper, bool bImportDefNames ) :
348cdf0e10cSrcweir     BiffWorkbookContextBase( rHelper ),
349cdf0e10cSrcweir     mbImportDefNames( bImportDefNames )
350cdf0e10cSrcweir {
351cdf0e10cSrcweir }
352cdf0e10cSrcweir 
~BiffExternalSheetDataContext()353cdf0e10cSrcweir BiffExternalSheetDataContext::~BiffExternalSheetDataContext()
354cdf0e10cSrcweir {
355cdf0e10cSrcweir }
356cdf0e10cSrcweir 
importRecord(BiffInputStream & rStrm)357cdf0e10cSrcweir void BiffExternalSheetDataContext::importRecord( BiffInputStream& rStrm )
358cdf0e10cSrcweir {
359cdf0e10cSrcweir     sal_uInt16 nRecId = rStrm.getRecId();
360cdf0e10cSrcweir     switch( getBiff() )
361cdf0e10cSrcweir     {
362cdf0e10cSrcweir         case BIFF2: switch( nRecId )
363cdf0e10cSrcweir         {
364cdf0e10cSrcweir             case BIFF2_ID_EXTERNALNAME: importExternalName( rStrm );    break;
365cdf0e10cSrcweir             case BIFF_ID_EXTERNSHEET:   importExternSheet( rStrm );     break;
366cdf0e10cSrcweir             case BIFF2_ID_DEFINEDNAME:  importDefinedName( rStrm );     break;
367cdf0e10cSrcweir         }
368cdf0e10cSrcweir         break;
369cdf0e10cSrcweir         case BIFF3: switch( nRecId )
370cdf0e10cSrcweir         {
371cdf0e10cSrcweir             case BIFF_ID_CRN:           importCrn( rStrm );             break;
372cdf0e10cSrcweir             case BIFF3_ID_EXTERNALNAME: importExternalName( rStrm );    break;
373cdf0e10cSrcweir             case BIFF_ID_EXTERNSHEET:   importExternSheet( rStrm );     break;
374cdf0e10cSrcweir             case BIFF3_ID_DEFINEDNAME:  importDefinedName( rStrm );     break;
375cdf0e10cSrcweir             case BIFF_ID_XCT:           importXct( rStrm );             break;
376cdf0e10cSrcweir         }
377cdf0e10cSrcweir         break;
378cdf0e10cSrcweir         case BIFF4: switch( nRecId )
379cdf0e10cSrcweir         {
380cdf0e10cSrcweir             case BIFF_ID_CRN:           importCrn( rStrm );             break;
381cdf0e10cSrcweir             case BIFF3_ID_EXTERNALNAME: importExternalName( rStrm );    break;
382cdf0e10cSrcweir             case BIFF_ID_EXTERNSHEET:   importExternSheet( rStrm );     break;
383cdf0e10cSrcweir             case BIFF3_ID_DEFINEDNAME:  importDefinedName( rStrm );     break;
384cdf0e10cSrcweir             case BIFF_ID_XCT:           importXct( rStrm );             break;
385cdf0e10cSrcweir         }
386cdf0e10cSrcweir         break;
387cdf0e10cSrcweir         case BIFF5: switch( nRecId )
388cdf0e10cSrcweir         {
389cdf0e10cSrcweir             case BIFF_ID_CRN:           importCrn( rStrm );             break;
390cdf0e10cSrcweir             case BIFF5_ID_EXTERNALNAME: importExternalName( rStrm );    break;
391cdf0e10cSrcweir             case BIFF_ID_EXTERNSHEET:   importExternSheet( rStrm );     break;
392cdf0e10cSrcweir             case BIFF5_ID_DEFINEDNAME:  importDefinedName( rStrm );     break;
393cdf0e10cSrcweir             case BIFF_ID_XCT:           importXct( rStrm );             break;
394cdf0e10cSrcweir         }
395cdf0e10cSrcweir         break;
396cdf0e10cSrcweir         case BIFF8: switch( nRecId )
397cdf0e10cSrcweir         {
398cdf0e10cSrcweir             case BIFF_ID_CRN:           importCrn( rStrm );             break;
399cdf0e10cSrcweir             case BIFF_ID_EXTERNALBOOK:  importExternalBook( rStrm );    break;
400cdf0e10cSrcweir             case BIFF5_ID_EXTERNALNAME: importExternalName( rStrm );    break;
401cdf0e10cSrcweir             case BIFF_ID_EXTERNSHEET:   importExternSheet( rStrm );     break;
402cdf0e10cSrcweir             case BIFF5_ID_DEFINEDNAME:  importDefinedName( rStrm );     break;
403cdf0e10cSrcweir             case BIFF_ID_XCT:           importXct( rStrm );             break;
404cdf0e10cSrcweir         }
405cdf0e10cSrcweir         break;
406cdf0e10cSrcweir         case BIFF_UNKNOWN: break;
407cdf0e10cSrcweir     }
408cdf0e10cSrcweir }
409cdf0e10cSrcweir 
410cdf0e10cSrcweir // private --------------------------------------------------------------------
411cdf0e10cSrcweir 
importExternSheet(BiffInputStream & rStrm)412cdf0e10cSrcweir void BiffExternalSheetDataContext::importExternSheet( BiffInputStream& rStrm )
413cdf0e10cSrcweir {
414cdf0e10cSrcweir     mxSheetCache.clear();
415cdf0e10cSrcweir     if( getBiff() == BIFF8 )
416cdf0e10cSrcweir         getExternalLinks().importExternSheet8( rStrm );
417cdf0e10cSrcweir     else
418cdf0e10cSrcweir         mxExtLink = getExternalLinks().importExternSheet( rStrm );
419cdf0e10cSrcweir }
420cdf0e10cSrcweir 
importExternalBook(BiffInputStream & rStrm)421cdf0e10cSrcweir void BiffExternalSheetDataContext::importExternalBook( BiffInputStream& rStrm )
422cdf0e10cSrcweir {
423cdf0e10cSrcweir     mxSheetCache.clear();
424cdf0e10cSrcweir     mxExtLink = getExternalLinks().importExternalBook( rStrm );
425cdf0e10cSrcweir }
426cdf0e10cSrcweir 
importExternalName(BiffInputStream & rStrm)427cdf0e10cSrcweir void BiffExternalSheetDataContext::importExternalName( BiffInputStream& rStrm )
428cdf0e10cSrcweir {
429cdf0e10cSrcweir     if( mxExtLink.get() )
430cdf0e10cSrcweir         mxExtLink->importExternalName( rStrm );
431cdf0e10cSrcweir }
432cdf0e10cSrcweir 
importXct(BiffInputStream & rStrm)433cdf0e10cSrcweir void BiffExternalSheetDataContext::importXct( BiffInputStream& rStrm )
434cdf0e10cSrcweir {
435cdf0e10cSrcweir     mxSheetCache.clear();
436cdf0e10cSrcweir     if( mxExtLink.get() && (mxExtLink->getLinkType() == LINKTYPE_EXTERNAL) )
437cdf0e10cSrcweir     {
438cdf0e10cSrcweir         switch( getBiff() )
439cdf0e10cSrcweir         {
440cdf0e10cSrcweir             case BIFF2:
441cdf0e10cSrcweir             break;
442cdf0e10cSrcweir             case BIFF3:
443cdf0e10cSrcweir             case BIFF4:
444cdf0e10cSrcweir             case BIFF5:
445cdf0e10cSrcweir                 mxSheetCache = mxExtLink->getSheetCache( 0 );
446cdf0e10cSrcweir             break;
447cdf0e10cSrcweir             case BIFF8:
448cdf0e10cSrcweir                 rStrm.skip( 2 );
449cdf0e10cSrcweir                 mxSheetCache = mxExtLink->getSheetCache( rStrm.readInt16() );
450cdf0e10cSrcweir             break;
451cdf0e10cSrcweir             case BIFF_UNKNOWN:
452cdf0e10cSrcweir             break;
453cdf0e10cSrcweir         }
454cdf0e10cSrcweir     }
455cdf0e10cSrcweir }
456cdf0e10cSrcweir 
importCrn(BiffInputStream & rStrm)457cdf0e10cSrcweir void BiffExternalSheetDataContext::importCrn( BiffInputStream& rStrm )
458cdf0e10cSrcweir {
459cdf0e10cSrcweir     if( !mxSheetCache.is() ) return;
460cdf0e10cSrcweir 
461cdf0e10cSrcweir     sal_uInt8 nCol2, nCol1;
462cdf0e10cSrcweir     sal_uInt16 nRow;
463cdf0e10cSrcweir     rStrm >> nCol2 >> nCol1 >> nRow;
464cdf0e10cSrcweir     bool bLoop = true;
465cdf0e10cSrcweir     for( BinAddress aBinAddr( nCol1, nRow ); bLoop && !rStrm.isEof() && (aBinAddr.mnCol <= nCol2); ++aBinAddr.mnCol )
466cdf0e10cSrcweir     {
467cdf0e10cSrcweir         switch( rStrm.readuInt8() )
468cdf0e10cSrcweir         {
469cdf0e10cSrcweir             case BIFF_DATATYPE_EMPTY:
470cdf0e10cSrcweir                 rStrm.skip( 8 );
471cdf0e10cSrcweir                 setCellValue( aBinAddr, Any( OUString() ) );
472cdf0e10cSrcweir             break;
473cdf0e10cSrcweir             case BIFF_DATATYPE_DOUBLE:
474cdf0e10cSrcweir                 setCellValue( aBinAddr, Any( rStrm.readDouble() ) );
475cdf0e10cSrcweir             break;
476cdf0e10cSrcweir             case BIFF_DATATYPE_STRING:
477cdf0e10cSrcweir             {
478cdf0e10cSrcweir                 OUString aText = (getBiff() == BIFF8) ? rStrm.readUniString() : rStrm.readByteStringUC( false, getTextEncoding() );
479cdf0e10cSrcweir                 setCellValue( aBinAddr, Any( aText ) );
480cdf0e10cSrcweir             }
481cdf0e10cSrcweir             break;
482cdf0e10cSrcweir             case BIFF_DATATYPE_BOOL:
483cdf0e10cSrcweir             {
484cdf0e10cSrcweir                 double fValue = (rStrm.readuInt8() == 0) ? 0.0 : 1.0;
485cdf0e10cSrcweir                 setCellValue( aBinAddr, Any( fValue ) );
486cdf0e10cSrcweir                 rStrm.skip( 7 );
487cdf0e10cSrcweir             }
488cdf0e10cSrcweir             break;
489cdf0e10cSrcweir             case BIFF_DATATYPE_ERROR:
490cdf0e10cSrcweir                 setCellValue( aBinAddr, Any( BiffHelper::calcDoubleFromError( rStrm.readuInt8() ) ) );
491cdf0e10cSrcweir                 rStrm.skip( 7 );
492cdf0e10cSrcweir             break;
493cdf0e10cSrcweir             default:
494cdf0e10cSrcweir                 OSL_ENSURE( false, "BiffExternalSheetDataContext::importCrn - unknown data type" );
495cdf0e10cSrcweir                 bLoop = false;
496cdf0e10cSrcweir         }
497cdf0e10cSrcweir     }
498cdf0e10cSrcweir }
499cdf0e10cSrcweir 
importDefinedName(BiffInputStream & rStrm)500cdf0e10cSrcweir void BiffExternalSheetDataContext::importDefinedName( BiffInputStream& rStrm )
501cdf0e10cSrcweir {
502cdf0e10cSrcweir     if( mbImportDefNames )
503cdf0e10cSrcweir         getDefinedNames().importDefinedName( rStrm );
504cdf0e10cSrcweir }
505cdf0e10cSrcweir 
setCellValue(const BinAddress & rBinAddr,const Any & rValue)506cdf0e10cSrcweir void BiffExternalSheetDataContext::setCellValue( const BinAddress& rBinAddr, const Any& rValue )
507cdf0e10cSrcweir {
508cdf0e10cSrcweir     CellAddress aCellPos;
509cdf0e10cSrcweir     if( mxSheetCache.is() && getAddressConverter().convertToCellAddress( aCellPos, rBinAddr, 0, false ) ) try
510cdf0e10cSrcweir     {
511cdf0e10cSrcweir         mxSheetCache->setCellValue( aCellPos.Column, aCellPos.Row, rValue );
512cdf0e10cSrcweir     }
513cdf0e10cSrcweir     catch( Exception& )
514cdf0e10cSrcweir     {
515cdf0e10cSrcweir     }
516cdf0e10cSrcweir }
517cdf0e10cSrcweir 
518cdf0e10cSrcweir // ============================================================================
519cdf0e10cSrcweir 
BiffExternalLinkFragment(const BiffWorkbookFragmentBase & rParent)520cdf0e10cSrcweir BiffExternalLinkFragment::BiffExternalLinkFragment( const BiffWorkbookFragmentBase& rParent ) :
521cdf0e10cSrcweir     BiffWorkbookFragmentBase( rParent )
522cdf0e10cSrcweir {
523cdf0e10cSrcweir }
524cdf0e10cSrcweir 
importFragment()525cdf0e10cSrcweir bool BiffExternalLinkFragment::importFragment()
526cdf0e10cSrcweir {
527cdf0e10cSrcweir     // process all record in this sheet fragment
528cdf0e10cSrcweir     BiffExternalSheetDataContext aSheetContext( *this, false );
529cdf0e10cSrcweir     BiffInputStream& rStrm = getInputStream();
530cdf0e10cSrcweir     while( rStrm.startNextRecord() && (rStrm.getRecId() != BIFF_ID_EOF) )
531cdf0e10cSrcweir     {
532cdf0e10cSrcweir         if( BiffHelper::isBofRecord( rStrm ) )
533cdf0e10cSrcweir             skipFragment();  // skip unknown embedded fragments
534cdf0e10cSrcweir         else
535cdf0e10cSrcweir             aSheetContext.importRecord( rStrm );
536cdf0e10cSrcweir     }
537cdf0e10cSrcweir     return !rStrm.isEof() && (rStrm.getRecId() == BIFF_ID_EOF);
538cdf0e10cSrcweir }
539cdf0e10cSrcweir 
540cdf0e10cSrcweir // ============================================================================
541cdf0e10cSrcweir // ============================================================================
542cdf0e10cSrcweir 
543cdf0e10cSrcweir } // namespace xls
544cdf0e10cSrcweir } // namespace oox
545