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