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/excelfilter.hxx"
25cdf0e10cSrcweir
26cdf0e10cSrcweir #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
27cdf0e10cSrcweir #include "oox/dump/biffdumper.hxx"
28cdf0e10cSrcweir #include "oox/dump/xlsbdumper.hxx"
29cdf0e10cSrcweir #include "oox/helper/binaryinputstream.hxx"
30cdf0e10cSrcweir #include "oox/xls/biffdetector.hxx"
31cdf0e10cSrcweir #include "oox/xls/biffinputstream.hxx"
32cdf0e10cSrcweir #include "oox/xls/excelchartconverter.hxx"
33cdf0e10cSrcweir #include "oox/xls/excelvbaproject.hxx"
34cdf0e10cSrcweir #include "oox/xls/stylesbuffer.hxx"
35cdf0e10cSrcweir #include "oox/xls/themebuffer.hxx"
36cdf0e10cSrcweir #include "oox/xls/workbookfragment.hxx"
37cdf0e10cSrcweir
38cdf0e10cSrcweir namespace oox {
39cdf0e10cSrcweir namespace xls {
40cdf0e10cSrcweir
41cdf0e10cSrcweir // ============================================================================
42cdf0e10cSrcweir
43cdf0e10cSrcweir using namespace ::com::sun::star::lang;
44cdf0e10cSrcweir using namespace ::com::sun::star::sheet;
45cdf0e10cSrcweir using namespace ::com::sun::star::uno;
46cdf0e10cSrcweir using namespace ::com::sun::star::xml::sax;
47cdf0e10cSrcweir using namespace ::oox::core;
48cdf0e10cSrcweir
49cdf0e10cSrcweir using ::rtl::OUString;
50cdf0e10cSrcweir using ::oox::drawingml::table::TableStyleListPtr;
51cdf0e10cSrcweir
52cdf0e10cSrcweir // ============================================================================
53cdf0e10cSrcweir
ExcelFilterBase()54cdf0e10cSrcweir ExcelFilterBase::ExcelFilterBase() :
55cdf0e10cSrcweir mpBookGlob( 0 )
56cdf0e10cSrcweir {
57cdf0e10cSrcweir }
58cdf0e10cSrcweir
~ExcelFilterBase()59cdf0e10cSrcweir ExcelFilterBase::~ExcelFilterBase()
60cdf0e10cSrcweir {
61cdf0e10cSrcweir OSL_ENSURE( !mpBookGlob, "ExcelFilterBase::~ExcelFilterBase - workbook data not cleared" );
62cdf0e10cSrcweir }
63cdf0e10cSrcweir
registerWorkbookGlobals(WorkbookGlobals & rBookGlob)64cdf0e10cSrcweir void ExcelFilterBase::registerWorkbookGlobals( WorkbookGlobals& rBookGlob )
65cdf0e10cSrcweir {
66cdf0e10cSrcweir mpBookGlob = &rBookGlob;
67cdf0e10cSrcweir }
68cdf0e10cSrcweir
getWorkbookGlobals() const69cdf0e10cSrcweir WorkbookGlobals& ExcelFilterBase::getWorkbookGlobals() const
70cdf0e10cSrcweir {
71cdf0e10cSrcweir OSL_ENSURE( mpBookGlob, "ExcelFilterBase::getWorkbookGlobals - missing workbook data" );
72cdf0e10cSrcweir return *mpBookGlob;
73cdf0e10cSrcweir }
74cdf0e10cSrcweir
unregisterWorkbookGlobals()75cdf0e10cSrcweir void ExcelFilterBase::unregisterWorkbookGlobals()
76cdf0e10cSrcweir {
77cdf0e10cSrcweir mpBookGlob = 0;
78cdf0e10cSrcweir }
79cdf0e10cSrcweir
80cdf0e10cSrcweir // ============================================================================
81cdf0e10cSrcweir
ExcelFilter_getImplementationName()82cdf0e10cSrcweir OUString SAL_CALL ExcelFilter_getImplementationName() throw()
83cdf0e10cSrcweir {
84cdf0e10cSrcweir return CREATE_OUSTRING( "com.sun.star.comp.oox.xls.ExcelFilter" );
85cdf0e10cSrcweir }
86cdf0e10cSrcweir
ExcelFilter_getSupportedServiceNames()87cdf0e10cSrcweir Sequence< OUString > SAL_CALL ExcelFilter_getSupportedServiceNames() throw()
88cdf0e10cSrcweir {
89cdf0e10cSrcweir Sequence< OUString > aSeq( 2 );
90cdf0e10cSrcweir aSeq[ 0 ] = CREATE_OUSTRING( "com.sun.star.document.ImportFilter" );
91cdf0e10cSrcweir aSeq[ 1 ] = CREATE_OUSTRING( "com.sun.star.document.ExportFilter" );
92cdf0e10cSrcweir return aSeq;
93cdf0e10cSrcweir }
94cdf0e10cSrcweir
ExcelFilter_createInstance(const Reference<XComponentContext> & rxContext)95cdf0e10cSrcweir Reference< XInterface > SAL_CALL ExcelFilter_createInstance(
96cdf0e10cSrcweir const Reference< XComponentContext >& rxContext ) throw( Exception )
97cdf0e10cSrcweir {
98cdf0e10cSrcweir return static_cast< ::cppu::OWeakObject* >( new ExcelFilter( rxContext ) );
99cdf0e10cSrcweir }
100cdf0e10cSrcweir
101cdf0e10cSrcweir // ----------------------------------------------------------------------------
102cdf0e10cSrcweir
ExcelFilter(const Reference<XComponentContext> & rxContext)103cdf0e10cSrcweir ExcelFilter::ExcelFilter( const Reference< XComponentContext >& rxContext ) throw( RuntimeException ) :
104cdf0e10cSrcweir XmlFilterBase( rxContext )
105cdf0e10cSrcweir {
106cdf0e10cSrcweir }
107cdf0e10cSrcweir
~ExcelFilter()108cdf0e10cSrcweir ExcelFilter::~ExcelFilter()
109cdf0e10cSrcweir {
110cdf0e10cSrcweir }
111cdf0e10cSrcweir
importDocument()112cdf0e10cSrcweir bool ExcelFilter::importDocument() throw()
113cdf0e10cSrcweir {
114cdf0e10cSrcweir /* To activate the XLSX/XLSB dumper, insert the full path to the file
115cdf0e10cSrcweir file:///<path-to-oox-module>/source/dump/xlsbdumper.ini
116cdf0e10cSrcweir into the environment variable OOO_XLSBDUMPER and start the office with
117cdf0e10cSrcweir this variable (nonpro only). */
118cdf0e10cSrcweir OOX_DUMP_FILE( ::oox::dump::xlsb::Dumper );
119cdf0e10cSrcweir
120cdf0e10cSrcweir OUString aWorkbookPath = getFragmentPathFromFirstType( CREATE_OFFICEDOC_RELATION_TYPE( "officeDocument" ) );
121cdf0e10cSrcweir if( aWorkbookPath.getLength() == 0 )
122cdf0e10cSrcweir return false;
123cdf0e10cSrcweir
124cdf0e10cSrcweir /* Construct the WorkbookGlobals object referred to by every instance of
125cdf0e10cSrcweir the class WorkbookHelper, and execute the import filter by constructing
126cdf0e10cSrcweir an instance of WorkbookFragment and loading the file. */
127cdf0e10cSrcweir WorkbookGlobalsRef xBookGlob = WorkbookHelper::constructGlobals( *this );
128cdf0e10cSrcweir return xBookGlob.get() && importFragment( new WorkbookFragment( *xBookGlob, aWorkbookPath ) );
129cdf0e10cSrcweir }
130cdf0e10cSrcweir
exportDocument()131cdf0e10cSrcweir bool ExcelFilter::exportDocument() throw()
132cdf0e10cSrcweir {
133cdf0e10cSrcweir return false;
134cdf0e10cSrcweir }
135cdf0e10cSrcweir
getCurrentTheme() const136cdf0e10cSrcweir const ::oox::drawingml::Theme* ExcelFilter::getCurrentTheme() const
137cdf0e10cSrcweir {
138cdf0e10cSrcweir return &WorkbookHelper( getWorkbookGlobals() ).getTheme();
139cdf0e10cSrcweir }
140cdf0e10cSrcweir
getVmlDrawing()141cdf0e10cSrcweir ::oox::vml::Drawing* ExcelFilter::getVmlDrawing()
142cdf0e10cSrcweir {
143cdf0e10cSrcweir return 0;
144cdf0e10cSrcweir }
145cdf0e10cSrcweir
getTableStyles()146cdf0e10cSrcweir const TableStyleListPtr ExcelFilter::getTableStyles()
147cdf0e10cSrcweir {
148cdf0e10cSrcweir return TableStyleListPtr();
149cdf0e10cSrcweir }
150cdf0e10cSrcweir
getChartConverter()151cdf0e10cSrcweir ::oox::drawingml::chart::ChartConverter& ExcelFilter::getChartConverter()
152cdf0e10cSrcweir {
153cdf0e10cSrcweir return WorkbookHelper( getWorkbookGlobals() ).getChartConverter();
154cdf0e10cSrcweir }
155cdf0e10cSrcweir
implCreateGraphicHelper() const156cdf0e10cSrcweir GraphicHelper* ExcelFilter::implCreateGraphicHelper() const
157cdf0e10cSrcweir {
158cdf0e10cSrcweir return new ExcelGraphicHelper( getWorkbookGlobals() );
159cdf0e10cSrcweir }
160cdf0e10cSrcweir
implCreateVbaProject() const161cdf0e10cSrcweir ::oox::ole::VbaProject* ExcelFilter::implCreateVbaProject() const
162cdf0e10cSrcweir {
163cdf0e10cSrcweir return new ExcelVbaProject( getComponentContext(), Reference< XSpreadsheetDocument >( getModel(), UNO_QUERY ) );
164cdf0e10cSrcweir }
165cdf0e10cSrcweir
implGetImplementationName() const166cdf0e10cSrcweir OUString ExcelFilter::implGetImplementationName() const
167cdf0e10cSrcweir {
168cdf0e10cSrcweir return ExcelFilter_getImplementationName();
169cdf0e10cSrcweir }
170cdf0e10cSrcweir
171cdf0e10cSrcweir // ============================================================================
172cdf0e10cSrcweir
ExcelBiffFilter_getImplementationName()173cdf0e10cSrcweir OUString SAL_CALL ExcelBiffFilter_getImplementationName() throw()
174cdf0e10cSrcweir {
175cdf0e10cSrcweir return CREATE_OUSTRING( "com.sun.star.comp.oox.xls.ExcelBiffFilter" );
176cdf0e10cSrcweir }
177cdf0e10cSrcweir
ExcelBiffFilter_getSupportedServiceNames()178cdf0e10cSrcweir Sequence< OUString > SAL_CALL ExcelBiffFilter_getSupportedServiceNames() throw()
179cdf0e10cSrcweir {
180cdf0e10cSrcweir Sequence< OUString > aSeq( 2 );
181cdf0e10cSrcweir aSeq[ 0 ] = CREATE_OUSTRING( "com.sun.star.document.ImportFilter" );
182cdf0e10cSrcweir aSeq[ 1 ] = CREATE_OUSTRING( "com.sun.star.document.ExportFilter" );
183cdf0e10cSrcweir return aSeq;
184cdf0e10cSrcweir }
185cdf0e10cSrcweir
ExcelBiffFilter_createInstance(const Reference<XComponentContext> & rxContext)186cdf0e10cSrcweir Reference< XInterface > SAL_CALL ExcelBiffFilter_createInstance(
187cdf0e10cSrcweir const Reference< XComponentContext >& rxContext ) throw( Exception )
188cdf0e10cSrcweir {
189cdf0e10cSrcweir return static_cast< ::cppu::OWeakObject* >( new ExcelBiffFilter( rxContext ) );
190cdf0e10cSrcweir }
191cdf0e10cSrcweir
192cdf0e10cSrcweir // ----------------------------------------------------------------------------
193cdf0e10cSrcweir
ExcelBiffFilter(const Reference<XComponentContext> & rxContext)194cdf0e10cSrcweir ExcelBiffFilter::ExcelBiffFilter( const Reference< XComponentContext >& rxContext ) throw( RuntimeException ) :
195cdf0e10cSrcweir BinaryFilterBase( rxContext )
196cdf0e10cSrcweir {
197cdf0e10cSrcweir }
198cdf0e10cSrcweir
~ExcelBiffFilter()199cdf0e10cSrcweir ExcelBiffFilter::~ExcelBiffFilter()
200cdf0e10cSrcweir {
201cdf0e10cSrcweir }
202cdf0e10cSrcweir
importDocument()203cdf0e10cSrcweir bool ExcelBiffFilter::importDocument() throw()
204cdf0e10cSrcweir {
205cdf0e10cSrcweir /* To activate the BIFF dumper, insert the full path to the file
206cdf0e10cSrcweir file:///<path-to-oox-module>/source/dump/biffdumper.ini
207cdf0e10cSrcweir into the environment variable OOO_BIFFDUMPER and start the office with
208cdf0e10cSrcweir this variable (nonpro only). */
209cdf0e10cSrcweir OOX_DUMP_FILE( ::oox::dump::biff::Dumper );
210cdf0e10cSrcweir
211cdf0e10cSrcweir /* The boolean argument "UseBiffFilter" passed through XInitialisation
212cdf0e10cSrcweir decides whether to import/export the document with this filter (true),
213cdf0e10cSrcweir or to only use the BIFF file dumper implemented in this filter (false
214cdf0e10cSrcweir or missing) */
215cdf0e10cSrcweir Any aUseBiffFilter = getArgument( CREATE_OUSTRING( "UseBiffFilter" ) );
216cdf0e10cSrcweir bool bUseBiffFilter = false;
217cdf0e10cSrcweir if( !(aUseBiffFilter >>= bUseBiffFilter) || !bUseBiffFilter )
218cdf0e10cSrcweir return true;
219cdf0e10cSrcweir
220cdf0e10cSrcweir // detect BIFF version and workbook stream name
221cdf0e10cSrcweir OUString aWorkbookName;
222cdf0e10cSrcweir BiffType eBiff = BiffDetector::detectStorageBiffVersion( aWorkbookName, getStorage() );
223cdf0e10cSrcweir OSL_ENSURE( eBiff != BIFF_UNKNOWN, "ExcelBiffFilter::ExcelBiffFilter - invalid file format" );
224cdf0e10cSrcweir if( eBiff == BIFF_UNKNOWN )
225cdf0e10cSrcweir return false;
226cdf0e10cSrcweir
227cdf0e10cSrcweir /* Construct the WorkbookGlobals object referred to by every instance of
228cdf0e10cSrcweir the class WorkbookHelper, and execute the import filter by constructing
229cdf0e10cSrcweir an instance of BiffWorkbookFragment and loading the file. */
230cdf0e10cSrcweir WorkbookGlobalsRef xBookGlob = WorkbookHelper::constructGlobals( *this, eBiff );
231cdf0e10cSrcweir return xBookGlob.get() && BiffWorkbookFragment( *xBookGlob, aWorkbookName ).importFragment();
232cdf0e10cSrcweir }
233cdf0e10cSrcweir
exportDocument()234cdf0e10cSrcweir bool ExcelBiffFilter::exportDocument() throw()
235cdf0e10cSrcweir {
236cdf0e10cSrcweir return false;
237cdf0e10cSrcweir }
238cdf0e10cSrcweir
implCreateGraphicHelper() const239cdf0e10cSrcweir GraphicHelper* ExcelBiffFilter::implCreateGraphicHelper() const
240cdf0e10cSrcweir {
241cdf0e10cSrcweir return new ExcelGraphicHelper( getWorkbookGlobals() );
242cdf0e10cSrcweir }
243cdf0e10cSrcweir
implCreateVbaProject() const244cdf0e10cSrcweir ::oox::ole::VbaProject* ExcelBiffFilter::implCreateVbaProject() const
245cdf0e10cSrcweir {
246cdf0e10cSrcweir return new ExcelVbaProject( getComponentContext(), Reference< XSpreadsheetDocument >( getModel(), UNO_QUERY ) );
247cdf0e10cSrcweir }
248cdf0e10cSrcweir
implGetImplementationName() const249cdf0e10cSrcweir OUString ExcelBiffFilter::implGetImplementationName() const
250cdf0e10cSrcweir {
251cdf0e10cSrcweir return ExcelBiffFilter_getImplementationName();
252cdf0e10cSrcweir }
253cdf0e10cSrcweir
254cdf0e10cSrcweir // ============================================================================
255cdf0e10cSrcweir
ExcelVbaProjectFilter_getImplementationName()256cdf0e10cSrcweir OUString SAL_CALL ExcelVbaProjectFilter_getImplementationName() throw()
257cdf0e10cSrcweir {
258cdf0e10cSrcweir return CREATE_OUSTRING( "com.sun.star.comp.oox.xls.ExcelVbaProjectFilter" );
259cdf0e10cSrcweir }
260cdf0e10cSrcweir
ExcelVbaProjectFilter_getSupportedServiceNames()261cdf0e10cSrcweir Sequence< OUString > SAL_CALL ExcelVbaProjectFilter_getSupportedServiceNames() throw()
262cdf0e10cSrcweir {
263cdf0e10cSrcweir Sequence< OUString > aSeq( 1 );
264cdf0e10cSrcweir aSeq[ 0 ] = CREATE_OUSTRING( "com.sun.star.document.ImportFilter" );
265cdf0e10cSrcweir return aSeq;
266cdf0e10cSrcweir }
267cdf0e10cSrcweir
ExcelVbaProjectFilter_createInstance(const Reference<XComponentContext> & rxContext)268cdf0e10cSrcweir Reference< XInterface > SAL_CALL ExcelVbaProjectFilter_createInstance(
269cdf0e10cSrcweir const Reference< XComponentContext >& rxContext ) throw( Exception )
270cdf0e10cSrcweir {
271cdf0e10cSrcweir return static_cast< ::cppu::OWeakObject* >( new ExcelVbaProjectFilter( rxContext ) );
272cdf0e10cSrcweir }
273cdf0e10cSrcweir
274cdf0e10cSrcweir // ----------------------------------------------------------------------------
275cdf0e10cSrcweir
ExcelVbaProjectFilter(const Reference<XComponentContext> & rxContext)276cdf0e10cSrcweir ExcelVbaProjectFilter::ExcelVbaProjectFilter( const Reference< XComponentContext >& rxContext ) throw( RuntimeException ) :
277cdf0e10cSrcweir ExcelBiffFilter( rxContext )
278cdf0e10cSrcweir {
279cdf0e10cSrcweir }
280cdf0e10cSrcweir
importDocument()281cdf0e10cSrcweir bool ExcelVbaProjectFilter::importDocument() throw()
282cdf0e10cSrcweir {
283cdf0e10cSrcweir // detect BIFF version and workbook stream name
284cdf0e10cSrcweir OUString aWorkbookName;
285cdf0e10cSrcweir BiffType eBiff = BiffDetector::detectStorageBiffVersion( aWorkbookName, getStorage() );
286cdf0e10cSrcweir OSL_ENSURE( eBiff == BIFF8, "ExcelVbaProjectFilter::ExcelVbaProjectFilter - invalid file format" );
287cdf0e10cSrcweir if( eBiff != BIFF8 )
288cdf0e10cSrcweir return false;
289cdf0e10cSrcweir
290cdf0e10cSrcweir StorageRef xVbaPrjStrg = openSubStorage( CREATE_OUSTRING( "_VBA_PROJECT_CUR" ), false );
291cdf0e10cSrcweir if( !xVbaPrjStrg || !xVbaPrjStrg->isStorage() )
292cdf0e10cSrcweir return false;
293cdf0e10cSrcweir
294cdf0e10cSrcweir /* Construct the WorkbookGlobals object referred to by every instance of
295cdf0e10cSrcweir the class WorkbookHelper. */
296cdf0e10cSrcweir WorkbookGlobalsRef xBookGlob = WorkbookHelper::constructGlobals( *this, eBiff );
297cdf0e10cSrcweir if( !xBookGlob.get() )
298cdf0e10cSrcweir return false;
299cdf0e10cSrcweir
300cdf0e10cSrcweir // set palette colors passed in service constructor
301cdf0e10cSrcweir Any aPalette = getArgument( CREATE_OUSTRING( "ColorPalette" ) );
302cdf0e10cSrcweir WorkbookHelper( *xBookGlob ).getStyles().importPalette( aPalette );
303cdf0e10cSrcweir // import the VBA project (getVbaProject() implemented in base class)
304cdf0e10cSrcweir getVbaProject().importVbaProject( *xVbaPrjStrg, getGraphicHelper() );
305cdf0e10cSrcweir return true;
306cdf0e10cSrcweir }
307cdf0e10cSrcweir
exportDocument()308cdf0e10cSrcweir bool ExcelVbaProjectFilter::exportDocument() throw()
309cdf0e10cSrcweir {
310cdf0e10cSrcweir return false;
311cdf0e10cSrcweir }
312cdf0e10cSrcweir
implGetImplementationName() const313cdf0e10cSrcweir OUString ExcelVbaProjectFilter::implGetImplementationName() const
314cdf0e10cSrcweir {
315cdf0e10cSrcweir return ExcelVbaProjectFilter_getImplementationName();
316cdf0e10cSrcweir }
317cdf0e10cSrcweir
318cdf0e10cSrcweir // ============================================================================
319cdf0e10cSrcweir
320cdf0e10cSrcweir } // namespace xls
321cdf0e10cSrcweir } // namespace oox
322