Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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>
32 : #ifdef DEBUG_IMPORT
33 : #include <iostream>
34 : #include <osl/process.h>
35 : #endif
36 :
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;
44 :
45 :
46 :
47 1176 : sal_Bool WriterFilter::filter( const uno::Sequence< beans::PropertyValue >& aDescriptor )
48 : throw (uno::RuntimeException, std::exception)
49 : {
50 1176 : if( m_xSrcDoc.is() )
51 : {
52 272 : uno::Reference< lang::XMultiServiceFactory > xMSF(m_xContext->getServiceManager(), uno::UNO_QUERY_THROW);
53 544 : uno::Reference< uno::XInterface > xIfc( xMSF->createInstance("com.sun.star.comp.Writer.DocxExport"), uno::UNO_QUERY_THROW);
54 272 : if (!xIfc.is())
55 0 : return sal_False;
56 544 : uno::Reference< document::XExporter > xExprtr(xIfc, uno::UNO_QUERY_THROW);
57 544 : uno::Reference< document::XFilter > xFltr(xIfc, uno::UNO_QUERY_THROW);
58 272 : if (!xExprtr.is() || !xFltr.is())
59 0 : return sal_False;
60 272 : xExprtr->setSourceDocument(m_xSrcDoc);
61 544 : return xFltr->filter(aDescriptor);
62 : }
63 904 : else if (m_xDstDoc.is())
64 : {
65 904 : MediaDescriptor aMediaDesc( aDescriptor );
66 904 : bool bRepairStorage = aMediaDesc.getUnpackedValueOrDefault( "RepairPackage", false );
67 :
68 1808 : uno::Reference< io::XInputStream > xInputStream;
69 : try
70 : {
71 : // use the oox.core.FilterDetect implementation to extract the decrypted ZIP package
72 904 : ::oox::core::FilterDetect aDetector( m_xContext );
73 904 : xInputStream = aDetector.extractUnencryptedPackage( aMediaDesc );
74 : }
75 0 : catch( uno::Exception& )
76 : {
77 : }
78 :
79 904 : if ( !xInputStream.is() )
80 0 : return sal_False;
81 :
82 : #ifdef DEBUG_IMPORT
83 : OUString sURL = aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_URL(), OUString() );
84 : ::std::string sURLc = OUStringToOString(sURL, RTL_TEXTENCODING_ASCII_US).getStr();
85 :
86 : writerfilter::TagLogger::Pointer_t debugLogger
87 : (writerfilter::TagLogger::getInstance("DEBUG"));
88 : debugLogger->setFileName(sURLc);
89 : debugLogger->startDocument();
90 :
91 : writerfilter::TagLogger::Pointer_t dmapperLogger
92 : (writerfilter::TagLogger::getInstance("DOMAINMAPPER"));
93 : dmapperLogger->setFileName(sURLc);
94 : dmapperLogger->startDocument();
95 : #endif
96 :
97 : writerfilter::dmapper::SourceDocumentType eType =
98 905 : (m_sFilterName == "writer_MS_Word_2007" || m_sFilterName == "writer_MS_Word_2007_Template" ||
99 0 : m_sFilterName == "writer_OOXML" || m_sFilterName == "writer_OOXML_Text_Template" ) ?
100 1808 : writerfilter::dmapper::DOCUMENT_OOXML : writerfilter::dmapper::DOCUMENT_DOC;
101 :
102 904 : writerfilter::dmapper::DomainMapper* aDomainMapper = new writerfilter::dmapper::DomainMapper(m_xContext, xInputStream, m_xDstDoc, bRepairStorage, eType, uno::Reference<text::XTextRange>());
103 1808 : writerfilter::Stream::Pointer_t pStream(aDomainMapper);
104 : //create the tokenizer and domain mapper
105 904 : if( eType == writerfilter::dmapper::DOCUMENT_OOXML )
106 : {
107 904 : writerfilter::ooxml::OOXMLStream::Pointer_t pDocStream = writerfilter::ooxml::OOXMLDocumentFactory::createStream(m_xContext, xInputStream, bRepairStorage);
108 1808 : uno::Reference<task::XStatusIndicator> xStatusIndicator = aMediaDesc.getUnpackedValueOrDefault(MediaDescriptor::PROP_STATUSINDICATOR(), uno::Reference<task::XStatusIndicator>());
109 1808 : writerfilter::ooxml::OOXMLDocument::Pointer_t pDocument(writerfilter::ooxml::OOXMLDocumentFactory::createDocument(pDocStream, xStatusIndicator));
110 :
111 1808 : uno::Reference<frame::XModel> xModel(m_xDstDoc, uno::UNO_QUERY_THROW);
112 904 : pDocument->setModel(xModel);
113 :
114 : uno::Reference<drawing::XDrawPageSupplier> xDrawings
115 1808 : (m_xDstDoc, uno::UNO_QUERY_THROW);
116 : uno::Reference<drawing::XDrawPage> xDrawPage
117 1808 : (xDrawings->getDrawPage(), uno::UNO_SET_THROW);
118 904 : pDocument->setDrawPage(xDrawPage);
119 :
120 904 : pDocument->resolve(*pStream);
121 :
122 : // Adding some properties to the document's grab bag for interoperability purposes:
123 1808 : uno::Sequence<beans::PropertyValue> aGrabBagProperties(10);
124 :
125 : // Adding the saved Theme DOM
126 904 : aGrabBagProperties[0].Name = "OOXTheme";
127 904 : aGrabBagProperties[0].Value = uno::makeAny( pDocument->getThemeDom() );
128 :
129 : // Adding the saved custom xml DOM
130 904 : aGrabBagProperties[1].Name = "OOXCustomXml";
131 904 : aGrabBagProperties[1].Value = uno::makeAny( pDocument->getCustomXmlDomList() );
132 904 : aGrabBagProperties[2].Name = "OOXCustomXmlProps";
133 904 : aGrabBagProperties[2].Value = uno::makeAny( pDocument->getCustomXmlDomPropsList() );
134 :
135 : // Adding the saved ActiveX DOM
136 904 : aGrabBagProperties[3].Name = "OOXActiveX";
137 904 : aGrabBagProperties[3].Value = uno::makeAny( pDocument->getActiveXDomList() );
138 904 : aGrabBagProperties[4].Name = "OOXActiveXBin";
139 904 : aGrabBagProperties[4].Value = uno::makeAny( pDocument->getActiveXBinList() );
140 :
141 : // Adding the saved w:themeFontLang setting
142 904 : aGrabBagProperties[5].Name = "ThemeFontLangProps";
143 904 : aGrabBagProperties[5].Value = uno::makeAny( aDomainMapper->GetThemeFontLangProperties() );
144 :
145 : // Adding the saved Glossary Documnet DOM to the document's grab bag
146 904 : aGrabBagProperties[6].Name = "OOXGlossary";
147 904 : aGrabBagProperties[6].Value = uno::makeAny( pDocument->getGlossaryDocDom() );
148 904 : aGrabBagProperties[7].Name = "OOXGlossaryDom";
149 904 : aGrabBagProperties[7].Value = uno::makeAny( pDocument->getGlossaryDomList() );
150 :
151 : // Adding the saved embedding document to document's grab bag
152 904 : aGrabBagProperties[8].Name = "OOXEmbeddings";
153 904 : aGrabBagProperties[8].Value = uno::makeAny( pDocument->getEmbeddingsList() );
154 :
155 : // Adding the saved compat settings
156 904 : aGrabBagProperties[9].Name = "CompatSettings";
157 904 : aGrabBagProperties[9].Value = uno::makeAny( aDomainMapper->GetCompatSettings() );
158 :
159 904 : putPropertiesToDocumentGrabBag( aGrabBagProperties );
160 :
161 1808 : writerfilter::ooxml::OOXMLStream::Pointer_t pVBAProjectStream(writerfilter::ooxml::OOXMLDocumentFactory::createStream( pDocStream, writerfilter::ooxml::OOXMLStream::VBAPROJECT ));
162 1808 : oox::StorageRef xVbaPrjStrg( new ::oox::ole::OleStorage( m_xContext, pVBAProjectStream->getDocumentStream(), false ) );
163 904 : if( xVbaPrjStrg.get() && xVbaPrjStrg->isStorage() )
164 : {
165 0 : ::oox::ole::VbaProject aVbaProject( m_xContext, xModel, "Writer" );
166 0 : uno::Reference< frame::XFrame > xFrame = aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_FRAME(), uno::Reference< frame::XFrame > () );
167 :
168 : // if no XFrame try fallback to what we can glean from the Model
169 0 : if ( !xFrame.is() )
170 : {
171 0 : uno::Reference< frame::XController > xController = xModel->getCurrentController();
172 0 : xFrame = xController.is() ? xController->getFrame() : NULL;
173 : }
174 :
175 0 : oox::GraphicHelper gHelper( m_xContext, xFrame, xVbaPrjStrg );
176 0 : aVbaProject.importVbaProject( *xVbaPrjStrg, gHelper );
177 904 : }
178 : }
179 :
180 904 : pStream.reset();
181 : #ifdef DEBUG_IMPORT
182 :
183 : dmapperLogger->endDocument();
184 : debugLogger->endDocument();
185 : #endif
186 :
187 1808 : return sal_True;
188 : }
189 0 : return sal_False;
190 : }
191 :
192 :
193 0 : void WriterFilter::cancel( ) throw (uno::RuntimeException, std::exception)
194 : {
195 0 : }
196 :
197 :
198 :
199 904 : void WriterFilter::setTargetDocument( const uno::Reference< lang::XComponent >& xDoc )
200 : throw (lang::IllegalArgumentException, uno::RuntimeException, std::exception)
201 : {
202 904 : m_xDstDoc = xDoc;
203 :
204 : // Set some compatibility options that are valid for all the formats
205 904 : uno::Reference< lang::XMultiServiceFactory > xFactory( xDoc, uno::UNO_QUERY );
206 1808 : uno::Reference< beans::XPropertySet > xSettings( xFactory->createInstance("com.sun.star.document.Settings"), uno::UNO_QUERY );
207 :
208 904 : xSettings->setPropertyValue( "AddFrameOffsets", uno::makeAny( sal_True ) );
209 904 : xSettings->setPropertyValue( "UseOldNumbering", uno::makeAny( sal_False ) );
210 904 : xSettings->setPropertyValue( "IgnoreFirstLineIndentInNumbering", uno::makeAny( sal_False ) );
211 904 : xSettings->setPropertyValue( "DoNotResetParaAttrsForNumFont", uno::makeAny( sal_False ) );
212 904 : xSettings->setPropertyValue( "UseFormerLineSpacing", uno::makeAny( sal_False ) );
213 904 : xSettings->setPropertyValue( "AddParaSpacingToTableCells", uno::makeAny( sal_True ) );
214 904 : xSettings->setPropertyValue( "UseFormerObjectPositioning", uno::makeAny( sal_False ) );
215 904 : xSettings->setPropertyValue( "ConsiderTextWrapOnObjPos", uno::makeAny( sal_True ) );
216 904 : xSettings->setPropertyValue( "UseFormerTextWrapping", uno::makeAny( sal_False ) );
217 904 : xSettings->setPropertyValue( "TableRowKeep", uno::makeAny( sal_True ) );
218 904 : xSettings->setPropertyValue( "IgnoreTabsAndBlanksForLineCalculation", uno::makeAny( sal_True ) );
219 904 : xSettings->setPropertyValue( "InvertBorderSpacing", uno::makeAny( sal_True ) );
220 904 : xSettings->setPropertyValue( "CollapseEmptyCellPara", uno::makeAny( sal_True ) );
221 904 : xSettings->setPropertyValue( "TabOverflow", uno::makeAny( sal_True ) );
222 904 : xSettings->setPropertyValue( "UnbreakableNumberings", uno::makeAny( sal_True ) );
223 :
224 : // Don't load the default style definitions to avoid weird mix
225 904 : xSettings->setPropertyValue( "StylesNoDefault", uno::makeAny( sal_True ) );
226 :
227 904 : xSettings->setPropertyValue("FloattableNomargins", uno::makeAny( sal_True ));
228 904 : xSettings->setPropertyValue( "ClippedPictures", uno::makeAny( sal_True ) );
229 904 : xSettings->setPropertyValue( "BackgroundParaOverDrawings", uno::makeAny( sal_True ) );
230 1808 : xSettings->setPropertyValue( "TabOverMargin", uno::makeAny( sal_True ) );
231 904 : }
232 :
233 272 : void WriterFilter::setSourceDocument( const uno::Reference< lang::XComponent >& xDoc )
234 : throw (lang::IllegalArgumentException, uno::RuntimeException, std::exception)
235 : {
236 272 : m_xSrcDoc = xDoc;
237 272 : }
238 :
239 :
240 :
241 1176 : void WriterFilter::initialize( const uno::Sequence< uno::Any >& aArguments ) throw (uno::Exception, uno::RuntimeException, std::exception)
242 : {
243 1176 : uno::Sequence < beans::PropertyValue > aAnySeq;
244 1176 : sal_Int32 nLength = aArguments.getLength();
245 1176 : if ( nLength && ( aArguments[0] >>= aAnySeq ) )
246 : {
247 1176 : const beans::PropertyValue * pValue = aAnySeq.getConstArray();
248 1176 : nLength = aAnySeq.getLength();
249 11760 : for ( sal_Int32 i = 0 ; i < nLength; i++)
250 : {
251 11760 : if ( pValue[i].Name == "Type" )
252 : {
253 1176 : pValue[i].Value >>= m_sFilterName;
254 1176 : break;
255 : }
256 : }
257 1176 : }
258 1176 : }
259 :
260 :
261 14 : OUString WriterFilter_getImplementationName () throw (uno::RuntimeException)
262 : {
263 14 : return OUString ( "com.sun.star.comp.Writer.WriterFilter" );
264 : }
265 :
266 9 : uno::Sequence< OUString > WriterFilter_getSupportedServiceNames( ) throw (uno::RuntimeException)
267 : {
268 9 : uno::Sequence < OUString > aRet(2);
269 9 : OUString* pArray = aRet.getArray();
270 9 : pArray[0] = "com.sun.star.document.ImportFilter";
271 9 : pArray[1] = "com.sun.star.document.ExportFilter";
272 9 : return aRet;
273 : }
274 :
275 1176 : uno::Reference< uno::XInterface > WriterFilter_createInstance( const uno::Reference< uno::XComponentContext >& xContext)
276 : throw( uno::Exception )
277 : {
278 1176 : return (cppu::OWeakObject*) new WriterFilter( xContext );
279 : }
280 :
281 :
282 :
283 0 : OUString WriterFilter::getImplementationName( ) throw (uno::RuntimeException, std::exception)
284 : {
285 0 : return WriterFilter_getImplementationName();
286 : }
287 :
288 :
289 0 : sal_Bool WriterFilter::supportsService( const OUString& rServiceName ) throw (uno::RuntimeException, std::exception)
290 : {
291 0 : return cppu::supportsService( this, rServiceName );
292 : }
293 :
294 :
295 0 : uno::Sequence< OUString > WriterFilter::getSupportedServiceNames( ) throw (uno::RuntimeException, std::exception)
296 : {
297 0 : return WriterFilter_getSupportedServiceNames();
298 : }
299 :
300 904 : void WriterFilter::putPropertiesToDocumentGrabBag( const uno::Sequence< beans::PropertyValue >& aProperties )
301 : {
302 : try
303 : {
304 904 : uno::Reference<beans::XPropertySet> xDocProps(m_xDstDoc, uno::UNO_QUERY);
305 904 : if( xDocProps.is() )
306 : {
307 904 : uno::Reference<beans::XPropertySetInfo> xPropsInfo = xDocProps->getPropertySetInfo();
308 :
309 1808 : const OUString aGrabBagPropName = "InteropGrabBag";
310 904 : if( xPropsInfo.is() && xPropsInfo->hasPropertyByName( aGrabBagPropName ) )
311 : {
312 : // get existing grab bag
313 904 : uno::Sequence<beans::PropertyValue> aGrabBag;
314 904 : xDocProps->getPropertyValue( aGrabBagPropName ) >>= aGrabBag;
315 904 : sal_Int32 length = aGrabBag.getLength();
316 :
317 : // update grab bag size to contain the new items
318 904 : aGrabBag.realloc( length + aProperties.getLength() );
319 :
320 : // put the new items
321 9944 : for( sal_Int32 i=0; i < aProperties.getLength(); ++i )
322 : {
323 9040 : aGrabBag[length + i].Name = aProperties[i].Name;
324 9040 : aGrabBag[length + i].Value = aProperties[i].Value;
325 : }
326 :
327 : // put it back to the document
328 904 : xDocProps->setPropertyValue( aGrabBagPropName, uno::Any( aGrabBag ) );
329 904 : }
330 904 : }
331 : }
332 0 : catch(const uno::Exception&)
333 : {
334 : SAL_WARN("writerfilter","Failed to save documents grab bag");
335 : }
336 946 : }
337 :
338 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|