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