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& )
76         {
77         }
78 
79         if ( !xInputStream.is() )
80         {
81             return sal_False;
82         }
83 
84 #ifdef DEBUG_IMPORT
85         OUString sURL = aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_URL(), OUString() );
86         ::std::string sURLc = OUStringToOString(sURL, RTL_TEXTENCODING_ASCII_US).getStr();
87 
88         writerfilter::TagLogger::Pointer_t debugLogger
89         (writerfilter::TagLogger::getInstance("DEBUG"));
90         debugLogger->setFileName(sURLc);
91         debugLogger->startDocument();
92 
93         writerfilter::TagLogger::Pointer_t dmapperLogger
94         (writerfilter::TagLogger::getInstance("DOMAINMAPPER"));
95         dmapperLogger->setFileName(sURLc);
96         dmapperLogger->startDocument();
97 #endif
98 
99     writerfilter::dmapper::SourceDocumentType eType =
100         (m_sFilterName.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "writer_MS_Word_2007" ) ) ||
101          m_sFilterName.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "writer_MS_Word_2007_Template" ) )) ?
102                 writerfilter::dmapper::DOCUMENT_OOXML : writerfilter::dmapper::DOCUMENT_DOC;
103     writerfilter::Stream::Pointer_t pStream(new writerfilter::dmapper::DomainMapper(m_xContext, xInputStream, m_xDstDoc, eType));
104     //create the tokenizer and domain mapper
105     if( eType == writerfilter::dmapper::DOCUMENT_OOXML )
106     {
107         writerfilter::ooxml::OOXMLStream::Pointer_t pDocStream = writerfilter::ooxml::OOXMLDocumentFactory::createStream(m_xContext, xInputStream);
108         writerfilter::ooxml::OOXMLDocument::Pointer_t pDocument(writerfilter::ooxml::OOXMLDocumentFactory::createDocument(pDocStream));
109 
110         uno::Reference<frame::XModel> xModel(m_xDstDoc, uno::UNO_QUERY_THROW);
111         pDocument->setModel(xModel);
112 
113         uno::Reference<drawing::XDrawPageSupplier> xDrawings
114             (m_xDstDoc, uno::UNO_QUERY_THROW);
115         uno::Reference<drawing::XDrawPage> xDrawPage
116             (xDrawings->getDrawPage(), uno::UNO_SET_THROW);
117         pDocument->setDrawPage(xDrawPage);
118 
119         pDocument->resolve(*pStream);
120     }
121     else
122     {
123         writerfilter::doctok::WW8Stream::Pointer_t pDocStream = writerfilter::doctok::WW8DocumentFactory::createStream(m_xContext, xInputStream);
124         writerfilter::doctok::WW8Document::Pointer_t pDocument(writerfilter::doctok::WW8DocumentFactory::createDocument(pDocStream));
125 
126         pDocument->resolve(*pStream);
127     }
128 
129 #ifdef DEBUG_IMPORT
130     writerfilter::TagLogger::dump("DOMAINMAPPER");
131     dmapperLogger->endDocument();
132     writerfilter::TagLogger::dump("DEBUG");
133     debugLogger->endDocument();
134 #endif
135 
136     return sal_True;
137     }
138     return sal_False;
139 }
140 /*-- 09.06.2006 10:15:20---------------------------------------------------
141 
142   -----------------------------------------------------------------------*/
cancel()143 void WriterFilter::cancel(  ) throw (uno::RuntimeException)
144 {
145 }
146 
147 /*-- 09.06.2006 10:15:20---------------------------------------------------
148 
149   -----------------------------------------------------------------------*/
setTargetDocument(const uno::Reference<lang::XComponent> & xDoc)150 void WriterFilter::setTargetDocument( const uno::Reference< lang::XComponent >& xDoc )
151    throw (lang::IllegalArgumentException, uno::RuntimeException)
152 {
153    m_xDstDoc = xDoc;
154 }
155 
setSourceDocument(const uno::Reference<lang::XComponent> & xDoc)156 void WriterFilter::setSourceDocument( const uno::Reference< lang::XComponent >& xDoc )
157    throw (lang::IllegalArgumentException, uno::RuntimeException)
158 {
159    m_xSrcDoc = xDoc;
160 }
161 
162 /*-- 09.06.2006 10:15:20---------------------------------------------------
163 
164   -----------------------------------------------------------------------*/
initialize(const uno::Sequence<uno::Any> & aArguments)165 void WriterFilter::initialize( const uno::Sequence< uno::Any >& aArguments ) throw (uno::Exception, uno::RuntimeException)
166 {
167    uno::Sequence < beans::PropertyValue > aAnySeq;
168    sal_Int32 nLength = aArguments.getLength();
169    if ( nLength && ( aArguments[0] >>= aAnySeq ) )
170    {
171        const beans::PropertyValue * pValue = aAnySeq.getConstArray();
172        nLength = aAnySeq.getLength();
173        for ( sal_Int32 i = 0 ; i < nLength; i++)
174        {
175            if ( pValue[i].Name.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "Type" ) ) )
176            {
177                pValue[i].Value >>= m_sFilterName;
178                break;
179            }
180        }
181    }
182 }
183 /*-- 09.06.2006 10:15:20---------------------------------------------------
184 
185   -----------------------------------------------------------------------*/
WriterFilter_getImplementationName()186 OUString WriterFilter_getImplementationName () throw (uno::RuntimeException)
187 {
188    return OUString ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.comp.Writer.WriterFilter" ) );
189 }
190 
191 #define SERVICE_NAME1 "com.sun.star.document.ImportFilter"
192 #define SERVICE_NAME2 "com.sun.star.document.ExportFilter"
193 /*-- 09.06.2006 10:15:20---------------------------------------------------
194 
195   -----------------------------------------------------------------------*/
WriterFilter_supportsService(const OUString & ServiceName)196 sal_Bool WriterFilter_supportsService( const OUString& ServiceName ) throw (uno::RuntimeException)
197 {
198    return (ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME1 ) ) ||
199            ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME1 ) ));
200 }
201 /*-- 09.06.2006 10:15:20---------------------------------------------------
202 
203   -----------------------------------------------------------------------*/
WriterFilter_getSupportedServiceNames()204 uno::Sequence< OUString > WriterFilter_getSupportedServiceNames(  ) throw (uno::RuntimeException)
205 {
206    uno::Sequence < OUString > aRet(2);
207    OUString* pArray = aRet.getArray();
208    pArray[0] =  OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME1 ) );
209    pArray[1] =  OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME2 ) );
210    return aRet;
211 }
212 #undef SERVICE_NAME1
213 #undef SERVICE_NAME2
214 
215 /*-- 09.06.2006 10:15:20---------------------------------------------------
216 
217   -----------------------------------------------------------------------*/
WriterFilter_createInstance(const uno::Reference<uno::XComponentContext> & xContext)218 uno::Reference< uno::XInterface > WriterFilter_createInstance( const uno::Reference< uno::XComponentContext >& xContext)
219                 throw( uno::Exception )
220 {
221    return (cppu::OWeakObject*) new WriterFilter( xContext );
222 }
223 
224 /*-- 09.06.2006 10:15:20---------------------------------------------------
225 
226   -----------------------------------------------------------------------*/
getImplementationName()227 OUString WriterFilter::getImplementationName(  ) throw (uno::RuntimeException)
228 {
229    return WriterFilter_getImplementationName();
230 }
231 /*-- 09.06.2006 10:15:20---------------------------------------------------
232 
233   -----------------------------------------------------------------------*/
supportsService(const OUString & rServiceName)234 sal_Bool WriterFilter::supportsService( const OUString& rServiceName ) throw (uno::RuntimeException)
235 {
236     return WriterFilter_supportsService( rServiceName );
237 }
238 /*-- 09.06.2006 10:15:20---------------------------------------------------
239 
240   -----------------------------------------------------------------------*/
getSupportedServiceNames()241 uno::Sequence< OUString > WriterFilter::getSupportedServiceNames(  ) throw (uno::RuntimeException)
242 {
243     return WriterFilter_getSupportedServiceNames();
244 }
245 
246