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