1*ca5ec200SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*ca5ec200SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*ca5ec200SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*ca5ec200SAndrew Rist * distributed with this work for additional information
6*ca5ec200SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*ca5ec200SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*ca5ec200SAndrew Rist * "License"); you may not use this file except in compliance
9*ca5ec200SAndrew Rist * with the License. You may obtain a copy of the License at
10*ca5ec200SAndrew Rist *
11*ca5ec200SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*ca5ec200SAndrew Rist *
13*ca5ec200SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*ca5ec200SAndrew Rist * software distributed under the License is distributed on an
15*ca5ec200SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ca5ec200SAndrew Rist * KIND, either express or implied. See the License for the
17*ca5ec200SAndrew Rist * specific language governing permissions and limitations
18*ca5ec200SAndrew Rist * under the License.
19*ca5ec200SAndrew Rist *
20*ca5ec200SAndrew Rist *************************************************************/
21*ca5ec200SAndrew Rist
22*ca5ec200SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #include "oox/xls/excelhandlers.hxx"
25cdf0e10cSrcweir
26cdf0e10cSrcweir #include "oox/core/filterbase.hxx"
27cdf0e10cSrcweir #include "oox/xls/biffinputstream.hxx"
28cdf0e10cSrcweir
29cdf0e10cSrcweir namespace oox {
30cdf0e10cSrcweir namespace xls {
31cdf0e10cSrcweir
32cdf0e10cSrcweir // ============================================================================
33cdf0e10cSrcweir
34cdf0e10cSrcweir using ::oox::core::FilterBase;
35cdf0e10cSrcweir using ::oox::core::FragmentHandler2;
36cdf0e10cSrcweir using ::rtl::OUString;
37cdf0e10cSrcweir
38cdf0e10cSrcweir // ============================================================================
39cdf0e10cSrcweir // ============================================================================
40cdf0e10cSrcweir
WorkbookFragmentBase(const WorkbookHelper & rHelper,const OUString & rFragmentPath)41cdf0e10cSrcweir WorkbookFragmentBase::WorkbookFragmentBase(
42cdf0e10cSrcweir const WorkbookHelper& rHelper, const OUString& rFragmentPath ) :
43cdf0e10cSrcweir FragmentHandler2( rHelper.getOoxFilter(), rFragmentPath ),
44cdf0e10cSrcweir WorkbookHelper( rHelper )
45cdf0e10cSrcweir {
46cdf0e10cSrcweir }
47cdf0e10cSrcweir
48cdf0e10cSrcweir // ============================================================================
49cdf0e10cSrcweir
WorksheetFragmentBase(const WorksheetHelper & rHelper,const OUString & rFragmentPath)50cdf0e10cSrcweir WorksheetFragmentBase::WorksheetFragmentBase(
51cdf0e10cSrcweir const WorksheetHelper& rHelper, const OUString& rFragmentPath ) :
52cdf0e10cSrcweir FragmentHandler2( rHelper.getOoxFilter(), rFragmentPath ),
53cdf0e10cSrcweir WorksheetHelper( rHelper )
54cdf0e10cSrcweir {
55cdf0e10cSrcweir }
56cdf0e10cSrcweir
57cdf0e10cSrcweir // ============================================================================
58cdf0e10cSrcweir // ============================================================================
59cdf0e10cSrcweir
~BiffContextHandler()60cdf0e10cSrcweir BiffContextHandler::~BiffContextHandler()
61cdf0e10cSrcweir {
62cdf0e10cSrcweir }
63cdf0e10cSrcweir
64cdf0e10cSrcweir // ----------------------------------------------------------------------------
65cdf0e10cSrcweir
BiffWorkbookContextBase(const WorkbookHelper & rHelper)66cdf0e10cSrcweir BiffWorkbookContextBase::BiffWorkbookContextBase( const WorkbookHelper& rHelper ) :
67cdf0e10cSrcweir WorkbookHelper( rHelper )
68cdf0e10cSrcweir {
69cdf0e10cSrcweir }
70cdf0e10cSrcweir
71cdf0e10cSrcweir // ----------------------------------------------------------------------------
72cdf0e10cSrcweir
BiffWorksheetContextBase(const WorksheetHelper & rHelper)73cdf0e10cSrcweir BiffWorksheetContextBase::BiffWorksheetContextBase( const WorksheetHelper& rHelper ) :
74cdf0e10cSrcweir WorksheetHelper( rHelper )
75cdf0e10cSrcweir {
76cdf0e10cSrcweir }
77cdf0e10cSrcweir
78cdf0e10cSrcweir // ============================================================================
79cdf0e10cSrcweir
80cdf0e10cSrcweir namespace {
81cdf0e10cSrcweir
82cdf0e10cSrcweir const sal_uInt16 BIFF_BOF_GLOBALS = 0x0005; /// BIFF5-BIFF8 workbook globals.
83cdf0e10cSrcweir const sal_uInt16 BIFF_BOF_MODULE = 0x0006; /// BIFF5-BIFF8 Visual Basic module.
84cdf0e10cSrcweir const sal_uInt16 BIFF_BOF_SHEET = 0x0010; /// BIFF2-BIFF8 worksheet/dialog sheet.
85cdf0e10cSrcweir const sal_uInt16 BIFF_BOF_CHART = 0x0020; /// BIFF2-BIFF8 chart sheet.
86cdf0e10cSrcweir const sal_uInt16 BIFF_BOF_MACRO = 0x0040; /// BIFF4-BIFF8 macro sheet.
87cdf0e10cSrcweir const sal_uInt16 BIFF_BOF_WORKSPACE = 0x0100; /// BIFF3-BIFF8 workspace.
88cdf0e10cSrcweir
89cdf0e10cSrcweir } // namespace
90cdf0e10cSrcweir
91cdf0e10cSrcweir // ----------------------------------------------------------------------------
92cdf0e10cSrcweir
BiffFragmentHandler(const FilterBase & rFilter,const OUString & rStrmName)93cdf0e10cSrcweir BiffFragmentHandler::BiffFragmentHandler( const FilterBase& rFilter, const OUString& rStrmName )
94cdf0e10cSrcweir {
95cdf0e10cSrcweir // do not automatically close the root stream (indicated by empty stream name)
96cdf0e10cSrcweir bool bRootStrm = rStrmName.getLength() == 0;
97cdf0e10cSrcweir mxXInStrm.reset( new BinaryXInputStream( rFilter.openInputStream( rStrmName ), !bRootStrm ) );
98cdf0e10cSrcweir mxBiffStrm.reset( new BiffInputStream( *mxXInStrm ) );
99cdf0e10cSrcweir }
100cdf0e10cSrcweir
~BiffFragmentHandler()101cdf0e10cSrcweir BiffFragmentHandler::~BiffFragmentHandler()
102cdf0e10cSrcweir {
103cdf0e10cSrcweir }
104cdf0e10cSrcweir
startFragment(BiffType eBiff)105cdf0e10cSrcweir BiffFragmentType BiffFragmentHandler::startFragment( BiffType eBiff )
106cdf0e10cSrcweir {
107cdf0e10cSrcweir BiffFragmentType eFragment = BIFF_FRAGMENT_UNKNOWN;
108cdf0e10cSrcweir /* #i23425# Don't rely on BOF record ID to read BOF contents, but on
109cdf0e10cSrcweir the detected BIFF version. */
110cdf0e10cSrcweir if( mxBiffStrm->startNextRecord() && BiffHelper::isBofRecord( *mxBiffStrm ) )
111cdf0e10cSrcweir {
112cdf0e10cSrcweir // BOF is always written unencrypted
113cdf0e10cSrcweir mxBiffStrm->enableDecoder( false );
114cdf0e10cSrcweir mxBiffStrm->skip( 2 );
115cdf0e10cSrcweir sal_uInt16 nType = mxBiffStrm->readuInt16();
116cdf0e10cSrcweir
117cdf0e10cSrcweir // decide which fragment types are valid for current BIFF version
118cdf0e10cSrcweir switch( eBiff )
119cdf0e10cSrcweir {
120cdf0e10cSrcweir case BIFF2: switch( nType )
121cdf0e10cSrcweir {
122cdf0e10cSrcweir case BIFF_BOF_CHART: eFragment = BIFF_FRAGMENT_EMPTYSHEET; break;
123cdf0e10cSrcweir case BIFF_BOF_MACRO: eFragment = BIFF_FRAGMENT_MACROSHEET; break;
124cdf0e10cSrcweir // #i51490# Excel interprets invalid types as worksheet
125cdf0e10cSrcweir default: eFragment = BIFF_FRAGMENT_WORKSHEET;
126cdf0e10cSrcweir }
127cdf0e10cSrcweir break;
128cdf0e10cSrcweir
129cdf0e10cSrcweir case BIFF3: switch( nType )
130cdf0e10cSrcweir {
131cdf0e10cSrcweir case BIFF_BOF_CHART: eFragment = BIFF_FRAGMENT_EMPTYSHEET; break;
132cdf0e10cSrcweir case BIFF_BOF_MACRO: eFragment = BIFF_FRAGMENT_MACROSHEET; break;
133cdf0e10cSrcweir case BIFF_BOF_WORKSPACE:eFragment = BIFF_FRAGMENT_UNKNOWN; break;
134cdf0e10cSrcweir // #i51490# Excel interprets invalid types as worksheet
135cdf0e10cSrcweir default: eFragment = BIFF_FRAGMENT_WORKSHEET;
136cdf0e10cSrcweir };
137cdf0e10cSrcweir break;
138cdf0e10cSrcweir
139cdf0e10cSrcweir case BIFF4: switch( nType )
140cdf0e10cSrcweir {
141cdf0e10cSrcweir case BIFF_BOF_CHART: eFragment = BIFF_FRAGMENT_EMPTYSHEET; break;
142cdf0e10cSrcweir case BIFF_BOF_MACRO: eFragment = BIFF_FRAGMENT_MACROSHEET; break;
143cdf0e10cSrcweir case BIFF_BOF_WORKSPACE:eFragment = BIFF_FRAGMENT_WORKSPACE; break;
144cdf0e10cSrcweir // #i51490# Excel interprets invalid types as worksheet
145cdf0e10cSrcweir default: eFragment = BIFF_FRAGMENT_WORKSHEET;
146cdf0e10cSrcweir };
147cdf0e10cSrcweir break;
148cdf0e10cSrcweir
149cdf0e10cSrcweir case BIFF5:
150cdf0e10cSrcweir case BIFF8: switch( nType )
151cdf0e10cSrcweir {
152cdf0e10cSrcweir case BIFF_BOF_GLOBALS: eFragment = BIFF_FRAGMENT_GLOBALS; break;
153cdf0e10cSrcweir case BIFF_BOF_CHART: eFragment = BIFF_FRAGMENT_CHARTSHEET; break;
154cdf0e10cSrcweir case BIFF_BOF_MACRO: eFragment = BIFF_FRAGMENT_MACROSHEET; break;
155cdf0e10cSrcweir case BIFF_BOF_MODULE: eFragment = BIFF_FRAGMENT_MODULESHEET; break;
156cdf0e10cSrcweir case BIFF_BOF_WORKSPACE:eFragment = BIFF_FRAGMENT_UNKNOWN; break;
157cdf0e10cSrcweir // #i51490# Excel interprets invalid types as worksheet
158cdf0e10cSrcweir default: eFragment = BIFF_FRAGMENT_WORKSHEET;
159cdf0e10cSrcweir };
160cdf0e10cSrcweir break;
161cdf0e10cSrcweir
162cdf0e10cSrcweir case BIFF_UNKNOWN: break;
163cdf0e10cSrcweir }
164cdf0e10cSrcweir }
165cdf0e10cSrcweir return eFragment;
166cdf0e10cSrcweir }
167cdf0e10cSrcweir
skipFragment()168cdf0e10cSrcweir bool BiffFragmentHandler::skipFragment()
169cdf0e10cSrcweir {
170cdf0e10cSrcweir while( mxBiffStrm->startNextRecord() && (mxBiffStrm->getRecId() != BIFF_ID_EOF) )
171cdf0e10cSrcweir if( BiffHelper::isBofRecord( *mxBiffStrm ) )
172cdf0e10cSrcweir skipFragment();
173cdf0e10cSrcweir return !mxBiffStrm->isEof() && (mxBiffStrm->getRecId() == BIFF_ID_EOF);
174cdf0e10cSrcweir }
175cdf0e10cSrcweir
176cdf0e10cSrcweir // ============================================================================
177cdf0e10cSrcweir
BiffWorkbookFragmentBase(const WorkbookHelper & rHelper,const OUString & rStrmName,bool bCloneDecoder)178cdf0e10cSrcweir BiffWorkbookFragmentBase::BiffWorkbookFragmentBase( const WorkbookHelper& rHelper, const OUString& rStrmName, bool bCloneDecoder ) :
179cdf0e10cSrcweir BiffFragmentHandler( rHelper.getBaseFilter(), rStrmName ),
180cdf0e10cSrcweir WorkbookHelper( rHelper )
181cdf0e10cSrcweir {
182cdf0e10cSrcweir if( bCloneDecoder )
183cdf0e10cSrcweir getCodecHelper().cloneDecoder( getInputStream() );
184cdf0e10cSrcweir }
185cdf0e10cSrcweir
186cdf0e10cSrcweir // ----------------------------------------------------------------------------
187cdf0e10cSrcweir
BiffWorksheetFragmentBase(const WorksheetHelper & rHelper,const BiffWorkbookFragmentBase & rParent)188cdf0e10cSrcweir BiffWorksheetFragmentBase::BiffWorksheetFragmentBase( const WorksheetHelper& rHelper, const BiffWorkbookFragmentBase& rParent ) :
189cdf0e10cSrcweir BiffFragmentHandler( rParent ),
190cdf0e10cSrcweir WorksheetHelper( rHelper )
191cdf0e10cSrcweir {
192cdf0e10cSrcweir }
193cdf0e10cSrcweir
194cdf0e10cSrcweir // ----------------------------------------------------------------------------
195cdf0e10cSrcweir
BiffSkipWorksheetFragment(const WorksheetHelper & rHelper,const BiffWorkbookFragmentBase & rParent)196cdf0e10cSrcweir BiffSkipWorksheetFragment::BiffSkipWorksheetFragment( const WorksheetHelper& rHelper, const BiffWorkbookFragmentBase& rParent ) :
197cdf0e10cSrcweir BiffWorksheetFragmentBase( rHelper, rParent )
198cdf0e10cSrcweir {
199cdf0e10cSrcweir }
200cdf0e10cSrcweir
importFragment()201cdf0e10cSrcweir bool BiffSkipWorksheetFragment::importFragment()
202cdf0e10cSrcweir {
203cdf0e10cSrcweir return skipFragment();
204cdf0e10cSrcweir }
205cdf0e10cSrcweir
206cdf0e10cSrcweir // ============================================================================
207cdf0e10cSrcweir // ============================================================================
208cdf0e10cSrcweir
209cdf0e10cSrcweir } // namespace xls
210cdf0e10cSrcweir } // namespace oox
211