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 <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
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 ::comphelper::MediaDescriptor;
44 :
45 :
46 :
47 211 : sal_Bool WriterFilter::filter( const uno::Sequence< beans::PropertyValue >& aDescriptor )
48 : throw (uno::RuntimeException)
49 : {
50 211 : if( m_xSrcDoc.is() )
51 : {
52 48 : uno::Reference< lang::XMultiServiceFactory > xMSF(m_xContext->getServiceManager(), uno::UNO_QUERY_THROW);
53 96 : uno::Reference< uno::XInterface > xIfc( xMSF->createInstance("com.sun.star.comp.Writer.DocxExport"), uno::UNO_QUERY_THROW);
54 48 : if (!xIfc.is())
55 0 : return sal_False;
56 96 : uno::Reference< document::XExporter > xExprtr(xIfc, uno::UNO_QUERY_THROW);
57 96 : uno::Reference< document::XFilter > xFltr(xIfc, uno::UNO_QUERY_THROW);
58 48 : if (!xExprtr.is() || !xFltr.is())
59 0 : return sal_False;
60 48 : xExprtr->setSourceDocument(m_xSrcDoc);
61 96 : return xFltr->filter(aDescriptor);
62 : }
63 163 : else if (m_xDstDoc.is())
64 : {
65 163 : MediaDescriptor aMediaDesc( aDescriptor );
66 163 : bool bRepairStorage = aMediaDesc.getUnpackedValueOrDefault( "RepairPackage", false );
67 :
68 326 : uno::Reference< io::XInputStream > xInputStream;
69 : try
70 : {
71 : // use the oox.core.FilterDetect implementation to extract the decrypted ZIP package
72 163 : ::oox::core::FilterDetect aDetector( m_xContext );
73 163 : xInputStream = aDetector.extractUnencryptedPackage( aMediaDesc );
74 : }
75 0 : catch( uno::Exception& )
76 : {
77 : }
78 :
79 163 : 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 164 : (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 326 : writerfilter::dmapper::DOCUMENT_OOXML : writerfilter::dmapper::DOCUMENT_DOC;
101 :
102 326 : 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 163 : if( eType == writerfilter::dmapper::DOCUMENT_OOXML )
105 : {
106 163 : writerfilter::ooxml::OOXMLStream::Pointer_t pDocStream = writerfilter::ooxml::OOXMLDocumentFactory::createStream(m_xContext, xInputStream, bRepairStorage);
107 326 : writerfilter::ooxml::OOXMLDocument::Pointer_t pDocument(writerfilter::ooxml::OOXMLDocumentFactory::createDocument(pDocStream));
108 :
109 326 : uno::Reference<frame::XModel> xModel(m_xDstDoc, uno::UNO_QUERY_THROW);
110 163 : pDocument->setModel(xModel);
111 :
112 : uno::Reference<drawing::XDrawPageSupplier> xDrawings
113 326 : (m_xDstDoc, uno::UNO_QUERY_THROW);
114 : uno::Reference<drawing::XDrawPage> xDrawPage
115 326 : (xDrawings->getDrawPage(), uno::UNO_SET_THROW);
116 163 : pDocument->setDrawPage(xDrawPage);
117 :
118 163 : pDocument->resolve(*pStream);
119 326 : writerfilter::ooxml::OOXMLStream::Pointer_t pVBAProjectStream(writerfilter::ooxml::OOXMLDocumentFactory::createStream( pDocStream, writerfilter::ooxml::OOXMLStream::VBAPROJECT ));
120 326 : oox::StorageRef xVbaPrjStrg( new ::oox::ole::OleStorage( m_xContext, pVBAProjectStream->getDocumentStream(), false ) );
121 163 : if( xVbaPrjStrg.get() && xVbaPrjStrg->isStorage() )
122 : {
123 0 : ::oox::ole::VbaProject aVbaProject( m_xContext, xModel, "Writer" );
124 0 : uno::Reference< frame::XFrame > xFrame = aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_FRAME(), uno::Reference< frame::XFrame > () );
125 :
126 : // if no XFrame try fallback to what we can glean from the Model
127 0 : if ( !xFrame.is() )
128 : {
129 0 : uno::Reference< frame::XController > xController = xModel->getCurrentController();
130 0 : xFrame = xController.is() ? xController->getFrame() : NULL;
131 : }
132 :
133 0 : oox::GraphicHelper gHelper( m_xContext, xFrame, xVbaPrjStrg );
134 0 : aVbaProject.importVbaProject( *xVbaPrjStrg, gHelper );
135 163 : }
136 : }
137 : else
138 : {
139 0 : writerfilter::doctok::WW8Stream::Pointer_t pDocStream = writerfilter::doctok::WW8DocumentFactory::createStream(m_xContext, xInputStream);
140 0 : writerfilter::doctok::WW8Document::Pointer_t pDocument(writerfilter::doctok::WW8DocumentFactory::createDocument(pDocStream));
141 :
142 0 : pDocument->resolve(*pStream);
143 : }
144 :
145 163 : pStream.reset();
146 : #ifdef DEBUG_IMPORT
147 :
148 : dmapperLogger->endDocument();
149 : debugLogger->endDocument();
150 : #endif
151 :
152 326 : return sal_True;
153 : }
154 0 : return sal_False;
155 : }
156 :
157 :
158 0 : void WriterFilter::cancel( ) throw (uno::RuntimeException)
159 : {
160 0 : }
161 :
162 :
163 :
164 163 : void WriterFilter::setTargetDocument( const uno::Reference< lang::XComponent >& xDoc )
165 : throw (lang::IllegalArgumentException, uno::RuntimeException)
166 : {
167 163 : m_xDstDoc = xDoc;
168 :
169 : // Set some compatibility options that are valid for all the formats
170 163 : uno::Reference< lang::XMultiServiceFactory > xFactory( xDoc, uno::UNO_QUERY );
171 326 : uno::Reference< beans::XPropertySet > xSettings( xFactory->createInstance("com.sun.star.document.Settings"), uno::UNO_QUERY );
172 :
173 163 : xSettings->setPropertyValue( "AddFrameOffsets", uno::makeAny( sal_True ) );
174 163 : xSettings->setPropertyValue( "UseOldNumbering", uno::makeAny( sal_False ) );
175 163 : xSettings->setPropertyValue( "IgnoreFirstLineIndentInNumbering", uno::makeAny( sal_False ) );
176 163 : xSettings->setPropertyValue( "DoNotResetParaAttrsForNumFont", uno::makeAny( sal_False ) );
177 163 : xSettings->setPropertyValue( "UseFormerLineSpacing", uno::makeAny( sal_False ) );
178 163 : xSettings->setPropertyValue( "AddParaSpacingToTableCells", uno::makeAny( sal_True ) );
179 163 : xSettings->setPropertyValue( "UseFormerObjectPositioning", uno::makeAny( sal_False ) );
180 163 : xSettings->setPropertyValue( "ConsiderTextWrapOnObjPos", uno::makeAny( sal_True ) );
181 163 : xSettings->setPropertyValue( "UseFormerTextWrapping", uno::makeAny( sal_False ) );
182 163 : xSettings->setPropertyValue( "TableRowKeep", uno::makeAny( sal_True ) );
183 163 : xSettings->setPropertyValue( "IgnoreTabsAndBlanksForLineCalculation", uno::makeAny( sal_True ) );
184 163 : xSettings->setPropertyValue( "InvertBorderSpacing", uno::makeAny( sal_True ) );
185 163 : xSettings->setPropertyValue( "CollapseEmptyCellPara", uno::makeAny( sal_True ) );
186 163 : xSettings->setPropertyValue( "TabOverflow", uno::makeAny( sal_True ) );
187 163 : xSettings->setPropertyValue( "UnbreakableNumberings", uno::makeAny( sal_True ) );
188 :
189 : // Don't load the default style definitions to avoid weird mix
190 163 : xSettings->setPropertyValue( "StylesNoDefault", uno::makeAny( sal_True ) );
191 :
192 163 : xSettings->setPropertyValue("FloattableNomargins", uno::makeAny( sal_True ));
193 163 : xSettings->setPropertyValue( "ClippedPictures", uno::makeAny( sal_True ) );
194 163 : xSettings->setPropertyValue( "BackgroundParaOverDrawings", uno::makeAny( sal_True ) );
195 326 : xSettings->setPropertyValue( "TabOverMargin", uno::makeAny( sal_True ) );
196 163 : }
197 :
198 48 : void WriterFilter::setSourceDocument( const uno::Reference< lang::XComponent >& xDoc )
199 : throw (lang::IllegalArgumentException, uno::RuntimeException)
200 : {
201 48 : m_xSrcDoc = xDoc;
202 48 : }
203 :
204 :
205 :
206 211 : void WriterFilter::initialize( const uno::Sequence< uno::Any >& aArguments ) throw (uno::Exception, uno::RuntimeException)
207 : {
208 211 : uno::Sequence < beans::PropertyValue > aAnySeq;
209 211 : sal_Int32 nLength = aArguments.getLength();
210 211 : if ( nLength && ( aArguments[0] >>= aAnySeq ) )
211 : {
212 211 : const beans::PropertyValue * pValue = aAnySeq.getConstArray();
213 211 : nLength = aAnySeq.getLength();
214 2110 : for ( sal_Int32 i = 0 ; i < nLength; i++)
215 : {
216 2110 : if ( pValue[i].Name == "Type" )
217 : {
218 211 : pValue[i].Value >>= m_sFilterName;
219 211 : break;
220 : }
221 : }
222 211 : }
223 211 : }
224 :
225 :
226 9 : OUString WriterFilter_getImplementationName () throw (uno::RuntimeException)
227 : {
228 9 : return OUString ( "com.sun.star.comp.Writer.WriterFilter" );
229 : }
230 :
231 : #define SERVICE_NAME1 "com.sun.star.document.ImportFilter"
232 : #define SERVICE_NAME2 "com.sun.star.document.ExportFilter"
233 :
234 :
235 0 : sal_Bool WriterFilter_supportsService( const OUString& ServiceName ) throw (uno::RuntimeException)
236 : {
237 0 : return (ServiceName == SERVICE_NAME1 || ServiceName == SERVICE_NAME2 );
238 : }
239 :
240 :
241 5 : uno::Sequence< OUString > WriterFilter_getSupportedServiceNames( ) throw (uno::RuntimeException)
242 : {
243 5 : uno::Sequence < OUString > aRet(2);
244 5 : OUString* pArray = aRet.getArray();
245 5 : pArray[0] = SERVICE_NAME1;
246 5 : pArray[1] = SERVICE_NAME2;
247 5 : return aRet;
248 : }
249 : #undef SERVICE_NAME1
250 : #undef SERVICE_NAME2
251 :
252 :
253 :
254 211 : uno::Reference< uno::XInterface > WriterFilter_createInstance( const uno::Reference< uno::XComponentContext >& xContext)
255 : throw( uno::Exception )
256 : {
257 211 : return (cppu::OWeakObject*) new WriterFilter( xContext );
258 : }
259 :
260 :
261 :
262 0 : OUString WriterFilter::getImplementationName( ) throw (uno::RuntimeException)
263 : {
264 0 : return WriterFilter_getImplementationName();
265 : }
266 :
267 :
268 0 : sal_Bool WriterFilter::supportsService( const OUString& rServiceName ) throw (uno::RuntimeException)
269 : {
270 0 : return WriterFilter_supportsService( rServiceName );
271 : }
272 :
273 :
274 0 : uno::Sequence< OUString > WriterFilter::getSupportedServiceNames( ) throw (uno::RuntimeException)
275 : {
276 0 : return WriterFilter_getSupportedServiceNames();
277 24 : }
278 :
279 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|