Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include "externallinkfragment.hxx"
30 : :
31 : : #include <com/sun/star/sheet/XExternalSheetCache.hpp>
32 : : #include "oox/helper/attributelist.hxx"
33 : : #include "biffinputstream.hxx"
34 : : #include "defnamesbuffer.hxx"
35 : : #include "sheetdatacontext.hxx"
36 : : #include "unitconverter.hxx"
37 : :
38 : : namespace oox {
39 : : namespace xls {
40 : :
41 : : // ============================================================================
42 : :
43 : : using namespace ::com::sun::star::sheet;
44 : : using namespace ::com::sun::star::table;
45 : : using namespace ::com::sun::star::uno;
46 : : using namespace ::oox::core;
47 : :
48 : : using ::rtl::OUString;
49 : :
50 : : // ============================================================================
51 : : // ============================================================================
52 : :
53 : 0 : ExternalSheetDataContext::ExternalSheetDataContext(
54 : : WorkbookFragmentBase& rFragment, const Reference< XExternalSheetCache >& rxSheetCache ) :
55 : : WorkbookContextBase( rFragment ),
56 : 0 : mxSheetCache( rxSheetCache )
57 : : {
58 : : OSL_ENSURE( mxSheetCache.is(), "ExternalSheetDataContext::ExternalSheetDataContext - missing sheet cache" );
59 : 0 : }
60 : :
61 : 0 : ContextHandlerRef ExternalSheetDataContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
62 : : {
63 [ # # # # ]: 0 : switch( getCurrentElement() )
64 : : {
65 : : case XLS_TOKEN( sheetData ):
66 [ # # ]: 0 : if( nElement == XLS_TOKEN( row ) ) return this;
67 : 0 : break;
68 : : case XLS_TOKEN( row ):
69 [ # # ]: 0 : if( nElement == XLS_TOKEN( cell ) ) { importCell( rAttribs ); return this; }
70 : 0 : break;
71 : : case XLS_TOKEN( cell ):
72 [ # # ]: 0 : if( nElement == XLS_TOKEN( v ) ) return this; // collect characters in onCharacters()
73 : 0 : break;
74 : : }
75 : 0 : return 0;
76 : : }
77 : :
78 : 0 : void ExternalSheetDataContext::onCharacters( const OUString& rChars )
79 : : {
80 [ # # ]: 0 : if( isCurrentElement( XLS_TOKEN( v ) ) )
81 : : {
82 [ # # # # ]: 0 : switch( mnCurrType )
83 : : {
84 : : case XML_b:
85 : : case XML_n:
86 [ # # ][ # # ]: 0 : setCellValue( Any( rChars.toDouble() ) );
87 : 0 : break;
88 : : case XML_e:
89 [ # # ][ # # ]: 0 : setCellValue( Any( BiffHelper::calcDoubleFromError( getUnitConverter().calcBiffErrorCode( rChars ) ) ) );
90 : 0 : break;
91 : : case XML_str:
92 [ # # ]: 0 : setCellValue( Any( rChars ) );
93 : 0 : break;
94 : : }
95 : 0 : mnCurrType = XML_TOKEN_INVALID;
96 : : }
97 : 0 : }
98 : :
99 : 0 : ContextHandlerRef ExternalSheetDataContext::onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm )
100 : : {
101 [ # # # ]: 0 : switch( getCurrentElement() )
102 : : {
103 : : case BIFF12_ID_EXTSHEETDATA:
104 [ # # ]: 0 : if( nRecId == BIFF12_ID_EXTROW ) { maCurrPos.Row = rStrm.readInt32(); return this; }
105 : 0 : break;
106 : : case BIFF12_ID_EXTROW:
107 [ # # # # : 0 : switch( nRecId )
# # ]
108 : : {
109 : 0 : case BIFF12_ID_EXTCELL_BLANK: importExtCellBlank( rStrm ); break;
110 : 0 : case BIFF12_ID_EXTCELL_BOOL: importExtCellBool( rStrm ); break;
111 : 0 : case BIFF12_ID_EXTCELL_DOUBLE: importExtCellDouble( rStrm ); break;
112 : 0 : case BIFF12_ID_EXTCELL_ERROR: importExtCellError( rStrm ); break;
113 : 0 : case BIFF12_ID_EXTCELL_STRING: importExtCellString( rStrm ); break;
114 : : }
115 : 0 : break;
116 : : }
117 : 0 : return 0;
118 : : }
119 : :
120 : : // private --------------------------------------------------------------------
121 : :
122 : 0 : void ExternalSheetDataContext::importCell( const AttributeList& rAttribs )
123 : : {
124 [ # # ][ # # ]: 0 : if( getAddressConverter().convertToCellAddress( maCurrPos, rAttribs.getString( XML_r, OUString() ), 0, false ) )
[ # # ][ # # ]
125 : 0 : mnCurrType = rAttribs.getToken( XML_t, XML_n );
126 : : else
127 : 0 : mnCurrType = XML_TOKEN_INVALID;
128 : 0 : }
129 : :
130 : 0 : void ExternalSheetDataContext::importExtCellBlank( SequenceInputStream& rStrm )
131 : : {
132 : 0 : maCurrPos.Column = rStrm.readInt32();
133 [ # # ][ # # ]: 0 : setCellValue( Any( OUString() ) );
134 : 0 : }
135 : :
136 : 0 : void ExternalSheetDataContext::importExtCellBool( SequenceInputStream& rStrm )
137 : : {
138 [ # # ]: 0 : maCurrPos.Column = rStrm.readInt32();
139 [ # # ][ # # ]: 0 : double fValue = (rStrm.readuInt8() == 0) ? 0.0 : 1.0;
140 [ # # ][ # # ]: 0 : setCellValue( Any( fValue ) );
141 : 0 : }
142 : :
143 : 0 : void ExternalSheetDataContext::importExtCellDouble( SequenceInputStream& rStrm )
144 : : {
145 : 0 : maCurrPos.Column = rStrm.readInt32();
146 [ # # ][ # # ]: 0 : setCellValue( Any( rStrm.readDouble() ) );
147 : 0 : }
148 : :
149 : 0 : void ExternalSheetDataContext::importExtCellError( SequenceInputStream& rStrm )
150 : : {
151 : 0 : maCurrPos.Column = rStrm.readInt32();
152 [ # # ][ # # ]: 0 : setCellValue( Any( BiffHelper::calcDoubleFromError( rStrm.readuInt8() ) ) );
153 : 0 : }
154 : :
155 : 0 : void ExternalSheetDataContext::importExtCellString( SequenceInputStream& rStrm )
156 : : {
157 : 0 : maCurrPos.Column = rStrm.readInt32();
158 [ # # ][ # # ]: 0 : setCellValue( Any( BiffHelper::readString( rStrm ) ) );
159 : 0 : }
160 : :
161 : 0 : void ExternalSheetDataContext::setCellValue( const Any& rValue )
162 : : {
163 [ # # ][ # # ]: 0 : if( mxSheetCache.is() && getAddressConverter().checkCellAddress( maCurrPos, false ) ) try
[ # # ]
164 : : {
165 [ # # ][ # # ]: 0 : mxSheetCache->setCellValue( maCurrPos.Column, maCurrPos.Row, rValue );
166 : : }
167 : 0 : catch( Exception& )
168 : : {
169 : : }
170 [ # # ]: 0 : }
171 : :
172 : : // ============================================================================
173 : :
174 : 0 : ExternalLinkFragment::ExternalLinkFragment( const WorkbookHelper& rHelper,
175 : : const OUString& rFragmentPath, ExternalLink& rExtLink ) :
176 : : WorkbookFragmentBase( rHelper, rFragmentPath ),
177 : : mrExtLink( rExtLink ),
178 [ # # ]: 0 : mnResultType( XML_TOKEN_INVALID )
179 : : {
180 : 0 : }
181 : :
182 : 0 : ContextHandlerRef ExternalLinkFragment::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
183 : : {
184 [ # # # # : 0 : switch( getCurrentElement() )
# # # # #
# # # #
# ]
185 : : {
186 : : case XML_ROOT_CONTEXT:
187 [ # # ]: 0 : if( nElement == XLS_TOKEN( externalLink ) ) return this;
188 : 0 : break;
189 : :
190 : : case XLS_TOKEN( externalLink ):
191 [ # # # # ]: 0 : switch( nElement )
192 : : {
193 : 0 : case XLS_TOKEN( externalBook ): mrExtLink.importExternalBook( getRelations(), rAttribs ); return this;
194 : 0 : case XLS_TOKEN( ddeLink ): mrExtLink.importDdeLink( rAttribs ); return this;
195 : 0 : case XLS_TOKEN( oleLink ): mrExtLink.importOleLink( getRelations(), rAttribs ); return this;
196 : : }
197 : 0 : break;
198 : :
199 : : case XLS_TOKEN( externalBook ):
200 [ # # ]: 0 : switch( nElement )
201 : : {
202 : : case XLS_TOKEN( sheetNames ):
203 : : case XLS_TOKEN( definedNames ):
204 : 0 : case XLS_TOKEN( sheetDataSet ): return this;
205 : : }
206 : 0 : break;
207 : :
208 : : case XLS_TOKEN( sheetNames ):
209 [ # # ]: 0 : if( nElement == XLS_TOKEN( sheetName ) ) mrExtLink.importSheetName( rAttribs );
210 : 0 : break;
211 : : case XLS_TOKEN( definedNames ):
212 [ # # ]: 0 : if( nElement == XLS_TOKEN( definedName ) ) mrExtLink.importDefinedName( rAttribs );
213 : 0 : break;
214 : : case XLS_TOKEN( sheetDataSet ):
215 [ # # ][ # # ]: 0 : if( (nElement == XLS_TOKEN( sheetData )) && (mrExtLink.getLinkType() == LINKTYPE_EXTERNAL) )
[ # # ]
216 : 0 : return createSheetDataContext( rAttribs.getInteger( XML_sheetId, -1 ) );
217 : 0 : break;
218 : :
219 : : case XLS_TOKEN( ddeLink ):
220 [ # # ]: 0 : if( nElement == XLS_TOKEN( ddeItems ) ) return this;
221 : 0 : break;
222 : : case XLS_TOKEN( ddeItems ):
223 [ # # ]: 0 : if( nElement == XLS_TOKEN( ddeItem ) )
224 : : {
225 [ # # ]: 0 : mxExtName = mrExtLink.importDdeItem( rAttribs );
226 : 0 : return this;
227 : : }
228 : 0 : break;
229 : : case XLS_TOKEN( ddeItem ):
230 [ # # ]: 0 : if( nElement == XLS_TOKEN( values ) )
231 : : {
232 [ # # ]: 0 : if( mxExtName.get() ) mxExtName->importValues( rAttribs );
233 : 0 : return this;
234 : : }
235 : 0 : break;
236 : : case XLS_TOKEN( values ):
237 [ # # ]: 0 : if( nElement == XLS_TOKEN( value ) )
238 : : {
239 : 0 : mnResultType = rAttribs.getToken( XML_t, XML_n );
240 : 0 : return this;
241 : : }
242 : 0 : break;
243 : : case XLS_TOKEN( value ):
244 [ # # ]: 0 : if( nElement == XLS_TOKEN( val ) ) return this; // collect value in onCharacters()
245 : 0 : break;
246 : :
247 : : case XLS_TOKEN( oleLink ):
248 [ # # ]: 0 : if( nElement == XLS_TOKEN( oleItems ) ) return this;
249 : 0 : break;
250 : : case XLS_TOKEN( oleItems ):
251 [ # # ][ # # ]: 0 : if( nElement == XLS_TOKEN( oleItem ) ) mxExtName = mrExtLink.importOleItem( rAttribs );
252 : 0 : break;
253 : : }
254 : 0 : return 0;
255 : : }
256 : :
257 : 0 : void ExternalLinkFragment::onCharacters( const OUString& rChars )
258 : : {
259 [ # # ]: 0 : if( isCurrentElement( XLS_TOKEN( val ) ) )
260 : 0 : maResultValue = rChars;
261 : 0 : }
262 : :
263 : 0 : void ExternalLinkFragment::onEndElement()
264 : : {
265 [ # # ][ # # ]: 0 : if( isCurrentElement( XLS_TOKEN( value ) ) && mxExtName.get() ) switch( mnResultType )
[ # # ][ # #
# # # ]
266 : : {
267 : : case XML_b:
268 [ # # ]: 0 : mxExtName->appendResultValue( maResultValue.toDouble() );
269 : 0 : break;
270 : : case XML_e:
271 [ # # ]: 0 : mxExtName->appendResultValue( BiffHelper::calcDoubleFromError( getUnitConverter().calcBiffErrorCode( maResultValue ) ) );
272 : 0 : break;
273 : : case XML_n:
274 [ # # ]: 0 : mxExtName->appendResultValue( maResultValue.toDouble() );
275 : 0 : break;
276 : : case XML_str:
277 : 0 : mxExtName->appendResultValue( maResultValue );
278 : 0 : break;
279 : : default:
280 [ # # ]: 0 : mxExtName->appendResultValue( BiffHelper::calcDoubleFromError( BIFF_ERR_NA ) );
281 : : }
282 : 0 : }
283 : :
284 : 0 : ContextHandlerRef ExternalLinkFragment::onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm )
285 : : {
286 [ # # # # : 0 : switch( getCurrentElement() )
# ]
287 : : {
288 : : case XML_ROOT_CONTEXT:
289 [ # # ]: 0 : if( nRecId == BIFF12_ID_EXTERNALBOOK )
290 : : {
291 : 0 : mrExtLink.importExternalBook( getRelations(), rStrm );
292 : 0 : return this;
293 : : }
294 : 0 : break;
295 : :
296 : : case BIFF12_ID_EXTERNALBOOK:
297 [ # # # # ]: 0 : switch( nRecId )
298 : : {
299 : : case BIFF12_ID_EXTSHEETDATA:
300 [ # # ]: 0 : if( mrExtLink.getLinkType() == LINKTYPE_EXTERNAL )
301 : 0 : return createSheetDataContext( rStrm.readInt32() );
302 : 0 : break;
303 : :
304 : 0 : case BIFF12_ID_EXTSHEETNAMES: mrExtLink.importExtSheetNames( rStrm ); break;
305 [ # # ]: 0 : case BIFF12_ID_EXTERNALNAME: mxExtName = mrExtLink.importExternalName( rStrm ); return this;
306 : : }
307 : 0 : break;
308 : :
309 : : case BIFF12_ID_EXTERNALNAME:
310 [ # # # ]: 0 : switch( nRecId )
311 : : {
312 [ # # ]: 0 : case BIFF12_ID_EXTERNALNAMEFLAGS: if( mxExtName.get() ) mxExtName->importExternalNameFlags( rStrm ); break;
313 [ # # ]: 0 : case BIFF12_ID_DDEITEMVALUES: if( mxExtName.get() ) mxExtName->importDdeItemValues( rStrm ); return this;
314 : : }
315 : 0 : break;
316 : :
317 : : case BIFF12_ID_DDEITEMVALUES:
318 [ # # # # : 0 : switch( nRecId )
# ]
319 : : {
320 [ # # ]: 0 : case BIFF12_ID_DDEITEM_BOOL: if( mxExtName.get() ) mxExtName->importDdeItemBool( rStrm ); break;
321 [ # # ]: 0 : case BIFF12_ID_DDEITEM_DOUBLE: if( mxExtName.get() ) mxExtName->importDdeItemDouble( rStrm ); break;
322 [ # # ]: 0 : case BIFF12_ID_DDEITEM_ERROR: if( mxExtName.get() ) mxExtName->importDdeItemError( rStrm ); break;
323 [ # # ]: 0 : case BIFF12_ID_DDEITEM_STRING: if( mxExtName.get() ) mxExtName->importDdeItemString( rStrm ); break;
324 : : }
325 : 0 : break;
326 : : }
327 : 0 : return 0;
328 : : }
329 : :
330 : 0 : ContextHandlerRef ExternalLinkFragment::createSheetDataContext( sal_Int32 nSheetId )
331 : : {
332 [ # # ]: 0 : return new ExternalSheetDataContext( *this, mrExtLink.getSheetCache( nSheetId ) );
333 : : }
334 : :
335 : 0 : const RecordInfo* ExternalLinkFragment::getRecordInfos() const
336 : : {
337 : : static const RecordInfo spRecInfos[] =
338 : : {
339 : : { BIFF12_ID_DDEITEMVALUES, BIFF12_ID_DDEITEMVALUES + 1 },
340 : : { BIFF12_ID_EXTERNALBOOK, BIFF12_ID_EXTERNALBOOK + 228 },
341 : : { BIFF12_ID_EXTERNALNAME, BIFF12_ID_EXTERNALNAME + 10 },
342 : : { BIFF12_ID_EXTROW, -1 },
343 : : { BIFF12_ID_EXTSHEETDATA, BIFF12_ID_EXTSHEETDATA + 1 },
344 : : { -1, -1 }
345 : : };
346 : 0 : return spRecInfos;
347 : : }
348 : :
349 : : // ============================================================================
350 : :
351 : : } // namespace xls
352 [ + - ][ + - ]: 24 : } // namespace oox
353 : :
354 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|