update credits
[LibreOffice.git] / writerfilter / source / filter / ImportFilter.cxx
blobe56cbca4dff6d05f9aee5617d42a2150c88a3339
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <comphelper/mediadescriptor.hxx>
27 #include <oox/core/filterdetect.hxx>
28 #include <dmapper/DomainMapper.hxx>
29 #include <WriterFilter.hxx>
30 #include <doctok/WW8Document.hxx>
31 #include <ooxml/OOXMLDocument.hxx>
32 #ifdef DEBUG_IMPORT
33 #include <iostream>
34 #include <osl/process.h>
35 #endif
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 ::comphelper::MediaDescriptor;
47 sal_Bool WriterFilter::filter( const uno::Sequence< beans::PropertyValue >& aDescriptor )
48 throw (uno::RuntimeException)
50 if( m_xSrcDoc.is() )
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);
54 if (!xIfc.is())
55 return sal_False;
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())
59 return sal_False;
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;
69 try
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() )
80 return sal_False;
82 #ifdef DEBUG_IMPORT
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();
95 #endif
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::Stream::Pointer_t pStream(new writerfilter::dmapper::DomainMapper(m_xContext, xInputStream, m_xDstDoc, bRepairStorage, eType, uno::Reference<text::XTextRange>()));
103 //create the tokenizer and domain mapper
104 if( eType == writerfilter::dmapper::DOCUMENT_OOXML )
106 writerfilter::ooxml::OOXMLStream::Pointer_t pDocStream = writerfilter::ooxml::OOXMLDocumentFactory::createStream(m_xContext, xInputStream, bRepairStorage);
107 writerfilter::ooxml::OOXMLDocument::Pointer_t pDocument(writerfilter::ooxml::OOXMLDocumentFactory::createDocument(pDocStream));
109 uno::Reference<frame::XModel> xModel(m_xDstDoc, uno::UNO_QUERY_THROW);
110 pDocument->setModel(xModel);
112 uno::Reference<drawing::XDrawPageSupplier> xDrawings
113 (m_xDstDoc, uno::UNO_QUERY_THROW);
114 uno::Reference<drawing::XDrawPage> xDrawPage
115 (xDrawings->getDrawPage(), uno::UNO_SET_THROW);
116 pDocument->setDrawPage(xDrawPage);
118 pDocument->resolve(*pStream);
119 writerfilter::ooxml::OOXMLStream::Pointer_t pVBAProjectStream(writerfilter::ooxml::OOXMLDocumentFactory::createStream( pDocStream, writerfilter::ooxml::OOXMLStream::VBAPROJECT ));
120 oox::StorageRef xVbaPrjStrg( new ::oox::ole::OleStorage( m_xContext, pVBAProjectStream->getDocumentStream(), false ) );
121 if( xVbaPrjStrg.get() && xVbaPrjStrg->isStorage() )
123 ::oox::ole::VbaProject aVbaProject( m_xContext, xModel, "Writer" );
124 uno::Reference< frame::XFrame > xFrame = aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_FRAME(), uno::Reference< frame::XFrame > () );
126 // if no XFrame try fallback to what we can glean from the Model
127 if ( !xFrame.is() )
129 uno::Reference< frame::XController > xController = xModel->getCurrentController();
130 xFrame = xController.is() ? xController->getFrame() : NULL;
133 oox::GraphicHelper gHelper( m_xContext, xFrame, xVbaPrjStrg );
134 aVbaProject.importVbaProject( *xVbaPrjStrg, gHelper );
137 else
139 writerfilter::doctok::WW8Stream::Pointer_t pDocStream = writerfilter::doctok::WW8DocumentFactory::createStream(m_xContext, xInputStream);
140 writerfilter::doctok::WW8Document::Pointer_t pDocument(writerfilter::doctok::WW8DocumentFactory::createDocument(pDocStream));
142 pDocument->resolve(*pStream);
145 pStream.reset();
146 #ifdef DEBUG_IMPORT
148 dmapperLogger->endDocument();
149 debugLogger->endDocument();
150 #endif
152 return sal_True;
154 return sal_False;
158 void WriterFilter::cancel( ) throw (uno::RuntimeException)
164 void WriterFilter::setTargetDocument( const uno::Reference< lang::XComponent >& xDoc )
165 throw (lang::IllegalArgumentException, uno::RuntimeException)
167 m_xDstDoc = xDoc;
169 // Set some compatibility options that are valid for all the formats
170 uno::Reference< lang::XMultiServiceFactory > xFactory( xDoc, uno::UNO_QUERY );
171 uno::Reference< beans::XPropertySet > xSettings( xFactory->createInstance("com.sun.star.document.Settings"), uno::UNO_QUERY );
173 xSettings->setPropertyValue( "AddFrameOffsets", uno::makeAny( sal_True ) );
174 xSettings->setPropertyValue( "UseOldNumbering", uno::makeAny( sal_False ) );
175 xSettings->setPropertyValue( "IgnoreFirstLineIndentInNumbering", uno::makeAny( sal_False ) );
176 xSettings->setPropertyValue( "DoNotResetParaAttrsForNumFont", uno::makeAny( sal_False ) );
177 xSettings->setPropertyValue( "UseFormerLineSpacing", uno::makeAny( sal_False ) );
178 xSettings->setPropertyValue( "AddParaSpacingToTableCells", uno::makeAny( sal_True ) );
179 xSettings->setPropertyValue( "UseFormerObjectPositioning", uno::makeAny( sal_False ) );
180 xSettings->setPropertyValue( "ConsiderTextWrapOnObjPos", uno::makeAny( sal_True ) );
181 xSettings->setPropertyValue( "UseFormerTextWrapping", uno::makeAny( sal_False ) );
182 xSettings->setPropertyValue( "TableRowKeep", uno::makeAny( sal_True ) );
183 xSettings->setPropertyValue( "IgnoreTabsAndBlanksForLineCalculation", uno::makeAny( sal_True ) );
184 xSettings->setPropertyValue( "InvertBorderSpacing", uno::makeAny( sal_True ) );
185 xSettings->setPropertyValue( "CollapseEmptyCellPara", uno::makeAny( sal_True ) );
186 xSettings->setPropertyValue( "TabOverflow", uno::makeAny( sal_True ) );
187 xSettings->setPropertyValue( "UnbreakableNumberings", uno::makeAny( sal_True ) );
189 // Don't load the default style definitions to avoid weird mix
190 xSettings->setPropertyValue( "StylesNoDefault", uno::makeAny( sal_True ) );
192 xSettings->setPropertyValue("FloattableNomargins", uno::makeAny( sal_True ));
193 xSettings->setPropertyValue( "ClippedPictures", uno::makeAny( sal_True ) );
194 xSettings->setPropertyValue( "BackgroundParaOverDrawings", uno::makeAny( sal_True ) );
195 xSettings->setPropertyValue( "TabOverMargin", uno::makeAny( sal_True ) );
198 void WriterFilter::setSourceDocument( const uno::Reference< lang::XComponent >& xDoc )
199 throw (lang::IllegalArgumentException, uno::RuntimeException)
201 m_xSrcDoc = xDoc;
206 void WriterFilter::initialize( const uno::Sequence< uno::Any >& aArguments ) throw (uno::Exception, uno::RuntimeException)
208 uno::Sequence < beans::PropertyValue > aAnySeq;
209 sal_Int32 nLength = aArguments.getLength();
210 if ( nLength && ( aArguments[0] >>= aAnySeq ) )
212 const beans::PropertyValue * pValue = aAnySeq.getConstArray();
213 nLength = aAnySeq.getLength();
214 for ( sal_Int32 i = 0 ; i < nLength; i++)
216 if ( pValue[i].Name == "Type" )
218 pValue[i].Value >>= m_sFilterName;
219 break;
226 OUString WriterFilter_getImplementationName () throw (uno::RuntimeException)
228 return OUString ( "com.sun.star.comp.Writer.WriterFilter" );
231 #define SERVICE_NAME1 "com.sun.star.document.ImportFilter"
232 #define SERVICE_NAME2 "com.sun.star.document.ExportFilter"
235 sal_Bool WriterFilter_supportsService( const OUString& ServiceName ) throw (uno::RuntimeException)
237 return (ServiceName == SERVICE_NAME1 || ServiceName == SERVICE_NAME2 );
241 uno::Sequence< OUString > WriterFilter_getSupportedServiceNames( ) throw (uno::RuntimeException)
243 uno::Sequence < OUString > aRet(2);
244 OUString* pArray = aRet.getArray();
245 pArray[0] = SERVICE_NAME1;
246 pArray[1] = SERVICE_NAME2;
247 return aRet;
249 #undef SERVICE_NAME1
250 #undef SERVICE_NAME2
254 uno::Reference< uno::XInterface > WriterFilter_createInstance( const uno::Reference< uno::XComponentContext >& xContext)
255 throw( uno::Exception )
257 return (cppu::OWeakObject*) new WriterFilter( xContext );
262 OUString WriterFilter::getImplementationName( ) throw (uno::RuntimeException)
264 return WriterFilter_getImplementationName();
268 sal_Bool WriterFilter::supportsService( const OUString& rServiceName ) throw (uno::RuntimeException)
270 return WriterFilter_supportsService( rServiceName );
274 uno::Sequence< OUString > WriterFilter::getSupportedServiceNames( ) throw (uno::RuntimeException)
276 return WriterFilter_getSupportedServiceNames();
279 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */