1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include <osl/diagnose.h>
25 #include <com/sun/star/uno/XComponentContext.hpp>
26 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
27 #include <com/sun/star/io/XInputStream.hpp>
28 #include <comphelper/mediadescriptor.hxx>
29 #include <oox/core/filterdetect.hxx>
30 #include <dmapper/DomainMapper.hxx>
31 #include <WriterFilter.hxx>
32 #include <doctok/WW8Document.hxx>
33 #include <ooxml/OOXMLDocument.hxx>
34 #ifdef DEBUG_IMPORT
35 #include <iostream>
36 #include <osl/process.h>
37 #endif
38 
39 #include <resourcemodel/TagLogger.hxx>
40 using namespace ::rtl;
41 using namespace ::com::sun::star;
42 using ::comphelper::MediaDescriptor;
43 
44 /*-- 09.06.2006 10:15:20---------------------------------------------------
45 
46   -----------------------------------------------------------------------*/
filter(const uno::Sequence<beans::PropertyValue> & aDescriptor)47 sal_Bool WriterFilter::filter( const uno::Sequence< beans::PropertyValue >& aDescriptor )
48    throw (uno::RuntimeException)
49 {
50     if( m_xSrcDoc.is() )
51     {
52 		uno::Reference< lang::XMultiServiceFactory > xMSF(m_xContext->getServiceManager(), uno::UNO_QUERY_THROW);
53 		uno::Reference< uno::XInterface > xIfc( xMSF->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "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);
62 	}
63 	else if (m_xDstDoc.is())
64 	{
65         MediaDescriptor aMediaDesc( aDescriptor );
66         OUString sFilterName = aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_FILTERNAME(), OUString() );
67 
68         uno::Reference< io::XInputStream > xInputStream;
69         try
70         {
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 );
74         }
75         catch( uno::Exception& e)
76         {
77             (void) e;
78         }
79 
80         if ( !xInputStream.is() )
81         {
82             return sal_False;
83         }
84 
85 #ifdef DEBUG_IMPORT
86         OUString sURL = aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_URL(), OUString() );
87         ::std::string sURLc = OUStringToOString(sURL, RTL_TEXTENCODING_ASCII_US).getStr();
88 
89         writerfilter::TagLogger::Pointer_t debugLogger
90         (writerfilter::TagLogger::getInstance("DEBUG"));
91         debugLogger->setFileName(sURLc);
92         debugLogger->startDocument();
93 
94         writerfilter::TagLogger::Pointer_t dmapperLogger
95         (writerfilter::TagLogger::getInstance("DOMAINMAPPER"));
96         dmapperLogger->setFileName(sURLc);
97         dmapperLogger->startDocument();
98 #endif
99 
100         writerfilter::dmapper::SourceDocumentType eType =
101             (m_sFilterName.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "writer_MS_Word_2007" ) ) ||
102              m_sFilterName.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "writer_MS_Word_2007_Template" ) )) ?
103                     writerfilter::dmapper::DOCUMENT_OOXML : writerfilter::dmapper::DOCUMENT_DOC;
104         writerfilter::Stream::Pointer_t pStream(new writerfilter::dmapper::DomainMapper(m_xContext, xInputStream, m_xDstDoc, eType));
105         //create the tokenizer and domain mapper
106         if( eType == writerfilter::dmapper::DOCUMENT_OOXML )
107         {
108             writerfilter::ooxml::OOXMLStream::Pointer_t pDocStream;
109             try
110             {
111                 pDocStream = writerfilter::ooxml::OOXMLDocumentFactory::createStream(m_xContext, xInputStream);
112             }
113             catch (uno::Exception &e)
114             {
115                 throw uno::RuntimeException(e.Message, uno::Reference< uno::XInterface >());
116             }
117             writerfilter::ooxml::OOXMLDocument::Pointer_t pDocument(writerfilter::ooxml::OOXMLDocumentFactory::createDocument(pDocStream));
118 
119             uno::Reference<frame::XModel> xModel(m_xDstDoc, uno::UNO_QUERY_THROW);
120             pDocument->setModel(xModel);
121 
122             uno::Reference<drawing::XDrawPageSupplier> xDrawings
123                 (m_xDstDoc, uno::UNO_QUERY_THROW);
124             uno::Reference<drawing::XDrawPage> xDrawPage
125                 (xDrawings->getDrawPage(), uno::UNO_SET_THROW);
126             pDocument->setDrawPage(xDrawPage);
127 
128             pDocument->resolve(*pStream);
129         }
130         else
131         {
132             writerfilter::doctok::WW8Stream::Pointer_t pDocStream = writerfilter::doctok::WW8DocumentFactory::createStream(m_xContext, xInputStream);
133             writerfilter::doctok::WW8Document::Pointer_t pDocument(writerfilter::doctok::WW8DocumentFactory::createDocument(pDocStream));
134 
135             pDocument->resolve(*pStream);
136         }
137 
138 #ifdef DEBUG_IMPORT
139         writerfilter::TagLogger::dump("DOMAINMAPPER");
140         dmapperLogger->endDocument();
141         writerfilter::TagLogger::dump("DEBUG");
142         debugLogger->endDocument();
143 #endif
144 
145         return sal_True;
146     }
147     return sal_False;
148 }
149 /*-- 09.06.2006 10:15:20---------------------------------------------------
150 
151   -----------------------------------------------------------------------*/
cancel()152 void WriterFilter::cancel(  ) throw (uno::RuntimeException)
153 {
154 }
155 
156 /*-- 09.06.2006 10:15:20---------------------------------------------------
157 
158   -----------------------------------------------------------------------*/
setTargetDocument(const uno::Reference<lang::XComponent> & xDoc)159 void WriterFilter::setTargetDocument( const uno::Reference< lang::XComponent >& xDoc )
160    throw (lang::IllegalArgumentException, uno::RuntimeException)
161 {
162    m_xDstDoc = xDoc;
163 }
164 
setSourceDocument(const uno::Reference<lang::XComponent> & xDoc)165 void WriterFilter::setSourceDocument( const uno::Reference< lang::XComponent >& xDoc )
166    throw (lang::IllegalArgumentException, uno::RuntimeException)
167 {
168    m_xSrcDoc = xDoc;
169 }
170 
171 /*-- 09.06.2006 10:15:20---------------------------------------------------
172 
173   -----------------------------------------------------------------------*/
initialize(const uno::Sequence<uno::Any> & aArguments)174 void WriterFilter::initialize( const uno::Sequence< uno::Any >& aArguments ) throw (uno::Exception, uno::RuntimeException)
175 {
176    uno::Sequence < beans::PropertyValue > aAnySeq;
177    sal_Int32 nLength = aArguments.getLength();
178    if ( nLength && ( aArguments[0] >>= aAnySeq ) )
179    {
180        const beans::PropertyValue * pValue = aAnySeq.getConstArray();
181        nLength = aAnySeq.getLength();
182        for ( sal_Int32 i = 0 ; i < nLength; i++)
183        {
184            if ( pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "Type" ) ) )
185            {
186                pValue[i].Value >>= m_sFilterName;
187                break;
188            }
189        }
190    }
191 }
192 /*-- 09.06.2006 10:15:20---------------------------------------------------
193 
194   -----------------------------------------------------------------------*/
WriterFilter_getImplementationName()195 OUString WriterFilter_getImplementationName () throw (uno::RuntimeException)
196 {
197    return OUString ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.comp.Writer.WriterFilter" ) );
198 }
199 
200 #define SERVICE_NAME1 "com.sun.star.document.ImportFilter"
201 #define SERVICE_NAME2 "com.sun.star.document.ExportFilter"
202 /*-- 09.06.2006 10:15:20---------------------------------------------------
203 
204   -----------------------------------------------------------------------*/
WriterFilter_supportsService(const OUString & ServiceName)205 sal_Bool WriterFilter_supportsService( const OUString& ServiceName ) throw (uno::RuntimeException)
206 {
207    return (ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME1 ) ) ||
208            ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME1 ) ));
209 }
210 /*-- 09.06.2006 10:15:20---------------------------------------------------
211 
212   -----------------------------------------------------------------------*/
WriterFilter_getSupportedServiceNames()213 uno::Sequence< OUString > WriterFilter_getSupportedServiceNames(  ) throw (uno::RuntimeException)
214 {
215    uno::Sequence < OUString > aRet(2);
216    OUString* pArray = aRet.getArray();
217    pArray[0] =  OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME1 ) );
218    pArray[1] =  OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME2 ) );
219    return aRet;
220 }
221 #undef SERVICE_NAME1
222 #undef SERVICE_NAME2
223 
224 /*-- 09.06.2006 10:15:20---------------------------------------------------
225 
226   -----------------------------------------------------------------------*/
WriterFilter_createInstance(const uno::Reference<uno::XComponentContext> & xContext)227 uno::Reference< uno::XInterface > WriterFilter_createInstance( const uno::Reference< uno::XComponentContext >& xContext)
228                 throw( uno::Exception )
229 {
230    return (cppu::OWeakObject*) new WriterFilter( xContext );
231 }
232 
233 /*-- 09.06.2006 10:15:20---------------------------------------------------
234 
235   -----------------------------------------------------------------------*/
getImplementationName()236 OUString WriterFilter::getImplementationName(  ) throw (uno::RuntimeException)
237 {
238    return WriterFilter_getImplementationName();
239 }
240 /*-- 09.06.2006 10:15:20---------------------------------------------------
241 
242   -----------------------------------------------------------------------*/
supportsService(const OUString & rServiceName)243 sal_Bool WriterFilter::supportsService( const OUString& rServiceName ) throw (uno::RuntimeException)
244 {
245     return WriterFilter_supportsService( rServiceName );
246 }
247 /*-- 09.06.2006 10:15:20---------------------------------------------------
248 
249   -----------------------------------------------------------------------*/
getSupportedServiceNames()250 uno::Sequence< OUString > WriterFilter::getSupportedServiceNames(  ) throw (uno::RuntimeException)
251 {
252     return WriterFilter_getSupportedServiceNames();
253 }
254 
255