Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "externallinkfragment.hxx"
21 :
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"
28 :
29 : namespace oox {
30 : namespace xls {
31 :
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;
36 :
37 :
38 :
39 3 : ExternalSheetDataContext::ExternalSheetDataContext(
40 : WorkbookFragmentBase& rFragment, const Reference< XExternalSheetCache >& rxSheetCache )
41 : : WorkbookContextBase(rFragment)
42 : , mxSheetCache(rxSheetCache)
43 3 : , mnCurrType(XML_TOKEN_INVALID)
44 : {
45 : OSL_ENSURE( mxSheetCache.is(), "ExternalSheetDataContext::ExternalSheetDataContext - missing sheet cache" );
46 3 : }
47 :
48 12 : ContextHandlerRef ExternalSheetDataContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
49 : {
50 12 : switch( getCurrentElement() )
51 : {
52 : case XLS_TOKEN( sheetData ):
53 4 : if( nElement == XLS_TOKEN( row ) ) return this;
54 0 : break;
55 : case XLS_TOKEN( row ):
56 4 : if( nElement == XLS_TOKEN( cell ) ) { importCell( rAttribs ); return this; }
57 0 : break;
58 : case XLS_TOKEN( cell ):
59 4 : if( nElement == XLS_TOKEN( v ) ) return this; // collect characters in onCharacters()
60 0 : break;
61 : }
62 0 : return 0;
63 : }
64 :
65 4 : void ExternalSheetDataContext::onCharacters( const OUString& rChars )
66 : {
67 4 : if( isCurrentElement( XLS_TOKEN( v ) ) )
68 : {
69 4 : switch( mnCurrType )
70 : {
71 : case XML_b:
72 : case XML_n:
73 0 : setCellValue( Any( rChars.toDouble() ) );
74 0 : break;
75 : case XML_e:
76 0 : setCellValue( Any( BiffHelper::calcDoubleFromError( getUnitConverter().calcBiffErrorCode( rChars ) ) ) );
77 0 : break;
78 : case XML_str:
79 4 : setCellValue( Any( rChars ) );
80 4 : break;
81 : }
82 4 : mnCurrType = XML_TOKEN_INVALID;
83 : }
84 4 : }
85 :
86 0 : ContextHandlerRef ExternalSheetDataContext::onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm )
87 : {
88 0 : switch( getCurrentElement() )
89 : {
90 : case BIFF12_ID_EXTSHEETDATA:
91 0 : if( nRecId == BIFF12_ID_EXTROW ) { maCurrPos.Row = rStrm.readInt32(); return this; }
92 0 : break;
93 : case BIFF12_ID_EXTROW:
94 0 : switch( nRecId )
95 : {
96 0 : case BIFF12_ID_EXTCELL_BLANK: importExtCellBlank( rStrm ); break;
97 0 : case BIFF12_ID_EXTCELL_BOOL: importExtCellBool( rStrm ); break;
98 0 : case BIFF12_ID_EXTCELL_DOUBLE: importExtCellDouble( rStrm ); break;
99 0 : case BIFF12_ID_EXTCELL_ERROR: importExtCellError( rStrm ); break;
100 0 : case BIFF12_ID_EXTCELL_STRING: importExtCellString( rStrm ); break;
101 : }
102 0 : break;
103 : }
104 0 : return 0;
105 : }
106 :
107 : // private --------------------------------------------------------------------
108 :
109 4 : void ExternalSheetDataContext::importCell( const AttributeList& rAttribs )
110 : {
111 4 : if( getAddressConverter().convertToCellAddress( maCurrPos, rAttribs.getString( XML_r, OUString() ), 0, false ) )
112 4 : mnCurrType = rAttribs.getToken( XML_t, XML_n );
113 : else
114 0 : mnCurrType = XML_TOKEN_INVALID;
115 4 : }
116 :
117 0 : void ExternalSheetDataContext::importExtCellBlank( SequenceInputStream& rStrm )
118 : {
119 0 : maCurrPos.Column = rStrm.readInt32();
120 0 : setCellValue( Any( OUString() ) );
121 0 : }
122 :
123 0 : void ExternalSheetDataContext::importExtCellBool( SequenceInputStream& rStrm )
124 : {
125 0 : maCurrPos.Column = rStrm.readInt32();
126 0 : double fValue = (rStrm.readuInt8() == 0) ? 0.0 : 1.0;
127 0 : setCellValue( Any( fValue ) );
128 0 : }
129 :
130 0 : void ExternalSheetDataContext::importExtCellDouble( SequenceInputStream& rStrm )
131 : {
132 0 : maCurrPos.Column = rStrm.readInt32();
133 0 : setCellValue( Any( rStrm.readDouble() ) );
134 0 : }
135 :
136 0 : void ExternalSheetDataContext::importExtCellError( SequenceInputStream& rStrm )
137 : {
138 0 : maCurrPos.Column = rStrm.readInt32();
139 0 : setCellValue( Any( BiffHelper::calcDoubleFromError( rStrm.readuInt8() ) ) );
140 0 : }
141 :
142 0 : void ExternalSheetDataContext::importExtCellString( SequenceInputStream& rStrm )
143 : {
144 0 : maCurrPos.Column = rStrm.readInt32();
145 0 : setCellValue( Any( BiffHelper::readString( rStrm ) ) );
146 0 : }
147 :
148 4 : void ExternalSheetDataContext::setCellValue( const Any& rValue )
149 : {
150 4 : if( mxSheetCache.is() && getAddressConverter().checkCellAddress( maCurrPos, false ) ) try
151 : {
152 4 : mxSheetCache->setCellValue( maCurrPos.Column, maCurrPos.Row, rValue );
153 : }
154 0 : catch( Exception& )
155 : {
156 : }
157 4 : }
158 :
159 1 : ExternalLinkFragment::ExternalLinkFragment( const WorkbookHelper& rHelper,
160 : const OUString& rFragmentPath, ExternalLink& rExtLink ) :
161 : WorkbookFragmentBase( rHelper, rFragmentPath ),
162 : mrExtLink( rExtLink ),
163 1 : mnResultType( XML_TOKEN_INVALID )
164 : {
165 1 : }
166 :
167 10 : ContextHandlerRef ExternalLinkFragment::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
168 : {
169 10 : switch( getCurrentElement() )
170 : {
171 : case XML_ROOT_CONTEXT:
172 1 : if( nElement == XLS_TOKEN( externalLink ) ) return this;
173 0 : break;
174 :
175 : case XLS_TOKEN( externalLink ):
176 1 : switch( nElement )
177 : {
178 1 : case XLS_TOKEN( externalBook ): mrExtLink.importExternalBook( getRelations(), rAttribs ); return this;
179 0 : case XLS_TOKEN( ddeLink ): mrExtLink.importDdeLink( rAttribs ); return this;
180 0 : case XLS_TOKEN( oleLink ): mrExtLink.importOleLink( getRelations(), rAttribs ); return this;
181 : }
182 0 : break;
183 :
184 : case XLS_TOKEN( externalBook ):
185 2 : switch( nElement )
186 : {
187 : case XLS_TOKEN( sheetNames ):
188 : case XLS_TOKEN( definedNames ):
189 2 : case XLS_TOKEN( sheetDataSet ): return this;
190 : }
191 0 : break;
192 :
193 : case XLS_TOKEN( sheetNames ):
194 3 : if( nElement == XLS_TOKEN( sheetName ) ) mrExtLink.importSheetName( rAttribs );
195 3 : break;
196 : case XLS_TOKEN( definedNames ):
197 0 : if( nElement == XLS_TOKEN( definedName ) ) mrExtLink.importDefinedName( rAttribs );
198 0 : break;
199 : case XLS_TOKEN( sheetDataSet ):
200 3 : if( (nElement == XLS_TOKEN( sheetData )) && (mrExtLink.getLinkType() == LINKTYPE_EXTERNAL) )
201 3 : return createSheetDataContext( rAttribs.getInteger( XML_sheetId, -1 ) );
202 0 : break;
203 :
204 : case XLS_TOKEN( ddeLink ):
205 0 : if( nElement == XLS_TOKEN( ddeItems ) ) return this;
206 0 : break;
207 : case XLS_TOKEN( ddeItems ):
208 0 : if( nElement == XLS_TOKEN( ddeItem ) )
209 : {
210 0 : mxExtName = mrExtLink.importDdeItem( rAttribs );
211 0 : return this;
212 : }
213 0 : break;
214 : case XLS_TOKEN( ddeItem ):
215 0 : if( nElement == XLS_TOKEN( values ) )
216 : {
217 0 : if( mxExtName.get() ) mxExtName->importValues( rAttribs );
218 0 : return this;
219 : }
220 0 : break;
221 : case XLS_TOKEN( values ):
222 0 : if( nElement == XLS_TOKEN( value ) )
223 : {
224 0 : mnResultType = rAttribs.getToken( XML_t, XML_n );
225 0 : return this;
226 : }
227 0 : break;
228 : case XLS_TOKEN( value ):
229 0 : if( nElement == XLS_TOKEN( val ) ) return this; // collect value in onCharacters()
230 0 : break;
231 :
232 : case XLS_TOKEN( oleLink ):
233 0 : if( nElement == XLS_TOKEN( oleItems ) ) return this;
234 0 : break;
235 : case XLS_TOKEN( oleItems ):
236 0 : if( nElement == XLS_TOKEN( oleItem ) ) mxExtName = mrExtLink.importOleItem( rAttribs );
237 0 : break;
238 : }
239 3 : return 0;
240 : }
241 :
242 0 : void ExternalLinkFragment::onCharacters( const OUString& rChars )
243 : {
244 0 : if( isCurrentElement( XLS_TOKEN( val ) ) )
245 0 : maResultValue = rChars;
246 0 : }
247 :
248 4 : void ExternalLinkFragment::onEndElement()
249 : {
250 4 : if( isCurrentElement( XLS_TOKEN( value ) ) && mxExtName.get() ) switch( mnResultType )
251 : {
252 : case XML_b:
253 0 : mxExtName->appendResultValue( maResultValue.toDouble() );
254 0 : break;
255 : case XML_e:
256 0 : mxExtName->appendResultValue( BiffHelper::calcDoubleFromError( getUnitConverter().calcBiffErrorCode( maResultValue ) ) );
257 0 : break;
258 : case XML_n:
259 0 : mxExtName->appendResultValue( maResultValue.toDouble() );
260 0 : break;
261 : case XML_str:
262 0 : mxExtName->appendResultValue( maResultValue );
263 0 : break;
264 : default:
265 0 : mxExtName->appendResultValue( BiffHelper::calcDoubleFromError( BIFF_ERR_NA ) );
266 : }
267 4 : }
268 :
269 0 : ContextHandlerRef ExternalLinkFragment::onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm )
270 : {
271 0 : switch( getCurrentElement() )
272 : {
273 : case XML_ROOT_CONTEXT:
274 0 : if( nRecId == BIFF12_ID_EXTERNALBOOK )
275 : {
276 0 : mrExtLink.importExternalBook( getRelations(), rStrm );
277 0 : return this;
278 : }
279 0 : break;
280 :
281 : case BIFF12_ID_EXTERNALBOOK:
282 0 : switch( nRecId )
283 : {
284 : case BIFF12_ID_EXTSHEETDATA:
285 0 : if( mrExtLink.getLinkType() == LINKTYPE_EXTERNAL )
286 0 : return createSheetDataContext( rStrm.readInt32() );
287 0 : break;
288 :
289 0 : case BIFF12_ID_EXTSHEETNAMES: mrExtLink.importExtSheetNames( rStrm ); break;
290 0 : case BIFF12_ID_EXTERNALNAME: mxExtName = mrExtLink.importExternalName( rStrm ); return this;
291 : }
292 0 : break;
293 :
294 : case BIFF12_ID_EXTERNALNAME:
295 0 : switch( nRecId )
296 : {
297 0 : case BIFF12_ID_EXTERNALNAMEFLAGS: if( mxExtName.get() ) mxExtName->importExternalNameFlags( rStrm ); break;
298 0 : case BIFF12_ID_DDEITEMVALUES: if( mxExtName.get() ) mxExtName->importDdeItemValues( rStrm ); return this;
299 : }
300 0 : break;
301 :
302 : case BIFF12_ID_DDEITEMVALUES:
303 0 : switch( nRecId )
304 : {
305 0 : case BIFF12_ID_DDEITEM_BOOL: if( mxExtName.get() ) mxExtName->importDdeItemBool( rStrm ); break;
306 0 : case BIFF12_ID_DDEITEM_DOUBLE: if( mxExtName.get() ) mxExtName->importDdeItemDouble( rStrm ); break;
307 0 : case BIFF12_ID_DDEITEM_ERROR: if( mxExtName.get() ) mxExtName->importDdeItemError( rStrm ); break;
308 0 : case BIFF12_ID_DDEITEM_STRING: if( mxExtName.get() ) mxExtName->importDdeItemString( rStrm ); break;
309 : }
310 0 : break;
311 : }
312 0 : return 0;
313 : }
314 :
315 3 : ContextHandlerRef ExternalLinkFragment::createSheetDataContext( sal_Int32 nSheetId )
316 : {
317 3 : return new ExternalSheetDataContext( *this, mrExtLink.getSheetCache( nSheetId ) );
318 : }
319 :
320 0 : const RecordInfo* ExternalLinkFragment::getRecordInfos() const
321 : {
322 : static const RecordInfo spRecInfos[] =
323 : {
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 }
330 : };
331 0 : return spRecInfos;
332 : }
333 :
334 : } // namespace xls
335 18 : } // namespace oox
336 :
337 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|