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