1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "biffinputstream.hxx"
25 #include "defnamesbuffer.hxx"
26 #include "sheetdatacontext.hxx"
27 #include "unitconverter.hxx"
32 using namespace ::com::sun::star::sheet
;
33 using namespace ::com::sun::star::table
;
34 using namespace ::com::sun::star::uno
;
35 using namespace ::oox::core
;
37 ExternalSheetDataContext::ExternalSheetDataContext(
38 WorkbookFragmentBase
& rFragment
, const Reference
< XExternalSheetCache
>& rxSheetCache
)
39 : WorkbookContextBase(rFragment
)
40 , mxSheetCache(rxSheetCache
)
41 , mnCurrType(XML_TOKEN_INVALID
)
43 OSL_ENSURE( mxSheetCache
.is(), "ExternalSheetDataContext::ExternalSheetDataContext - missing sheet cache" );
46 ContextHandlerRef
ExternalSheetDataContext::onCreateContext( sal_Int32 nElement
, const AttributeList
& rAttribs
)
48 switch( getCurrentElement() )
50 case XLS_TOKEN( sheetData
):
51 if( nElement
== XLS_TOKEN( row
) ) return this;
53 case XLS_TOKEN( row
):
54 if( nElement
== XLS_TOKEN( cell
) ) { importCell( rAttribs
); return this; }
56 case XLS_TOKEN( cell
):
57 if( nElement
== XLS_TOKEN( v
) ) return this; // collect characters in onCharacters()
63 void ExternalSheetDataContext::onCharacters( const OUString
& rChars
)
65 if( isCurrentElement( XLS_TOKEN( v
) ) )
71 setCellValue( Any( rChars
.toDouble() ) );
74 setCellValue( Any( BiffHelper::calcDoubleFromError( getUnitConverter().calcBiffErrorCode( rChars
) ) ) );
77 setCellValue( Any( rChars
) );
80 mnCurrType
= XML_TOKEN_INVALID
;
84 ContextHandlerRef
ExternalSheetDataContext::onCreateRecordContext( sal_Int32 nRecId
, SequenceInputStream
& rStrm
)
86 switch( getCurrentElement() )
88 case BIFF12_ID_EXTSHEETDATA
:
89 if( nRecId
== BIFF12_ID_EXTROW
) { maCurrPos
.Row
= rStrm
.readInt32(); return this; }
91 case BIFF12_ID_EXTROW
:
94 case BIFF12_ID_EXTCELL_BLANK
: importExtCellBlank( rStrm
); break;
95 case BIFF12_ID_EXTCELL_BOOL
: importExtCellBool( rStrm
); break;
96 case BIFF12_ID_EXTCELL_DOUBLE
: importExtCellDouble( rStrm
); break;
97 case BIFF12_ID_EXTCELL_ERROR
: importExtCellError( rStrm
); break;
98 case BIFF12_ID_EXTCELL_STRING
: importExtCellString( rStrm
); break;
105 // private --------------------------------------------------------------------
107 void ExternalSheetDataContext::importCell( const AttributeList
& rAttribs
)
109 if( getAddressConverter().convertToCellAddress( maCurrPos
, rAttribs
.getString( XML_r
, OUString() ), 0, false ) )
110 mnCurrType
= rAttribs
.getToken( XML_t
, XML_n
);
112 mnCurrType
= XML_TOKEN_INVALID
;
115 void ExternalSheetDataContext::importExtCellBlank( SequenceInputStream
& rStrm
)
117 maCurrPos
.Column
= rStrm
.readInt32();
118 setCellValue( Any( OUString() ) );
121 void ExternalSheetDataContext::importExtCellBool( SequenceInputStream
& rStrm
)
123 maCurrPos
.Column
= rStrm
.readInt32();
124 double fValue
= (rStrm
.readuInt8() == 0) ? 0.0 : 1.0;
125 setCellValue( Any( fValue
) );
128 void ExternalSheetDataContext::importExtCellDouble( SequenceInputStream
& rStrm
)
130 maCurrPos
.Column
= rStrm
.readInt32();
131 setCellValue( Any( rStrm
.readDouble() ) );
134 void ExternalSheetDataContext::importExtCellError( SequenceInputStream
& rStrm
)
136 maCurrPos
.Column
= rStrm
.readInt32();
137 setCellValue( Any( BiffHelper::calcDoubleFromError( rStrm
.readuInt8() ) ) );
140 void ExternalSheetDataContext::importExtCellString( SequenceInputStream
& rStrm
)
142 maCurrPos
.Column
= rStrm
.readInt32();
143 setCellValue( Any( BiffHelper::readString( rStrm
) ) );
146 void ExternalSheetDataContext::setCellValue( const Any
& rValue
)
148 if( mxSheetCache
.is() && getAddressConverter().checkCellAddress( maCurrPos
, false ) ) try
150 mxSheetCache
->setCellValue( maCurrPos
.Column
, maCurrPos
.Row
, rValue
);
157 ExternalLinkFragment::ExternalLinkFragment( const WorkbookHelper
& rHelper
,
158 const OUString
& rFragmentPath
, ExternalLink
& rExtLink
) :
159 WorkbookFragmentBase( rHelper
, rFragmentPath
),
160 mrExtLink( rExtLink
),
161 mnResultType( XML_TOKEN_INVALID
)
165 ContextHandlerRef
ExternalLinkFragment::onCreateContext( sal_Int32 nElement
, const AttributeList
& rAttribs
)
167 switch( getCurrentElement() )
169 case XML_ROOT_CONTEXT
:
170 if( nElement
== XLS_TOKEN( externalLink
) ) return this;
173 case XLS_TOKEN( externalLink
):
176 case XLS_TOKEN( externalBook
): mrExtLink
.importExternalBook( getRelations(), rAttribs
); return this;
177 case XLS_TOKEN( ddeLink
): mrExtLink
.importDdeLink( rAttribs
); return this;
178 case XLS_TOKEN( oleLink
): mrExtLink
.importOleLink( getRelations(), rAttribs
); return this;
182 case XLS_TOKEN( externalBook
):
185 case XLS_TOKEN( sheetNames
):
186 case XLS_TOKEN( definedNames
):
187 case XLS_TOKEN( sheetDataSet
): return this;
191 case XLS_TOKEN( sheetNames
):
192 if( nElement
== XLS_TOKEN( sheetName
) ) mrExtLink
.importSheetName( rAttribs
);
194 case XLS_TOKEN( definedNames
):
195 if( nElement
== XLS_TOKEN( definedName
) ) mrExtLink
.importDefinedName( rAttribs
);
197 case XLS_TOKEN( sheetDataSet
):
198 if( (nElement
== XLS_TOKEN( sheetData
)) && (mrExtLink
.getLinkType() == LINKTYPE_EXTERNAL
) )
199 return createSheetDataContext( rAttribs
.getInteger( XML_sheetId
, -1 ) );
202 case XLS_TOKEN( ddeLink
):
203 if( nElement
== XLS_TOKEN( ddeItems
) ) return this;
205 case XLS_TOKEN( ddeItems
):
206 if( nElement
== XLS_TOKEN( ddeItem
) )
208 mxExtName
= mrExtLink
.importDdeItem( rAttribs
);
212 case XLS_TOKEN( ddeItem
):
213 if( nElement
== XLS_TOKEN( values
) )
215 if( mxExtName
.get() ) mxExtName
->importValues( rAttribs
);
219 case XLS_TOKEN( values
):
220 if( nElement
== XLS_TOKEN( value
) )
222 mnResultType
= rAttribs
.getToken( XML_t
, XML_n
);
226 case XLS_TOKEN( value
):
227 if( nElement
== XLS_TOKEN( val
) ) return this; // collect value in onCharacters()
230 case XLS_TOKEN( oleLink
):
231 if( nElement
== XLS_TOKEN( oleItems
) ) return this;
233 case XLS_TOKEN( oleItems
):
234 if( nElement
== XLS_TOKEN( oleItem
) ) mxExtName
= mrExtLink
.importOleItem( rAttribs
);
240 void ExternalLinkFragment::onCharacters( const OUString
& rChars
)
242 if( isCurrentElement( XLS_TOKEN( val
) ) )
243 maResultValue
= rChars
;
246 void ExternalLinkFragment::onEndElement()
248 if( isCurrentElement( XLS_TOKEN( value
) ) && mxExtName
.get() ) switch( mnResultType
)
251 mxExtName
->appendResultValue( maResultValue
.toDouble() );
254 mxExtName
->appendResultValue( BiffHelper::calcDoubleFromError( getUnitConverter().calcBiffErrorCode( maResultValue
) ) );
257 mxExtName
->appendResultValue( maResultValue
.toDouble() );
260 mxExtName
->appendResultValue( maResultValue
);
263 mxExtName
->appendResultValue( BiffHelper::calcDoubleFromError( BIFF_ERR_NA
) );
267 ContextHandlerRef
ExternalLinkFragment::onCreateRecordContext( sal_Int32 nRecId
, SequenceInputStream
& rStrm
)
269 switch( getCurrentElement() )
271 case XML_ROOT_CONTEXT
:
272 if( nRecId
== BIFF12_ID_EXTERNALBOOK
)
274 mrExtLink
.importExternalBook( getRelations(), rStrm
);
279 case BIFF12_ID_EXTERNALBOOK
:
282 case BIFF12_ID_EXTSHEETDATA
:
283 if( mrExtLink
.getLinkType() == LINKTYPE_EXTERNAL
)
284 return createSheetDataContext( rStrm
.readInt32() );
287 case BIFF12_ID_EXTSHEETNAMES
: mrExtLink
.importExtSheetNames( rStrm
); break;
288 case BIFF12_ID_EXTERNALNAME
: mxExtName
= mrExtLink
.importExternalName( rStrm
); return this;
292 case BIFF12_ID_EXTERNALNAME
:
295 case BIFF12_ID_EXTERNALNAMEFLAGS
: if( mxExtName
.get() ) mxExtName
->importExternalNameFlags( rStrm
); break;
296 case BIFF12_ID_DDEITEMVALUES
: if( mxExtName
.get() ) mxExtName
->importDdeItemValues( rStrm
); return this;
300 case BIFF12_ID_DDEITEMVALUES
:
303 case BIFF12_ID_DDEITEM_BOOL
: if( mxExtName
.get() ) mxExtName
->importDdeItemBool( rStrm
); break;
304 case BIFF12_ID_DDEITEM_DOUBLE
: if( mxExtName
.get() ) mxExtName
->importDdeItemDouble( rStrm
); break;
305 case BIFF12_ID_DDEITEM_ERROR
: if( mxExtName
.get() ) mxExtName
->importDdeItemError( rStrm
); break;
306 case BIFF12_ID_DDEITEM_STRING
: if( mxExtName
.get() ) mxExtName
->importDdeItemString( rStrm
); break;
313 ContextHandlerRef
ExternalLinkFragment::createSheetDataContext( sal_Int32 nSheetId
)
315 return new ExternalSheetDataContext( *this, mrExtLink
.getSheetCache( nSheetId
) );
318 const RecordInfo
* ExternalLinkFragment::getRecordInfos() const
320 static const RecordInfo spRecInfos
[] =
322 { BIFF12_ID_DDEITEMVALUES
, BIFF12_ID_DDEITEMVALUES
+ 1 },
323 { BIFF12_ID_EXTERNALBOOK
, BIFF12_ID_EXTERNALBOOK
+ 228 },
324 { BIFF12_ID_EXTERNALNAME
, BIFF12_ID_EXTERNALNAME
+ 10 },
325 { BIFF12_ID_EXTROW
, -1 },
326 { BIFF12_ID_EXTSHEETDATA
, BIFF12_ID_EXTSHEETDATA
+ 1 },
335 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */