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 <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>
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;
54 case XLS_TOKEN( row
):
55 if( nElement
== XLS_TOKEN( cell
) ) { importCell( rAttribs
); return this; }
57 case XLS_TOKEN( cell
):
58 if( nElement
== XLS_TOKEN( v
) ) return this; // collect characters in onCharacters()
64 void ExternalSheetDataContext::onCharacters( const OUString
& rChars
)
66 if( !isCurrentElement( XLS_TOKEN( v
) ) )
73 setCellValue( Any( rChars
.toDouble() ) );
76 setCellValue( Any( BiffHelper::calcDoubleFromError( getUnitConverter().calcBiffErrorCode( rChars
) ) ) );
79 setCellValue( Any( rChars
) );
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; }
92 case BIFF12_ID_EXTROW
:
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;
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
);
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
);
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;
174 case XLS_TOKEN( externalLink
):
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;
183 case XLS_TOKEN( externalBook
):
186 case XLS_TOKEN( sheetNames
):
187 case XLS_TOKEN( definedNames
):
188 case XLS_TOKEN( sheetDataSet
): return this;
192 case XLS_TOKEN( sheetNames
):
193 if( nElement
== XLS_TOKEN( sheetName
) ) mrExtLink
.importSheetName( rAttribs
);
195 case XLS_TOKEN( definedNames
):
196 if( nElement
== XLS_TOKEN( definedName
) ) mrExtLink
.importDefinedName( rAttribs
);
198 case XLS_TOKEN( sheetDataSet
):
199 if( (nElement
== XLS_TOKEN( sheetData
)) && (mrExtLink
.getLinkType() == ExternalLinkType::External
) )
200 return createSheetDataContext( rAttribs
.getInteger( XML_sheetId
, -1 ) );
203 case XLS_TOKEN( ddeLink
):
204 if( nElement
== XLS_TOKEN( ddeItems
) ) return this;
206 case XLS_TOKEN( ddeItems
):
207 if( nElement
== XLS_TOKEN( ddeItem
) )
209 mxExtName
= mrExtLink
.importDdeItem( rAttribs
);
213 case XLS_TOKEN( ddeItem
):
214 if( nElement
== XLS_TOKEN( values
) )
216 if( mxExtName
) mxExtName
->importValues( rAttribs
);
220 case XLS_TOKEN( values
):
221 if( nElement
== XLS_TOKEN( value
) )
223 mnResultType
= rAttribs
.getToken( XML_t
, XML_n
);
227 case XLS_TOKEN( value
):
228 if( nElement
== XLS_TOKEN( val
) ) return this; // collect value in onCharacters()
231 case XLS_TOKEN( oleLink
):
232 if( nElement
== XLS_TOKEN( oleItems
) ) return this;
234 case XLS_TOKEN( oleItems
):
235 if( nElement
== XLS_TOKEN( oleItem
) ) mxExtName
= mrExtLink
.importOleItem( rAttribs
);
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
) )
252 switch( mnResultType
)
255 mxExtName
->appendResultValue( maResultValue
.toDouble() );
258 mxExtName
->appendResultValue( BiffHelper::calcDoubleFromError( getUnitConverter().calcBiffErrorCode( maResultValue
) ) );
261 mxExtName
->appendResultValue( maResultValue
.toDouble() );
264 mxExtName
->appendResultValue( maResultValue
);
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
);
283 case BIFF12_ID_EXTERNALBOOK
:
286 case BIFF12_ID_EXTSHEETDATA
:
287 if( mrExtLink
.getLinkType() == ExternalLinkType::External
)
288 return createSheetDataContext( rStrm
.readInt32() );
291 case BIFF12_ID_EXTSHEETNAMES
: mrExtLink
.importExtSheetNames( rStrm
); break;
292 case BIFF12_ID_EXTERNALNAME
: mxExtName
= mrExtLink
.importExternalName( rStrm
); return this;
296 case BIFF12_ID_EXTERNALNAME
:
299 case BIFF12_ID_EXTERNALNAMEFLAGS
: if( mxExtName
) mxExtName
->importExternalNameFlags( rStrm
); break;
300 case BIFF12_ID_DDEITEMVALUES
: if( mxExtName
) mxExtName
->importDdeItemValues( rStrm
); return this;
304 case BIFF12_ID_DDEITEMVALUES
:
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;
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 },
336 } // namespace oox::xls
338 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */