merge the formfield patch from ooo-build
[ooovba.git] / lotuswordpro / source / filter / schxmlwrapper.cxx
blob3ac4ee03034490d3de3a91577c617883a59e6cd0
1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * either of the following licenses
6 * - GNU Lesser General Public License Version 2.1
7 * - Sun Industry Standards Source License Version 1.1
9 * Sun Microsystems Inc., October, 2000
11 * GNU Lesser General Public License Version 2.1
12 * =============================================
13 * Copyright 2000 by Sun Microsystems, Inc.
14 * 901 San Antonio Road, Palo Alto, CA 94303, USA
16 * This library is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License version 2.1, as published by the Free Software Foundation.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 * MA 02111-1307 USA
31 * Sun Industry Standards Source License Version 1.1
32 * =================================================
33 * The contents of this file are subject to the Sun Industry Standards
34 * Source License Version 1.1 (the "License"); You may not use this file
35 * except in compliance with the License. You may obtain a copy of the
36 * License at http://www.openoffice.org/license.html.
38 * Software provided under this License is provided on an "AS IS" basis,
39 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
40 * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
41 * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
42 * See the License for the specific provisions governing your rights and
43 * obligations concerning the Software.
45 * The Initial Developer of the Original Code is: IBM Corporation
47 * Copyright: 2008 by IBM Corporation
49 * All Rights Reserved.
51 * Contributor(s): _______________________________________
54 ************************************************************************/
56 #ifndef _COM_SUN_STAR_BEANS_PROPERTYATTRIBUTE_HXX_
57 #include <com/sun/star/beans/PropertyAttribute.hpp>
58 #endif
59 #ifndef _COM_SUN_STAR_TASK_XSTATUSINDICATORSUPPLIER_HPP_
60 #include <com/sun/star/task/XStatusIndicatorSupplier.hpp>
61 #endif
62 #ifndef _COM_SUN_STAR_DOCUMENT_XIMPORTER_HPP_
63 #include <com/sun/star/document/XImporter.hpp>
64 #endif
65 #ifndef _COM_SUN_STAR_DOCUMENT_XFILTER_HPP_
66 #include <com/sun/star/document/XFilter.hpp>
67 #endif
68 #ifndef _COM_SUN_STAR_DOCUMENT_XExporter_HPP_
69 #include <com/sun/star/document/XExporter.hpp>
70 #endif
72 #ifndef _SFXDOCFILE_HXX
73 #include <sfx2/docfile.hxx>
74 #endif
75 #ifndef _SFXECODE_HXX
76 #include <svtools/sfxecode.hxx>
77 #endif
79 #include "schxmlwrapper.hxx"
81 #ifndef _UTL_STREAM_WRAPPER_HXX_
82 #include <unotools/streamwrap.hxx>
83 #endif
84 #ifndef _XMLGRHLP_HXX
85 #include <svx/xmlgrhlp.hxx>
86 #endif
87 #ifndef _TOOLS_DEBUG_HXX
88 #include <tools/debug.hxx>
89 #endif
90 #ifndef _COMPHELPER_PROCESSFACTORY_HXX_
91 #include <comphelper/processfactory.hxx>
92 #endif
94 #ifndef _COMPHELPER_GENERICPROPERTYSET_HXX_
95 #include <comphelper/genericpropertyset.hxx>
96 #endif
97 #ifndef _COMPHELPER_PROPERTSETINFO_HXX_
98 #include <comphelper/propertysetinfo.hxx>
99 #endif
101 #ifndef INCLUDED_SVTOOLS_SAVEOPT_HXX
102 #include <svtools/saveopt.hxx>
103 #endif
105 #ifndef _COM_SUN_STAR_XML_SAX_XERRORHANDLER_HPP_
106 #include <com/sun/star/xml/sax/XErrorHandler.hpp>
107 #endif
108 #ifndef _COM_SUN_STAR_XML_SAX_XENTITYRESOLVER_HPP_
109 #include <com/sun/star/xml/sax/XEntityResolver.hpp>
110 #endif
111 #ifndef _COM_SUN_STAR_XML_SAX_INPUTSOURCE_HPP_
112 #include <com/sun/star/xml/sax/InputSource.hpp>
113 #endif
114 #ifndef _COM_SUN_STAR_XML_SAX_XDTDHANDLER_HPP_
115 #include <com/sun/star/xml/sax/XDTDHandler.hpp>
116 #endif
117 #ifndef _COM_SUN_STAR_XML_SAX_XPARSER_HPP_
118 #include <com/sun/star/xml/sax/XParser.hpp>
119 #endif
120 #ifndef _COM_SUN_STAR_IO_XACTIVEDATASOURCE_HPP_
121 #include <com/sun/star/io/XActiveDataSource.hpp>
122 #endif
123 #ifndef _COM_SUN_STAR_IO_XACTIVEDATACONTROL_HPP_
124 #include <com/sun/star/io/XActiveDataControl.hpp>
125 #endif
126 #ifndef _COM_SUN_STAR_LANG_XMULTISERVICEFACTORY_HPP_
127 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
128 #endif
129 #ifndef _COM_SUN_STAR_XML_SAX_SAXPARSEEXCEPTION_HPP_
130 #include <com/sun/star/xml/sax/SAXParseException.hpp>
131 #endif
132 #ifndef _COM_SUN_STAR_PACKAGES_ZIP_ZIPIOEXCEPTION_HPP_
133 #include <com/sun/star/packages/zip/ZipIOException.hpp>
134 #endif
136 #define XML_STRING(i, x) sal_Char __READONLY_DATA i[sizeof(x)] = x
137 #define MAP_LEN(x) x, sizeof(x) - 1
139 XML_STRING( sXML_metaStreamName, "meta.xml");
140 XML_STRING( sXML_styleStreamName, "styles.xml" );
141 XML_STRING( sXML_contentStreamName, "content.xml" );
142 XML_STRING( sXML_oldContentStreamName, "Content.xml" );
144 XML_STRING( sXML_export_chart_meta_service, "com.sun.star.comp.Chart.XMLMetaExporter" );
145 XML_STRING( sXML_export_chart_styles_service, "com.sun.star.comp.Chart.XMLStylesExporter" );
146 XML_STRING( sXML_export_chart_content_service, "com.sun.star.comp.Chart.XMLContentExporter" );
147 XML_STRING( sXML_export_chart_old_content_service, "com.sun.star.office.sax.exporter.Chart" );
149 XML_STRING( sXML_export_chart_allinone_service, "com.sun.star.comp.Chart.XMLExporter" );
153 XML_STRING( sXML_import_chart_meta_service, "com.sun.star.comp.Chart.XMLMetaImporter" );
154 XML_STRING( sXML_import_chart_styles_service, "com.sun.star.comp.Chart.XMLStylesImporter" );
155 XML_STRING( sXML_import_chart_content_service, "com.sun.star.comp.Chart.XMLContentImporter" );
156 XML_STRING( sXML_import_chart_old_content_service, "com.sun.star.office.sax.importer.Chart" );
158 using namespace ::com::sun::star;
159 using namespace ::rtl;
160 using namespace comphelper;
162 SchXMLWrapper::SchXMLWrapper( uno::Reference< frame::XModel >& xModel,
163 SvStorage& rStorage,
164 sal_Bool bShowProgress ) :
165 mxModel( xModel ),
166 mrStorage( rStorage ),
167 mbShowProgress( bShowProgress )
171 sal_Int32 SchXMLWrapper::ImportStream(
172 const ::rtl::OUString& rsStreamName,
173 const ::rtl::OUString& rsServiceName,
174 uno::Reference< xml::sax::XParser >& xParser,
175 uno::Reference< lang::XMultiServiceFactory >& xServiceFactory,
176 uno::Reference< document::XGraphicObjectResolver >& xGraphObjResolver )
178 xml::sax::InputSource aParserInput;
179 SvStorageStreamRef rInpStream;
180 uno::Reference< io::XActiveDataSource > xSource;
181 sal_Bool bEncrypted = sal_False;
185 String sStreamName( rsStreamName );
186 if( ! mrStorage.IsStream( String( rsStreamName )))
187 return sal_False;
189 rInpStream = mrStorage.OpenStream( sStreamName, STREAM_READ | STREAM_NOCREATE );
190 if( ! rInpStream.Is())
191 return sal_False;
193 uno::Any aAny;
194 bEncrypted = rInpStream->GetProperty( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Encrypted" )), aAny )
195 && aAny.getValueType() == ::getBooleanCppuType()
196 && *(sal_Bool *)aAny.getValue();
198 aParserInput.aInputStream = rInpStream->GetXInputStream();
200 if( aParserInput.aInputStream.is() )
202 sal_Int32 nArgs = 0;
203 if( mxStatusIndicator.is())
204 nArgs++;
205 if( xGraphObjResolver.is())
206 nArgs++;
208 uno::Sequence< uno::Any > aArgs( nArgs );
210 nArgs = 0;
211 if( mxStatusIndicator.is())
212 aArgs[ nArgs++ ] <<= mxStatusIndicator;
214 if( xGraphObjResolver.is())
215 aArgs[ nArgs++ ] <<= xGraphObjResolver;
217 uno::Reference< xml::sax::XDocumentHandler > xDocHandler(
218 xServiceFactory->createInstanceWithArguments( rsServiceName, aArgs ),
219 uno::UNO_QUERY );
221 if( xDocHandler.is() )
223 uno::Reference< document::XImporter > xImporter( xDocHandler, uno::UNO_QUERY );
224 uno::Reference< lang::XComponent > xComponent( mxModel, uno::UNO_QUERY );
226 if( xImporter.is() )
228 xImporter->setTargetDocument( xComponent );
229 xParser->setDocumentHandler( xDocHandler );
231 xParser->parseStream( aParserInput );
236 catch( xml::sax::SAXParseException&)
238 if( bEncrypted )
239 return ERRCODE_SFX_WRONGPASSWORD;
240 return ERRCODE_SFX_GENERAL;
242 catch( xml::sax::SAXException&)
244 if( bEncrypted )
245 return ERRCODE_SFX_WRONGPASSWORD;
246 return ERRCODE_SFX_GENERAL;
248 catch( io::IOException&)
250 return ERRCODE_SFX_GENERAL;
252 catch( packages::zip::ZipIOException& )
254 return ERRCODE_IO_BROKENPACKAGE;
256 catch( uno::Exception&)
258 return ERRCODE_SFX_GENERAL;
261 return 0;
264 sal_Int32 SchXMLWrapper::Import()
266 sal_Int32 nWarning = 0;
268 if( !mxModel.is() )
270 DBG_ERROR("Got NO Model in XMLImport");
271 return sal_False;
274 uno::Reference<lang::XServiceInfo> xServiceInfo( mxModel, uno::UNO_QUERY );
276 if( !xServiceInfo.is() || !xServiceInfo->supportsService( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart.ChartDocument" ) ) ) )
278 DBG_ERROR( "Model is no ChartDocument in XMLImport" );
279 return sal_False;
282 uno::Reference< lang::XMultiServiceFactory> xServiceFactory( ::comphelper::getProcessServiceFactory() );
284 if( !xServiceFactory.is() )
286 DBG_ERROR( "XMLReader::Read: got no service manager" );
287 return sal_False;
290 // get the sax parser component
291 uno::Reference< xml::sax::XParser > xXMLParser(
292 xServiceFactory->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.xml.sax.Parser" )),
293 uno::UNO_QUERY );
295 if( ! xXMLParser.is() )
297 DBG_ERROR( "com.sun.star.xml.sax.Parser service missing" );
298 return sal_False;
301 // create graphics resolver component
302 SvXMLGraphicHelper* pGraphicHelper = SvXMLGraphicHelper::Create( mrStorage, GRAPHICHELPER_MODE_READ );
303 uno::Reference< document::XGraphicObjectResolver > xGraphObjResolver = pGraphicHelper;
305 // import meta information
306 ImportStream(
307 ::rtl::OUString::createFromAscii( sXML_metaStreamName ),
308 ::rtl::OUString::createFromAscii( sXML_import_chart_meta_service ),
309 xXMLParser, xServiceFactory, xGraphObjResolver );
311 // import styles
312 ImportStream(
313 ::rtl::OUString::createFromAscii( sXML_styleStreamName ),
314 ::rtl::OUString::createFromAscii( sXML_import_chart_styles_service ),
315 xXMLParser, xServiceFactory, xGraphObjResolver );
317 // import content
318 nWarning = ImportStream(
319 ::rtl::OUString::createFromAscii( sXML_contentStreamName ),
320 ::rtl::OUString::createFromAscii( sXML_import_chart_content_service ),
321 xXMLParser, xServiceFactory, xGraphObjResolver );
323 // import of "content.xml" didn't work - try old "Content.xml" stream
324 if( nWarning != 0 )
326 nWarning = ImportStream(
327 ::rtl::OUString::createFromAscii( sXML_oldContentStreamName ),
328 ::rtl::OUString::createFromAscii( sXML_import_chart_old_content_service ),
329 xXMLParser, xServiceFactory, xGraphObjResolver );
332 // graphics resolver has to be destroyed this way!
333 SvXMLGraphicHelper::Destroy( pGraphicHelper );
335 return nWarning;
338 // -----------------------------------------------------------------------------
340 sal_Bool SchXMLWrapper::ExportStream(
341 const ::rtl::OUString& rsStreamName,
342 const ::rtl::OUString& rsServiceName,
343 uno::Reference< io::XActiveDataSource >& xDataSource,
344 uno::Reference< lang::XMultiServiceFactory>& xServiceFactory,
345 uno::Sequence< uno::Any >& aArgs )
347 sal_Bool bRet = sal_False;
351 // create output stream
352 SvStorageStreamRef rOutputStream( mrStorage.OpenStream(
353 String( rsStreamName ), STREAM_WRITE | STREAM_SHARE_DENYWRITE | STREAM_TRUNC ));
355 rtl::OUString sMIMEType( RTL_CONSTASCII_USTRINGPARAM( "text/xml" ) );
356 uno::Any aAny;
357 aAny <<= sMIMEType;
358 rOutputStream->SetProperty( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MediaType" )), aAny );
360 // "Encrypted" means "Encryptable". Has to be set for all but the meta stream (which doesn't exist in charts)
361 aAny <<= (sal_Bool)(sal_True);
362 rOutputStream->SetProperty( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Encrypted" )), aAny );
364 rOutputStream->SetBufferSize( 0x4000 ); // 16*1024
366 if( xDataSource.is())
367 xDataSource->setOutputStream( new ::utl::OOutputStreamWrapper( *rOutputStream ));
369 uno::Reference< document::XFilter > xFilter(
370 xServiceFactory->createInstanceWithArguments( rsServiceName, aArgs ),
371 uno::UNO_QUERY );
373 if( xFilter.is())
375 uno::Reference< document::XExporter > xExporter( xFilter, uno::UNO_QUERY );
376 if( xExporter.is())
378 uno::Reference< lang::XComponent > xModelComponent( mxModel, uno::UNO_QUERY );
379 xExporter->setSourceDocument( xModelComponent );
382 // empty Descriptior (formerly FileName was given)
383 uno::Sequence< beans::PropertyValue > aEmptyDescriptor( 0 );
384 bRet = xFilter->filter( aEmptyDescriptor );
386 if( bRet && rOutputStream.Is())
387 rOutputStream->Commit();
390 catch( uno::Exception )
394 return bRet;
397 sal_Bool SchXMLWrapper::Export()
399 sal_Bool bRet = sal_False;
403 if( !mxModel.is() )
405 DBG_ERROR("Got NO Model in XMLExport");
406 return sal_False;
409 uno::Reference< lang::XServiceInfo > xServiceInfo( mxModel, uno::UNO_QUERY );
411 if( ! xServiceInfo.is() || !xServiceInfo->supportsService(
412 OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart.ChartDocument" ) ) ) )
414 DBG_ERROR( "Model is no ChartDocument in XMLExport" );
415 return sal_False;
418 uno::Reference< lang::XMultiServiceFactory> xServiceFactory( ::comphelper::getProcessServiceFactory() );
420 if( !xServiceFactory.is() )
422 DBG_ERROR( "got no service manager" );
423 return sal_False;
426 uno::Reference< uno::XInterface > xWriter( xServiceFactory->createInstance(
427 OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Writer" ) ) ) );
429 if( !xWriter.is() )
431 DBG_ERROR( "com.sun.star.xml.sax.Writer service missing" );
432 return sal_False;
434 uno::Reference<xml::sax::XDocumentHandler > xHandler( xWriter, uno::UNO_QUERY );
435 uno::Sequence< beans::PropertyValue > aEmptyDescriptor( 0 );
438 /** property map for export info set */
439 PropertyMapEntry aExportInfoMap[] =
441 { MAP_LEN( "UsePrettyPrinting"),0, &::getBooleanCppuType(), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0},
442 { NULL, 0, 0, NULL, 0, 0 }
445 uno::Reference< beans::XPropertySet > xInfoSet( GenericPropertySet_CreateInstance( new PropertySetInfo( aExportInfoMap ) ) );
447 SvtSaveOptions aSaveOpt;
448 OUString sUsePrettyPrinting(RTL_CONSTASCII_USTRINGPARAM("UsePrettyPrinting"));
449 sal_Bool bUsePrettyPrinting( aSaveOpt.IsPrettyPrinting() );
450 xInfoSet->setPropertyValue( sUsePrettyPrinting, uno::makeAny( bUsePrettyPrinting ) );
453 SvXMLGraphicHelper* pGraphicHelper = SvXMLGraphicHelper::Create( mrStorage, GRAPHICHELPER_MODE_WRITE, sal_False );
454 uno::Reference< document::XGraphicObjectResolver > xGraphObjResolver( pGraphicHelper );
455 uno::Reference< io::XActiveDataSource > xDataSource( xWriter, uno::UNO_QUERY );
457 sal_Int32 nArgs = 2;
458 if( mxStatusIndicator.is())
459 nArgs++;
460 if( xGraphObjResolver.is())
461 nArgs++;
463 uno::Sequence< uno::Any > aArgs( nArgs );
465 nArgs = 0;
466 aArgs[ nArgs++ ] <<= xHandler;
467 aArgs[ nArgs++ ] <<= xInfoSet;
469 if( mxStatusIndicator.is())
470 aArgs[ nArgs++ ] <<= mxStatusIndicator;
471 if( xGraphObjResolver.is())
472 aArgs[ nArgs++ ] <<= xGraphObjResolver;
474 //export to one stream
475 bRet = ExportStream(
476 ::rtl::OUString::createFromAscii( sXML_contentStreamName ),
477 ::rtl::OUString::createFromAscii( sXML_export_chart_allinone_service ),
478 xDataSource, xServiceFactory, aArgs );
480 // graphics resolver has to be destroyed this way!
481 SvXMLGraphicHelper::Destroy( pGraphicHelper );
483 catch( uno::Exception)
487 return bRet;