Update git submodules
[LibreOffice.git] / sc / source / filter / oox / externallinkfragment.cxx
blob07fbece61698ce9cb0ade0f24cc646db97a820d3
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::xls {
34 using namespace ::com::sun::star::sheet;
35 using namespace ::com::sun::star::uno;
36 using namespace ::oox::core;
38 ExternalSheetDataContext::ExternalSheetDataContext(
39 WorkbookFragmentBase& rFragment, const Reference< XExternalSheetCache >& rxSheetCache )
40 : WorkbookContextBase(rFragment)
41 , mxSheetCache(rxSheetCache)
42 , mnCurrType(XML_TOKEN_INVALID)
44 OSL_ENSURE( mxSheetCache.is(), "ExternalSheetDataContext::ExternalSheetDataContext - missing sheet cache" );
47 ContextHandlerRef ExternalSheetDataContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
49 switch( getCurrentElement() )
51 case XLS_TOKEN( sheetData ):
52 if( nElement == XLS_TOKEN( row ) ) return this;
53 break;
54 case XLS_TOKEN( row ):
55 if( nElement == XLS_TOKEN( cell ) ) { importCell( rAttribs ); return this; }
56 break;
57 case XLS_TOKEN( cell ):
58 if( nElement == XLS_TOKEN( v ) ) return this; // collect characters in onCharacters()
59 break;
61 return nullptr;
64 void ExternalSheetDataContext::onCharacters( const OUString& rChars )
66 if( !isCurrentElement( XLS_TOKEN( v ) ) )
67 return;
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;
85 ContextHandlerRef ExternalSheetDataContext::onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm )
87 switch( getCurrentElement() )
89 case BIFF12_ID_EXTSHEETDATA:
90 if( nRecId == BIFF12_ID_EXTROW ) { maCurrPos.SetRow( rStrm.readInt32() ); return this; }
91 break;
92 case BIFF12_ID_EXTROW:
93 switch( nRecId )
95 case BIFF12_ID_EXTCELL_BLANK: importExtCellBlank( rStrm ); break;
96 case BIFF12_ID_EXTCELL_BOOL: importExtCellBool( rStrm ); break;
97 case BIFF12_ID_EXTCELL_DOUBLE: importExtCellDouble( rStrm ); break;
98 case BIFF12_ID_EXTCELL_ERROR: importExtCellError( rStrm ); break;
99 case BIFF12_ID_EXTCELL_STRING: importExtCellString( rStrm ); break;
101 break;
103 return nullptr;
106 // private --------------------------------------------------------------------
108 void ExternalSheetDataContext::importCell( const AttributeList& rAttribs )
110 if( getAddressConverter().convertToCellAddress( maCurrPos, rAttribs.getString( XML_r, OUString() ), 0, false ) )
111 mnCurrType = rAttribs.getToken( XML_t, XML_n );
112 else
113 mnCurrType = XML_TOKEN_INVALID;
116 void ExternalSheetDataContext::importExtCellBlank( SequenceInputStream& rStrm )
118 maCurrPos.SetCol( rStrm.readInt32() );
119 setCellValue( Any( OUString() ) );
122 void ExternalSheetDataContext::importExtCellBool( SequenceInputStream& rStrm )
124 maCurrPos.SetCol( rStrm.readInt32() );
125 double fValue = (rStrm.readuInt8() == 0) ? 0.0 : 1.0;
126 setCellValue( Any( fValue ) );
129 void ExternalSheetDataContext::importExtCellDouble( SequenceInputStream& rStrm )
131 maCurrPos.SetCol( rStrm.readInt32() );
132 setCellValue( Any( rStrm.readDouble() ) );
135 void ExternalSheetDataContext::importExtCellError( SequenceInputStream& rStrm )
137 maCurrPos.SetCol( rStrm.readInt32() );
138 setCellValue( Any( BiffHelper::calcDoubleFromError( rStrm.readuInt8() ) ) );
141 void ExternalSheetDataContext::importExtCellString( SequenceInputStream& rStrm )
143 maCurrPos.SetCol( rStrm.readInt32() );
144 setCellValue( Any( BiffHelper::readString( rStrm ) ) );
147 void ExternalSheetDataContext::setCellValue( const Any& rValue )
149 if( mxSheetCache.is() && getAddressConverter().checkCellAddress( maCurrPos, false ) ) try
151 mxSheetCache->setCellValue( maCurrPos.Col(), maCurrPos.Row(), rValue );
153 catch( Exception& )
158 ExternalLinkFragment::ExternalLinkFragment( const WorkbookHelper& rHelper,
159 const OUString& rFragmentPath, ExternalLink& rExtLink ) :
160 WorkbookFragmentBase( rHelper, rFragmentPath ),
161 mrExtLink( rExtLink ),
162 mnResultType( XML_TOKEN_INVALID )
166 ContextHandlerRef ExternalLinkFragment::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
168 switch( getCurrentElement() )
170 case XML_ROOT_CONTEXT:
171 if( nElement == XLS_TOKEN( externalLink ) ) return this;
172 break;
174 case XLS_TOKEN( externalLink ):
175 switch( nElement )
177 case XLS_TOKEN( externalBook ): mrExtLink.importExternalBook( getRelations(), rAttribs ); return this;
178 case XLS_TOKEN( ddeLink ): mrExtLink.importDdeLink( rAttribs ); return this;
179 case XLS_TOKEN( oleLink ): mrExtLink.importOleLink( getRelations(), rAttribs ); return this;
181 break;
183 case XLS_TOKEN( externalBook ):
184 switch( nElement )
186 case XLS_TOKEN( sheetNames ):
187 case XLS_TOKEN( definedNames ):
188 case XLS_TOKEN( sheetDataSet ): return this;
190 break;
192 case XLS_TOKEN( sheetNames ):
193 if( nElement == XLS_TOKEN( sheetName ) ) mrExtLink.importSheetName( rAttribs );
194 break;
195 case XLS_TOKEN( definedNames ):
196 if( nElement == XLS_TOKEN( definedName ) ) mrExtLink.importDefinedName( rAttribs );
197 break;
198 case XLS_TOKEN( sheetDataSet ):
199 if( (nElement == XLS_TOKEN( sheetData )) && (mrExtLink.getLinkType() == ExternalLinkType::External) )
200 return createSheetDataContext( rAttribs.getInteger( XML_sheetId, -1 ) );
201 break;
203 case XLS_TOKEN( ddeLink ):
204 if( nElement == XLS_TOKEN( ddeItems ) ) return this;
205 break;
206 case XLS_TOKEN( ddeItems ):
207 if( nElement == XLS_TOKEN( ddeItem ) )
209 mxExtName = mrExtLink.importDdeItem( rAttribs );
210 return this;
212 break;
213 case XLS_TOKEN( ddeItem ):
214 if( nElement == XLS_TOKEN( values ) )
216 if( mxExtName ) mxExtName->importValues( rAttribs );
217 return this;
219 break;
220 case XLS_TOKEN( values ):
221 if( nElement == XLS_TOKEN( value ) )
223 mnResultType = rAttribs.getToken( XML_t, XML_n );
224 return this;
226 break;
227 case XLS_TOKEN( value ):
228 if( nElement == XLS_TOKEN( val ) ) return this; // collect value in onCharacters()
229 break;
231 case XLS_TOKEN( oleLink ):
232 if( nElement == XLS_TOKEN( oleItems ) ) return this;
233 break;
234 case XLS_TOKEN( oleItems ):
235 if( nElement == XLS_TOKEN( oleItem ) ) mxExtName = mrExtLink.importOleItem( rAttribs );
236 break;
238 return nullptr;
241 void ExternalLinkFragment::onCharacters( const OUString& rChars )
243 if( isCurrentElement( XLS_TOKEN( val ) ) )
244 maResultValue = rChars;
247 void ExternalLinkFragment::onEndElement()
249 if( !(isCurrentElement( XLS_TOKEN( value ) ) && mxExtName) )
250 return;
252 switch( mnResultType )
254 case XML_b:
255 mxExtName->appendResultValue( maResultValue.toDouble() );
256 break;
257 case XML_e:
258 mxExtName->appendResultValue( BiffHelper::calcDoubleFromError( getUnitConverter().calcBiffErrorCode( maResultValue ) ) );
259 break;
260 case XML_n:
261 mxExtName->appendResultValue( maResultValue.toDouble() );
262 break;
263 case XML_str:
264 mxExtName->appendResultValue( maResultValue );
265 break;
266 default:
267 mxExtName->appendResultValue( BiffHelper::calcDoubleFromError( BIFF_ERR_NA ) );
271 ContextHandlerRef ExternalLinkFragment::onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm )
273 switch( getCurrentElement() )
275 case XML_ROOT_CONTEXT:
276 if( nRecId == BIFF12_ID_EXTERNALBOOK )
278 mrExtLink.importExternalBook( getRelations(), rStrm );
279 return this;
281 break;
283 case BIFF12_ID_EXTERNALBOOK:
284 switch( nRecId )
286 case BIFF12_ID_EXTSHEETDATA:
287 if( mrExtLink.getLinkType() == ExternalLinkType::External )
288 return createSheetDataContext( rStrm.readInt32() );
289 break;
291 case BIFF12_ID_EXTSHEETNAMES: mrExtLink.importExtSheetNames( rStrm ); break;
292 case BIFF12_ID_EXTERNALNAME: mxExtName = mrExtLink.importExternalName( rStrm ); return this;
294 break;
296 case BIFF12_ID_EXTERNALNAME:
297 switch( nRecId )
299 case BIFF12_ID_EXTERNALNAMEFLAGS: if( mxExtName ) mxExtName->importExternalNameFlags( rStrm ); break;
300 case BIFF12_ID_DDEITEMVALUES: if( mxExtName ) mxExtName->importDdeItemValues( rStrm ); return this;
302 break;
304 case BIFF12_ID_DDEITEMVALUES:
305 switch( nRecId )
307 case BIFF12_ID_DDEITEM_BOOL: if( mxExtName ) mxExtName->importDdeItemBool( rStrm ); break;
308 case BIFF12_ID_DDEITEM_DOUBLE: if( mxExtName ) mxExtName->importDdeItemDouble( rStrm ); break;
309 case BIFF12_ID_DDEITEM_ERROR: if( mxExtName ) mxExtName->importDdeItemError( rStrm ); break;
310 case BIFF12_ID_DDEITEM_STRING: if( mxExtName ) mxExtName->importDdeItemString( rStrm ); break;
312 break;
314 return nullptr;
317 ContextHandlerRef ExternalLinkFragment::createSheetDataContext( sal_Int32 nSheetId )
319 return new ExternalSheetDataContext( *this, mrExtLink.getSheetCache( nSheetId ) );
322 const RecordInfo* ExternalLinkFragment::getRecordInfos() const
324 static const RecordInfo spRecInfos[] =
326 { BIFF12_ID_DDEITEMVALUES, BIFF12_ID_DDEITEMVALUES + 1 },
327 { BIFF12_ID_EXTERNALBOOK, BIFF12_ID_EXTERNALBOOK + 228 },
328 { BIFF12_ID_EXTERNALNAME, BIFF12_ID_EXTERNALNAME + 10 },
329 { BIFF12_ID_EXTROW, -1 },
330 { BIFF12_ID_EXTSHEETDATA, BIFF12_ID_EXTSHEETDATA + 1 },
331 { -1, -1 }
333 return spRecInfos;
336 } // namespace oox::xls
338 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */