Update ooo320-m1
[ooovba.git] / oox / source / xls / excelhandlers.cxx
blobfa72d9602fedade05b2550f8a4233cb3e5f17b75
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: excelhandlers.cxx,v $
10 * $Revision: 1.3.20.2 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "oox/xls/excelhandlers.hxx"
32 #include "oox/core/filterbase.hxx"
33 #include "oox/xls/biffinputstream.hxx"
35 using ::rtl::OUString;
36 using ::oox::core::FilterBase;
37 using ::oox::core::FragmentHandler2;
39 namespace oox {
40 namespace xls {
42 // ============================================================================
43 // ============================================================================
45 OoxWorkbookFragmentBase::OoxWorkbookFragmentBase(
46 const WorkbookHelper& rHelper, const OUString& rFragmentPath ) :
47 FragmentHandler2( rHelper.getOoxFilter(), rFragmentPath ),
48 WorkbookHelper( rHelper )
52 // ============================================================================
54 OoxWorksheetFragmentBase::OoxWorksheetFragmentBase( const WorkbookHelper& rHelper,
55 const OUString& rFragmentPath, ISegmentProgressBarRef xProgressBar, WorksheetType eSheetType, sal_Int16 nSheet ) :
56 FragmentHandler2( rHelper.getOoxFilter(), rFragmentPath ),
57 WorksheetHelperRoot( rHelper, xProgressBar, eSheetType, nSheet )
61 OoxWorksheetFragmentBase::OoxWorksheetFragmentBase(
62 const WorksheetHelper& rHelper, const OUString& rFragmentPath ) :
63 FragmentHandler2( rHelper.getOoxFilter(), rFragmentPath ),
64 WorksheetHelperRoot( rHelper )
68 // ============================================================================
69 // ============================================================================
71 namespace {
73 const sal_uInt16 BIFF_BOF_GLOBALS = 0x0005; /// BIFF5-BIFF8 workbook globals.
74 const sal_uInt16 BIFF_BOF_MODULE = 0x0006; /// BIFF5-BIFF8 Visual Basic module.
75 const sal_uInt16 BIFF_BOF_SHEET = 0x0010; /// BIFF2-BIFF8 worksheet/dialog sheet.
76 const sal_uInt16 BIFF_BOF_CHART = 0x0020; /// BIFF2-BIFF8 chart sheet.
77 const sal_uInt16 BIFF_BOF_MACRO = 0x0040; /// BIFF4-BIFF8 macro sheet.
78 const sal_uInt16 BIFF_BOF_WORKSPACE = 0x0100; /// BIFF3-BIFF8 workspace.
80 } // namespace
82 // ============================================================================
84 BiffHandlerBase::~BiffHandlerBase()
88 bool BiffHandlerBase::skipRecordBlock( sal_uInt16 nEndRecId )
90 sal_uInt16 nStartRecId = mrStrm.getRecId();
91 while( mrStrm.startNextRecord() && (mrStrm.getRecId() != nEndRecId) )
92 if( mrStrm.getRecId() == nStartRecId )
93 skipRecordBlock( nEndRecId );
94 return !mrStrm.isEof() && (mrStrm.getRecId() == nEndRecId);
97 bool BiffHandlerBase::isBofRecord() const
99 return
100 (mrStrm.getRecId() == BIFF2_ID_BOF) ||
101 (mrStrm.getRecId() == BIFF3_ID_BOF) ||
102 (mrStrm.getRecId() == BIFF4_ID_BOF) ||
103 (mrStrm.getRecId() == BIFF5_ID_BOF);
106 // ============================================================================
108 BiffContextHandler::BiffContextHandler( const BiffHandlerBase& rParent ) :
109 BiffHandlerBase( rParent )
113 // ============================================================================
115 namespace prv {
117 BiffFragmentStreamOwner::BiffFragmentStreamOwner( const FilterBase& rFilter, const OUString& rStrmName )
119 // do not automatically close the root stream (indicated by empty stream name)
120 mxXInStrm.reset( new BinaryXInputStream( rFilter.openInputStream( rStrmName ), rStrmName.getLength() > 0 ) );
121 mxBiffStrm.reset( new BiffInputStream( *mxXInStrm ) );
124 BiffFragmentStreamOwner::~BiffFragmentStreamOwner()
128 } // namespace prv
130 // ----------------------------------------------------------------------------
132 BiffFragmentHandler::BiffFragmentHandler( const FilterBase& rFilter, const OUString& rStrmName ) :
133 prv::BiffFragmentStreamOwner( rFilter, rStrmName ),
134 BiffHandlerBase( *mxBiffStrm )
138 BiffFragmentHandler::BiffFragmentHandler( const BiffFragmentHandler& rHandler ) :
139 prv::BiffFragmentStreamOwner( rHandler ),
140 BiffHandlerBase( rHandler )
144 BiffFragmentType BiffFragmentHandler::startFragment( BiffType eBiff )
146 return mrStrm.startNextRecord() ? implStartFragment( eBiff ) : BIFF_FRAGMENT_UNKNOWN;
149 BiffFragmentType BiffFragmentHandler::startFragment( BiffType eBiff, sal_Int64 nRecHandle )
151 return mrStrm.startRecordByHandle( nRecHandle ) ? implStartFragment( eBiff ) : BIFF_FRAGMENT_UNKNOWN;
154 bool BiffFragmentHandler::skipFragment()
156 while( mrStrm.startNextRecord() && (mrStrm.getRecId() != BIFF_ID_EOF) )
157 if( isBofRecord() )
158 skipFragment();
159 return !mrStrm.isEof() && (mrStrm.getRecId() == BIFF_ID_EOF);
162 BiffFragmentType BiffFragmentHandler::implStartFragment( BiffType eBiff )
164 BiffFragmentType eFragment = BIFF_FRAGMENT_UNKNOWN;
165 /* #i23425# Don't rely on BOF record ID to read BOF contents, but on
166 the detected BIFF version. */
167 if( isBofRecord() )
169 // BOF is always written unencrypted
170 mrStrm.enableDecoder( false );
171 mrStrm.skip( 2 );
172 sal_uInt16 nType = mrStrm.readuInt16();
174 // decide which fragment types are valid for current BIFF version
175 switch( eBiff )
177 case BIFF2: switch( nType )
179 case BIFF_BOF_CHART: eFragment = BIFF_FRAGMENT_EMPTYSHEET; break;
180 case BIFF_BOF_MACRO: eFragment = BIFF_FRAGMENT_MACROSHEET; break;
181 // #i51490# Excel interprets invalid types as worksheet
182 default: eFragment = BIFF_FRAGMENT_WORKSHEET;
184 break;
186 case BIFF3: switch( nType )
188 case BIFF_BOF_CHART: eFragment = BIFF_FRAGMENT_EMPTYSHEET; break;
189 case BIFF_BOF_MACRO: eFragment = BIFF_FRAGMENT_MACROSHEET; break;
190 case BIFF_BOF_WORKSPACE:eFragment = BIFF_FRAGMENT_UNKNOWN; break;
191 // #i51490# Excel interprets invalid types as worksheet
192 default: eFragment = BIFF_FRAGMENT_WORKSHEET;
194 break;
196 case BIFF4: switch( nType )
198 case BIFF_BOF_CHART: eFragment = BIFF_FRAGMENT_EMPTYSHEET; break;
199 case BIFF_BOF_MACRO: eFragment = BIFF_FRAGMENT_MACROSHEET; break;
200 case BIFF_BOF_WORKSPACE:eFragment = BIFF_FRAGMENT_WORKSPACE; break;
201 // #i51490# Excel interprets invalid types as worksheet
202 default: eFragment = BIFF_FRAGMENT_WORKSHEET;
204 break;
206 case BIFF5:
207 case BIFF8: switch( nType )
209 case BIFF_BOF_GLOBALS: eFragment = BIFF_FRAGMENT_GLOBALS; break;
210 case BIFF_BOF_CHART: eFragment = BIFF_FRAGMENT_CHARTSHEET; break;
211 case BIFF_BOF_MACRO: eFragment = BIFF_FRAGMENT_MACROSHEET; break;
212 case BIFF_BOF_MODULE: eFragment = BIFF_FRAGMENT_MODULESHEET; break;
213 case BIFF_BOF_WORKSPACE:eFragment = BIFF_FRAGMENT_UNKNOWN; break;
214 // #i51490# Excel interprets invalid types as worksheet
215 default: eFragment = BIFF_FRAGMENT_WORKSHEET;
217 break;
219 case BIFF_UNKNOWN: break;
222 return eFragment;
225 // ============================================================================
227 BiffWorkbookFragmentBase::BiffWorkbookFragmentBase( const WorkbookHelper& rHelper, const OUString& rStrmName, bool bCloneDecoder ) :
228 BiffFragmentHandler( rHelper.getBaseFilter(), rStrmName ),
229 WorkbookHelper( rHelper )
231 if( bCloneDecoder )
232 getCodecHelper().cloneDecoder( mrStrm );
235 // ============================================================================
237 BiffWorksheetFragmentBase::BiffWorksheetFragmentBase( const BiffWorkbookFragmentBase& rParent,
238 ISegmentProgressBarRef xProgressBar, WorksheetType eSheetType, sal_Int16 nSheet ) :
239 BiffFragmentHandler( rParent ),
240 WorksheetHelperRoot( rParent, xProgressBar, eSheetType, nSheet )
244 // ============================================================================
246 BiffSkipWorksheetFragment::BiffSkipWorksheetFragment(
247 const BiffWorkbookFragmentBase& rParent, ISegmentProgressBarRef xProgressBar, sal_Int16 nSheet ) :
248 BiffWorksheetFragmentBase( rParent, xProgressBar, SHEETTYPE_EMPTYSHEET, nSheet )
252 bool BiffSkipWorksheetFragment::importFragment()
254 return skipFragment();
257 // ============================================================================
258 // ============================================================================
260 } // namespace xls
261 } // namespace oox