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 <rtl/ustring.hxx>
21 : #include <cppuhelper/implementationentry.hxx>
22 : #include <osl/module.hxx>
23 : #include <tools/solar.h>
24 : #include <RtfFilter.hxx>
25 : #include <comphelper/mediadescriptor.hxx>
26 : #include <dmapper/DomainMapper.hxx>
27 : #include <rtftok/RTFDocument.hxx>
28 : #include <com/sun/star/frame/XFrame.hpp>
29 : #include <com/sun/star/task/XStatusIndicator.hpp>
30 : #include <com/sun/star/io/WrongFormatException.hpp>
31 : #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
32 : #include <com/sun/star/text/XTextRange.hpp>
33 : #include <unotools/localfilehelper.hxx>
34 : #include <tools/stream.hxx>
35 : #include <unotools/ucbstreamhelper.hxx>
36 : #include <unotools/streamwrap.hxx>
37 :
38 : using namespace ::rtl;
39 : using namespace ::cppu;
40 : using namespace ::com::sun::star;
41 : using ::comphelper::MediaDescriptor;
42 :
43 223 : RtfFilter::RtfFilter( const uno::Reference< uno::XComponentContext >& rxContext) :
44 223 : m_xContext( rxContext )
45 : {
46 223 : }
47 :
48 446 : RtfFilter::~RtfFilter()
49 : {
50 446 : }
51 :
52 235 : sal_Bool RtfFilter::filter( const uno::Sequence< beans::PropertyValue >& aDescriptor )
53 : throw (uno::RuntimeException)
54 : {
55 235 : sal_uInt32 nStartTime = osl_getGlobalTimer();
56 235 : if( m_xSrcDoc.is() )
57 : {
58 41 : uno::Reference< lang::XMultiServiceFactory > xMSF(m_xContext->getServiceManager(), uno::UNO_QUERY_THROW);
59 82 : uno::Reference< uno::XInterface > xIfc( xMSF->createInstance("com.sun.star.comp.Writer.RtfExport"), uno::UNO_QUERY_THROW);
60 41 : if (!xIfc.is())
61 0 : return sal_False;
62 82 : uno::Reference< document::XExporter > xExprtr(xIfc, uno::UNO_QUERY_THROW);
63 82 : uno::Reference< document::XFilter > xFltr(xIfc, uno::UNO_QUERY_THROW);
64 41 : if (!xExprtr.is() || !xFltr.is())
65 0 : return sal_False;
66 41 : xExprtr->setSourceDocument(m_xSrcDoc);
67 82 : return xFltr->filter(aDescriptor);
68 : }
69 :
70 194 : sal_Bool bResult(sal_False);
71 194 : uno::Reference<task::XStatusIndicator> xStatusIndicator;
72 :
73 : try
74 : {
75 194 : MediaDescriptor aMediaDesc( aDescriptor );
76 194 : bool bRepairStorage = aMediaDesc.getUnpackedValueOrDefault( "RepairPackage", false );
77 194 : bool bIsNewDoc = aMediaDesc.getUnpackedValueOrDefault( "IsNewDoc", true );
78 388 : uno::Reference<text::XTextRange> xInsertTextRange = aMediaDesc.getUnpackedValueOrDefault( "TextInsertModeRange", uno::Reference<text::XTextRange>());
79 : #ifdef DEBUG_IMPORT
80 : OUString sURL = aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_URL(), OUString() );
81 : ::std::string sURLc = OUStringToOString(sURL, RTL_TEXTENCODING_ASCII_US).getStr();
82 :
83 : writerfilter::TagLogger::Pointer_t dmapperLogger
84 : (writerfilter::TagLogger::getInstance("DOMAINMAPPER"));
85 : dmapperLogger->setFileName(sURLc);
86 : dmapperLogger->startDocument();
87 : #endif
88 388 : uno::Reference< io::XInputStream > xInputStream;
89 :
90 194 : aMediaDesc.addInputStream();
91 194 : aMediaDesc[ MediaDescriptor::PROP_INPUTSTREAM() ] >>= xInputStream;
92 :
93 : // If this is set, write to this file, instead of the real document during paste.
94 194 : char* pEnv = getenv("SW_DEBUG_RTF_PASTE_TO");
95 388 : OUString aOutStr;
96 194 : if (!bIsNewDoc && pEnv && utl::LocalFileHelper::ConvertPhysicalNameToURL(OStringToOUString(pEnv, RTL_TEXTENCODING_UTF8), aOutStr))
97 : {
98 0 : SvStream* pOut = utl::UcbStreamHelper::CreateStream(aOutStr, STREAM_WRITE);
99 0 : SvStream* pIn = utl::UcbStreamHelper::CreateStream(xInputStream);
100 0 : *pOut << *pIn;
101 0 : delete pOut;
102 0 : return true;
103 : }
104 :
105 : // If this is set, read from this file, instead of the real clipboard during paste.
106 194 : pEnv = getenv("SW_DEBUG_RTF_PASTE_FROM");
107 194 : if (!bIsNewDoc && pEnv)
108 : {
109 0 : OUString aInStr;
110 0 : utl::LocalFileHelper::ConvertPhysicalNameToURL(OStringToOUString(pEnv, RTL_TEXTENCODING_UTF8), aInStr);
111 0 : SvStream* pStream = utl::UcbStreamHelper::CreateStream(aInStr, STREAM_READ);
112 0 : uno::Reference<io::XStream> xStream(new utl::OStreamWrapper(*pStream));
113 0 : xInputStream.set(xStream, uno::UNO_QUERY);
114 : }
115 :
116 194 : uno::Reference<frame::XFrame> xFrame = aMediaDesc.getUnpackedValueOrDefault(MediaDescriptor::PROP_FRAME(),
117 582 : uno::Reference<frame::XFrame>());
118 :
119 388 : xStatusIndicator = aMediaDesc.getUnpackedValueOrDefault(MediaDescriptor::PROP_STATUSINDICATOR(),
120 194 : uno::Reference<task::XStatusIndicator>());
121 :
122 : writerfilter::Stream::Pointer_t pStream(
123 388 : new writerfilter::dmapper::DomainMapper(m_xContext, xInputStream, m_xDstDoc, bRepairStorage, writerfilter::dmapper::DOCUMENT_RTF, xInsertTextRange, bIsNewDoc));
124 : writerfilter::rtftok::RTFDocument::Pointer_t const pDocument(
125 386 : writerfilter::rtftok::RTFDocumentFactory::createDocument(m_xContext, xInputStream, m_xDstDoc, xFrame, xStatusIndicator));
126 193 : pDocument->resolve(*pStream);
127 190 : bResult = sal_True;
128 : #ifdef DEBUG_IMPORT
129 : dmapperLogger->endDocument();
130 : #endif
131 190 : sal_uInt32 nEndTime = osl_getGlobalTimer();
132 194 : SAL_INFO("writerfilter.profile", OSL_THIS_FUNC << " finished in " << nEndTime - nStartTime << " ms");
133 : }
134 2 : catch (const io::WrongFormatException& e)
135 : {
136 : // cannot throw WrongFormatException directly :(
137 : throw lang::WrappedTargetRuntimeException("",
138 2 : static_cast<OWeakObject*>(this), uno::makeAny(e));
139 : }
140 2 : catch (const uno::Exception& e)
141 : {
142 : SAL_INFO("writerfilter", "Exception caught: " << e.Message);
143 : }
144 :
145 192 : if (xStatusIndicator.is())
146 176 : xStatusIndicator->end();
147 194 : return bResult;
148 : }
149 :
150 0 : void RtfFilter::cancel( ) throw (uno::RuntimeException)
151 : {
152 0 : }
153 :
154 41 : void RtfFilter::setSourceDocument( const uno::Reference< lang::XComponent >& xDoc )
155 : throw (lang::IllegalArgumentException, uno::RuntimeException)
156 : {
157 41 : m_xSrcDoc = xDoc;
158 41 : }
159 :
160 181 : void RtfFilter::setTargetDocument( const uno::Reference< lang::XComponent >& xDoc )
161 : throw (lang::IllegalArgumentException, uno::RuntimeException)
162 : {
163 181 : m_xDstDoc = xDoc;
164 181 : }
165 :
166 218 : void RtfFilter::initialize( const uno::Sequence< uno::Any >& /*aArguments*/ ) throw (uno::Exception, uno::RuntimeException)
167 : {
168 : // The DOCX exporter here extracts 'type' of the filter, ie 'Word' or
169 : // 'Word Template' but we don't need it for RTF.
170 218 : }
171 :
172 0 : OUString RtfFilter::getImplementationName( ) throw (uno::RuntimeException)
173 : {
174 0 : return RtfFilter_getImplementationName();
175 : }
176 :
177 : #define SERVICE_NAME1 "com.sun.star.document.ImportFilter"
178 : #define SERVICE_NAME2 "com.sun.star.document.ExportFilter"
179 0 : sal_Bool RtfFilter::supportsService( const OUString& rServiceName ) throw (uno::RuntimeException)
180 : {
181 0 : return ( rServiceName == SERVICE_NAME1 || rServiceName == SERVICE_NAME2 );
182 : }
183 :
184 0 : uno::Sequence< OUString > RtfFilter::getSupportedServiceNames( ) throw (uno::RuntimeException)
185 : {
186 0 : return RtfFilter_getSupportedServiceNames();
187 : }
188 :
189 : /* Helpers, used by shared lib exports. */
190 :
191 9 : OUString RtfFilter_getImplementationName () throw (uno::RuntimeException)
192 : {
193 9 : return OUString ( "com.sun.star.comp.Writer.RtfFilter" );
194 : }
195 :
196 4 : uno::Sequence< OUString > RtfFilter_getSupportedServiceNames( ) throw (uno::RuntimeException)
197 : {
198 4 : uno::Sequence < OUString > aRet(2);
199 4 : OUString* pArray = aRet.getArray();
200 4 : pArray[0] = SERVICE_NAME1;
201 4 : pArray[1] = SERVICE_NAME2;
202 4 : return aRet;
203 : }
204 : #undef SERVICE_NAME1
205 : #undef SERVICE_NAME2
206 :
207 223 : uno::Reference< uno::XInterface > RtfFilter_createInstance( const uno::Reference< uno::XComponentContext >& xContext)
208 : throw( uno::Exception )
209 : {
210 223 : return (cppu::OWeakObject*) new RtfFilter( xContext );
211 24 : }
212 :
213 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|