merge the formfield patch from ooo-build
[ooovba.git] / sw / source / filter / xml / wrtxml.cxx
blob87e42465045b1e16eb6210485d7a67e2bfa8ec66
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: wrtxml.cxx,v $
10 * $Revision: 1.62 $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
34 #include <com/sun/star/embed/XStorage.hpp>
35 #include <com/sun/star/embed/ElementModes.hpp>
36 #include <com/sun/star/container/XIndexContainer.hpp>
37 #include <com/sun/star/beans/PropertyAttribute.hpp>
38 #include <com/sun/star/task/XStatusIndicatorFactory.hpp>
39 #include <com/sun/star/io/XActiveDataSource.hpp>
40 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
41 #include <com/sun/star/document/XExporter.hpp>
42 #include <com/sun/star/document/XFilter.hpp>
43 #include <com/sun/star/frame/XModule.hpp>
44 #include <comphelper/processfactory.hxx>
45 #include <comphelper/genericpropertyset.hxx>
46 #include <unotools/streamwrap.hxx>
47 #include <svx/xmlgrhlp.hxx>
48 #include <svx/xmleohlp.hxx>
49 #include <svtools/saveopt.hxx>
50 #include <tools/urlobj.hxx>
51 #include <svtools/stritem.hxx>
52 #include <sfx2/frame.hxx>
53 #include <sfx2/docfile.hxx>
54 #include <pam.hxx>
55 #include <doc.hxx>
56 #include <docstat.hxx>
57 #include <docsh.hxx>
59 #include <unotools/ucbstreamhelper.hxx>
60 #include <errhdl.hxx>
61 #include <swerror.h>
62 #include <wrtxml.hxx>
63 #include <statstr.hrc>
64 #include <rtl/logfile.hxx>
66 #include <comphelper/documentconstants.hxx>
67 #include <comphelper/makesequence.hxx>
68 #include <com/sun/star/rdf/XDocumentMetadataAccess.hpp>
70 using ::rtl::OUString;
71 using namespace ::com::sun::star;
72 using namespace ::com::sun::star::uno;
73 using namespace ::com::sun::star::container;
74 using namespace ::com::sun::star::document;
75 using namespace ::com::sun::star::beans;
76 using namespace ::com::sun::star::lang;
78 #define LOGFILE_AUTHOR "mb93740"
80 SwXMLWriter::SwXMLWriter( const String& rBaseURL )
82 SetBaseURL( rBaseURL );
86 __EXPORT SwXMLWriter::~SwXMLWriter()
91 sal_uInt32 SwXMLWriter::_Write( SfxMedium* pTargetMedium )
93 DBG_ASSERT( pTargetMedium, "No medium is provided!" );
94 // Get service factory
95 uno::Reference< lang::XMultiServiceFactory > xServiceFactory =
96 comphelper::getProcessServiceFactory();
97 ASSERT( xServiceFactory.is(),
98 "SwXMLWriter::Write: got no service manager" );
99 if( !xServiceFactory.is() )
100 return ERR_SWG_WRITE_ERROR;
102 // Get data sink ...
103 uno::Reference< io::XOutputStream > xOut;
104 SvStorageStreamRef xDocStream;
105 uno::Reference< document::XGraphicObjectResolver > xGraphicResolver;
106 SvXMLGraphicHelper *pGraphicHelper = 0;
107 uno::Reference< document::XEmbeddedObjectResolver > xObjectResolver;
108 SvXMLEmbeddedObjectHelper *pObjectHelper = 0;
110 ASSERT( xStg.is(), "Where is my storage?" );
111 pGraphicHelper = SvXMLGraphicHelper::Create( xStg,
112 GRAPHICHELPER_MODE_WRITE,
113 sal_False );
114 xGraphicResolver = pGraphicHelper;
116 SfxObjectShell *pPersist = pDoc->GetPersist();
117 if( pPersist )
119 pObjectHelper = SvXMLEmbeddedObjectHelper::Create(
120 xStg, *pPersist,
121 EMBEDDEDOBJECTHELPER_MODE_WRITE,
122 sal_False );
123 xObjectResolver = pObjectHelper;
126 // create and prepare the XPropertySet that gets passed through
127 // the components, and the XStatusIndicator that shows progress to
128 // the user.
130 // create XPropertySet with three properties for status indicator
131 comphelper::PropertyMapEntry aInfoMap[] =
133 { "ProgressRange", sizeof("ProgressRange")-1, 0,
134 &::getCppuType((sal_Int32*)0),
135 beans::PropertyAttribute::MAYBEVOID, 0},
136 { "ProgressMax", sizeof("ProgressMax")-1, 0,
137 &::getCppuType((sal_Int32*)0),
138 beans::PropertyAttribute::MAYBEVOID, 0},
139 { "ProgressCurrent", sizeof("ProgressCurrent")-1, 0,
140 &::getCppuType((sal_Int32*)0),
141 beans::PropertyAttribute::MAYBEVOID, 0},
142 { "WrittenNumberStyles", sizeof("WrittenNumberStyles")-1, 0,
143 &::getCppuType((uno::Sequence<sal_Int32> *)0),
144 beans::PropertyAttribute::MAYBEVOID, 0},
145 { "UsePrettyPrinting", sizeof("UsePrettyPrinting")-1, 0,
146 &::getBooleanCppuType(),
147 beans::PropertyAttribute::MAYBEVOID, 0},
148 { "ShowChanges", sizeof("ShowChanges")-1, 0,
149 &::getBooleanCppuType(),
150 beans::PropertyAttribute::MAYBEVOID, 0 },
151 { "RedlineProtectionKey", sizeof("RedlineProtectionKey")-1, 0,
152 #if (defined(__SUNPRO_CC) && (__SUNPRO_CC == 0x500))
153 new uno::Type(::getCppuType((Sequence<sal_Int8>*)0)),
154 #else
155 &::getCppuType((Sequence<sal_Int8>*)0),
156 #endif
157 beans::PropertyAttribute::MAYBEVOID, 0 },
158 { "BaseURI", sizeof("BaseURI")-1, 0,
159 &::getCppuType( (OUString *)0 ),
160 beans::PropertyAttribute::MAYBEVOID, 0 },
161 { "StreamRelPath", sizeof("StreamRelPath")-1, 0,
162 &::getCppuType( (OUString *)0 ),
163 beans::PropertyAttribute::MAYBEVOID, 0 },
164 { "StreamName", sizeof("StreamName")-1, 0,
165 &::getCppuType( (OUString *)0 ),
166 beans::PropertyAttribute::MAYBEVOID, 0 },
167 { "AutoTextMode", sizeof("AutoTextMode")-1, 0,
168 &::getBooleanCppuType(),
169 beans::PropertyAttribute::MAYBEVOID, 0 },
170 { "StyleNames", sizeof("StyleNames")-1, 0,
171 &::getCppuType( (Sequence<OUString>*)0 ),
172 beans::PropertyAttribute::MAYBEVOID, 0 },
173 { "StyleFamilies", sizeof("StyleFamilies")-1, 0,
174 &::getCppuType( (Sequence<sal_Int32>*)0 ),
175 beans::PropertyAttribute::MAYBEVOID, 0 },
176 // --> OD 2006-09-26 #i69627#
177 { "OutlineStyleAsNormalListStyle", sizeof("OutlineStyleAsNormalListStyle")-1, 0,
178 &::getBooleanCppuType(),
179 beans::PropertyAttribute::MAYBEVOID, 0 },
180 // <--
181 { "TargetStorage", sizeof("TargetStorage")-1,0, &embed::XStorage::static_type(),
182 ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
184 { NULL, 0, 0, NULL, 0, 0 }
186 uno::Reference< beans::XPropertySet > xInfoSet(
187 comphelper::GenericPropertySet_CreateInstance(
188 new comphelper::PropertySetInfo( aInfoMap ) ) );
190 const OUString sTargetStorage( RTL_CONSTASCII_USTRINGPARAM("TargetStorage") );
191 xInfoSet->setPropertyValue( sTargetStorage, Any( xStg ) );
193 // create XStatusIndicator
194 uno::Reference<task::XStatusIndicator> xStatusIndicator;
196 uno::Any aAny;
197 if (bShowProgress)
199 // retrieve status indicator from the medium MediaDescriptor
200 if ( pTargetMedium )
202 const SfxUnoAnyItem* pStatusBarItem = static_cast<const SfxUnoAnyItem*>(
203 pTargetMedium->GetItemSet()->GetItem(SID_PROGRESS_STATUSBAR_CONTROL) );
205 if ( pStatusBarItem )
206 pStatusBarItem->GetValue() >>= xStatusIndicator;
209 // try
210 // {
211 // uno::Reference<frame::XModel> xModel( pDoc->GetDocShell()->GetModel());
212 // if (xModel.is())
213 // {
214 // uno::Sequence< beans::PropertyValue > xMediaDescr
215 // uno::Reference<frame::XController> xController(
216 // xModel->getCurrentController());
217 // if( xController.is())
218 // {
219 // uno::Reference<frame::XFrame> xFrame( xController->getFrame());
220 // if( xFrame.is())
221 // {
222 // uno::Reference<task::XStatusIndicatorFactory> xFactory(
223 // xFrame, uno::UNO_QUERY );
224 // if( xFactory.is())
225 // {
226 // xStatusIndicator =
227 // xFactory->createStatusIndicator();
228 // }
229 // }
230 // }
231 // }
232 // }
233 // catch( const RuntimeException& )
234 // {
235 // xStatusIndicator = 0;
236 // }
238 // set progress range and start status indicator
239 sal_Int32 nProgressRange(1000000);
240 if (xStatusIndicator.is())
242 xStatusIndicator->start(SW_RESSTR( STR_STATSTR_SWGWRITE),
243 nProgressRange);
245 aAny <<= nProgressRange;
246 OUString sProgressRange(RTL_CONSTASCII_USTRINGPARAM("ProgressRange"));
247 xInfoSet->setPropertyValue(sProgressRange, aAny);
249 aAny <<= static_cast < sal_Int32 >( -1 );
250 OUString sProgressMax(RTL_CONSTASCII_USTRINGPARAM("ProgressMax"));
251 xInfoSet->setPropertyValue(sProgressMax, aAny);
253 SvtSaveOptions aSaveOpt;
254 OUString sUsePrettyPrinting(RTL_CONSTASCII_USTRINGPARAM("UsePrettyPrinting"));
255 sal_Bool bUsePrettyPrinting( aSaveOpt.IsPrettyPrinting() );
256 aAny.setValue( &bUsePrettyPrinting, ::getBooleanCppuType() );
257 xInfoSet->setPropertyValue( sUsePrettyPrinting, aAny );
259 // save show redline mode ...
260 OUString sShowChanges(RTL_CONSTASCII_USTRINGPARAM("ShowChanges"));
261 sal_uInt16 nRedlineMode = pDoc->GetRedlineMode();
262 sal_Bool bShowChanges( IDocumentRedlineAccess::IsShowChanges( nRedlineMode ) );
263 aAny.setValue( &bShowChanges, ::getBooleanCppuType() );
264 xInfoSet->setPropertyValue( sShowChanges, aAny );
265 // ... and hide redlines for export
266 nRedlineMode &= ~nsRedlineMode_t::REDLINE_SHOW_MASK;
267 nRedlineMode |= nsRedlineMode_t::REDLINE_SHOW_INSERT;
268 pDoc->SetRedlineMode((RedlineMode_t)( nRedlineMode ));
270 // Set base URI
271 OUString sPropName( RTL_CONSTASCII_USTRINGPARAM("BaseURI") );
272 xInfoSet->setPropertyValue( sPropName, makeAny( ::rtl::OUString( GetBaseURL() ) ) );
274 if( SFX_CREATE_MODE_EMBEDDED == pDoc->GetDocShell()->GetCreateMode() )
276 OUString aName;
277 if ( pTargetMedium && pTargetMedium->GetItemSet() )
279 const SfxStringItem* pDocHierarchItem = static_cast<const SfxStringItem*>(
280 pTargetMedium->GetItemSet()->GetItem(SID_DOC_HIERARCHICALNAME) );
281 if ( pDocHierarchItem )
282 aName = pDocHierarchItem->GetValue();
284 else
285 aName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "dummyObjectName" ) );
287 if( aName.getLength() )
289 sPropName = OUString(RTL_CONSTASCII_USTRINGPARAM("StreamRelPath"));
290 xInfoSet->setPropertyValue( sPropName, makeAny( aName ) );
294 if( bBlock )
296 OUString sAutoTextMode(
297 RTL_CONSTASCII_USTRINGPARAM("AutoTextMode"));
298 sal_Bool bTmp = sal_True;
299 Any aAny2;
300 aAny2.setValue( &bTmp, ::getBooleanCppuType() );
301 xInfoSet->setPropertyValue( sAutoTextMode, aAny2 );
304 // --> OD 2006-09-26 #i69627#
305 const sal_Bool bOASIS = ( SotStorage::GetVersion( xStg ) > SOFFICE_FILEFORMAT_60 );
306 if ( bOASIS &&
307 docfunc::HasOutlineStyleToBeWrittenAsNormalListStyle( *pDoc ) )
309 OUString sOutlineStyleAsNormalListStyle(
310 RTL_CONSTASCII_USTRINGPARAM("OutlineStyleAsNormalListStyle") );
311 xInfoSet->setPropertyValue( sOutlineStyleAsNormalListStyle, makeAny( sal_True ) );
313 // <--
315 // filter arguments
316 // - graphics + object resolver for styles + content
317 // - status indicator
318 // - info property set
319 // - else empty
320 sal_Int32 nArgs = 1;
321 if( xStatusIndicator.is() )
322 nArgs++;
324 Sequence < Any > aEmptyArgs( nArgs );
325 Any *pArgs = aEmptyArgs.getArray();
326 *pArgs++ <<= xInfoSet;
327 if( xStatusIndicator.is() )
328 *pArgs++ <<= xStatusIndicator;
330 if( xGraphicResolver.is() )
331 nArgs++;
332 if( xObjectResolver.is() )
333 nArgs++;
335 Sequence < Any > aFilterArgs( nArgs );
336 pArgs = aFilterArgs.getArray();
337 *pArgs++ <<= xInfoSet;
338 if( xGraphicResolver.is() )
339 *pArgs++ <<= xGraphicResolver;
340 if( xObjectResolver.is() )
341 *pArgs++ <<= xObjectResolver;
342 if( xStatusIndicator.is() )
343 *pArgs++ <<= xStatusIndicator;
345 //Get model
346 uno::Reference< lang::XComponent > xModelComp(
347 pDoc->GetDocShell()->GetModel(), UNO_QUERY );
348 ASSERT( xModelComp.is(), "XMLWriter::Write: got no model" );
349 if( !xModelComp.is() )
350 return ERR_SWG_WRITE_ERROR;
352 PutNumFmtFontsInAttrPool();
353 PutEditEngFontsInAttrPool();
355 // properties
356 Sequence < PropertyValue > aProps( pOrigFileName ? 1 : 0 );
357 if( pOrigFileName )
359 PropertyValue *pProps = aProps.getArray();
360 pProps->Name = OUString( RTL_CONSTASCII_USTRINGPARAM("FileName") );
361 (pProps++)->Value <<= OUString( *pOrigFileName );
364 // export sub streams for package, else full stream into a file
365 sal_Bool bWarn = sal_False, bErr = sal_False;
366 String sWarnFile, sErrFile;
368 // RDF metadata: export if ODF >= 1.2
369 // N.B.: embedded documents have their own manifest.rdf!
370 if ( bOASIS )
372 const uno::Reference<beans::XPropertySet> xPropSet(xStg,
373 uno::UNO_QUERY_THROW);
374 const ::rtl::OUString VersionProp(
375 ::rtl::OUString::createFromAscii("Version"));
378 ::rtl::OUString Version;
379 // ODF >= 1.2
380 if ((xPropSet->getPropertyValue(VersionProp) >>= Version)
381 && !Version.equals(ODFVER_010_TEXT)
382 && !Version.equals(ODFVER_011_TEXT))
384 const uno::Reference<rdf::XDocumentMetadataAccess> xDMA(
385 xModelComp, uno::UNO_QUERY_THROW);
386 xDMA->storeMetadataToStorage(xStg);
389 catch (beans::UnknownPropertyException &)
390 { /* ignore */ }
391 catch (uno::Exception &)
393 bWarn = sal_True;
397 sal_Bool bStoreMeta = ( SFX_CREATE_MODE_EMBEDDED != pDoc->GetDocShell()->GetCreateMode() );
398 if ( !bStoreMeta )
402 Reference< frame::XModule > xModule( xModelComp, UNO_QUERY );
403 if ( xModule.is() )
405 ::rtl::OUString aModuleID = xModule->getIdentifier();
406 bStoreMeta = ( aModuleID.getLength()
407 && ( aModuleID.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sdb.FormDesign" ) ) )
408 || aModuleID.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sdb.TextReportDesign" ) ) ) ) );
411 catch( uno::Exception& )
415 if( !bOrganizerMode && !bBlock && bStoreMeta )
417 if( !WriteThroughComponent(
418 xModelComp, "meta.xml", xServiceFactory,
419 (bOASIS ? "com.sun.star.comp.Writer.XMLOasisMetaExporter"
420 : "com.sun.star.comp.Writer.XMLMetaExporter"),
421 aEmptyArgs, aProps, sal_True ) )
423 bWarn = sal_True;
424 sWarnFile = String( RTL_CONSTASCII_STRINGPARAM("meta.xml"),
425 RTL_TEXTENCODING_ASCII_US );
429 if( !bErr )
431 if( !bBlock )
433 if( !WriteThroughComponent(
434 xModelComp, "settings.xml", xServiceFactory,
435 (bOASIS ? "com.sun.star.comp.Writer.XMLOasisSettingsExporter"
436 : "com.sun.star.comp.Writer.XMLSettingsExporter"),
437 aEmptyArgs, aProps, sal_False ) )
439 if( !bWarn )
441 bWarn = sal_True;
442 sWarnFile = String( RTL_CONSTASCII_STRINGPARAM("settings.xml"),
443 RTL_TEXTENCODING_ASCII_US );
449 if( !WriteThroughComponent(
450 xModelComp, "styles.xml", xServiceFactory,
451 (bOASIS ? "com.sun.star.comp.Writer.XMLOasisStylesExporter"
452 : "com.sun.star.comp.Writer.XMLStylesExporter"),
453 aFilterArgs, aProps, sal_False ) )
455 bErr = sal_True;
456 sErrFile = String( RTL_CONSTASCII_STRINGPARAM("styles.xml"),
457 RTL_TEXTENCODING_ASCII_US );
461 if( !bOrganizerMode && !bErr )
463 if( !WriteThroughComponent(
464 xModelComp, "content.xml", xServiceFactory,
465 (bOASIS ? "com.sun.star.comp.Writer.XMLOasisContentExporter"
466 : "com.sun.star.comp.Writer.XMLContentExporter"),
467 aFilterArgs, aProps, sal_False ) )
469 bErr = sal_True;
470 sErrFile = String( RTL_CONSTASCII_STRINGPARAM("content.xml"),
471 RTL_TEXTENCODING_ASCII_US );
475 if( pDoc->GetRootFrm() && pDoc->GetDocStat().nPage > 1 &&
476 !(bOrganizerMode || bBlock || bErr) )
478 // DBG_ASSERT( !pDoc->GetDocStat().bModified,
479 // "doc stat is modified!" );
480 OUString sStreamName( RTL_CONSTASCII_USTRINGPARAM("layout-cache") );
483 uno::Reference < io::XStream > xStm = xStg->openStreamElement( sStreamName, embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE );
484 SvStream* pStream = utl::UcbStreamHelper::CreateStream( xStm );
485 if( !pStream->GetError() )
487 uno::Reference < beans::XPropertySet > xSet( xStm, UNO_QUERY );
488 String aPropName( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM("MediaType") ) );
489 OUString aMime( RTL_CONSTASCII_USTRINGPARAM("application/binary") );
490 uno::Any aAny2;
491 aAny2 <<= aMime;
492 xSet->setPropertyValue( aPropName, aAny2 );
493 pDoc->WriteLayoutCache( *pStream );
496 delete pStream;
498 catch ( uno::Exception& )
503 if( pGraphicHelper )
504 SvXMLGraphicHelper::Destroy( pGraphicHelper );
505 xGraphicResolver = 0;
507 if( pObjectHelper )
508 SvXMLEmbeddedObjectHelper::Destroy( pObjectHelper );
509 xObjectResolver = 0;
511 // restore redline mode
512 aAny = xInfoSet->getPropertyValue( sShowChanges );
513 nRedlineMode = pDoc->GetRedlineMode();
514 nRedlineMode &= ~nsRedlineMode_t::REDLINE_SHOW_MASK;
515 nRedlineMode |= nsRedlineMode_t::REDLINE_SHOW_INSERT;
516 if ( *(sal_Bool*)aAny.getValue() )
517 nRedlineMode |= nsRedlineMode_t::REDLINE_SHOW_DELETE;
518 pDoc->SetRedlineMode((RedlineMode_t)( nRedlineMode ));
520 if (xStatusIndicator.is())
522 xStatusIndicator->end();
525 if( bErr )
527 if( sErrFile.Len() )
528 return *new StringErrorInfo( ERR_WRITE_ERROR_FILE, sErrFile,
529 ERRCODE_BUTTON_OK | ERRCODE_MSG_ERROR );
530 else
531 return ERR_SWG_WRITE_ERROR;
533 else if( bWarn )
535 if( sWarnFile.Len() )
536 return *new StringErrorInfo( WARN_WRITE_ERROR_FILE, sWarnFile,
537 ERRCODE_BUTTON_OK | ERRCODE_MSG_ERROR );
538 else
539 return WARN_SWG_FEATURES_LOST;
542 return 0;
545 ULONG SwXMLWriter::WriteStorage()
547 return _Write();
550 ULONG SwXMLWriter::WriteMedium( SfxMedium& aTargetMedium )
552 return _Write( &aTargetMedium );
555 ULONG SwXMLWriter::Write( SwPaM& rPaM, SfxMedium& rMed,
556 const String* pFileName )
558 return IsStgWriter()
559 ? ((StgWriter *)this)->Write( rPaM, rMed.GetOutputStorage(), pFileName, &rMed )
560 : ((Writer *)this)->Write( rPaM, *rMed.GetOutStream(), pFileName );
563 sal_Bool SwXMLWriter::WriteThroughComponent(
564 const uno::Reference<XComponent> & xComponent,
565 const sal_Char* pStreamName,
566 const uno::Reference<lang::XMultiServiceFactory> & rFactory,
567 const sal_Char* pServiceName,
568 const Sequence<Any> & rArguments,
569 const Sequence<beans::PropertyValue> & rMediaDesc,
570 sal_Bool bPlainStream )
572 DBG_ASSERT( xStg.is(), "Need storage!" );
573 DBG_ASSERT( NULL != pStreamName, "Need stream name!" );
574 DBG_ASSERT( NULL != pServiceName, "Need service name!" );
576 RTL_LOGFILE_TRACE_AUTHOR1( "sw", LOGFILE_AUTHOR,
577 "SwXMLWriter::WriteThroughComponent : stream %s",
578 pStreamName );
580 // open stream
581 sal_Bool bRet = sal_False;
584 OUString sStreamName = OUString::createFromAscii( pStreamName );
585 uno::Reference<io::XStream> xStream =
586 xStg->openStreamElement( sStreamName,
587 embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE );
589 uno::Reference <beans::XPropertySet > xSet( xStream, uno::UNO_QUERY );
590 if( !xSet.is() )
591 return sal_False;
593 String aPropName( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM("MediaType") ) );
594 OUString aMime( RTL_CONSTASCII_USTRINGPARAM("text/xml") );
595 uno::Any aAny;
596 aAny <<= aMime;
597 xSet->setPropertyValue( aPropName, aAny );
599 OUString aUseCommonPassPropName( RTL_CONSTASCII_USTRINGPARAM("UseCommonStoragePasswordEncryption") );
600 if( bPlainStream )
602 OUString aCompressPropName( RTL_CONSTASCII_USTRINGPARAM("Compressed") );
603 sal_Bool bFalse = sal_False;
604 aAny.setValue( &bFalse, ::getBooleanCppuType() );
605 xSet->setPropertyValue( aCompressPropName, aAny );
608 // even plain stream should be encrypted in encrypted documents
609 sal_Bool bTrue = sal_True;
610 aAny.setValue( &bTrue, ::getBooleanCppuType() );
611 xSet->setPropertyValue( aUseCommonPassPropName, aAny );
613 // set buffer and create outputstream
614 uno::Reference< io::XOutputStream > xOutputStream = xStream->getOutputStream();
616 // set Base URL
617 uno::Reference< beans::XPropertySet > xInfoSet;
618 if( rArguments.getLength() > 0 )
619 rArguments.getConstArray()[0] >>= xInfoSet;
620 DBG_ASSERT( xInfoSet.is(), "missing property set" );
621 if( xInfoSet.is() )
623 OUString sPropName( RTL_CONSTASCII_USTRINGPARAM("StreamName") );
624 xInfoSet->setPropertyValue( sPropName, makeAny( sStreamName ) );
627 // write the stuff
628 bRet = WriteThroughComponent(
629 xOutputStream, xComponent, rFactory,
630 pServiceName, rArguments, rMediaDesc );
632 catch ( uno::Exception& )
636 return bRet;
640 sal_Bool SwXMLWriter::WriteThroughComponent(
641 const uno::Reference<io::XOutputStream> & xOutputStream,
642 const uno::Reference<XComponent> & xComponent,
643 const uno::Reference<XMultiServiceFactory> & rFactory,
644 const sal_Char* pServiceName,
645 const Sequence<Any> & rArguments,
646 const Sequence<PropertyValue> & rMediaDesc )
648 ASSERT( xOutputStream.is(), "I really need an output stream!" );
649 ASSERT( xComponent.is(), "Need component!" );
650 ASSERT( NULL != pServiceName, "Need component name!" );
652 RTL_LOGFILE_CONTEXT_AUTHOR( aFilterLog, "sw", LOGFILE_AUTHOR,
653 "SwXMLWriter::WriteThroughComponent" );
655 // get component
656 uno::Reference< io::XActiveDataSource > xSaxWriter(
657 rFactory->createInstance(
658 String::CreateFromAscii(RTL_CONSTASCII_STRINGPARAM(
659 "com.sun.star.xml.sax.Writer")) ),
660 UNO_QUERY );
661 ASSERT( xSaxWriter.is(), "can't instantiate XML writer" );
662 if(!xSaxWriter.is())
663 return sal_False;
665 RTL_LOGFILE_CONTEXT_TRACE( aFilterLog, "SAX-Writer created" );
667 // connect XML writer to output stream
668 xSaxWriter->setOutputStream( xOutputStream );
670 // prepare arguments (prepend doc handler to given arguments)
671 uno::Reference<xml::sax::XDocumentHandler> xDocHandler( xSaxWriter,UNO_QUERY);
672 Sequence<Any> aArgs( 1 + rArguments.getLength() );
673 aArgs[0] <<= xDocHandler;
674 for(sal_Int32 i = 0; i < rArguments.getLength(); i++)
675 aArgs[i+1] = rArguments[i];
677 // get filter component
678 uno::Reference< document::XExporter > xExporter(
679 rFactory->createInstanceWithArguments(
680 OUString::createFromAscii(pServiceName), aArgs), UNO_QUERY);
681 ASSERT( xExporter.is(),
682 "can't instantiate export filter component" );
683 if( !xExporter.is() )
684 return sal_False;
685 RTL_LOGFILE_CONTEXT_TRACE1( aFilterLog, "%s instantiated.", pServiceName );
687 // connect model and filter
688 xExporter->setSourceDocument( xComponent );
690 // filter!
691 RTL_LOGFILE_CONTEXT_TRACE( aFilterLog, "call filter()" );
692 uno::Reference<XFilter> xFilter( xExporter, UNO_QUERY );
693 return xFilter->filter( rMediaDesc );
697 // -----------------------------------------------------------------------
699 void GetXMLWriter( const String& /*rName*/, const String& rBaseURL, WriterRef& xRet )
701 xRet = new SwXMLWriter( rBaseURL );
704 // -----------------------------------------------------------------------