Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sc / source / filter / oox / externallinkfragment.cxx
blob7c29458e6f835a60922689bd1604271952163444
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <externallinkfragment.hxx>
22 #include <com/sun/star/sheet/XExternalSheetCache.hpp>
23 #include <oox/helper/attributelist.hxx>
24 #include <oox/helper/binaryinputstream.hxx>
25 #include <oox/token/namespaces.hxx>
26 #include <oox/token/tokens.hxx>
27 #include <osl/diagnose.h>
28 #include <addressconverter.hxx>
29 #include <unitconverter.hxx>
30 #include <biffhelper.hxx>
32 namespace oox {
33 namespace xls {
35 using namespace ::com::sun::star::sheet;
36 using namespace ::com::sun::star::uno;
37 using namespace ::oox::core;
39 ExternalSheetDataContext::ExternalSheetDataContext(
40 WorkbookFragmentBase& rFragment, const Reference< XExternalSheetCache >& rxSheetCache )
41 : WorkbookContextBase(rFragment)
42 , mxSheetCache(rxSheetCache)
43 , mnCurrType(XML_TOKEN_INVALID)
45 OSL_ENSURE( mxSheetCache.is(), "ExternalSheetDataContext::ExternalSheetDataContext - missing sheet cache" );
48 ContextHandlerRef ExternalSheetDataContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
50 switch( getCurrentElement() )
52 case XLS_TOKEN( sheetData ):
53 if( nElement == XLS_TOKEN( row ) ) return this;
54 break;
55 case XLS_TOKEN( row ):
56 if( nElement == XLS_TOKEN( cell ) ) { importCell( rAttribs ); return this; }
57 break;
58 case XLS_TOKEN( cell ):
59 if( nElement == XLS_TOKEN( v ) ) return this; // collect characters in onCharacters()
60 break;
62 return nullptr;
65 void ExternalSheetDataContext::onCharacters( const OUString& rChars )
67 if( isCurrentElement( XLS_TOKEN( v ) ) )
69 switch( mnCurrType )
71 case XML_b:
72 case XML_n:
73 setCellValue( Any( rChars.toDouble() ) );
74 break;
75 case XML_e:
76 setCellValue( Any( BiffHelper::calcDoubleFromError( getUnitConverter().calcBiffErrorCode( rChars ) ) ) );
77 break;
78 case XML_str:
79 setCellValue( Any( rChars ) );
80 break;
82 mnCurrType = XML_TOKEN_INVALID;
86 ContextHandlerRef ExternalSheetDataContext::onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm )
88 switch( getCurrentElement() )
90 case BIFF12_ID_EXTSHEETDATA:
91 if( nRecId == BIFF12_ID_EXTROW ) { maCurrPos.SetRow( rStrm.readInt32() ); return this; }
92 break;
93 case BIFF12_ID_EXTROW:
94 switch( nRecId )
96 case BIFF12_ID_EXTCELL_BLANK: importExtCellBlank( rStrm ); break;
97 case BIFF12_ID_EXTCELL_BOOL: importExtCellBool( rStrm ); break;
98 case BIFF12_ID_EXTCELL_DOUBLE: importExtCellDouble( rStrm ); break;
99 case BIFF12_ID_EXTCELL_ERROR: importExtCellError( rStrm ); break;
100 case BIFF12_ID_EXTCELL_STRING: importExtCellString( rStrm ); break;
102 break;
104 return nullptr;
107 // private --------------------------------------------------------------------
109 void ExternalSheetDataContext::importCell( const AttributeList& rAttribs )
111 if( getAddressConverter().convertToCellAddress( maCurrPos, rAttribs.getString( XML_r, OUString() ), 0, false ) )
112 mnCurrType = rAttribs.getToken( XML_t, XML_n );
113 else
114 mnCurrType = XML_TOKEN_INVALID;
117 void ExternalSheetDataContext::importExtCellBlank( SequenceInputStream& rStrm )
119 maCurrPos.SetCol( rStrm.readInt32() );
120 setCellValue( Any( OUString() ) );
123 void ExternalSheetDataContext::importExtCellBool( SequenceInputStream& rStrm )
125 maCurrPos.SetCol( rStrm.readInt32() );
126 double fValue = (rStrm.readuInt8() == 0) ? 0.0 : 1.0;
127 setCellValue( Any( fValue ) );
130 void ExternalSheetDataContext::importExtCellDouble( SequenceInputStream& rStrm )
132 maCurrPos.SetCol( rStrm.readInt32() );
133 setCellValue( Any( rStrm.readDouble() ) );
136 void ExternalSheetDataContext::importExtCellError( SequenceInputStream& rStrm )
138 maCurrPos.SetCol( rStrm.readInt32() );
139 setCellValue( Any( BiffHelper::calcDoubleFromError( rStrm.readuInt8() ) ) );
142 void ExternalSheetDataContext::importExtCellString( SequenceInputStream& rStrm )
144 maCurrPos.SetCol( rStrm.readInt32() );
145 setCellValue( Any( BiffHelper::readString( rStrm ) ) );
148 void ExternalSheetDataContext::setCellValue( const Any& rValue )
150 if( mxSheetCache.is() && getAddressConverter().checkCellAddress( maCurrPos, false ) ) try
152 mxSheetCache->setCellValue( maCurrPos.Col(), maCurrPos.Row(), rValue );
154 catch( Exception& )
159 ExternalLinkFragment::ExternalLinkFragment( const WorkbookHelper& rHelper,
160 const OUString& rFragmentPath, ExternalLink& rExtLink ) :
161 WorkbookFragmentBase( rHelper, rFragmentPath ),
162 mrExtLink( rExtLink ),
163 mnResultType( XML_TOKEN_INVALID )
167 ContextHandlerRef ExternalLinkFragment::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
169 switch( getCurrentElement() )
171 case XML_ROOT_CONTEXT:
172 if( nElement == XLS_TOKEN( externalLink ) ) return this;
173 break;
175 case XLS_TOKEN( externalLink ):
176 switch( nElement )
178 case XLS_TOKEN( externalBook ): mrExtLink.importExternalBook( getRelations(), rAttribs ); return this;
179 case XLS_TOKEN( ddeLink ): mrExtLink.importDdeLink( rAttribs ); return this;
180 case XLS_TOKEN( oleLink ): mrExtLink.importOleLink( getRelations(), rAttribs ); return this;
182 break;
184 case XLS_TOKEN( externalBook ):
185 switch( nElement )
187 case XLS_TOKEN( sheetNames ):
188 case XLS_TOKEN( definedNames ):
189 case XLS_TOKEN( sheetDataSet ): return this;
191 break;
193 case XLS_TOKEN( sheetNames ):
194 if( nElement == XLS_TOKEN( sheetName ) ) mrExtLink.importSheetName( rAttribs );
195 break;
196 case XLS_TOKEN( definedNames ):
197 if( nElement == XLS_TOKEN( definedName ) ) mrExtLink.importDefinedName( rAttribs );
198 break;
199 case XLS_TOKEN( sheetDataSet ):
200 if( (nElement == XLS_TOKEN( sheetData )) && (mrExtLink.getLinkType() == ExternalLinkType::External) )
201 return createSheetDataContext( rAttribs.getInteger( XML_sheetId, -1 ) );
202 break;
204 case XLS_TOKEN( ddeLink ):
205 if( nElement == XLS_TOKEN( ddeItems ) ) return this;
206 break;
207 case XLS_TOKEN( ddeItems ):
208 if( nElement == XLS_TOKEN( ddeItem ) )
210 mxExtName = mrExtLink.importDdeItem( rAttribs );
211 return this;
213 break;
214 case XLS_TOKEN( ddeItem ):
215 if( nElement == XLS_TOKEN( values ) )
217 if( mxExtName.get() ) mxExtName->importValues( rAttribs );
218 return this;
220 break;
221 case XLS_TOKEN( values ):
222 if( nElement == XLS_TOKEN( value ) )
224 mnResultType = rAttribs.getToken( XML_t, XML_n );
225 return this;
227 break;
228 case XLS_TOKEN( value ):
229 if( nElement == XLS_TOKEN( val ) ) return this; // collect value in onCharacters()
230 break;
232 case XLS_TOKEN( oleLink ):
233 if( nElement == XLS_TOKEN( oleItems ) ) return this;
234 break;
235 case XLS_TOKEN( oleItems ):
236 if( nElement == XLS_TOKEN( oleItem ) ) mxExtName = mrExtLink.importOleItem( rAttribs );
237 break;
239 return nullptr;
242 void ExternalLinkFragment::onCharacters( const OUString& rChars )
244 if( isCurrentElement( XLS_TOKEN( val ) ) )
245 maResultValue = rChars;
248 void ExternalLinkFragment::onEndElement()
250 if( isCurrentElement( XLS_TOKEN( value ) ) && mxExtName.get() ) switch( mnResultType )
252 case XML_b:
253 mxExtName->appendResultValue( maResultValue.toDouble() );
254 break;
255 case XML_e:
256 mxExtName->appendResultValue( BiffHelper::calcDoubleFromError( getUnitConverter().calcBiffErrorCode( maResultValue ) ) );
257 break;
258 case XML_n:
259 mxExtName->appendResultValue( maResultValue.toDouble() );
260 break;
261 case XML_str:
262 mxExtName->appendResultValue( maResultValue );
263 break;
264 default:
265 mxExtName->appendResultValue( BiffHelper::calcDoubleFromError( BIFF_ERR_NA ) );
269 ContextHandlerRef ExternalLinkFragment::onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm )
271 switch( getCurrentElement() )
273 case XML_ROOT_CONTEXT:
274 if( nRecId == BIFF12_ID_EXTERNALBOOK )
276 mrExtLink.importExternalBook( getRelations(), rStrm );
277 return this;
279 break;
281 case BIFF12_ID_EXTERNALBOOK:
282 switch( nRecId )
284 case BIFF12_ID_EXTSHEETDATA:
285 if( mrExtLink.getLinkType() == ExternalLinkType::External )
286 return createSheetDataContext( rStrm.readInt32() );
287 break;
289 case BIFF12_ID_EXTSHEETNAMES: mrExtLink.importExtSheetNames( rStrm ); break;
290 case BIFF12_ID_EXTERNALNAME: mxExtName = mrExtLink.importExternalName( rStrm ); return this;
292 break;
294 case BIFF12_ID_EXTERNALNAME:
295 switch( nRecId )
297 case BIFF12_ID_EXTERNALNAMEFLAGS: if( mxExtName.get() ) mxExtName->importExternalNameFlags( rStrm ); break;
298 case BIFF12_ID_DDEITEMVALUES: if( mxExtName.get() ) mxExtName->importDdeItemValues( rStrm ); return this;
300 break;
302 case BIFF12_ID_DDEITEMVALUES:
303 switch( nRecId )
305 case BIFF12_ID_DDEITEM_BOOL: if( mxExtName.get() ) mxExtName->importDdeItemBool( rStrm ); break;
306 case BIFF12_ID_DDEITEM_DOUBLE: if( mxExtName.get() ) mxExtName->importDdeItemDouble( rStrm ); break;
307 case BIFF12_ID_DDEITEM_ERROR: if( mxExtName.get() ) mxExtName->importDdeItemError( rStrm ); break;
308 case BIFF12_ID_DDEITEM_STRING: if( mxExtName.get() ) mxExtName->importDdeItemString( rStrm ); break;
310 break;
312 return nullptr;
315 ContextHandlerRef ExternalLinkFragment::createSheetDataContext( sal_Int32 nSheetId )
317 return new ExternalSheetDataContext( *this, mrExtLink.getSheetCache( nSheetId ) );
320 const RecordInfo* ExternalLinkFragment::getRecordInfos() const
322 static const RecordInfo spRecInfos[] =
324 { BIFF12_ID_DDEITEMVALUES, BIFF12_ID_DDEITEMVALUES + 1 },
325 { BIFF12_ID_EXTERNALBOOK, BIFF12_ID_EXTERNALBOOK + 228 },
326 { BIFF12_ID_EXTERNALNAME, BIFF12_ID_EXTERNALNAME + 10 },
327 { BIFF12_ID_EXTROW, -1 },
328 { BIFF12_ID_EXTSHEETDATA, BIFF12_ID_EXTSHEETDATA + 1 },
329 { -1, -1 }
331 return spRecInfos;
334 } // namespace xls
335 } // namespace oox
337 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */