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>
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;
55 case XLS_TOKEN( row
):
56 if( nElement
== XLS_TOKEN( cell
) ) { importCell( rAttribs
); return this; }
58 case XLS_TOKEN( cell
):
59 if( nElement
== XLS_TOKEN( v
) ) return this; // collect characters in onCharacters()
65 void ExternalSheetDataContext::onCharacters( const OUString
& rChars
)
67 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
;
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; }
93 case BIFF12_ID_EXTROW
:
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;
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
);
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
);
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;
175 case XLS_TOKEN( externalLink
):
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;
184 case XLS_TOKEN( externalBook
):
187 case XLS_TOKEN( sheetNames
):
188 case XLS_TOKEN( definedNames
):
189 case XLS_TOKEN( sheetDataSet
): return this;
193 case XLS_TOKEN( sheetNames
):
194 if( nElement
== XLS_TOKEN( sheetName
) ) mrExtLink
.importSheetName( rAttribs
);
196 case XLS_TOKEN( definedNames
):
197 if( nElement
== XLS_TOKEN( definedName
) ) mrExtLink
.importDefinedName( rAttribs
);
199 case XLS_TOKEN( sheetDataSet
):
200 if( (nElement
== XLS_TOKEN( sheetData
)) && (mrExtLink
.getLinkType() == ExternalLinkType::External
) )
201 return createSheetDataContext( rAttribs
.getInteger( XML_sheetId
, -1 ) );
204 case XLS_TOKEN( ddeLink
):
205 if( nElement
== XLS_TOKEN( ddeItems
) ) return this;
207 case XLS_TOKEN( ddeItems
):
208 if( nElement
== XLS_TOKEN( ddeItem
) )
210 mxExtName
= mrExtLink
.importDdeItem( rAttribs
);
214 case XLS_TOKEN( ddeItem
):
215 if( nElement
== XLS_TOKEN( values
) )
217 if( mxExtName
.get() ) mxExtName
->importValues( rAttribs
);
221 case XLS_TOKEN( values
):
222 if( nElement
== XLS_TOKEN( value
) )
224 mnResultType
= rAttribs
.getToken( XML_t
, XML_n
);
228 case XLS_TOKEN( value
):
229 if( nElement
== XLS_TOKEN( val
) ) return this; // collect value in onCharacters()
232 case XLS_TOKEN( oleLink
):
233 if( nElement
== XLS_TOKEN( oleItems
) ) return this;
235 case XLS_TOKEN( oleItems
):
236 if( nElement
== XLS_TOKEN( oleItem
) ) mxExtName
= mrExtLink
.importOleItem( rAttribs
);
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
)
253 mxExtName
->appendResultValue( maResultValue
.toDouble() );
256 mxExtName
->appendResultValue( BiffHelper::calcDoubleFromError( getUnitConverter().calcBiffErrorCode( maResultValue
) ) );
259 mxExtName
->appendResultValue( maResultValue
.toDouble() );
262 mxExtName
->appendResultValue( maResultValue
);
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
);
281 case BIFF12_ID_EXTERNALBOOK
:
284 case BIFF12_ID_EXTSHEETDATA
:
285 if( mrExtLink
.getLinkType() == ExternalLinkType::External
)
286 return createSheetDataContext( rStrm
.readInt32() );
289 case BIFF12_ID_EXTSHEETNAMES
: mrExtLink
.importExtSheetNames( rStrm
); break;
290 case BIFF12_ID_EXTERNALNAME
: mxExtName
= mrExtLink
.importExternalName( rStrm
); return this;
294 case BIFF12_ID_EXTERNALNAME
:
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;
302 case BIFF12_ID_DDEITEMVALUES
:
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;
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 },
337 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */