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