1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <osl/diagnose.h>
21 #include <com/sun/star/uno/XComponentContext.hpp>
22 #include <com/sun/star/beans/XPropertySet.hpp>
23 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
24 #include <com/sun/star/io/XInputStream.hpp>
25 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 #include <unotools/mediadescriptor.hxx>
27 #include <cppuhelper/supportsservice.hxx>
28 #include <oox/core/filterdetect.hxx>
29 #include <dmapper/DomainMapper.hxx>
30 #include <WriterFilter.hxx>
31 #include <ooxml/OOXMLDocument.hxx>
34 #include <osl/process.h>
37 #include <resourcemodel/TagLogger.hxx>
38 #include <oox/ole/olestorage.hxx>
39 #include <oox/ole/vbaproject.hxx>
40 #include <oox/helper/graphichelper.hxx>
41 using namespace ::rtl
;
42 using namespace ::com::sun::star
;
43 using utl::MediaDescriptor
;
47 sal_Bool
WriterFilter::filter( const uno::Sequence
< beans::PropertyValue
>& aDescriptor
)
48 throw (uno::RuntimeException
, std::exception
)
52 uno::Reference
< lang::XMultiServiceFactory
> xMSF(m_xContext
->getServiceManager(), uno::UNO_QUERY_THROW
);
53 uno::Reference
< uno::XInterface
> xIfc( xMSF
->createInstance("com.sun.star.comp.Writer.DocxExport"), uno::UNO_QUERY_THROW
);
56 uno::Reference
< document::XExporter
> xExprtr(xIfc
, uno::UNO_QUERY_THROW
);
57 uno::Reference
< document::XFilter
> xFltr(xIfc
, uno::UNO_QUERY_THROW
);
58 if (!xExprtr
.is() || !xFltr
.is())
60 xExprtr
->setSourceDocument(m_xSrcDoc
);
61 return xFltr
->filter(aDescriptor
);
63 else if (m_xDstDoc
.is())
65 MediaDescriptor
aMediaDesc( aDescriptor
);
66 bool bRepairStorage
= aMediaDesc
.getUnpackedValueOrDefault( "RepairPackage", false );
68 uno::Reference
< io::XInputStream
> xInputStream
;
71 // use the oox.core.FilterDetect implementation to extract the decrypted ZIP package
72 ::oox::core::FilterDetect
aDetector( m_xContext
);
73 xInputStream
= aDetector
.extractUnencryptedPackage( aMediaDesc
);
75 catch( uno::Exception
& )
79 if ( !xInputStream
.is() )
83 OUString sURL
= aMediaDesc
.getUnpackedValueOrDefault( MediaDescriptor::PROP_URL(), OUString() );
84 ::std::string sURLc
= OUStringToOString(sURL
, RTL_TEXTENCODING_ASCII_US
).getStr();
86 writerfilter::TagLogger::Pointer_t debugLogger
87 (writerfilter::TagLogger::getInstance("DEBUG"));
88 debugLogger
->setFileName(sURLc
);
89 debugLogger
->startDocument();
91 writerfilter::TagLogger::Pointer_t dmapperLogger
92 (writerfilter::TagLogger::getInstance("DOMAINMAPPER"));
93 dmapperLogger
->setFileName(sURLc
);
94 dmapperLogger
->startDocument();
97 writerfilter::dmapper::SourceDocumentType eType
=
98 (m_sFilterName
== "writer_MS_Word_2007" || m_sFilterName
== "writer_MS_Word_2007_Template" ||
99 m_sFilterName
== "writer_OOXML" || m_sFilterName
== "writer_OOXML_Text_Template" ) ?
100 writerfilter::dmapper::DOCUMENT_OOXML
: writerfilter::dmapper::DOCUMENT_DOC
;
102 writerfilter::dmapper::DomainMapper
* aDomainMapper
= new writerfilter::dmapper::DomainMapper(m_xContext
, xInputStream
, m_xDstDoc
, bRepairStorage
, eType
, uno::Reference
<text::XTextRange
>());
103 writerfilter::Stream::Pointer_t
pStream(aDomainMapper
);
104 //create the tokenizer and domain mapper
105 if( eType
== writerfilter::dmapper::DOCUMENT_OOXML
)
107 writerfilter::ooxml::OOXMLStream::Pointer_t pDocStream
= writerfilter::ooxml::OOXMLDocumentFactory::createStream(m_xContext
, xInputStream
, bRepairStorage
);
108 uno::Reference
<task::XStatusIndicator
> xStatusIndicator
= aMediaDesc
.getUnpackedValueOrDefault(MediaDescriptor::PROP_STATUSINDICATOR(), uno::Reference
<task::XStatusIndicator
>());
109 writerfilter::ooxml::OOXMLDocument::Pointer_t
pDocument(writerfilter::ooxml::OOXMLDocumentFactory::createDocument(pDocStream
, xStatusIndicator
));
111 uno::Reference
<frame::XModel
> xModel(m_xDstDoc
, uno::UNO_QUERY_THROW
);
112 pDocument
->setModel(xModel
);
114 uno::Reference
<drawing::XDrawPageSupplier
> xDrawings
115 (m_xDstDoc
, uno::UNO_QUERY_THROW
);
116 uno::Reference
<drawing::XDrawPage
> xDrawPage
117 (xDrawings
->getDrawPage(), uno::UNO_SET_THROW
);
118 pDocument
->setDrawPage(xDrawPage
);
120 pDocument
->resolve(*pStream
);
122 // Adding some properties to the document's grab bag for interoperability purposes:
123 uno::Sequence
<beans::PropertyValue
> aGrabBagProperties(10);
125 // Adding the saved Theme DOM
126 aGrabBagProperties
[0].Name
= "OOXTheme";
127 aGrabBagProperties
[0].Value
= uno::makeAny( pDocument
->getThemeDom() );
129 // Adding the saved custom xml DOM
130 aGrabBagProperties
[1].Name
= "OOXCustomXml";
131 aGrabBagProperties
[1].Value
= uno::makeAny( pDocument
->getCustomXmlDomList() );
132 aGrabBagProperties
[2].Name
= "OOXCustomXmlProps";
133 aGrabBagProperties
[2].Value
= uno::makeAny( pDocument
->getCustomXmlDomPropsList() );
135 // Adding the saved ActiveX DOM
136 aGrabBagProperties
[3].Name
= "OOXActiveX";
137 aGrabBagProperties
[3].Value
= uno::makeAny( pDocument
->getActiveXDomList() );
138 aGrabBagProperties
[4].Name
= "OOXActiveXBin";
139 aGrabBagProperties
[4].Value
= uno::makeAny( pDocument
->getActiveXBinList() );
141 // Adding the saved w:themeFontLang setting
142 aGrabBagProperties
[5].Name
= "ThemeFontLangProps";
143 aGrabBagProperties
[5].Value
= uno::makeAny( aDomainMapper
->GetThemeFontLangProperties() );
145 // Adding the saved Glossary Documnet DOM to the document's grab bag
146 aGrabBagProperties
[6].Name
= "OOXGlossary";
147 aGrabBagProperties
[6].Value
= uno::makeAny( pDocument
->getGlossaryDocDom() );
148 aGrabBagProperties
[7].Name
= "OOXGlossaryDom";
149 aGrabBagProperties
[7].Value
= uno::makeAny( pDocument
->getGlossaryDomList() );
151 // Adding the saved embedding document to document's grab bag
152 aGrabBagProperties
[8].Name
= "OOXEmbeddings";
153 aGrabBagProperties
[8].Value
= uno::makeAny( pDocument
->getEmbeddingsList() );
155 // Adding the saved compat settings
156 aGrabBagProperties
[9].Name
= "CompatSettings";
157 aGrabBagProperties
[9].Value
= uno::makeAny( aDomainMapper
->GetCompatSettings() );
159 putPropertiesToDocumentGrabBag( aGrabBagProperties
);
161 writerfilter::ooxml::OOXMLStream::Pointer_t
pVBAProjectStream(writerfilter::ooxml::OOXMLDocumentFactory::createStream( pDocStream
, writerfilter::ooxml::OOXMLStream::VBAPROJECT
));
162 oox::StorageRef
xVbaPrjStrg( new ::oox::ole::OleStorage( m_xContext
, pVBAProjectStream
->getDocumentStream(), false ) );
163 if( xVbaPrjStrg
.get() && xVbaPrjStrg
->isStorage() )
165 ::oox::ole::VbaProject
aVbaProject( m_xContext
, xModel
, "Writer" );
166 uno::Reference
< frame::XFrame
> xFrame
= aMediaDesc
.getUnpackedValueOrDefault( MediaDescriptor::PROP_FRAME(), uno::Reference
< frame::XFrame
> () );
168 // if no XFrame try fallback to what we can glean from the Model
171 uno::Reference
< frame::XController
> xController
= xModel
->getCurrentController();
172 xFrame
= xController
.is() ? xController
->getFrame() : NULL
;
175 oox::GraphicHelper
gHelper( m_xContext
, xFrame
, xVbaPrjStrg
);
176 aVbaProject
.importVbaProject( *xVbaPrjStrg
, gHelper
);
183 dmapperLogger
->endDocument();
184 debugLogger
->endDocument();
193 void WriterFilter::cancel( ) throw (uno::RuntimeException
, std::exception
)
199 void WriterFilter::setTargetDocument( const uno::Reference
< lang::XComponent
>& xDoc
)
200 throw (lang::IllegalArgumentException
, uno::RuntimeException
, std::exception
)
204 // Set some compatibility options that are valid for all the formats
205 uno::Reference
< lang::XMultiServiceFactory
> xFactory( xDoc
, uno::UNO_QUERY
);
206 uno::Reference
< beans::XPropertySet
> xSettings( xFactory
->createInstance("com.sun.star.document.Settings"), uno::UNO_QUERY
);
208 xSettings
->setPropertyValue( "AddFrameOffsets", uno::makeAny( sal_True
) );
209 xSettings
->setPropertyValue( "UseOldNumbering", uno::makeAny( sal_False
) );
210 xSettings
->setPropertyValue( "IgnoreFirstLineIndentInNumbering", uno::makeAny( sal_False
) );
211 xSettings
->setPropertyValue( "DoNotResetParaAttrsForNumFont", uno::makeAny( sal_False
) );
212 xSettings
->setPropertyValue( "UseFormerLineSpacing", uno::makeAny( sal_False
) );
213 xSettings
->setPropertyValue( "AddParaSpacingToTableCells", uno::makeAny( sal_True
) );
214 xSettings
->setPropertyValue( "UseFormerObjectPositioning", uno::makeAny( sal_False
) );
215 xSettings
->setPropertyValue( "ConsiderTextWrapOnObjPos", uno::makeAny( sal_True
) );
216 xSettings
->setPropertyValue( "UseFormerTextWrapping", uno::makeAny( sal_False
) );
217 xSettings
->setPropertyValue( "TableRowKeep", uno::makeAny( sal_True
) );
218 xSettings
->setPropertyValue( "IgnoreTabsAndBlanksForLineCalculation", uno::makeAny( sal_True
) );
219 xSettings
->setPropertyValue( "InvertBorderSpacing", uno::makeAny( sal_True
) );
220 xSettings
->setPropertyValue( "CollapseEmptyCellPara", uno::makeAny( sal_True
) );
221 xSettings
->setPropertyValue( "TabOverflow", uno::makeAny( sal_True
) );
222 xSettings
->setPropertyValue( "UnbreakableNumberings", uno::makeAny( sal_True
) );
224 xSettings
->setPropertyValue("FloattableNomargins", uno::makeAny( sal_True
));
225 xSettings
->setPropertyValue( "ClippedPictures", uno::makeAny( sal_True
) );
226 xSettings
->setPropertyValue( "BackgroundParaOverDrawings", uno::makeAny( sal_True
) );
227 xSettings
->setPropertyValue( "TabOverMargin", uno::makeAny( sal_True
) );
228 xSettings
->setPropertyValue("PropLineSpacingShrinksFirstLine", uno::makeAny(sal_True
));
231 void WriterFilter::setSourceDocument( const uno::Reference
< lang::XComponent
>& xDoc
)
232 throw (lang::IllegalArgumentException
, uno::RuntimeException
, std::exception
)
239 void WriterFilter::initialize( const uno::Sequence
< uno::Any
>& aArguments
) throw (uno::Exception
, uno::RuntimeException
, std::exception
)
241 uno::Sequence
< beans::PropertyValue
> aAnySeq
;
242 sal_Int32 nLength
= aArguments
.getLength();
243 if ( nLength
&& ( aArguments
[0] >>= aAnySeq
) )
245 const beans::PropertyValue
* pValue
= aAnySeq
.getConstArray();
246 nLength
= aAnySeq
.getLength();
247 for ( sal_Int32 i
= 0 ; i
< nLength
; i
++)
249 if ( pValue
[i
].Name
== "Type" )
251 pValue
[i
].Value
>>= m_sFilterName
;
259 OUString
WriterFilter_getImplementationName () throw (uno::RuntimeException
)
261 return OUString ( "com.sun.star.comp.Writer.WriterFilter" );
264 uno::Sequence
< OUString
> WriterFilter_getSupportedServiceNames( ) throw (uno::RuntimeException
)
266 uno::Sequence
< OUString
> aRet(2);
267 OUString
* pArray
= aRet
.getArray();
268 pArray
[0] = "com.sun.star.document.ImportFilter";
269 pArray
[1] = "com.sun.star.document.ExportFilter";
273 uno::Reference
< uno::XInterface
> WriterFilter_createInstance( const uno::Reference
< uno::XComponentContext
>& xContext
)
274 throw( uno::Exception
)
276 return (cppu::OWeakObject
*) new WriterFilter( xContext
);
281 OUString
WriterFilter::getImplementationName( ) throw (uno::RuntimeException
, std::exception
)
283 return WriterFilter_getImplementationName();
287 sal_Bool
WriterFilter::supportsService( const OUString
& rServiceName
) throw (uno::RuntimeException
, std::exception
)
289 return cppu::supportsService( this, rServiceName
);
293 uno::Sequence
< OUString
> WriterFilter::getSupportedServiceNames( ) throw (uno::RuntimeException
, std::exception
)
295 return WriterFilter_getSupportedServiceNames();
298 void WriterFilter::putPropertiesToDocumentGrabBag( const uno::Sequence
< beans::PropertyValue
>& aProperties
)
302 uno::Reference
<beans::XPropertySet
> xDocProps(m_xDstDoc
, uno::UNO_QUERY
);
305 uno::Reference
<beans::XPropertySetInfo
> xPropsInfo
= xDocProps
->getPropertySetInfo();
307 const OUString aGrabBagPropName
= "InteropGrabBag";
308 if( xPropsInfo
.is() && xPropsInfo
->hasPropertyByName( aGrabBagPropName
) )
310 // get existing grab bag
311 uno::Sequence
<beans::PropertyValue
> aGrabBag
;
312 xDocProps
->getPropertyValue( aGrabBagPropName
) >>= aGrabBag
;
313 sal_Int32 length
= aGrabBag
.getLength();
315 // update grab bag size to contain the new items
316 aGrabBag
.realloc( length
+ aProperties
.getLength() );
319 for( sal_Int32 i
=0; i
< aProperties
.getLength(); ++i
)
321 aGrabBag
[length
+ i
].Name
= aProperties
[i
].Name
;
322 aGrabBag
[length
+ i
].Value
= aProperties
[i
].Value
;
325 // put it back to the document
326 xDocProps
->setPropertyValue( aGrabBagPropName
, uno::Any( aGrabBag
) );
330 catch(const uno::Exception
&)
332 SAL_WARN("writerfilter","Failed to save documents grab bag");
336 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */